class Package(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Creates empty __init__.py inside package folder' default = True def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def getImportLine(self): return (None) def dependencyCheck(self): # This hook checks if self.cfg holds whatever # values we need. Config should have already been # validated at a basic level at this point # by FAXCore/FAXConfig. # # Returns (True/False,'optional message') # self.api.report_config_dependency('global', 'author') self.api.report_config_dependency('global', 'basename') return def setupVars(self): self.vars = {} self.vars['author_name'] = self.cfg['global']['author'] self.vars['basename'] = self.cfg['global']['basename'] def render(self): # print("This is hook render() at {}".format(self.name)) # First, we create our own variables, adapted # for template readability lb = self.vars['basename'].lower() path = '{}/'.format(lb) fn = '__init__.py' output = self.api.render(template='package.tpl', variables=self.vars) retObj = FAXRender(creator=self.name, relpath=path, filename=fn, contents=output) self.api.report_render_object(retObj) return (retObj)
class Services(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Creates flask-restful code for each defined service' default = True def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def dependencyCheck(self): # This hook checks if self.cfg holds whatever # values we need. Config should have already been # validated at a basic level at this point # by FAXCore/FAXConfig. # # good place to create plugin-wide template vars # although most plugins build their templatevars # on the render hook # self.reqitems = ('author', 'basename', 'backname', 'backname_services') for item in self.reqitems: self.api.report_config_dependency('global', item) for service in self.cfg['global']['backname_services']: self.api.report_config_dependency(service, 'paths') return def setupVars(self): self.names = {} for item in self.reqitems: self.names[item] = self.cfg['global'][item] self.templatevars = {} self.templatevars['names'] = self.names # NOTE: This plugin creates multiple files # and for that reason, some templatevars # will be set on render() def getImportLine(self): # This function returns a string # that represents the python code to import the # generated module. # e.g 'from blah_dbtools import BLAHDb' # It is used by entrypoint plugin. # TODO: actually implement all this # example: from fapi.cp1fw_nftables import CP1FW_Nftables # from t1.t2 import t3 # t1 = basename.lower() # t2 = backname.lower()_service.lower() # t3 = backname.upper()_service.capitalize() t1 = self.names['basename'].lower() # fapi b = self.names['backname'].lower() # cp1fw services = self.names['backname_services'] # nftables for s in services: t1 = self.names['basename'].lower() t2 = '{}_{}'.format(b, s) t3 = '{}_{}'.format(b.upper(), s.capitalize()) rs = 'from {}.{} import {}'.format(t1, t2, t3) self.api.report_import_line(rs) return (None) def paramhelper(self, service): params = [] rex = re.compile('\<\w+\>') paths = self.cfg[service]['paths'] if isinstance(paths, list): paths = ''.join(paths) for path in paths.split(','): pp = rex.findall(path) for p in pp: p = p.replace('<', '').replace('>', '=None') if p not in params: params.append(p) s = ', '.join(params) return (" {}".format(s)) def render(self): t1 = self.names['basename'].lower() # fapi b = self.names['backname'].lower() # cp1fw # TODO: move to setupVars services = self.names['backname_services'] # nftables for s in services: self.templatevars['servicename'] = s self.templatevars['parameters'] = self.paramhelper(s) t1 = self.names['basename'].lower() path = '{}/'.format(t1) fn = '{}_{}.py'.format(b, s) output = self.api.render(template='backname_service.tpl', variables=self.templatevars) retObj = FAXRender(creator=self.name, relpath=path, filename=fn, contents=output) self.api.report_render_object(retObj) return (None)
class RotatingLog(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Adds rotated logging to your application' def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def dependencyCheck(self): self.api.report_config_dependency('global', 'basename') self.api.report_config_dependency(section='global', option='author') def setupVars(self): """ This hook is run after dependencyCheck for the purposes of setting up any variables that might be required for other hooks, particularly by the render() hook. """ self.names = {} self.templatevars = {} self.names['basename'] = self.cfg['global']['basename'] self.dbtools = {} self.dbtools['user'] = self.cfg['dbtools']['user'] self.dbtools['pass'] = self.cfg['dbtools']['pass'] self.dbtools['host'] = self.cfg['dbtools']['host'] self.dbtools['db'] = self.cfg['dbtools']['db'] self.dbtools_ro = {} self.dbtools_ro['user'] = self.cfg['dbtools_readonly']['user'] self.dbtools_ro['pass'] = self.cfg['dbtools_readonly']['pass'] self.dbtools_ro['host'] = self.cfg['dbtools_readonly']['host'] self.dbtools_ro['db'] = self.cfg['dbtools_readonly']['db'] self.templatevars['names'] = self.names self.templatevars['dbtools'] = self.dbtools self.templatevars['dbtools_readonly'] = self.dbtools_ro self.templatevars['author_name'] = self.cfg['global']['author'] return def getImportLine(self): # This function returns a string # that represents the python code to import the # generated module. # e.g 'from blah_dbtools import BLAHDb' # It is used by entrypoint plugin. # TODO: actually implement all this # example: from cuac_dbtools import CUACDb l = self.names['basename'].lower() u = l.upper() rs = 'from {}.{}_dbtools import {}Db'.format(l, l, u) self.api.report_import_line(rs) return (rs) def render(self): # First, we create our own variables, adapted # for template readability lb = self.names['basename'].lower() vars = self.templatevars path = '{}/'.format(lb) fn = '{}_rotatinglog.py'.format(lb) output = self.api.render(template='dbtools.tpl', variables=vars) retObj = FAXRender(creator=self.name, relpath=path, filename=fn, contents=output) self.api.report_render_object(retObj) return (retObj)
class SamplePlugin(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Sample plugin. Read it if you want to code your own.' default = False def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def getImportLine(self): s = 'from someblah import someBlah' return (None) def dependencyCheck(self): # This hook checks if self.cfg holds whatever # values we need. Config should have already been # validated at a basic level at this point # by FAXCore/FAXConfig. # # Returns (True/False,'optional message') # # self.api.report_config_dependency('somesection','someoption') pass def setupVars(self): # This hook is run post-dependencyCheck # Variables that are required by render() # are created here, inside self. pass def render(self): # First, we create our own variables, adapted # for template readability # vars = {} # vars['author_name'] = self.cfg['global']['author'] # lb = self.cfg['global']['basename'].lower() # path = '{}/'.format(lb) # fn = '__init__.py' # output = self.api.render(template='package.tpl', # variables=vars) # retObj = FAXRender(creator=self.name, # relpath=path, # filename=fn, # contents=output) # self.api.report_render_object(retObj) pass def filter(self): # This hook is called after render hook. # We can get all the FAXRender objects the other # plugins reported to fax_core. # rObjs = self.api.get_render_objects() # We could modify those objects, for any reason. # for obj in rObjs: # if obj.creator is "DBTools": # obj.contents = "f**k you dbtools" # self.api.replace_render_objects(rObjs) pass
class EntryPoint(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Creates primary Flask-RESTful entrypoint.' default = True def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def getImportLine(self): # EntryPoint does not require an import string # TODO: test the None return return (None) def dependencyCheck(self): # This hook checks if self.cfg holds whatever # values we need. # PluginApi() offers report_config_dependency() for this # more complicated needs might require the plugin # author to print an error message and sys.exit() self.api.report_config_dependency('global', 'basename') self.api.report_config_dependency('global', 'privname') self.api.report_config_dependency('global', 'backname') self.api.report_config_dependency('global', 'author') self.api.report_config_dependency('global', 'backname_services') for service in self.cfg['global']['backname_services']: self.api.report_config_dependency(service, 'paths') return def setupVars(self): self.vars = {} self.vars['author_name'] = self.cfg['global']['author'] self.vars['services'] = self.cfg['global']['backname_services'] paths = {} names = {} for service in self.vars['services']: if isinstance(self.cfg[service]['paths'], str): paths[service] = [self.cfg[service]['paths']] else: paths[service] = self.cfg[service]['paths'] self.vars['paths'] = paths names['basename'] = self.cfg['global']['basename'] names['privname'] = self.cfg['global']['privname'] names['backname'] = self.cfg['global']['backname'] self.vars['names'] = names self.vars['import_lines'] = self.api.get_import_lines() def render(self): # print("This is hook render() at {}".format(self.name)) # First, we create our own variables, adapted # for template readability fn = '{}.py'.format(self.vars['names']['basename'].lower()) pprint(self.vars['import_lines']) output = self.api.render(template='entrypoint.tpl', variables=self.vars) retObj = FAXRender(creator=self.name, filename=fn, contents=output) self.api.report_render_object(retObj) return (retObj)
class DBTools(FAXPlugin): author = 'Arturo Busleiman <*****@*****.**>' description = 'Adds MySQL support to your application' def __init__(self): self.name = self.__class__.__name__ self.api = PluginApi(callerName=self.name) self.cfg = self.api.config() def dependencyCheck(self): # This hook checks if self.cfg holds whatever # values we need. Config should have already been # validated at a basic level at this point # by FAXCore/FAXConfig. # # good place to create plugin-wide template vars # # TODO: error-control this for proper fax exit() self.api.report_config_dependency('global', 'basename') self.api.report_config_dependency(section='global', option='author') reqoptions = ('user', 'pass', 'host', 'db') reqsections = ('dbtools', 'dbtools_readonly') for section in reqsections: for option in reqoptions: self.api.report_config_dependency(section, option) def setupVars(self): """ This hook is run after dependencyCheck for the purposes of setting up any variables that might be required for other hooks, particularly by the render() hook. """ self.names = {} self.templatevars = {} self.names['basename'] = self.cfg['global']['basename'] self.dbtools = {} self.dbtools['user'] = self.cfg['dbtools']['user'] self.dbtools['pass'] = self.cfg['dbtools']['pass'] self.dbtools['host'] = self.cfg['dbtools']['host'] self.dbtools['db'] = self.cfg['dbtools']['db'] self.dbtools_ro = {} self.dbtools_ro['user'] = self.cfg['dbtools_readonly']['user'] self.dbtools_ro['pass'] = self.cfg['dbtools_readonly']['pass'] self.dbtools_ro['host'] = self.cfg['dbtools_readonly']['host'] self.dbtools_ro['db'] = self.cfg['dbtools_readonly']['db'] self.templatevars['names'] = self.names self.templatevars['dbtools'] = self.dbtools self.templatevars['dbtools_readonly'] = self.dbtools_ro self.templatevars['author_name'] = self.cfg['global']['author'] return def getImportLine(self): # This function returns a string # that represents the python code to import the # generated module. # e.g 'from blah_dbtools import BLAHDb' # It is used by entrypoint plugin. # TODO: actually implement all this # example: from cuac_dbtools import CUACDb l = self.names['basename'].lower() u = l.upper() rs = 'from {}.{}_dbtools import {}Db'.format(l, l, u) self.api.report_import_line(rs) return (rs) def render(self): # First, we create our own variables, adapted # for template readability lb = self.names['basename'].lower() vars = self.templatevars path = '{}/'.format(lb) fn = '{}_dbtools.py'.format(lb) output = self.api.render(template='dbtools.tpl', variables=vars) retObj = FAXRender(creator=self.name, relpath=path, filename=fn, contents=output) self.api.report_render_object(retObj) return (retObj)