def get_resource_files(self, resource): """Get a dict for all files assigned to a resource. First we calculate the files matching the file expression and then we apply all translation excpetions. The resulting dict will be in this format: { 'en': 'path/foo/en/bar.po', 'de': 'path/foo/de/bar.po', 'es': 'path/exceptions/es.po'} NOTE: All paths are relative to the root of the project """ tr_files = {} if self.config.has_section(resource): try: file_filter = self.config.get(resource, "file_filter") except configparser.NoOptionError: file_filter = "$^" source_lang = self.config.get(resource, "source_lang") source_file = self.get_source_file(resource) expr_re = utils.regex_from_filefilter(file_filter, self.root) expr_rec = re.compile(expr_re) for f_path in utils.files_in_project(self.root): match = expr_rec.match(posix_path(f_path)) if match: lang = match.group(1) if lang != source_lang: f_path = os.path.relpath(f_path, self.root) if f_path != source_file: tr_files.update({lang: f_path}) for (name, value) in self.config.items(resource): if name.startswith("trans."): value = native_path(value) lang = name.split('.')[1] # delete language which has same file if value in list(tr_files.values()): keys = [] for k, v in six.iteritems(tr_files): if v == value: keys.append(k) if len(keys) == 1: del tr_files[keys[0]] else: raise Exception("Your configuration seems wrong. " "You have multiple languages " "pointing to the same file.") # Add language with correct file tr_files.update({lang: value}) return tr_files return None
def update(self, other=None, **kwargs): # Copied from python's UserDict.DictMixin code. # Make progressively weaker assumptions about "other" if other is None: pass elif hasattr(other, 'items'): for k, v in six.iteritems(other): self[k] = v elif hasattr(other, 'keys'): for k in list(other.keys()): self[k] = other[k] else: for k, v in other: self[k] = v if kwargs: self.update(kwargs)
def iter_fields(fields): """ .. deprecated:: 1.6 Iterate over fields. The addition of :class:`~urllib3.fields.RequestField` makes this function obsolete. Instead, use :func:`iter_field_objects`, which returns :class:`~urllib3.fields.RequestField` objects. Supports list of (k, v) tuples and dicts. """ if isinstance(fields, dict): return ((k, v) for k, v in six.iteritems(fields)) return ((k, v) for k, v in fields)
def iter_field_objects(fields): """ Iterate over fields. Supports list of (k, v) tuples and dicts, and lists of :class:`~urllib3.fields.RequestField`. """ if isinstance(fields, dict): i = six.iteritems(fields) else: i = iter(fields) for field in i: if isinstance(field, RequestField): yield field else: yield RequestField.from_tuples(*field)
def __init__(self, *args, **kw): self._flip = dict.__new__(self.__class__) setattr(self._flip, "_flip", self) for key, val in six.iteritems(dict(*args, **kw)): self[key] = val