Example #1
0
    def __init__(self, code, subject, headers):
        self.code = code
        self.subject = subject
        self.headers = headers

        if code in (http.BAD_REQUEST, http.NOT_FOUND):
            if type(self.subject) not in [unicode, str]:
                log.msg(brown(str(self.subject)), logLevel=logging.WARN)
            else:
                log.msg(brown(self.subject), logLevel=logging.WARN)
Example #2
0
    def __init__(self, code, subject, headers):
        self.code = code
        self.subject = subject
        self.headers = headers

        if code in (http.BAD_REQUEST, http.NOT_FOUND):
            if type(self.subject) not in [unicode, str]:
                log.msg(brown(str(self.subject)), logLevel=logging.WARN)
            else:
                log.msg(brown(self.subject), logLevel=logging.WARN)
Example #3
0
    def reload(self, module):
        """Reload a controller module

        :param module: the module to reload
        :type module: str
        """

        module_store = self.lookup(module)
        object = module_store.get('object')
        tmp_module = module_store.get('module')
        if object is None or object.loaded is False:
            raise ModuleError('Tried to reload %s that is not yet loaded' %
                              module)

        log.msg('{}: {} {}'.format(output.green('Reloading module'),
                                   object.__class__.__name__, object))

        try:
            rebuild.rebuild(tmp_module, False)
        except Exception as error:
            log.msg('{}: {}\n{}'.format(output.brown('Error reloading module'),
                                        error,
                                        traceback.format_exc()[:-1]))
        finally:
            object_name = object.__class__.__name__
            del self._modules[module]['object']
            gc.collect()
            temp_object = getattr(tmp_module, object_name)()
            temp_object.loaded = True
            self._modules[module]['object'] = temp_object
Example #4
0
    def __init__(self, code, subject, headers):
        self.code = code
        self.subject = subject
        self.headers = headers

        if code in (http.BAD_REQUEST, http.NOT_FOUND):
            log.msg(brown(self.subject), logLevel=logging.WARN)
        elif code == http.OK:
            pass
        else:
            log.err(self)
Example #5
0
    def __init__(self, code, subject, headers):
        self.code = code
        self.subject = subject
        self.headers = headers

        if code in (http.BAD_REQUEST, http.NOT_FOUND):
            log.msg(brown(self.subject), logLevel=logging.WARN)
        elif code == http.OK:
            pass
        else:
            log.err(self)
Example #6
0
    def install_routes(self, controller):
        """
        Install all the routes in a controller.

        :param controller: the controller where to fid routes
        :type controller: :class:`~mamba.Controller`
        """

        for func in inspect.getmembers(controller, predicate=inspect.ismethod):
            error = False
            if hasattr(func[1], 'route'):
                route = getattr(func[1], 'route')
                route.url = UrlSanitizer().sanitize_string(
                    controller.get_register_path() + route.url
                )
                route.compile()
                # normalize parameters
                real_args = inspect.getargspec(route.callback)[0]
                if real_args:
                    real_args = real_args[real_args.index('request') + 1:]
                    for arg in real_args:
                        if arg not in route.arguments.keys():
                            green = output.darkgreen
                            log.msg(
                                '{ERROR}: {arg} is not present in the {rout} '
                                'url for function {func_name}. Please, revise '
                                'your @route url string. The paramters names '
                                'in the url string should match the ones in '
                                'the function call. Skipping route registering'
                                '... this route ({route}) shouldn\'t be'
                                ' available at {controller}'.format(
                                    ERROR=output.brown('ERROR'),
                                    rout=green('@route'),
                                    arg=output.darkgreen(arg),
                                    func_name=green(route.callback.__name__),
                                    route=green('@route -> {}'.format(
                                        route.url
                                    )),
                                    controller=green(
                                        controller.__class__.__name__
                                    )
                                )
                            )
                            error = True

                if not error:
                    self.register_route(controller, route)
Example #7
0
    def install_routes(self, controller):
        """Install all the routes in a controller.

        :param controller: the controller where to fid routes
        :type controller: :class:`~mamba.Controller`
        """

        for func in inspect.getmembers(controller, predicate=inspect.ismethod):
            error = False
            if hasattr(func[1], 'route'):
                route = getattr(func[1], 'route')
                route.url = UrlSanitizer().sanitize_string(
                    controller.get_register_path() + route.url
                )
                route.compile()
                # normalize parameters
                real_args = inspect.getargspec(route.callback)[0]
                if real_args:
                    real_args = real_args[real_args.index('request') + 1:]
                    for arg in real_args:
                        if arg not in route.arguments.keys():
                            green = output.darkgreen
                            log.msg(
                                '{ERROR}: {arg} is not present in the {rout} '
                                'url for function {func_name}. Please, revise '
                                'your @route url string. The paramters names '
                                'in the url string should match the ones in '
                                'the function call. Skipping route registering'
                                '... this route ({route}) shouldn\'t be'
                                ' available at {controller}'.format(
                                    ERROR=output.brown('ERROR'),
                                    rout=green('@route'),
                                    arg=output.darkgreen(arg),
                                    func_name=green(route.callback.__name__),
                                    route=green('@route -> {}'.format(
                                        route.url
                                    )),
                                    controller=green(
                                        controller.__class__.__name__
                                    )
                                )
                            )
                            error = True

                if not error:
                    self.register_route(controller, route, func[0])
Example #8
0
    def _generate_application_directory(self):
        """Generates the application directory in the current path
        """

        try:
            # create project directory
            directory = filepath.FilePath(
                '{}/{}'.format(filepath.os.getcwd(), self.app_dir))
            if directory.exists() and self.noask is False:
                print(
                    'Seems like an application named {} already exists in '
                    'your current path.'.format(directory.basename())
                )

                query = 'Would you like to rename it? (Ctrl+c to abort)'
                if Interaction.userquery(query) == 'Yes':
                    rnd = str(filepath.random.randrange(1, 10000))
                    filepath.os.rename(
                        directory.path, '{}{}'.format(directory.path, rnd)
                    )

                    print('The old directory has been saved as {}'.format(
                        brown('{}{}'.format(directory.path, rnd))
                    ))
                else:
                    query = 'The {} directory is going to be {}, Are you sure?'
                    query = query.format(
                        directory.basename(), darkred('deleted')
                    )
                    if Interaction.userquery(query) == 'Yes':
                        print('Deleting {}...'.format(directory.basename()))
                        directory.remove()
                    else:
                        print('Quitting.')
                        sys.exit(0)
            elif directory.exists() and self.noask is True:
                directory.remove()

            print('Creating {} directory...'.format(
                directory.basename()).ljust(73), end=''
            )
            directory.createDirectory()
        except OSError as error:
            print(darkred(error))
            sys.exit(-1)
Example #9
0
    def reload(self, module):
        """Reload a controller module

        :param module: the module to reload
        :type module: str
        """

        module_store = self.lookup(module)
        object = module_store.get('object')
        tmp_module = module_store.get('module')
        if object is None or object.loaded is False:
            raise ModuleError(
                'Tried to reload %s that is not yet loaded' % module)

        log.info(
            '{}: {} {}'.format(
                output.green('Reloading module'),
                object.__class__.__name__, object
            )
        )

        try:
            rebuild.rebuild(tmp_module, False)
        except Exception as error:
            log.info(
                '{}: {}\n{}'.format(
                    output.brown('Error reloading module'),
                    error,
                    traceback.format_exc()[:-1]
                )
            )
        finally:
            object_name = object.__class__.__name__
            del self._modules[module]['object']
            gc.collect()
            temp_object = getattr(tmp_module, object_name)()
            temp_object.loaded = True
            self._modules[module]['object'] = temp_object