Example #1
0
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)
Example #2
0
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)
Example #3
0
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)