Ejemplo n.º 1
0
def list_or_caseless_string(value):
    """
    Converts the value into a list of ``caseless_string``'s. If the value is
    not iterable a one-element list with stringified value will be returned.

    """
    if isinstance(value, str):
        return [caseless_string(value)]
    else:
        try:
            return list(map(caseless_string, value))
        except ValueError:
            return [caseless_string(value)]
Ejemplo n.º 2
0
def list_or_caseless_string(value):
    """
    Converts the value into a list of ``caseless_string``'s. If the value is
    not iterable a one-element list with stringified value will be returned.

    """
    if isinstance(value, str):
        return [caseless_string(value)]
    else:
        try:
            return list(map(caseless_string, value))
        except ValueError:
            return [caseless_string(value)]
Ejemplo n.º 3
0
    def __init__(self, path, path_kind):
        self.path = path

        path_kind = caseless_string(path_kind)
        if path_kind not in self.path_kinds:
            msg = '{} is not a valid path_kind [{}]'
            raise ValueError(msg.format(path_kind, ' '.join(self.path_kinds)))
        self.path_kind = path_kind
Ejemplo n.º 4
0
 def cpu_names(self):
     cpu_names = []
     global_name = None
     for section in self.sections:
         if 'processor' in section:
             if 'CPU part' in section:
                 cpu_names.append(_get_part_name(section))
             elif 'model name' in section:
                 cpu_names.append(_get_model_name(section))
             else:
                 cpu_names.append(None)
         elif 'CPU part' in section:
             global_name = _get_part_name(section)
     return [caseless_string(c or global_name) for c in cpu_names]
Ejemplo n.º 5
0
def enum(args, start=0, step=1):
    """
    Creates a class with attributes named by the first argument.
    Each attribute is a ``level`` so they behave is integers in comparisons.
    The value of the first attribute is specified by the second argument
    (``0`` if not specified).

    ::
        MyEnum = enum(['A', 'B', 'C'])

    is roughly equivalent of::

        class MyEnum(object):
            A = 0
            B = 1
            C = 2

    however it also implement some specialized behaviors for comparisons and
    instantiation.

    """
    class Enum(with_metaclass(_EnumMeta, object)):
        @classmethod
        def from_pod(cls, pod):
            lv = level.from_pod(pod)
            for enum_level in cls.levels:
                if enum_level == lv:
                    return enum_level
            msg = 'Unexpected value "{}" for enum.'
            raise ValueError(msg.format(pod))

        def __new__(cls, name):
            for attr_name in dir(cls):
                if attr_name.startswith('__'):
                    continue

                attr = getattr(cls, attr_name)
                if name == attr:
                    return attr

            try:
                return Enum.from_pod(name)
            except ValueError:
                raise ValueError('Invalid enum value: {}'.format(repr(name)))

    reserved = ['values', 'levels', 'names']

    levels = []
    n = start
    for v in args:
        id_v = identifier(v)
        if id_v in reserved:
            message = 'Invalid enum level name "{}"; must not be in {}'
            raise ValueError(message.format(v, reserved))
        name = caseless_string(id_v)
        lv = level(v, n)
        setattr(Enum, name, lv)
        levels.append(lv)
        n += step

    setattr(Enum, 'levels', levels)
    setattr(Enum, 'values', [lvl.value for lvl in levels])
    setattr(Enum, 'names', [lvl.name for lvl in levels])

    return Enum
Ejemplo n.º 6
0
 def __init__(self, name, value):
     self.name = caseless_string(name)
     self.value = numeric(value)
Ejemplo n.º 7
0
def enum(args, start=0, step=1):
    """
    Creates a class with attributes named by the first argument.
    Each attribute is a ``level`` so they behave is integers in comparisons.
    The value of the first attribute is specified by the second argument
    (``0`` if not specified).

    ::
        MyEnum = enum(['A', 'B', 'C'])

    is roughly equivalent of::

        class MyEnum(object):
            A = 0
            B = 1
            C = 2

    however it also implement some specialized behaviors for comparisons and
    instantiation.

    """

    class Enum(with_metaclass(_EnumMeta, object)):

        @classmethod
        def from_pod(cls, pod):
            lv = level.from_pod(pod)
            for enum_level in cls.levels:
                if enum_level == lv:
                    return enum_level
            msg = 'Unexpected value "{}" for enum.'
            raise ValueError(msg.format(pod))

        def __new__(cls, name):
            for attr_name in dir(cls):
                if attr_name.startswith('__'):
                    continue

                attr = getattr(cls, attr_name)
                if name == attr:
                    return attr

            raise ValueError('Invalid enum value: {}'.format(repr(name)))

    reserved = ['values', 'levels', 'names']

    levels = []
    n = start
    for v in args:
        id_v = identifier(v)
        if id_v in reserved:
            message = 'Invalid enum level name "{}"; must not be in {}'
            raise ValueError(message.format(v, reserved))
        name = caseless_string(id_v)
        lv = level(v, n)
        setattr(Enum, name, lv)
        levels.append(lv)
        n += step

    setattr(Enum, 'levels', levels)
    setattr(Enum, 'values', [lvl.value for lvl in levels])
    setattr(Enum, 'names', [lvl.name for lvl in levels])

    return Enum
Ejemplo n.º 8
0
 def __init__(self, name, value):
     self.name = caseless_string(name)
     self.value = numeric(value)