def __init__(self, config_path=None): if config_path == None: self.__config_file_path__ = "%s/etc/config.json" % ( global_const().get_value("BASEDIR")) else: self.__config_file_path__ = config_path self.__parse__()
def run(self): search_dir = "%s/core/action" % (global_const().get_value('BASEDIR')) cmd_array = [] for file in os.listdir(search_dir): filename, extname = os.path.splitext(file) if extname == ".py": m = importlib.import_module("core.action." + filename) try: class_func = getattr(m, "action_" + filename) o = class_func() # 检查是否server_action类型,如果是这个类型的话,表示是一个指令 if o.__class__.__base__.__name__ == 'server_action': cmd_info = (filename, o) cmd_array.append(cmd_info) except Exception as e: pass sw_name = os.path.basename(sys.argv[0]) print("Usage: %s cmd [options]" % (sw_name)) print("可以使用的cmd如下:") for cmd, cmd_obj in cmd_array: start_line = "%s %s" % (sw_name, cmd) print("\t%s %s %s" % (start_line, ' ' * (35 - len(start_line)), cmd_obj.description()))
def __init__(self): self._config_file_path = global_const().get_value( 'BASEDIR') + "/etc/options.json" with open(self._config_file_path, "a+") as f: try: # 读取文件,从文件初始化_config对象 f.seek(0) self._config = json.loads(f.read()) except: self._config = {}
def run(self): # 模板代码 tpl_code = '''#coding=utf-8 import sys import getopt from core.interface.action import server_action from core.helper.usage import usage_helper class action_%s(server_action): # 参数列表示例,可以在这边增加参数描述 __parameters__ = [ {"name":"h", "needarg":False, "desc":"显示这条帮助信息"} ] def __init__(self): # 创建帮助信息 self.__usage_helper__ = usage_helper(sys.argv[0], "%s", self.__parameters__) def __usage__(self): # 输出action的帮助信息 self.__usage_helper__.output() # action的简要描述,当执行smng help时,这个会输出到屏幕 def description(self): return "填入action的简单介绍,这个介绍会在help action中输出" # 通用的参数解析方法,如果需要增加参数处理过程请在这个方法内添加 def parse_parameters(self): try: opts, argv = getopt.getopt(sys.argv[2:], self.__usage_helper__.get_opt_string()) except Exception as e: self.__usage__() exit() for opt,arg in opts: if opt == '-h': self.__usage__() exit() # action实际执行的动作,请将action的行为添加到这个方法内 def run(self): pass ''' % (self._name_, self._name_) with open( "%s/core/action/%s.py" % (global_const().get_value('BASEDIR'), self._name_), "w+") as f: f.write(tpl_code)
def run(self): # 模板代码 tpl_code = '''#coding=utf-8 import sys import getopt from core.interface.action import server_action from core.helper.usage import usage_helper class action_%s(server_action): # 参数列表示例,可以在这边增加参数描述,最终代码将根据argname生成对对应的参数 _parameters = [ {"name":"h", "needarg":False, "desc":"显示这条帮助信息", "argname":"help"} ] def __init__(self): # 创建帮助信息 self._usage_helper = usage_helper(sys.argv[0], "%s", self._parameters) def _usage(self): # 输出action的帮助信息 self._usage_helper.output() # 对参数进行预处理 # 将参数描述数组重组成便于描述的字典,用于后续参数解析 # 另外根据argname初始化参数,需要参数值的初始化成None,不需要参数值的初始化成False def _prepare_parameters(self): recognized_parameter={} for obj in self._parameters: obj_key = '-' + obj['name'] # 类似参数-h -n -a的样式作为字典的key recognized_parameter[obj_key] = obj # 原参数描述的内容原封不动的存到字典里 parameter_name = "_%%s"%%(obj['argname']) if obj['needarg'] == True: setattr(self, parameter_name, None) else: setattr(self, parameter_name, False) return recognized_parameter # action的简要描述,当执行smng help时,这个会输出到屏幕 def description(self): return "填入action的简单介绍,这个介绍会在help action中输出" # 通用的参数解析方法,如果需要增加参数处理过程请在这个方法内添加 def parse_parameters(self): try: opts, argv = getopt.getopt(sys.argv[2:], self._usage_helper.get_opt_string()) except Exception as e: self._usage() exit() parameters = self._prepare_parameters() for opt,arg in opts: if parameters[opt]['needarg'] == True: setattr(self,"_%%s"%%(parameters[opt]['argname']), arg) # 需要传入值的参数设置值 else: setattr(self,"_%%s"%%(parameters[opt]['argname']), True) # 不需要传入值的参数设置成True # 单独处理help参数,默认输出帮助信息 if self._help == True: self._usage() exit() # ToDo: 自定义的解析方法 pass # action实际执行的动作,请将action的行为添加到这个方法内 def run(self): pass ''' % (self._name, self._name) with open( "%s/core/action/%s.py" % (global_const().get_value('BASEDIR'), self._name), "w+") as f: f.write(tpl_code)
def _load_from_default(self): self._user = global_const().get_value('DEFAULT_SSH_USER') self._password = global_const().get_value('DEFAULT_SSH_PASSWORD') self._port = global_const().get_value('DEFAULT_SSH_PORT')
def __init__(self): global_const().set_value('BASEDIR', os.path.dirname(__file__))