Ejemplo n.º 1
0
def _limit_chars(_string, length):
    length = int(length)
    if length < 0:
        raise exceptions.IRException('length to crop should be int, not ' +
                                     str(length))

    return _string[:length]
Ejemplo n.º 2
0
def _limit_chars_constructor(loader, node):
    """
    Usage:
        !limit_chars [<string>, <length>]
    Method returns first param cropped to <length> chars.
    """

    params = loader.construct_sequence(node)
    if len(params) != 2:
        raise exceptions.IRException(
            'limit_chars requires two params: string length')
    return _limit_chars(params[0], params[1])
Ejemplo n.º 3
0
    def resolve(self, value):
        arguments = value.split(DictValue.ARG_SEPARATOR)
        res = {}
        for argument in arguments:
            argument = argument.strip()
            if '=' in argument:
                name, value = argument.split('=', 1)
                res[name] = value
            else:
                raise exceptions.IRException(
                    "Wrong argument format for {}. \n\t Use format: "
                    "'--options=\"option1=value1;option2=value2\"'".format(
                        value))

        return res
Ejemplo n.º 4
0
def _generate_config_file(file_name, subcommand, defaults):
    """
    Generates configuration file based on defaults from specs

    :param file_name: Name of the new configuration that will be generated.
    :param subcommand: The subparser for which the conf file is generated
    :param defaults: the default options values.
    """
    # TODO(yfried): Add required arguments to file
    # TODO (aopincar): try block is too wide
    # TODO (aopincar): if file_name exists, update it instead of overwrite it
    try:
        out_config = ConfigParser.ConfigParser()

        out_config.add_section(subcommand)
        for opt, value in defaults.iteritems():
            out_config.set(subcommand, opt, value)
        with open(file_name, 'w') as configfile:  # save
            out_config.write(configfile)
    except Exception as ex:
        raise exceptions.IRException(ex.message)