def register_extensions(self, extensions = []): try: for extension, config in self.config['extensions'].items(): # extension module base string ext_bstr = 'ext.%s' % (extension) # extension core module string ext_cmstr = '%s.%s' % (ext_bstr, extension) # extension module object ext_module = import_module(ext_cmstr) # extension class ext_class = getattr(ext_module, extension.title()) # check if extension is bootable if issubclass(ext_class, Facade): cext_class = getattr(ext_module, '%sExtension' % (extension.title())) ext_class.register(cext_class, config) # register extension commands if exists ext_cmdstr = '%s.%s' % (ext_bstr, 'commands') ext_cmd_module = import_module(ext_cmdstr, pass_errors = True) if ext_cmd_module is not None: self.commandadapter.register_extension(ext_cmd_module, extension) except Exception, e: Log.error(e)
def register_extensions(self): """ Function registers extensions given extensions list Args ---- extensions (list) : the extensions dict on app.config.<env> Raises ------ Exception: Raises exception when extension can't be loaded properly. """ try: for extension, config in self.config['extensions'].items(): extension_bstr = '' # gather package name if exists extension_pieces = extension.split('.') # if the extensions is not in glim_extensions package if len(extension_pieces) > 1: extension_bstr = '.'.join(extension_pieces) else: # if the extension is in glim_extensions package extension_bstr = 'glim_extensions.%s' % extension_pieces[0] extension_module = import_module(extension_bstr) if extension_module: extension_startstr = '%s.%s' % (extension_bstr, 'start') extension_start = import_module(extension_startstr, pass_errors=True) extension_cmdsstr = '%s.%s' % (extension_bstr, 'commands') extension_cmds = import_module(extension_cmdsstr, pass_errors=True) if extension_start is not None: before = extension_start.before before(config) if extension_cmds is not None: if self.commandadapter is not None: self.commandadapter.register_extension( extension_cmds, extension_pieces[0]) else: GlimLog.error('Extension %s could not be loaded' % extension) except Exception as e: GlimLog.error(traceback.format_exc())
def make_app(env, commandadapter=None): """ Function creates an app given environment """ mconfig = import_module('app.config.%s' % env, pass_errors=True) if mconfig is None and paths.app_exists(): print(colored('Configuration for "%s" environment is not found' % env, 'red')) return None mstart = import_module('app.start') mroutes = import_module('app.routes') mcontrollers = import_module('app.controllers') before = mstart.before return Glim(commandadapter, mconfig, mroutes, mcontrollers, env, before)
def register_extensions(self): """ Function registers extensions given extensions list Args ---- extensions (list) : the extensions dict on app.config.<env> Raises ------ Exception: Raises exception when extension can't be loaded properly. """ try: for extension, config in self.config['extensions'].items(): extension_bstr = '' # gather package name if exists extension_pieces = extension.split('.') # if the extensions is not in glim_extensions package if len(extension_pieces) > 1: extension_bstr = '.'.join(extension_pieces) else: # if the extension is in glim_extensions package extension_bstr = 'glim_extensions.%s' % extension_pieces[0] extension_module = import_module(extension_bstr) if extension_module: extension_startstr = '%s.%s' % (extension_bstr, 'start') extension_start = import_module(extension_startstr, pass_errors=True) extension_cmdsstr = '%s.%s' % (extension_bstr, 'commands') extension_cmds = import_module(extension_cmdsstr, pass_errors=True) if extension_start is not None: before = extension_start.before before(config) if extension_cmds is not None: if self.commandadapter is not None: self.commandadapter.register_extension(extension_cmds, extension_pieces[0]) else: GlimLog.error('Extension %s could not be loaded' % extension) except Exception as e: GlimLog.error(traceback.format_exc())
def __init__(self, db, orm, migrations_mstr): self.db = db self.orm = orm self.migrations_mstr = migrations_mstr self.migrations_module = import_module(migrations_mstr, pass_errors=True) self.migrations = [] self.retrieve_migrations()
def main(): # register the global parser preparser = argparse.ArgumentParser(description = description, add_help = False) preparser.add_argument('--env', '-e', dest = 'env', default = 'development', help = 'choose application environment') # parse existing options namespace, extra = preparser.parse_known_args() env = namespace.env parser = argparse.ArgumentParser(parents = [preparser], description = description, add_help = True) subparsers = parser.add_subparsers(title = 'commands', help = 'commands') # initialize a command adapter with subparsers commandadapter = CommandAdapter(subparsers) # register glim commands commandadapter.register(glim.commands) # register app commands appcommands = import_module('app.commands', pass_errors = True) commandadapter.register(appcommands) # check if a new app is being created new = True if 'new' in extra else False if new: app = None else: app = App(commandadapter, env) args = parser.parse_args() command = commandadapter.match(args) commandadapter.dispatch(command, app)
def make_app(env, commandadapter=None): """ Function creates an app given environment """ mconfig = import_module('app.config.%s' % env, pass_errors=True) if mconfig is None and paths.app_exists(): print( colored('Configuration for "%s" environment is not found' % env, 'red')) return None mstart = import_module('app.start') mroutes = import_module('app.routes') mcontrollers = import_module('app.controllers') before = mstart.before return Glim(commandadapter, mconfig, mroutes, mcontrollers, env, before)
def dispatch_request(self, request): adapter = self.url_map.bind_to_environ(request.environ) try: endpoint, values = adapter.match() mcontroller = import_module('app.controllers') # detect filters filters = endpoint.split(',') endpoint_pieces = filters[-1].split('.') # if there exists any filter defined if len(filters) > 1: filters = filters[:-1] # here run filters for f in filters: fpieces = f.split('.') cls = fpieces[0] fnc = fpieces[1] mfilter = mcontroller obj = getattr(mfilter, cls) ifilter = obj(request) raw = getattr(ifilter, fnc)(** values) if isinstance(raw, basestring): return Response(raw) if isinstance(raw, Response): return raw cls = endpoint_pieces[0] restful = False try: fnc = endpoint_pieces[1] except: restful = True fnc = None obj = getattr(mcontroller, cls) instance = obj(request) raw = None if restful: raw = getattr(instance, request.method.lower()(**values)) else: raw = getattr(instance, fnc)(** values) if isinstance(raw, Response): return raw else: return Response(raw) except HTTPException, e: return e
def __init__(self, commandadapter, env = 'development'): self.commandadapter = commandadapter self.mconfig = import_module('app.config.%s' % env) if self.mconfig is None: print colored('Configuration for %s not found' % env, 'red') exit() self.config = self.mconfig.config self.register_config() self.register_log() self.register_database() self.register_extensions() self.register_ioc() self.register_view() # find out start mstart = import_module('app.start') self.before = mstart.before
def register_extensions(self, extensions=[]): """ Function registers extensions given extensions list Args ---- extensions (list) : the extensions dict on app.config.<env> Raises ------ Exception: Raises exception when extension can't be loaded properly. """ try: for extension, config in self.config['extensions'].items(): # extension module base string ext_bstr = 'ext.%s' % (extension) # start script ext_sstr = '%s.start' % ext_bstr ext_startmodule = import_module(ext_sstr, pass_errors=True) if ext_startmodule is not None: before = getattr(ext_startmodule, 'before') before(config) # register extension commands if exists ext_cmdstr = '%s.%s' % (ext_bstr, 'commands') ext_cmd_module = import_module(ext_cmdstr, pass_errors=True) if ext_cmd_module is not None: self.commandadapter.register_extension(ext_cmd_module, extension) except Exception as e: print(traceback.format_exc()) GlimLog.error(e)
def start(self, host = '127.0.0.1', port = '8080', env = 'development'): try: self.before() mroutes = import_module('app.routes') app = Glim(mroutes.urls, self.config['app']) if 'static' in self.config['app']: app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { self.config['app']['static']['url'] : self.config['app']['static']['path'] }) run_simple(host, int(port), app, use_debugger = self.config['app']['debugger'], use_reloader = self.config['app']['reloader']) except Exception, e: print traceback.format_exc() exit()
def start(self, host='127.0.0.1', port='8080', env='development'): """ Function initiates a werkzeug wsgi app using app.routes module. Note: Function will register a static path for css, js, img, etc. files using SharedDataMiddleware, else it won't register any static script path. Args ---- host (string): the host ip address to start the web server port (string): the port of ip address env (string): the application environment Raises ------ Exception: Raises any exception coming from werkzeug's web server """ try: self.before() mroutes = import_module('app.routes') app = Glim(mroutes.urls, self.config['app']) if 'static' in self.config['app']: app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { self.config['app']['static']['url']: self.config['app']['static']['path'] }) run_simple(host, int(port), app, use_debugger=self.config['app']['debugger'], use_reloader=self.config['app']['reloader']) except Exception as e: print(traceback.format_exc()) exit()
def main(): """ The single entry point to glim command line interface.Main method is called from pypi console_scripts key or by glim.py on root.This function initializes a new app given the glim commands and app commands if app exists. Usage ----- $ python glim/cli.py start $ python glim.py start (on root folder) """ # register the global parser preparser = argparse.ArgumentParser(description=description, add_help=False) preparser.add_argument('--env', '-e', dest='env', default='development', help='choose application environment') # parse existing options namespace, extra = preparser.parse_known_args() env = namespace.env # register the subparsers parser = argparse.ArgumentParser(parents=[preparser], description=description, add_help=True) subparsers = parser.add_subparsers(title='commands', help='commands') # initialize a command adapter with subparsers commandadapter = CommandAdapter(subparsers) # register glim commands commandadapter.register(glim.commands) # register app commands appcommands = import_module('app.commands', pass_errors=True) commandadapter.register(appcommands) app = None if paths.app_exists() is False: # check if a new app is being created new = True if 'new' in extra else False if ('help' in extra) or ('--help' in extra) or ('-h' in extra): help = True else: help = False if help: parser.print_help() exit() else: app = make_app(env, commandadapter) args = parser.parse_args() command = commandadapter.match(args) commandadapter.dispatch(command, app)
def main(): """ The single entry point to glim command line interface.Main method is called from pypi console_scripts key or by glim.py on root.This function initializes a new app given the glim commands and app commands if app exists. Usage ----- $ python glim/cli.py start $ python glim.py start (on root folder) """ # register the global parser preparser = argparse.ArgumentParser(description=description, add_help=False) preparser.add_argument('--env', '-e', dest='env', default='development', help='choose application environment') # parse existing options namespace, extra = preparser.parse_known_args() env = namespace.env # register the subparsers parser = argparse.ArgumentParser(parents=[preparser], description=description, add_help=True) subparsers = parser.add_subparsers(title='commands', help='commands') # initialize a command adapter with subparsers commandadapter = CommandAdapter(subparsers) # register glim commands commandadapter.register(glim.commands) # register app commands appcommands = import_module('app.commands', pass_errors=True) commandadapter.register(appcommands) # check if a new app is being created new = True if 'new' in extra else False if ('help' in extra) or ('--help' in extra) or ('-h' in extra): help = True else: help = False # check if help is being called when the app is not created if paths.app_exists() is False and help is True: parser.print_help() exit() # load the config module mconfig = import_module('app.config.%s' % env) # check if mconfig is None if mconfig is None: print(colored('Configuration for "%s" environment is not found' % env, 'red')) exit() # load the start hook mstart = import_module('app.start') before = mstart.before # create the app app = None if new else App(commandadapter, mconfig, env, before) args = parser.parse_args() command = commandadapter.match(args) commandadapter.dispatch(command, app)
def dispatch_request(self, request): """ Function dispatches the request. It also handles route filtering. Args ---- request (werkzeug.wrappers.Request): the request object. Returns ------- response (werkzeug.wrappers.Response): the response object. """ adapter = self.url_map.bind_to_environ(request.environ) try: endpoint, values = adapter.match() mcontroller = import_module("app.controllers") # detect filters filters = endpoint.split(",") endpoint_pieces = filters[-1].split(".") # if there exists any filter defined if len(filters) > 1: filters = filters[:-1] # here run filters for f in filters: fpieces = f.split(".") cls = fpieces[0] fnc = fpieces[1] mfilter = mcontroller obj = getattr(mfilter, cls) ifilter = obj(request) raw = getattr(ifilter, fnc)(**values) if isinstance(raw, basestring): return Response(raw) if isinstance(raw, Response): return raw cls = endpoint_pieces[0] restful = False try: fnc = endpoint_pieces[1] except: restful = True fnc = None obj = getattr(mcontroller, cls) instance = obj(request) raw = None if restful: raw = getattr(instance, request.method.lower()(**values)) else: raw = getattr(instance, fnc)(**values) if isinstance(raw, Response): return raw else: return Response(raw) except HTTPException as e: return e