Esempio n. 1
0
    def is_valid(clazz, what):
        'Return True if what is a valid value or name.'

        if isinstance(what, clazz):
            return True
        elif issubclass(clazz, enum.IntEnum):
            if check.is_int(what):
                return clazz.value_is_valid(what)
            elif check.is_string(what):
                return clazz.name_is_valid(what)
        elif issubclass(clazz, enum.Enum):
            if check.is_string(what):
                return clazz.value_is_valid(what) or clazz.name_is_valid(what)
        raise ValueError(
            f'{clazz.__name__}: Invalid enumeration value: {value} - {type(value)}'
        )
Esempio n. 2
0
 def __new__(clazz, name, unixpath, pythonpath, requires, variables):
     check.check_string(name)
     unixpath = unixpath or []
     if check.is_string(unixpath):
         unixpath = unixpath.split(':')
     check.check_string_seq(unixpath)
     pythonpath = pythonpath or []
     if check.is_string(pythonpath):
         pythonpath = pythonpath.split(':')
     check.check_string_seq(pythonpath)
     requires = requires or set()
     check.check_set(requires)
     unixpath = [file_path.normalize_sep(p) for p in unixpath]
     pythonpath = [file_path.normalize_sep(p) for p in pythonpath]
     return clazz.__bases__[0].__new__(clazz, name, unixpath, pythonpath,
                                       requires, variables)
Esempio n. 3
0
 def _determine_content(self):
     if not self.content:
         return b''
     if check.is_string(self.content) and self.content.startswith('file:'):
         _, _, source_filename = self.content.partition(':')
         source_filename = source_filename.strip()
         if not path.isfile(source_filename):
             raise IOError(
                 'source file not found: {}'.format(source_filename))
         return self._file_read(source_filename)
     else:
         return self.content
Esempio n. 4
0
    def parse(clazz, what):
        'Parse a string, int or enum return an enum item or raise an error if name is invalid.'

        try:
            if isinstance(what, clazz):
                return what
            elif issubclass(clazz, enum.IntEnum):
                if check.is_string(what):
                    return clazz[what]
                elif check.is_int(what):
                    return clazz(what)
            elif issubclass(clazz, enum.Enum):
                if check.is_string(what):
                    if what in clazz.values:
                        return clazz(what)
                    elif what in clazz.name_to_item_dict:
                        return clazz.name_to_item_dict[what]
        except ValueError as ex:
            pass
        raise ValueError(
            f'{clazz.__name__}: Invalid enumeration value: {what} - {type(what)}'
        )
Esempio n. 5
0
    def value_is_valid(clazz, value):
        'Return True if value is valid.'

        if isinstance(value, clazz):
            return True
        elif issubclass(clazz, enum.IntEnum):
            if check.is_int(value):
                return value in clazz.values
        elif issubclass(clazz, enum.Enum):
            if check.is_string(value):
                return value in clazz.values
        raise ValueError(
            f'{clazz.__name__}: Invalid enumeration value: {value} - {type(value)}'
        )
Esempio n. 6
0
    def _partition_info_by_criteria(clazz, files, dst_dir_abs, criteria,
                                    options):
        if not criteria:
            raise RuntimeError(
                'No partition_criteria given for partition_type "criteria"')
        resolved_files = clazz._resolve_files(files, options.recursive)
        for f in resolved_files:
            clazz._log.log_d(f'resolved_file: {f.root_dir} - {f.filename}')

        classifications = {}
        for f in resolved_files:
            classification = criteria.classify(f.filename_abs)
            if classification != None:
                if not check.is_string(classification):
                    raise TypeError(
                        f'Classify should return a string: {criteria}')
                if not classification in classifications:
                    classifications[classification] = dir_operation_item_list()
                if options.flatten:
                    dst_basename = path.basename(f.filename_abs)
                else:
                    dst_basename = f.filename
                dst_filename = path.join(dst_dir_abs, classification,
                                         dst_basename)
                item = dir_operation_item(f.filename_abs, dst_filename)
                classifications[classification].append(item)

        items = dir_operation_item_list()
        for classification, classification_items in classifications.items():
            num_files = len(classification_items)
            threshold_met = options.threshold == None or num_files >= options.threshold
            clazz._log.log_d(
                f'classification={classification} num_files={num_files} threshold_met={threshold_met}'
            )
            if threshold_met:
                items.extend(classification_items)
        items.sort(key=lambda item: item.src_filename)
        return clazz._partition_info_result(items, resolved_files)
Esempio n. 7
0
 def __eq__(self, other):
     if isinstance(other, enum.Enum):
         return self.value == other.value
     if check.is_string(other):
         return self.__eq__(self.parse(other))
     return super(checked_enum_mixin, self).__eq__(other)
Esempio n. 8
0
 def test_is_string_seq(self):
     self.assertFalse(C.is_string(['foo']))
     self.assertTrue(C.is_string('foo'))