Commit c5eea486 authored by Seblu's avatar Seblu
Browse files

CliHandler class has now its own module

parent 96e82f17
Loading
Loading
Loading
Loading
+1 −39
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ import cccli
from cccli.exception import *
from cccli.printer import Printer, color
from cccli.command import Command, Alias
from cccli.handler import CliHandler

from sjrpc.client import SimpleRpcClient
from sjrpc.utils import RpcHandler, pure
@@ -171,42 +172,3 @@ class Cli(object):
        if len(texte) > 0 and texte[0] == "?":
            return [ "?%s"%c for c in  Command.list() + self.alias.keys() if c.startswith(texte[1:]) ]
        return [ c for c in  Command.list() + self.alias.keys() if c.startswith(texte) ]

class CliHandler(RpcHandler):
    '''Handle RPC incoming request'''

    @pure
    def quit(self, rpc=None):
        Printer().fatal("Disconnected from server!")

    @pure
    def get_tags(self, tags=None, resolve=False):
        if tags is None:
            tags = [ x[8:] for x in dir(self) if x.startswith("get_tag_") ]
        d = dict()
        for t in tags:
            method = "get_tag_%s"%t
            if hasattr(self, method):
                d[t] = getattr(self, method)(resolve)
        return d

    def get_tag_version(self, resolve):
        '''Return tag version'''
        return { "value": cccli.version, "ttl": -1 }

    def get_tag_os(self, resolve):
        '''Return Operating system tag'''
        if resolve:
            return { "value": platform.system(), "ttl": -1 }
        return { "ttl": -1 }

    def get_tag_uname(self, resolve):
        '''Return value of tag uname'''
        if resolve:
            try:
                p = subprocess.Popen(["uname", "-a"], close_fds=True, shell=False, stdout=subprocess.PIPE)
                return  { "value": p.stdout.read().rstrip("\n")  , "ttl": -1 }
            except Exception:
                return { "value": "", "ttl": -1 }
        else:
            return { "ttl": -1 }

cccli/handler.py

0 → 100644
+58 −0
Original line number Diff line number Diff line
#!/usr/bin/env python
#coding=utf8

'''
CloudControl CLI RPC Handler
'''

import os, os.path
import subprocess
import platform

import cccli
from cccli.exception import *
from cccli.printer import Printer, color

from sjrpc.client import SimpleRpcClient
from sjrpc.utils import RpcHandler, pure
from sjrpc.core.exceptions import *


class CliHandler(RpcHandler):
    '''Handle RPC incoming request'''

    @pure
    def quit(self, rpc=None):
        Printer().fatal("Disconnected from server!")

    @pure
    def get_tags(self, tags=None, resolve=False):
        if tags is None:
            tags = [ x[8:] for x in dir(self) if x.startswith("get_tag_") ]
        d = dict()
        for t in tags:
            method = "get_tag_%s"%t
            if hasattr(self, method):
                d[t] = getattr(self, method)(resolve)
        return d

    def get_tag_version(self, resolve):
        '''Return tag version'''
        return { "value": cccli.version, "ttl": -1 }

    def get_tag_os(self, resolve):
        '''Return Operating system tag'''
        if resolve:
            return { "value": platform.system(), "ttl": -1 }
        return { "ttl": -1 }

    def get_tag_uname(self, resolve):
        '''Return value of tag uname'''
        if resolve:
            try:
                p = subprocess.Popen(["uname", "-a"], close_fds=True, shell=False, stdout=subprocess.PIPE)
                return  { "value": p.stdout.read().rstrip("\n")  , "ttl": -1 }
            except Exception:
                return { "value": "", "ttl": -1 }
        else:
            return { "ttl": -1 }