Ejemplo n.º 1
0
def get_method_filename(app_name, resource_name, method_name):
    method_abs_path = '%s/%s' % (get_resource_abs_path(
        app_name, resource_name), get_app_file(method_name))
    if os.path.isfile(method_abs_path):
        return method_abs_path
    else:
        exception.handle('Method file does not exist: %s' % method_name)
Ejemplo n.º 2
0
def get_resource_abs_path(app_name, resource_name):
    resource_abs_path = "%s/%s" % (get_resources_abs_path(app_name), resource_name)

    if os.path.isdir(resource_abs_path):
        return resource_abs_path
    else:
        exception.handle("Resource path does not exist: %s" % resource_name)
Ejemplo n.º 3
0
def get_method(app_name, version, resource_name, method_name):
    '''
    Get method's setting
    '''
    try:
        return apps[app_name][version]['resources'][resource_name][method_name]
    except KeyError:
        exception.handle('No method found')
Ejemplo n.º 4
0
def get_resource_abs_path(app_name, resource_name):
    resource_abs_path = '%s/%s' % (get_resources_abs_path(app_name),
                                   resource_name)

    if os.path.isdir(resource_abs_path):
        return resource_abs_path
    else:
        exception.handle('Resource path does not exist: %s' % resource_name)
Ejemplo n.º 5
0
def get_method(app_name, version, resource_name, method_name):
    '''
    Get method's setting
    '''
    try:
        return apps[app_name][version]['resources'][resource_name][method_name]
    except KeyError:
        exception.handle('No method found')
Ejemplo n.º 6
0
def load_and_parse(filename):
    '''
    Load and parse JSON file
    '''
    with open(filename, 'r') as f:
        try:
            return json.load(f)
        except ValueError as detail:
            exception.handle('Failed to parse the JSON.', detail)
Ejemplo n.º 7
0
def get_app_abs_path(app_name):
    """
    Get application absolute path
    """
    app_abs_path = os.path.abspath(os.path.join(setting.APPS_DIRECTORY, app_name))

    if os.path.isdir(app_abs_path):
        return app_abs_path
    else:
        exception.handle("Invalid application directory: %s" % app_abs_path)
Ejemplo n.º 8
0
def get_resources_abs_path(app_name):
    """
    Validate and return the resource directory
    """
    abs_resources_dir = os.path.abspath(os.path.join(get_app_abs_path(app_name), setting.RESOURCES_DIRECTORY))

    if os.path.isdir(abs_resources_dir):
        return abs_resources_dir
    else:
        exception.handle("Invalid resources directory: %s" % abs_resources_dir)
Ejemplo n.º 9
0
def get_app_abs_path(app_name):
    '''
    Get application absolute path
    '''
    app_abs_path = os.path.abspath(
        os.path.join(setting.APPS_DIRECTORY, app_name))

    if os.path.isdir(app_abs_path):
        return app_abs_path
    else:
        exception.handle('Invalid application directory: %s' % app_abs_path)
Ejemplo n.º 10
0
def get_resources_abs_path(app_name):
    '''
    Validate and return the resource directory
    '''
    abs_resources_dir = os.path.abspath(
        os.path.join(get_app_abs_path(app_name), setting.RESOURCES_DIRECTORY))

    if os.path.isdir(abs_resources_dir):
        return abs_resources_dir
    else:
        exception.handle('Invalid resources directory: %s' % abs_resources_dir)
Ejemplo n.º 11
0
def add_resource(app_name, version, resource_name):
    '''
    Add a new resource to in-memory dictionary
    '''
    if not apps.has_key(app_name):
        exception.handle('Undefined `app_name`, call `add_app` before `add_resource`')

    if not apps[app_name].has_key(version):
        exception.handle('Undefined app version')

    apps[app_name][version]['resources'][resource_name] = {}

    # add log
    logger.info('Adding resource `%s` to application `%s`' % (resource_name, app_name))
Ejemplo n.º 12
0
def get_app_setting_filename(app_name):
    """
    Get application setting filename
    """
    app_abs_path = get_app_abs_path(app_name)

    app_setting_filename = os.path.join(app_abs_path, get_app_file(setting.APP_SETTING_FILENAME))

    if os.path.isfile(app_setting_filename):
        return app_setting_filename
    else:
        exception.handle(
            "The given application doesn't have setting file: %s" % get_app_file(setting.APP_SETTING_FILENAME)
        )
Ejemplo n.º 13
0
def get_app_setting_filename(app_name):
    '''
    Get application setting filename
    '''
    app_abs_path = get_app_abs_path(app_name)

    app_setting_filename = os.path.join(
        app_abs_path, get_app_file(setting.APP_SETTING_FILENAME))

    if os.path.isfile(app_setting_filename):
        return app_setting_filename
    else:
        exception.handle(
            'The given application doesn\'t have setting file: %s' %
            get_app_file(setting.APP_SETTING_FILENAME))
Ejemplo n.º 14
0
def add_resource(app_name, version, resource_name):
    '''
    Add a new resource to in-memory dictionary
    '''
    if not apps.has_key(app_name):
        exception.handle(
            'Undefined `app_name`, call `add_app` before `add_resource`')

    if not apps[app_name].has_key(version):
        exception.handle('Undefined app version')

    apps[app_name][version]['resources'][resource_name] = {}

    # add log
    logger.info('Adding resource `%s` to application `%s`' %
                (resource_name, app_name))
Ejemplo n.º 15
0
    def render_GET(self, request):
        logger.info('Processing request for %s' % request.path)
        route_item = route.get(request.postpath)

        if route_item is None:
            exception.handle('No route found for: %s' % request.path)

        route_object, route_params = route_item

        # set route name
        route_name = route_object[1]

        if route_name == 'root':
            return 'root'
        elif route_name == 'app':
            return 'app: %s' % route_params
        elif route_name == 'resource':
            return 'resource'
        elif route_name == 'method':
            return request_handler.method(request, route_params)
Ejemplo n.º 16
0
def parse_param(value):
    '''
    Parse resources JSON setting file parameters
    '''

    formatter_name = None
    is_list = False
    list_count = 10

    # if the value is a string (single value)
    if isinstance(value, basestring):
        formatter_name = value
    # or if it's a dictionary
    elif isinstance(value, dict):
        if 'liaar_formatter' in value:
            formatter_name = value['liaar_formatter']
        if 'liaar_type' in value:
            is_list = True
        if 'liaar_count' in value:
            list_count = value['liaar_count']

        nested_result = {}
        # now we should iterate over keys and call `parse_param` again
        if formatter_name is None:
            for value_item in value:
                nested_result[value_item] = parse_param(value[value_item])

            return nested_result
    else:
        exception.handle('Unknown resource property: %s' % value)

    if formatter_name is None:
        exception.handle('Formatter is empty: %s' % value)

    # ok let's execute the function
    try:
        faker = Factory.create()

        if not is_list:
            return faker.__getattribute__(formatter_name)()
        else:
            list_output = []
            for i in range(list_count):
                list_output.append(faker.__getattribute__(formatter_name)())

            return list_output
    except AttributeError:
        exception.handle('Can\'t parse resource\'s property: %s' % value)
Ejemplo n.º 17
0
def add_method(app_name, version, resource_name, method_name, method_setting):
    '''
    Add a new method to in-memory dictionary
    '''
    if not apps.has_key(app_name):
        exception.handle('Undefined `app_name`, call `add_app` before `add_resource`')

    if not apps[app_name].has_key(version):
        exception.handle('Undefined app version')

    if not apps[app_name][version]['resources'].has_key(resource_name):
        exception.handle('Undefined resource name')

    apps[app_name][version]['resources'][resource_name][method_name] = method_setting

    # add log
    logger.info('Adding method `%s` to resource `%s`' % (method_name, resource_name))
Ejemplo n.º 18
0
def add_method(app_name, version, resource_name, method_name, method_setting):
    '''
    Add a new method to in-memory dictionary
    '''
    if not apps.has_key(app_name):
        exception.handle(
            'Undefined `app_name`, call `add_app` before `add_resource`')

    if not apps[app_name].has_key(version):
        exception.handle('Undefined app version')

    if not apps[app_name][version]['resources'].has_key(resource_name):
        exception.handle('Undefined resource name')

    apps[app_name][version]['resources'][resource_name][
        method_name] = method_setting

    # add log
    logger.info('Adding method `%s` to resource `%s`' %
                (method_name, resource_name))
Ejemplo n.º 19
0
def get_method_filename(app_name, resource_name, method_name):
    method_abs_path = "%s/%s" % (get_resource_abs_path(app_name, resource_name), get_app_file(method_name))
    if os.path.isfile(method_abs_path):
        return method_abs_path
    else:
        exception.handle("Method file does not exist: %s" % method_name)