コード例 #1
0
 def _make_unique(self, k, args):
     if k not in self:
         return args
     cur = conv.to_list(self[k])
     new_args = []
     for v in args:
         if v not in cur:
             new_args.append(v)
     return new_args
コード例 #2
0
 def _make_unique(self, k, args):
     if k not in self:
         return args
     cur = conv.to_list(self[k])
     new_args = []
     for v in args:
         if v not in cur:
             new_args.append(v)
     return new_args
コード例 #3
0
ファイル: path.py プロジェクト: zhang2018git/underworld2
def make_file_names(name, prefixes=[], suffixes=[]):
    """Combine prefixes and suffixes with a name. Returns a list
    with all combinations."""

    # If we weren't given any prefixes or suffixes add an empty
    # string to each. This way we will at least check for the name
    # which should be the default behavior.
    if not prefixes:
        prefixes = ['']
    else:
        prefixes = conv.to_list(prefixes)
    if not suffixes:
        suffixes = ['']
    else:
        suffixes = conv.to_list(suffixes)
        
    for p in prefixes:
        for s in suffixes:
            yield p + name + s
コード例 #4
0
 def gather(self, args):
     d = {}
     for a in args:
         if isinstance(a, tuple) and len(a) == 2:
             if a[0] in d:
                 if not isinstance(d[a[0]], list):
                     d[a[0]] = conv.to_list(d[a[0]])
                 d[a[0]].append(a[1])
             else:
                 d[a[0]] = a[1]
     return d
コード例 #5
0
 def gather(self, args):
     d = {}
     for a in args:
         if isinstance(a, tuple) and len(a) == 2:
             if a[0] in d:
                 if not isinstance(d[a[0]], list):
                     d[a[0]] = conv.to_list(d[a[0]])
                 d[a[0]].append(a[1])
             else:
                 d[a[0]] = a[1]
     return d
コード例 #6
0
 def _enum_to_str(self, val):
     val = conv.to_list(val)
     s = []
     for v in val:
         e = self.enum.get(v, None)
         if e is None:
             if v not in self.enum.values():
                 raise "Invalid enumerated option."
             e = v
         s.append(e)
     return self._str_to_str(self._pref_delim.join(s))
コード例 #7
0
 def _enum_to_str(self, val):
     val = conv.to_list(val)
     s = []
     for v in val:
         e = self.enum.get(v, None)
         if e is None:
             if v not in self.enum.values():
                 raise "Invalid enumerated option."
             e = v
         s.append(e)
     return self._str_to_str(self._pref_delim.join(s))
コード例 #8
0
 def add_option(self, opts, *args):
     opts = conv.to_list(opts)
     opts += args
     for o in opts:
         if o.name in self.options:
             raise RuntimeError, "Duplicate option name %s." % repr(o.name)
         self.options[o.name] = o
         self._order.append(o.name)
         for f in o._flg_to_var.iterkeys():
             if f in self._flg_to_opt:
                 raise "Duplicate option flag in OptionSet."
             self._flg_to_opt[f] = o
     self._expr = None
コード例 #9
0
 def add_option(self, opts, *args):
     opts = conv.to_list(opts)
     opts += args
     for o in opts:
         if o.name in self.options:
             raise RuntimeError, "Duplicate option name %s."%repr(o.name)
         self.options[o.name] = o
         self._order.append(o.name)
         for f in o._flg_to_var.iterkeys():
             if f in self._flg_to_opt:
                 raise "Duplicate option flag in OptionSet."
             self._flg_to_opt[f] = o
     self._expr = None
コード例 #10
0
 def prepend(self, k, *args, **kw):
     args = conv.flatten(list(args))
     args = [a for a in args if a != ""]
     if k in self:
         if not isinstance(self[k], list):
             self[k] = conv.to_list(self[k])
         self[k] = list(args) + self[k]
     elif len(args) == 1:
         if kw.get("make_list", False):
             self[k] = [args[0]]
         else:
             self[k] = args[0]
     elif len(args) > 1:
         self[k] = list(args)
コード例 #11
0
 def prepend(self, k, *args, **kw):
     args = conv.flatten(list(args))
     args = [a for a in args if a != ""]
     if k in self:
         if not isinstance(self[k], list):
             self[k] = conv.to_list(self[k])
         self[k] = list(args) + self[k]
     elif len(args) == 1:
         if kw.get("make_list", False):
             self[k] = [args[0]]
         else:
             self[k] = args[0]
     elif len(args) > 1:
         self[k] = list(args)
コード例 #12
0
ファイル: models.py プロジェクト: furious-luke/django-utils
def model_fill(obj, info_dict):
    all_fields = obj._meta.fields + obj._meta.many_to_many
    for field in all_fields:
        if field.name in ['id', 'pk']:
            continue
        if field.name not in info_dict:
            continue
        name = field.name
        cur_val = getattr(obj, field.name)
        if isinstance(field, ManyToManyField):
            for val in conv.to_list(info_dict[field.name]):
                if not cur_val.filter(pk=val.pk).exists():
                    cur_val.add(val)
        else:
            if not cur_val:
                setattr(obj, field.name, info_dict[field.name])
コード例 #13
0
ファイル: models.py プロジェクト: mcfoi/seipetali
def model_fill(obj, info_dict):
    all_fields = obj._meta.fields + obj._meta.many_to_many
    for field in all_fields:
        if field.name in ['id', 'pk']:
            continue
        if field.name not in info_dict:
            continue
        name = field.name
        cur_val = getattr(obj, field.name)
        if isinstance(field, ManyToManyField):
            for val in conv.to_list(info_dict[field.name]):
                if not cur_val.filter(pk=val.pk).exists():
                    cur_val.add(val)
        else:
            if not cur_val:
                setattr(obj, field.name, info_dict[field.name])
コード例 #14
0
 def append(self, k, *args, **kw):
     args = conv.flatten(list(args))
     args = [a for a in args if a != ""]
     if isinstance(k, (dict, Environment)):
         self._handle_dict(k, self.append, **kw)
     else:
         if k in self:
             if not isinstance(self[k], list):
                 self[k] = conv.to_list(self[k])
             self[k] += list(args)
         elif len(args) == 1:
             if kw.get("make_list", False):
                 self[k] = [args[0]]
             else:
                 self[k] = args[0]
         elif len(args) > 1:
             self[k] = list(args)
コード例 #15
0
 def append(self, k, *args, **kw):
     args = conv.flatten(list(args))
     args = [a for a in args if a != ""]
     if isinstance(k, (dict, Environment)):
         self._handle_dict(k, self.append, **kw)
     else:
         if k in self:
             if not isinstance(self[k], list):
                 self[k] = conv.to_list(self[k])
             self[k] += list(args)
         elif len(args) == 1:
             if kw.get("make_list", False):
                 self[k] = [args[0]]
             else:
                 self[k] = args[0]
         elif len(args) > 1:
             self[k] = list(args)
コード例 #16
0
    def __init__(self, name, *args, **kw):
        self.name = name

        # If we were given a 'variants' keyword then take that as
        # one or a list of OptionVariant instances.
        vars = kw.get("variants", None)
        if vars is None:
            # Otherwise all the parameters we were given are to create
            # a new OptionVariant.
            vars = OptionVariant(*args, **kw)
        self.variants = conv.to_list(vars)

        # We take the type of the first variant to be the type of
        # this option.
        self.type = self.variants[0].type

        # This mapping is for moving from a flag string to the variant
        # that represents it.
        self._flg_to_var = {}

        for v in self.variants:

            # Make sure we don't have multiple types in the variants.
            if self.type != v.type:
                raise "Inconsistent OptionVariant types."

            for f in v.flags:

                # Make sure we don't have multiple variants trying to
                # represent the same flag.
                if f in self._flg_to_var:
                    raise "Duplicate option flags in Option."

                # Inser the flag/variant into the mapping.
                self._flg_to_var[f] = v

        self.help = kw.get("help", None)
        self.default = kw.get("default", None)
コード例 #17
0
    def __init__(self, name, *args, **kw):
        self.name = name

        # If we were given a 'variants' keyword then take that as
        # one or a list of OptionVariant instances.
        vars = kw.get("variants", None)
        if vars is None:
            # Otherwise all the parameters we were given are to create
            # a new OptionVariant.
            vars = OptionVariant(*args, **kw)
        self.variants = conv.to_list(vars)

        # We take the type of the first variant to be the type of
        # this option.
        self.type = self.variants[0].type

        # This mapping is for moving from a flag string to the variant
        # that represents it.
        self._flg_to_var = {}

        for v in self.variants:

            # Make sure we don't have multiple types in the variants.
            if self.type != v.type:
                raise "Inconsistent OptionVariant types."

            for f in v.flags:

                # Make sure we don't have multiple variants trying to
                # represent the same flag.
                if f in self._flg_to_var:
                    raise "Duplicate option flags in Option."

                # Inser the flag/variant into the mapping.
                self._flg_to_var[f] = v

        self.help = kw.get("help", None)
        self.default = kw.get("default", None)
コード例 #18
0
ファイル: path.py プロジェクト: zhang2018git/underworld2
def find(name, dirs=[], prefixes=[], suffixes=[], max=0):
    """Try to locate a file system entry by combining prefixes
    and suffixes with a name, then searching a set of provided
    directories. By default a list of all matches is returned,
    but this can be limited by 'max'."""

    # If we weren't given any directories to search try the current
    # working directory.
    if not dirs:
        dirs = [os.getcwd()]
    else:
        dirs = conv.to_list(dirs)

    names = [n for n in make_file_names(name, prefixes, suffixes)]
    cur = 0
    for d in dirs:
        for n in names:
            p = os.path.join(d, n)
            if os.path.exists(p):
                yield p
                if max:
                    cur += 1
                    if cur == max:
                        return
コード例 #19
0
    def __init__(self, flags, type="string", **kw):
        """flags: Either a string or list of the exact flags that
        can represent this option variant.
        type: A string representing the kind of option. One of "string",
        "enum" or "bool"."""

        # Convert the flags to a list.
        self.flags = conv.to_list(flags)

        # Extract parameters from the keywords.
        self.seps = conv.to_list(kw.get("seps", ["=", " "]))
        self.pref_sep = sep and kw.get("pref_sep", sep[0]) or None
        self.delims = conv.to_list(kw.get("delims", ""))
        self.pref_delim = delim and kw.get("pref_delim", delim[0]) or None

        # Error on duplicate flags.
        for i in range(len(self.flags) - 1):
            if self.flags[i] in self.flags[i + 1:]:
                raise "Duplicate flag given to OptionVariant."

        # Error on duplicate separators.
        for i in range(len(self.sep) - 1):
            if self.sep[i] in self.sep[i + 1:]:
                raise "Duplicate separators given to OptionVariant."

        # Error on separators that are not single characters.
        for s in self.seps:
            if len(s) != 1:
                raise "Separator of length != 1."

        # Error on duplicate delimiters.
        for i in range(len(self.delims) - 1):
            if self.delims[i] in self.delims[i + 1:]:
                raise "Duplicate delimiter given to OptionVariant."

        # Error on delimiters that are not single characters or
        # delimiters that are whitespaces.
        for d in self.delims:
            if len(d) != 1:
                raise "Delimeter of length != 1."
            if d == " ":
                raise "Whitespace delimeter."

        # Sort the separators and delimiters so that we don't mistake shorter
        # ones for longer ones.
        self.seps.sort(lambda x,y: len(y)-len(x))
        self.delims.sort(lambda x,y: len(y)-len(x))

        #
        # Handle the different kinds of option types. We need to set
        # the conversion method "to_str", set the parsing method
        # "parse_val" and setup any type specific details.
        #

        if self.type == "bool":

            self.to_str = self._bool_to_str
            self.parse_val = self._parse_bool

            # Boolean options accept an optional parameter defining
            # what indicates a positive and negative value. It should be
            # specified as a tuple of two lists, the first being the
            # positive values the second the negative.
            bf = kw.get("bool_values", (["", "yes", "1", "t"], ["no", "0", "f"]))

            # Boolean's also accept additional parameters for specifying
            # which positive and negative values are preferential. These
            # will influence conversion of option lists to strings. By default
            # the first values in the "bool_values" list are considered
            # preferential.
            self._pref_true_flag = kw.get("pref_true_value", conv.to_list(bf[0])[0])
            self._pref_false_flag = kw.get("pref_false_value", conv.to_list(bf[1])[0])

            # Setup the values. We sort the list by the length of the values
            # in descending order so that when we parse option strings we don't
            # mistake shorter options for longer ones.
            self.bool_values = (zip(conv.to_list(bf[0]), [True for i in range(len(bf[0]))]) +
                               zip(conv.to_list(bf[1]), [False for i in range(len(bf[1]))]))
            self.bool_values.sort(lambda x,y: len(y[0])-len(x[0]))

        elif self.type == "enum":

            self.to_str = self._enum_to_str
            self.parse_val = self._parse_enum

            # An additional option for enumerations is "enum", which defines a
            # a mapping from the string values expected in the option strings to
            # an internal string representation. It accepts either a dictionary
            # or a string.  In the case of a list each entry maps to itself.
            self.enum = kw.get("enum", {})
            if isinstance(self.enum, list):
                self.enum = dict(zip(self.enum, self.enum))

        else:

            self.to_str = self._str_to_str
            self.parse_val = self._parse_str
コード例 #20
0
    def __init__(self, flags, type="string", **kw):
        """flags: Either a string or list of the exact flags that
        can represent this option variant.
        type: A string representing the kind of option. One of "string",
        "enum" or "bool"."""

        # Convert the flags to a list.
        self.flags = conv.to_list(flags)

        # Extract parameters from the keywords.
        self.seps = conv.to_list(kw.get("seps", ["=", " "]))
        self.pref_sep = sep and kw.get("pref_sep", sep[0]) or None
        self.delims = conv.to_list(kw.get("delims", ""))
        self.pref_delim = delim and kw.get("pref_delim", delim[0]) or None

        # Error on duplicate flags.
        for i in range(len(self.flags) - 1):
            if self.flags[i] in self.flags[i + 1:]:
                raise "Duplicate flag given to OptionVariant."

        # Error on duplicate separators.
        for i in range(len(self.sep) - 1):
            if self.sep[i] in self.sep[i + 1:]:
                raise "Duplicate separators given to OptionVariant."

        # Error on separators that are not single characters.
        for s in self.seps:
            if len(s) != 1:
                raise "Separator of length != 1."

        # Error on duplicate delimiters.
        for i in range(len(self.delims) - 1):
            if self.delims[i] in self.delims[i + 1:]:
                raise "Duplicate delimiter given to OptionVariant."

        # Error on delimiters that are not single characters or
        # delimiters that are whitespaces.
        for d in self.delims:
            if len(d) != 1:
                raise "Delimeter of length != 1."
            if d == " ":
                raise "Whitespace delimeter."

        # Sort the separators and delimiters so that we don't mistake shorter
        # ones for longer ones.
        self.seps.sort(lambda x, y: len(y) - len(x))
        self.delims.sort(lambda x, y: len(y) - len(x))

        #
        # Handle the different kinds of option types. We need to set
        # the conversion method "to_str", set the parsing method
        # "parse_val" and setup any type specific details.
        #

        if self.type == "bool":

            self.to_str = self._bool_to_str
            self.parse_val = self._parse_bool

            # Boolean options accept an optional parameter defining
            # what indicates a positive and negative value. It should be
            # specified as a tuple of two lists, the first being the
            # positive values the second the negative.
            bf = kw.get("bool_values",
                        (["", "yes", "1", "t"], ["no", "0", "f"]))

            # Boolean's also accept additional parameters for specifying
            # which positive and negative values are preferential. These
            # will influence conversion of option lists to strings. By default
            # the first values in the "bool_values" list are considered
            # preferential.
            self._pref_true_flag = kw.get("pref_true_value",
                                          conv.to_list(bf[0])[0])
            self._pref_false_flag = kw.get("pref_false_value",
                                           conv.to_list(bf[1])[0])

            # Setup the values. We sort the list by the length of the values
            # in descending order so that when we parse option strings we don't
            # mistake shorter options for longer ones.
            self.bool_values = (
                zip(conv.to_list(bf[0]), [True for i in range(len(bf[0]))]) +
                zip(conv.to_list(bf[1]), [False for i in range(len(bf[1]))]))
            self.bool_values.sort(lambda x, y: len(y[0]) - len(x[0]))

        elif self.type == "enum":

            self.to_str = self._enum_to_str
            self.parse_val = self._parse_enum

            # An additional option for enumerations is "enum", which defines a
            # a mapping from the string values expected in the option strings to
            # an internal string representation. It accepts either a dictionary
            # or a string.  In the case of a list each entry maps to itself.
            self.enum = kw.get("enum", {})
            if isinstance(self.enum, list):
                self.enum = dict(zip(self.enum, self.enum))

        else:

            self.to_str = self._str_to_str
            self.parse_val = self._parse_str
コード例 #21
0
 def _str_to_str(self, val):
     val = conv.to_list(val)
     return "%s%s%s" % (self.flags[0], self._pref_sep,
                        self._pref_delim.join(val))
コード例 #22
0
 def _str_to_str(self, val):
     val = conv.to_list(val)
     return "%s%s%s" % (self.flags[0], self._pref_sep, self._pref_delim.join(val))