Ejemplo n.º 1
0
 def _load_functions(self, module, filename, prefix=''):
     """Load functions from a module and a user file. The functions from
     latter are able to override the those from the former. If the user
     file does not exist, then ignore loading functions from it.
     """
     # default function
     functions = {
         name: func
         for (name, func) in get_public_functions(module).items()
         if name.startswith(prefix)
     }
     if os.path.isfile(filename):
         user_mod = imp.load_source('', filename)
         user_funcs = get_public_functions(user_mod)
         functions.update({
             name: func
             for (name, func) in user_funcs.items()
             if name.startswith(prefix)
         })
     return functions
Ejemplo n.º 2
0
Archivo: app.py Proyecto: xxks-kkk/momo
FLASK_STATIC_FOLDER = os.path.join(FLASK_APP_ROOT, 'static')

app = Flask(
    import_name=__name__,
    template_folder=FLASK_TEMPLATE_FOLDER,
    static_folder=FLASK_STATIC_FOLDER,
)

# instrument app
Bootstrap(app)

# extensions
app.jinja_env.add_extension('jinja2.ext.do')

# register default filters
app.jinja_env.filters.update(get_public_functions(filters))

# register default global functions
app.jinja_env.globals.update(get_public_functions(functions))

app.url_map.strict_slashes = False
"""
Query strings for all views:

    ?sort=: sort by a key, which can be prefixed with a: (attr), n: (node
    object attribute), and f: (a pre-defined function). For example:

        ?sort=n.name means to sort by node name.
        ?sort=a.size means to sort by attr name "size".
        ?sort=f.rank means to sort using a sorting key function: sort_by_rank.
Ejemplo n.º 3
0
    def setup(self):
        bucket_name = self.settings.bucket.name
        self.configs = self.settings.plugins.get('flask',
                                                 {}).get(bucket_name, {})
        flask_dir = os.path.join(self.settings.settings_dir, 'flask',
                                 bucket_name)
        sys.path.append(flask_dir)

        # register user template folder
        user_template_folder = os.path.join(flask_dir, 'templates')
        self._reset_loader(user_template_folder)

        # configuration values
        # TODO: refactor these code
        app.config['MOMO_ROOT_NODE'] = self.settings.bucket.root
        app.config['MOMO_FILES_FOLDER'] = os.path.join(flask_dir, 'files')
        app.config['MOMO_SITENAME'] = (self.configs.get('sitename')
                                       or bucket_name.capitalize())
        app.config['MOMO_HEADER_ID'] = self.configs.get('header_id', False)
        app.config['MOMO_TOC_HEADER'] = self.configs.get('toc_title', True)
        app.config['MOMO_HEADER_NODE_COUNT'] = self.configs.get(
            'header_node_count', False)
        header_node_count_levels = self.configs.get('header_node_count_levels')
        app.config['MOMO_HEADER_NODE_COUNT_LEVELS'] = (
            to_list(header_node_count_levels)
            if header_node_count_levels else None)
        app.config['MOMO_INDEX_TABLE'] = self.configs.get('index_table', False)

        app.config['MOMO_PAGINATION_RECORD_NAME'] = self.configs.get(
            'pagination_record_name', 'node')
        app.config['MOMO_PAGINATION_INDEX_PER_PAGE'] = self.configs.get(
            'pagination_index_per_page', 30)
        app.config['MOMO_PAGINATION_NODE_PER_PAGE'] = self.configs.get(
            'pagination_node_per_page', 30)
        app.config['MOMO_PAGINATION_SEARCH_PER_PAGE'] = self.configs.get(
            'pagination_search_per_page', 30)
        app.config['MOMO_PAGINATION_DISPLAY_MSG'] = self.configs.get(
            'pagination_display_msg', '{total} {record_name}s.')

        app.config['MOMO_VIEW'] = self.configs.get('view', 'list')
        app.config['MOMO_VIEW_INDEX'] = self.configs.get('view_index')
        app.config['MOMO_VIEW_SEARCH'] = self.configs.get('view_search')
        app.config['MOMO_VIEW_NODE'] = self.configs.get('view_node')
        app.config['MOMO_LIST_NODE_ATTRS'] = \
            self.configs.get('list_node_attrs', True)
        app.config['MOMO_ROOT_REVERSED'] = self.configs.get('root_reversed')
        app.config['MOMO_MERGE_NODES'] = self.configs.get('merge_nodes')
        app.config['MOMO_CASE_INSENSITIVE'] = self.configs.get(
            'case_insensitive', False)
        app.config['MOMO_STRING_SEPARATOR'] = self.configs.get(
            'string_separator')
        app.config['MOMO_HOLDER_SIZE'] = self.configs.get(
            'holder_size', '125x125')
        app.config['MOMO_IMAGE_MAX_WIDTH'] = self.configs.get(
            'image_max_width')
        app.config['MOMO_PARENT_INDEX'] = self.configs.get('parent_index', 1)
        app.config['MOMO_CACHE'] = {}

        index_sorting_terms = self.configs.get('index_sorting_terms')
        app.config['MOMO_INDEX_SORTING_TERMS'] = (
            to_list(index_sorting_terms) if index_sorting_terms else None)
        search_sorting_terms = self.configs.get('search_sorting_terms')
        app.config['MOMO_SEARCH_SORTING_TERMS'] = (
            to_list(search_sorting_terms) if search_sorting_terms else None)
        node_sorting_terms = self.configs.get('node_sorting_terms')
        app.config['MOMO_NODE_SORTING_TERMS'] = (to_list(node_sorting_terms) if
                                                 node_sorting_terms else None)

        # load and register user-defined filter and global functions
        filters_f = os.path.join(flask_dir, 'filters.py')
        if os.path.isfile(filters_f):
            filters = imp.load_source('filters', filters_f)
            app.jinja_env.filters.update(get_public_functions(filters))

        functions_f = os.path.join(flask_dir, 'functions.py')
        if os.path.isfile(functions_f):
            functions = imp.load_source('functions', functions_f)
            app.jinja_env.globals.update(get_public_functions(functions))

        # load system and user-defined nodes functions
        app.config['MOMO_NODES_FUNCTIONS'] = self._load_functions(
            module=momo.plugins.flask.nodes,
            filename=os.path.join(flask_dir, 'nodes.py'),
        )

        # initialize default sorting function for attrs
        sort_attrs_asc = self.configs.get('sort_attrs_asc')
        app.config['MOMO_ATTRS_SORTING'] = lambda attrs: attrs
        if sort_attrs_asc is not None:
            app.config['MOMO_ATTRS_SORTING'] = \
                lambda attrs: sorted(
                    attrs,
                    key=lambda attr: attr.name,
                    reverse=not sort_attrs_asc,
                )
        app.jinja_env.filters.update(
            sort_attrs=app.config['MOMO_ATTRS_SORTING'])

        # initialize default sorting function for nodes
        sort_nodes_asc = self.configs.get('sort_nodes_asc')
        app.config['MOMO_NODES_SORTING'] = lambda nodes: nodes
        if sort_nodes_asc is not None:
            app.config['MOMO_NODES_SORTING'] = \
                lambda nodes: sort_nodes(
                    nodes,
                    func=lambda node: node.name,
                    desc=not sort_nodes_asc,
                )

        # initialize pinning (reordering) function for attrs
        pinned_attrs = self.configs.get('pinned_attrs') or []
        app.config['MOMO_ATTRS_PINNING'] = \
            self._get_pinning_function(pinned_attrs)
        app.jinja_env.filters.update(
            pin_attrs=app.config['MOMO_ATTRS_PINNING'])

        # load system and user-define sorting key functions
        app.config['MOMO_SORTING_FUNCTIONS'] = self._load_functions(
            module=momo.plugins.flask.sorting,
            filename=os.path.join(flask_dir, 'sorting.py'),
            prefix='sort_by_',
        )

        # if set, the path attr will render link with the given ip:port instead
        # of same as the flask app's ip:port
        app.config['MOMO_FILE_SERVING_ADDRESS'] = \
            self.configs.get('file_serving_address')

        # flask-bootstrap
        app.config['MOMO_USE_BOOTSTRAP_STYLES'] = \
            self.configs.get('use_bootstrap_styles', True)
        app.config['MOMO_USE_BOOTSTRAP_SCRIPTS'] = \
            self.configs.get('use_bootstrap_scripts', True)