Example #1
0
def validate_opts(mod_name, opts, only_mangle=False):
    """
    Validate opts checks for option correctness and fills in
    defaults. TODO: can oshell._validate_opts() use this function instead?
    """
    doc = api.documentation(mod_name)
    if not doc: return False
    opts_doc = doc["opts_doc"]
    mangled_fields = mangle_fields(mod_name)
    for k in opts_doc:
        if not only_mangle or k in mangled_fields:
            if not opts.has_key(k): # No option provided for this key
                if opts_doc[k].has_key("default"):
                    logger.debug("%s option '%s' not provided. Setting to default value.", mod_name, k)
                    if opts_doc[k]["default"] == None:
                        logger.error("Module %s has no default for required key %s", mod_name, k)
                        return False
                    opts[k] = opts_doc[k]["default"]
                else:
                    logger.error("Module %s has no default for required key %s", mod_name, k)
                    return False
            try:
                tmp = opts_doc[k]["type"](opts[k])
            except (ValueError, TypeError):
             #if not isinstance(opts[k], opts_doc[k]["type"]): # Wrong option type provided
                logger.error("%s option '%s' type mismatch.", mod_name, k)
                return False
    return True
Example #2
0
def mapper(oid, opts, jobid=False):
    logger.debug("mapper()")
    src = api.source(oid)
    if api.documentation(src)["set"]:
        return None
    if api.exists(name, oid, opts):
        return oid
    out_histo = build_ngrams(api.retrieve(src, oid, opts)["data"], opts["n"])
    api.store(name, oid, out_histo, opts)
    return oid
Example #3
0
def mapper(oid, opts, jobid=False):
    logger.debug("mapper()")
    src = api.source(oid)
    if api.documentation(src)["set"]:
        return None
    if api.exists(name, oid, opts):
        return oid
    opcodes = api.get_field("opcodes", oid, "opcodes")
    if not opcodes: return None
    out_histo = build_histo(opcodes)
    api.store(name, oid, out_histo, opts)
    return oid
Example #4
0
def mapper(oid, opts, jobid=False):
    logger.debug("mapper()")
    src = api.source(oid)
    if api.documentation(src)["set"]:
        return None
    if api.exists(name, oid, opts):
        return oid
    nops = api.retrieve("nops", oid, opts)
    if not nops:
        return None
    out_histo = nop_histo(nops)
    api.store(name, oid, out_histo, opts)
    return oid
Example #5
0
def mangle_fields(mod_name): # exported
    """ Returns the keys of the mangled options for the given module. The
    returned keys are sorted. """
    doc = api.documentation(mod_name)
    all_opts = doc["opts_doc"]
        
    mangles = list()
    for field in all_opts:
        try:
            if all_opts[field]["mangle"]:
                mangles.append(field)
        except KeyError:
            raise otypes.OxideError("%s Module writer left out the mangle field in options."%mod_name)
    mangles.sort()
    return mangles
Example #6
0
def build_suffix(mod_name, opts):
    """
    Support specifically for filesystem storage backend. The suffix will look
    like this: val1=val2=val3, where the values are in the order of their
    respective keys. Only mangle options are included in the suffix.
    """
    
    suffix = "" # default val
    mfields = mangle_fields(mod_name)
    if len(mfields) != 0: 
        doc = api.documentation(mod_name)
        slist = []
        for field in mfields: 
            slist.append(str((opts[field])))
        suffix = SUFFIX_DELIM.join(slist)
    return suffix
Example #7
0
def parse_suffix(mod_name, suffix):
    """
    Given module and suffix, return dictionary of mangle options with fields
    filled in from the suffix.
    """

    doc = api.documentation(mod_name)
    all_opts = doc["opts_doc"]

    # returns field names, sorted (same order as their vals appear in suffix)

    mangles = mangle_fields(mod_name)
    vals = [cast_string_to_type (s)
            for s in suffix.split(SUFFIX_DELIM)]
    
    if len(mangles) != len(vals):
        raise otypes.OxideError("suffix must contain as many values as there are mangle options")
    
    # build the key->val pairs from the suffix
    mangle_dict = dict (zip (mangles, vals))

    return mangle_dict