Beispiel #1
0
 def __init__(self,
              path,
              recursive=True,
              patterns=None,
              ignore_patterns=None,
              ignore_directories=False,
              case_sensitive=False,
              events=None,
              load_file=False,
              mode='auto',
              base64=False,
              defer_modified=0.5,
              **kwargs):
     super().__init__(**kwargs)
     self.path = path
     validator.is_directory(path=self.path)
     self.recursive = bool(recursive)
     self.patterns = make_list(patterns)
     self.ignore_patterns = make_list(ignore_patterns)
     self.ignore_directories = bool(ignore_directories)
     self.case_sensitive = bool(case_sensitive)
     self.load_file = bool(load_file)
     self.base64 = bool(base64)
     self.events = make_list(events)
     if self.events:
         validator.subset_of(self.EVENT_TYPES, events=self.events)
     self.mode = mode
     validator.one_of(FILE_MODES, mode=self.mode)
     self.defer_modified = float(defer_modified)
     if self.defer_modified < 0.0:
         self.defer_modified = 0.5
Beispiel #2
0
    def __init__(self, url, token, include=None, exclude=None, **kwargs):
        super().__init__(**kwargs)
        self.url = self._sanitize_url(url)
        self.token = str(token)
        self.include = make_list(include)
        self.exclude = make_list(exclude)
        self._websocket = None
        self._loop = None

        self._include_regex = self.include and wildcards_to_regex(self.include)
        self._exclude_regex = self.exclude and wildcards_to_regex(self.exclude)
Beispiel #3
0
    def __init__(self, directory=None, port=2121, user_pwd=None, events=None,
                 max_cons=256, max_cons_ip=5, **kwargs):
        super().__init__(**kwargs)
        self.port = int(port)
        self.max_cons = int(max_cons)
        self.max_cons_ip = int(max_cons_ip)
        validator.is_instance(str, allow_none=True, directory=directory)
        if directory:
            validator.is_directory(directory=directory)
        self.directory = directory
        if not user_pwd:  # No password -> anonymous access
            self.user = None
        elif isinstance(user_pwd, str):
            self.user = user_pwd
            self.password = ''
        elif is_iterable_but_no_str(user_pwd) and user_pwd:  # at least one element
            self.user = user_pwd[0]
            if len(user_pwd) > 1:  # Password too
                self.password = user_pwd[1]
        else:
            raise TypeError("Argument 'user_pwd' is expected to be a str (user) or a tuple of "
                            "user and password.")

        self.events = make_list(events) or copy.copy(self.ALL_EVENTS)
        if self.events:
            validator.subset_of(self.ALL_EVENTS, events=self.events)

        self.server = None
Beispiel #4
0
    def __init__(self, expressions, **kwargs):
        super().__init__(interval=60, instant_run=False, **kwargs)
        self.expressions = make_list(expressions)

        from cronex import CronExpression
        self.jobs = [
            CronExpression(expression) for expression in self.expressions
        ]
Beispiel #5
0
 def __init__(self,
              prefix_path: str,
              allowed_methods: Union[str, Iterable[str]] = 'GET',
              **kwargs: Any) -> None:
     super().__init__(**kwargs)
     self.prefix_path = str(prefix_path)
     self.allowed_methods = [
         str(m).upper() for m in make_list(allowed_methods)
     ]
     validator.subset_of(HTTP_METHODS, allowed_methods=self.allowed_methods)
Beispiel #6
0
 def __init__(self, paths, fail_on_error=True, **kwargs):
     super().__init__(**kwargs)
     if isinstance(paths, dict):
         self.file_paths = paths  # alias -> file path mapping
     else:
         # No explicit alias: Use the basename of the file
         self.file_paths = {
             os.path.basename(str(fp)): str(fp)
             for fp in make_list(paths)
         }
     self.fail_on_error = bool(fail_on_error)
Beispiel #7
0
def _lookup(instance: object, lookups: Union[str,
                                             Iterable[str]]) -> Optional[Any]:
    """
    Will lookup the instance for the given attribute names in `lookups`.
    Returns the first occurrence found.

    Examples:

        >>> class Foo:
        ...     def __init__(self):
        ...         self.a = 1
        ...         self.b = 2
        ...         self.c = 3
        ...         self.__a = 99
        >>> instance = Foo()
        >>> _lookup(instance, 'a')
        1
        >>> _lookup(instance, '__a')
        99
        >>> _lookup(instance, ['c', 'b'])
        3
        >>> _lookup(instance, ['d', 'b'])
        2
        >>> print(_lookup(instance, ['x', 'y', 'z']))
        None

    Args:
        instance: The class to lookup for the given attribute names.
        lookups: Attribute names to lookup.

    Returns:
        Returns the value of the first found attribute name in lookups. If none is found, simply
        None is returned.
    """
    lookups = cast(List[str], utils.make_list(lookups) or [])
    # There might be some name mangling for private attributes - add them as lookups
    privates = [
        '_{classname}{attrname}'.format(classname=type(instance).__name__,
                                        attrname=x) for x in lookups
        if x.startswith('__') and not x.endswith('__')
    ]
    lookups = lookups + privates
    _nothing = object()
    for lookup in lookups:
        val = getattr(instance, lookup, _nothing)
        if val is not _nothing:
            return val
    return None
Beispiel #8
0
 def __init__(self, pins, default=CONST_RISING, **kwargs):
     super().__init__(**kwargs)
     self._mode_default = default
     validator.one_of(CONST_RISING_OPTIONS + CONST_FALLING_OPTIONS +
                      CONST_SWITCH_OPTIONS + CONST_MOTION_OPTIONS,
                      mode_default=self._mode_default)
     self._pins = [
         Callback.from_str(pin_str, default=default)
         for pin_str in make_list(pins)
     ]
     _without_duplicate = set(self._pins)
     if len(_without_duplicate) != len(self._pins):
         diff = list(
             (Counter(self._pins) - Counter(_without_duplicate)).elements())
         self.logger.warning(
             "You provided duplicate gpio pin configurations. Will ignore '%s'",
             diff)
         self._pins = _without_duplicate
Beispiel #9
0
 def _parse_ping_users(val: Any):
     return make_list(val or [])
Beispiel #10
0
 def _loop():
     for name, fps in mapping.items():
         for fp in make_list(fps):
             yield name, self._load_fencodings(fp)
Beispiel #11
0
 def _parse_args(val):
     args = make_list(val)
     if not args:
         return None
     return [str(arg) for arg in args]