예제 #1
0
def main_cli():
    parser = argparse.ArgumentParser(description='GNU MediaGoblin utilities.')

    subparsers = parser.add_subparsers(help='sub-command help')
    for command_name, command_struct in SUBCOMMAND_MAP.iteritems():
        if command_struct.has_key('help'):
            subparser = subparsers.add_parser(command_name,
                                              help=command_struct['help'])
        else:
            subparser = subparsers.add_parser(command_name)

        setup_func = mg_util.import_component(command_struct['setup'])
        exec_func = mg_util.import_component(command_struct['func'])

        setup_func(subparser)

        subparser.set_defaults(func=exec_func)

    args = parser.parse_args()
    args.func(args)
예제 #2
0
def main_cli():
    parser = argparse.ArgumentParser(
        description='GNU MediaGoblin utilities.')

    subparsers = parser.add_subparsers(help='sub-command help')
    for command_name, command_struct in SUBCOMMAND_MAP.iteritems():
        if command_struct.has_key('help'):
            subparser = subparsers.add_parser(
                command_name, help=command_struct['help'])
        else:
            subparser = subparsers.add_parser(command_name)

        setup_func = mg_util.import_component(command_struct['setup'])
        exec_func = mg_util.import_component(command_struct['func'])

        setup_func(subparser)

        subparser.set_defaults(func=exec_func)

    args = parser.parse_args()
    args.func(args)
예제 #3
0
    def __call__(self, environ, start_response):
        request = Request(environ)
        path_info = request.path_info

        ## Routing / controller loading stuff
        route_match = self.routing.match(path_info)

        # No matching page?
        if route_match is None:
            # Try to do see if we have a match with a trailing slash
            # added and if so, redirect
            if not path_info.endswith('/') \
                    and request.method == 'GET' \
                    and self.routing.match(path_info + '/'):
                new_path_info = path_info + '/'
                if request.GET:
                    new_path_info = '%s?%s' % (new_path_info,
                                               urllib.urlencode(request.GET))
                redirect = exc.HTTPFound(location=new_path_info)
                return request.get_response(redirect)(environ, start_response)

            # Okay, no matches.  404 time!
            return exc.HTTPNotFound()(environ, start_response)

        controller = util.import_component(route_match['controller'])
        request.start_response = start_response

        ## Attach utilities to the request object
        request.matchdict = route_match
        request.urlgen = routes.URLGenerator(self.routing, environ)
        # Do we really want to load this via middleware?  Maybe?
        request.session = request.environ['beaker.session']
        # Attach self as request.app
        # Also attach a few utilities from request.app for convenience?
        request.app = self
        request.locale = util.get_locale_from_request(request)

        request.template_env = util.get_jinja_env(self.template_loader,
                                                  request.locale)
        request.db = self.db
        request.staticdirect = self.staticdirector

        util.setup_user_in_request(request)

        return controller(request)(environ, start_response)
예제 #4
0
    def __call__(self, environ, start_response):
        request = Request(environ)
        path_info = request.path_info

        ## Routing / controller loading stuff
        route_match = self.routing.match(path_info)

        # No matching page?
        if route_match is None:
            # Try to do see if we have a match with a trailing slash
            # added and if so, redirect
            if not path_info.endswith('/') \
                    and request.method == 'GET' \
                    and self.routing.match(path_info + '/'):
                new_path_info = path_info + '/'
                if request.GET:
                    new_path_info = '%s?%s' % (
                        new_path_info, urllib.urlencode(request.GET))
                redirect = exc.HTTPFound(location=new_path_info)
                return request.get_response(redirect)(environ, start_response)

            # Okay, no matches.  404 time!
            return exc.HTTPNotFound()(environ, start_response)

        controller = util.import_component(route_match['controller'])
        request.start_response = start_response

        ## Attach utilities to the request object
        request.matchdict = route_match
        request.urlgen = routes.URLGenerator(self.routing, environ)
        # Do we really want to load this via middleware?  Maybe?
        request.session = request.environ['beaker.session']
        # Attach self as request.app
        # Also attach a few utilities from request.app for convenience?
        request.app = self
        request.locale = util.get_locale_from_request(request)
            
        request.template_env = util.get_jinja_env(
            self.template_loader, request.locale)
        request.db = self.db
        request.staticdirect = self.staticdirector

        util.setup_user_in_request(request)

        return controller(request)(environ, start_response)
예제 #5
0
def storage_system_from_paste_config(paste_config, storage_prefix):
    """
    Utility for setting up a storage system from the paste app config.

    Note that a special argument may be passed in to the paste_config
    which is "${storage_prefix}_storage_class" which will provide an
    import path to a storage system.  This defaults to
    "mediagoblin.storage:BasicFileStorage" if otherwise undefined.

    Arguments:
     - paste_config: dictionary of config parameters
     - storage_prefix: the storage system we're setting up / will be
       getting keys/arguments from.  For example 'publicstore' will
       grab all arguments that are like 'publicstore_FOO'.

    Returns:
      An instantiated storage system.

    Example:
      storage_system_from_paste_config(
        {'publicstore_base_url': '/media/',
         'publicstore_base_dir': '/var/whatever/media/'},
        'publicstore')

       Will return:
         BasicFileStorage(
           base_url='/media/',
           base_dir='/var/whatever/media')
    """
    prefix_re = re.compile('^%s_(.+)$' % re.escape(storage_prefix))

    config_params = dict(
        [(prefix_re.match(key).groups()[0], value)
         for key, value in paste_config.iteritems()
         if prefix_re.match(key)])

    if config_params.has_key('storage_class'):
        storage_class = config_params['storage_class']
        config_params.pop('storage_class')
    else:
        storage_class = "mediagoblin.storage:BasicFileStorage"

    storage_class = util.import_component(storage_class)
    return storage_class(**config_params)
예제 #6
0
def storage_system_from_config(paste_config, storage_prefix):
    """
    Utility for setting up a storage system from the paste app config.

    Note that a special argument may be passed in to the paste_config
    which is "${storage_prefix}_storage_class" which will provide an
    import path to a storage system.  This defaults to
    "mediagoblin.storage:BasicFileStorage" if otherwise undefined.

    Arguments:
     - paste_config: dictionary of config parameters
     - storage_prefix: the storage system we're setting up / will be
       getting keys/arguments from.  For example 'publicstore' will
       grab all arguments that are like 'publicstore_FOO'.

    Returns:
      An instantiated storage system.

    Example:
      storage_system_from_config(
        {'publicstore_base_url': '/media/',
         'publicstore_base_dir': '/var/whatever/media/'},
        'publicstore')

       Will return:
         BasicFileStorage(
           base_url='/media/',
           base_dir='/var/whatever/media')
    """
    prefix_re = re.compile('^%s_(.+)$' % re.escape(storage_prefix))

    config_params = dict(
        [(prefix_re.match(key).groups()[0], value)
         for key, value in paste_config.iteritems()
         if prefix_re.match(key)])

    if config_params.has_key('storage_class'):
        storage_class = config_params['storage_class']
        config_params.pop('storage_class')
    else:
        storage_class = "mediagoblin.storage:BasicFileStorage"

    storage_class = util.import_component(storage_class)
    return storage_class(**config_params)
예제 #7
0
def test_import_component():
    imported_func = util.import_component(
        'mediagoblin.tests.test_util:_import_component_testing_method')
    result = imported_func('hooobaladoobala')
    expected = u"'hooobaladoobala' is the silliest string I've ever seen"
    assert result == expected
예제 #8
0
def test_import_component():
    imported_func = util.import_component(
        'mediagoblin.tests.test_util:_import_component_testing_method')
    result = imported_func('hooobaladoobala')
    expected = u"'hooobaladoobala' is the silliest string I've ever seen"
    assert result == expected