示例#1
0
 def _validate_multi_filter(self, filter: str, value: Any):
     if not isinstance(value, (tuple, list, str)):
         raise ArgumentError(
             f"{filter} filter invalid value (must be str or itarable of str)"
         )
     if isinstance(value, (list, tuple)):
         for v in value:
             if not isinstance(v, str):
                 raise ArgumentError(
                     f"{filter} filter invalid value (must be str or itarable of str)"
                 )
示例#2
0
    def _validate_filter_arg(self, allowed_filters: List[str], filter: str,
                             value: Any) -> None:
        if filter not in allowed_filters:
            raise ArgumentError(f"this endpoint doesnt accept {filter} filter")

        if filter in SINGLE_FILTERS and not isinstance(value, (int, str)):
            raise ArgumentError(f"{filter} filter only accepts 1 value")

        if filter in MULTI_FILTERS:
            self._validate_multi_filter(filter, value)

        self._validate_filter_value(filter, value)
示例#3
0
    def check_into_episode(
        self,
        *,
        episode: Union[Episode, Dict[str, Any]],
        message: Optional[str] = None,
        sharing: Optional[Union[Sharing, Dict[str, str]]] = None,
        **kwargs,
    ) -> EpisodeCheckin:
        data: Dict[str, Any] = self._prepare_common_data(
            **kwargs, message=message, sharing=sharing
        )

        if isinstance(episode, Episode):
            episode = {"ids": {"trakt": self._generic_get_id(episode)}}
        data["episode"] = episode

        if "show" in kwargs:
            show = kwargs["show"]
            if isinstance(show, Show):
                data["show"] = {"ids": {"trakt": self._generic_get_id(show)}}
            elif isinstance(show, dict):
                data["show"] = show
            else:
                raise ArgumentError("show: invalid argument value")

        return self.run("check_into_episode", **data, body=data)
示例#4
0
    def check_into(
        self,
        *,
        movie: Optional[Union[Movie, Dict[str, Any]]] = None,
        episode: Optional[Union[Episode, Dict[str, Any]]] = None,
        **kwargs: Any,
    ) -> Union[EpisodeCheckin, MovieCheckin]:
        if movie and episode:
            raise ArgumentError("you must provide exactly one of: [episode, movie]")

        if movie:
            return self.check_into_movie(movie=movie, **kwargs)
        elif episode:
            return self.check_into_episode(episode=episode, **kwargs)
        else:
            raise ArgumentError("you must provide exactly one of: [episode, movie]")
示例#5
0
    def pause_scrobble(
        self,
        *,
        movie: Optional[Union[Movie, Dict[str, Any]]] = None,
        episode: Optional[Union[Episode, Dict[str, Any]]] = None,
        show: Optional[Union[Show, Dict[str, Any]]] = None,
        **kwargs: Any,
    ) -> Union[MovieScrobble, EpisodeScrobble]:
        if movie and episode:
            raise ArgumentError("you must provide exactly one of: [episode, movie]")

        if movie:
            return self.pause_scrobble_movie(movie=movie, **kwargs)
        elif episode:
            return self.pause_scrobble_episode(episode=episode, show=show, **kwargs)
        else:
            raise ArgumentError("you must provide exactly one of: [episode, movie]")
示例#6
0
    def stop_scrobble(
        self,
        *,
        movie: Optional[Union[Movie, Dict[str, Any]]] = None,
        episode: Optional[Union[Episode, Dict[str, Any]]] = None,
        show: Optional[Union[Show, Dict[str, Any]]] = None,
        **kwargs: Any,
    ) -> Union[MovieScrobble, EpisodeScrobble]:
        if movie and episode:
            raise ArgumentError("you can either provide episode or movie, not both")

        if movie:
            return self.stop_scrobble_movie(movie=movie, **kwargs)
        elif episode:
            return self.stop_scrobble_episode(episode=episode, show=show, **kwargs)
        else:
            raise ArgumentError("missing both episode and movie arguments")
示例#7
0
 def _generic_get_id(
     item: Union[Movie, Episode, Show, Season, Comment, Person, str, int]
 ) -> Union[int, str]:
     if isinstance(item, (int, str)):
         return item
     if isinstance(item, Comment):
         return item.id
     elif isinstance(item, (Movie, Episode, Show, Season, TraktList, Person)):
         return item.ids.trakt
     else:
         raise ArgumentError("item: invalid id")
示例#8
0
    def take(self, n: int = -1) -> List[T]:
        """Take n next results. By default returns per_page results."""
        if n == -1:
            n = self._per_page

        if not isinstance(n, int) or n < 0:
            raise ArgumentError(
                f"argument n={n} is invalid; n must be an int and n >= 1")

        it = iter(self)
        return list(itertools.islice(it, n))
示例#9
0
    def validate(self, *args: Any, path: Any, **kwargs: Any) -> None:
        require_previous = False

        for p in path.opt_args[::-1]:
            arg_name = p[1:]
            if require_previous:
                if arg_name not in kwargs or kwargs[arg_name] in (None, [],
                                                                  {}):
                    raise ArgumentError(f"missing {arg_name} argument")
            elif arg_name in kwargs:
                require_previous = True
示例#10
0
    def validate(self, *args: Any, path: Any, **kwargs: Any) -> None:
        if "extended" in kwargs:
            if len(path.extended) == 1 and kwargs["extended"] is True:
                return  # True = enabled, to be substituted later

            if kwargs["extended"] and kwargs["extended"] not in path.extended:
                message = f"invalid extended={kwargs['extended']} argument value; "

                if path.extended:
                    message += f"possible extended values: {path.extended}"
                else:
                    message += "this endpoint doesn't accept extended parameter"

                raise ArgumentError(message)
示例#11
0
    def _prepare_common_data(
        self, **kwargs: Any
    ) -> Dict[str, Union[str, Dict[str, str]]]:
        d: Dict[str, Union[str, Dict[str, str]]] = {}

        if "sharing" in kwargs and kwargs["sharing"] is not None:
            if isinstance(kwargs["sharing"], Sharing):
                d["sharing"] = asdict(kwargs["sharing"])
            elif isinstance(kwargs["sharing"], dict):
                d["sharing"] = kwargs["sharing"]
            else:
                raise ArgumentError("sharing: invalid argument value")

        for f in ["message", "venue_id", "venue_name", "app_version", "app_date"]:
            if f in kwargs:
                v = kwargs[f]
                if v is not None:
                    d[f] = v

        return d
示例#12
0
 def validate(self, *args: Any, **kwargs: Any) -> None:
     if self.arg_name in kwargs:
         if not bool(self.boolean_check(kwargs[self.arg_name])):
             raise ArgumentError(
                 f"invalid {self.arg_name}={kwargs[self.arg_name]} argument value"
             )
示例#13
0
 def validate(self, *args: Any, path: Any, **kwargs: Any) -> None:
     for p in path.req_args:
         arg_name = p[1:]
         if arg_name not in kwargs or kwargs[arg_name] in (None, [], {}):
             raise ArgumentError(f"missing required {arg_name} argument")
示例#14
0
 def _validate_filter_value(self, filter: str, value: Any):
     if not isinstance(value, (list, tuple)):
         value = [value]
     for v in value:
         if not self.filter_value_validators[filter](v):
             raise ArgumentError(f"{filter}: {v} is not a valid value")