forked from tinify/tinify-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.py
More file actions
90 lines (71 loc) · 3.24 KB
/
source.py
File metadata and controls
90 lines (71 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import tinify
import sys
from tinify.result import Result
from tinify.result_meta import ResultMeta
try:
from typing import Union, Dict, IO, Any, Unpack, TYPE_CHECKING, overload
if sys.version_info.major > 3 and sys.version_info.minor > 8:
from tinify._typed import *
except ImportError:
TYPE_CHECKING = False # type: ignore
class Source(object):
@classmethod
def from_file(cls, path): # type: (Union[str, IO]) -> Source
if hasattr(path, 'read'):
return cls._shrink(path)
else:
with open(path, 'rb') as f:
return cls._shrink(f.read())
@classmethod
def from_buffer(cls, string): # type: (bytes) -> Source
return cls._shrink(string)
@classmethod
def from_url(cls, url): # type: (str) -> Source
return cls._shrink({"source": {"url": url}})
@classmethod
def _shrink(cls, obj): # type: (Any) -> Source
response = tinify.get_client().request('POST', '/shrink', obj)
return cls(response.headers['location'])
def __init__(self, url, **commands): # type: (str, **Any) -> None
self.url = url
self.commands = commands
def preserve(self, *options): # type: (*PreserveOption) -> "Source"
return type(self)(self.url, **self._merge_commands(preserve=self._flatten(options)))
def resize(self, **options): # type: (Unpack[ResizeOptions]) -> "Source"
return type(self)(self.url, **self._merge_commands(resize=options))
def convert(self, **options): # type: (Unpack[ConvertOptions]) -> "Source"
return type(self)(self.url, **self._merge_commands(convert=options))
def transform(self, **options): # type: (Unpack[TransformOptions]) -> "Source"
return type(self)(self.url, **self._merge_commands(transform=options))
if TYPE_CHECKING:
@overload
def store(self, **options): # type: (Unpack[S3StoreOptions]) -> ResultMeta
pass
@overload
def store(self, **options): # type: (Unpack[GCSStoreOptions]) -> ResultMeta
pass
def store(self, **options): # type: (Any) -> ResultMeta
response = tinify.get_client().request('POST', self.url, self._merge_commands(store=options))
return ResultMeta(response.headers)
def result(self): # type: () -> Result
if not self.commands:
response = tinify.get_client().request('GET', self.url, self.commands)
else:
response = tinify.get_client().request('POST', self.url, self.commands)
return Result(response.headers, response.content)
def to_file(self, path): # type: (Union[str, IO]) -> None
return self.result().to_file(path)
def to_buffer(self): # type: () -> bytes
return self.result().to_buffer()
def _merge_commands(self, **options): # type: (**Any) -> Dict[str, Any]
commands = self.commands.copy()
commands.update(options)
return commands
def _flatten(self, items, seqtypes=(list, tuple)):
items = list(items)
for i, x in enumerate(items):
while isinstance(items[i], seqtypes):
items[i:i+1] = items[i]
return items