示例#1
0
def parser(content, objconf, skip=False, **kwargs):
    """ Parsers the pipe content

    Args:
        content (scalar): The content to cast
        objconf (obj): The pipe configuration (an Objectify instance)
        skip (bool): Don't parse the content
        kwargs (dict): Keyword arguments

    Kwargs:
        assign (str): Attribute to assign parsed content (default: exchangerate)
        stream (dict): The original item

    Returns:
        dict: The item

    Examples:
        >>> from meza.fntools import Objectify
        >>>
        >>> item = {'content': '1.0'}
        >>> objconf = Objectify({'type': 'int'})
        >>> kwargs = {'stream': item, 'assign': 'content'}
        >>> parser(item['content'], objconf, **kwargs)
        1
    """
    return kwargs['stream'] if skip else cast(content, objconf.type)
示例#2
0
def parser(address, objconf, skip=False, **kwargs):
    """ Parses the pipe content

    Args:
        address (str): The address to lookup
        objconf (obj): The pipe configuration (an Objectify instance)
        skip (bool): Don't parse the content
        kwargs (dict): Keyword arguments

    Kwargs:
        assign (str): Attribute to assign parsed content (default: exchangerate)
        stream (dict): The original item

    Returns:
        Tuple(dict, bool): Tuple of (item, skip)

    Examples:
        >>> from riko import get_path
        >>> from meza.fntools import Objectify
        >>>
        >>> item = {'content': 'GBP'}
        >>> objconf = Objectify({'type': 'currency'})
        >>> kwargs = {'stream': item, 'assign': 'content'}
        >>> country = 'United Kingdom'
        >>> parser(item['content'], objconf, **kwargs)['country'] == country
        True
    """
    if skip:
        location = kwargs['stream']
    else:
        location = cast(address, 'location', loc_type=objconf.type)

    return location
示例#3
0
文件: input.py 项目: mnjstwins/riko
def parser(_, objconf, skip=False, **kwargs):
    """ Obtains the user input

    Args:
        _ (None): Ignored
        objconf (obj): The pipe configuration (an Objectify instance)
        skip (bool): Don't prompt for input

    Returns:
        dict: The casted user input

    Examples:
        >>> from meza.fntools import Objectify
        >>>
        >>> inputs = {'age': '30'}
        >>> conf = {'prompt': 'How old are you?', 'type': 'int'}
        >>> objconf = Objectify(conf)
        >>> kwargs = {'inputs': inputs, 'assign': 'age'}
        >>> parser(None, objconf, **kwargs) == {'age': 30}
        True
    """
    if kwargs.get('inputs'):
        value = kwargs['inputs'].get(kwargs['assign'], objconf.default)
    elif kwargs.get('test') or skip:
        value = objconf.default
    else:
        raw = input("%s (default=%s) " % (objconf.prompt, objconf.default))
        value = raw or objconf.default

    casted = cast(value, objconf.type)
    return casted if hasattr(casted, 'keys') else {kwargs['assign']: casted}
示例#4
0
文件: geolocate.py 项目: nerevu/riko
def parser(address, objconf, skip=False, **kwargs):
    """ Parses the pipe content

    Args:
        address (str): The address to lookup
        objconf (obj): The pipe configuration (an Objectify instance)
        skip (bool): Don't parse the content
        kwargs (dict): Keyword arguments

    Kwargs:
        assign (str): Attribute to assign parsed content (default: exchangerate)
        stream (dict): The original item

    Returns:
        Tuple(dict, bool): Tuple of (item, skip)

    Examples:
        >>> from riko import get_path
        >>> from meza.fntools import Objectify
        >>>
        >>> item = {'content': 'GBP'}
        >>> objconf = Objectify({'type': 'currency'})
        >>> kwargs = {'stream': item, 'assign': 'content'}
        >>> country = 'United Kingdom'
        >>> parser(item['content'], objconf, **kwargs)['country'] == country
        True
    """
    if skip:
        location = kwargs['stream']
    else:
        location = cast(address, 'location', loc_type=objconf.type)

    return location