Mercurial > silorider
annotate silorider/silos/base.py @ 9:8830c7d59d7e
Early return if not silos are defined.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 29 Jul 2018 20:01:30 -0700 |
parents | 27543b2e73b9 |
children | d3c4c5082bbc |
rev | line source |
---|---|
2 | 1 import urllib.request |
0 | 2 import logging |
2 | 3 import mimetypes |
0 | 4 from ..format import format_entry |
5 | |
6 | |
7 logger = logging.getLogger(__name__) | |
8 | |
9 | |
10 class SiloCreationContext: | |
11 def __init__(self, config, cache, silo_name): | |
12 self.config = config | |
13 self.cache = cache | |
14 self.silo_name = silo_name | |
15 | |
16 | |
17 class SiloContextBase: | |
18 def __init__(self, exec_ctx): | |
19 self.exec_ctx = exec_ctx | |
20 | |
21 @property | |
22 def args(self): | |
23 return self.exec_ctx.args | |
24 | |
25 @property | |
26 def config(self): | |
27 return self.exec_ctx.config | |
28 | |
29 @property | |
30 def cache(self): | |
31 return self.exec_ctx.cache | |
32 | |
33 | |
34 class SiloAuthenticationContext(SiloContextBase): | |
35 pass | |
36 | |
37 | |
38 class SiloPostingContext(SiloContextBase): | |
39 pass | |
40 | |
41 | |
42 class Silo: | |
43 SILO_TYPE = 'unknown' | |
44 | |
45 def __init__(self, ctx): | |
46 self.ctx = ctx | |
47 self._silo_cfg = dict(ctx.config.items('silo:%s' % self.name)) | |
48 | |
49 @property | |
50 def name(self): | |
51 return self.ctx.silo_name | |
52 | |
53 def getConfigItem(self, name, fallback=None): | |
54 return self._silo_cfg.get(name, fallback) | |
55 | |
56 def getConfigItems(self): | |
57 return self._silo_cfg.copy() | |
58 | |
59 def getCacheItem(self, name, valtype=str): | |
60 full_name = '%s_%s' % (self.name, name) | |
61 return self.ctx.cache.getCustomValue(full_name, valtype=valtype) | |
62 | |
63 def setCacheItem(self, name, val): | |
64 full_name = '%s_%s' % (self.name, name) | |
65 return self.ctx.cache.setCustomValue(full_name, val) | |
66 | |
67 def formatEntry(self, entry, *args, **kwargs): | |
68 return format_entry(entry, *args, **kwargs) | |
69 | |
70 def authenticate(self, ctx): | |
71 raise NotImplementedError() | |
72 | |
73 def onPostStart(self): | |
74 pass | |
75 | |
76 def postEntry(self, entry, ctx): | |
77 raise NotImplementedError() | |
78 | |
79 def onPostEnd(self): | |
80 pass | |
81 | |
82 | |
9
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
83 def _get_silo_section_names(config): |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
84 return [sn for sn in config.sections() if sn.startswith('silo:')] |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
85 |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
86 |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
87 def has_any_silo(config): |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
88 return bool(_get_silo_section_names(config)) |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
89 |
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
90 |
0 | 91 def load_silos(config, cache): |
92 from .print import PrintSilo | |
93 from .mastodon import MastodonSilo | |
2 | 94 from .twitter import TwitterSilo |
95 silo_types = [PrintSilo, MastodonSilo, TwitterSilo] | |
0 | 96 silo_dict = dict([(s.SILO_TYPE, s) for s in silo_types]) |
97 | |
98 silos = [] | |
9
8830c7d59d7e
Early return if not silos are defined.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
99 sec_names = _get_silo_section_names(config) |
0 | 100 for sec_name in sec_names: |
101 silo_name = sec_name[5:] | |
102 sec_items = dict(config.items(sec_name)) | |
103 silo_type = sec_items.get('type') | |
104 if not silo_type: | |
105 raise Exception("No silo type specified for: %s" % silo_name) | |
106 | |
107 silo_class = silo_dict.get(silo_type) | |
108 if not silo_class: | |
109 raise Exception("Unknown silo type: %s" % silo_type) | |
110 | |
111 logger.debug("Creating silo '%s' for '%s'." % (silo_type, silo_name)) | |
112 cctx = SiloCreationContext(config, cache, silo_name) | |
113 silo = silo_class(cctx) | |
114 silos.append(silo) | |
115 return silos | |
2 | 116 |
117 | |
118 def upload_silo_media(entry, propname, callback): | |
119 media_ids = None | |
120 urls = entry.get(propname, [], force_list=True) | |
121 if urls: | |
122 media_ids = [] | |
123 for url in urls: | |
124 mid = _do_upload_silo_media(url, callback) | |
125 if mid is not None: | |
126 media_ids.append(mid) | |
127 return media_ids | |
128 | |
129 | |
130 def _do_upload_silo_media(url, callback): | |
131 logger.debug("Downloading %s for upload to silo..." % url) | |
132 mt, enc = mimetypes.guess_type(url) | |
133 if not mt: | |
134 mt = mimetypes.common_types['.jpg'] | |
135 | |
136 ext = mimetypes.guess_extension(mt) or '.jpg' | |
137 logger.debug("Got MIME type and extension: %s %s" % (mt, ext)) | |
138 | |
139 try: | |
140 tmpfile, headers = urllib.request.urlretrieve(url) | |
141 logger.debug("Using temporary file: %s" % tmpfile) | |
142 return callback(tmpfile, mt) | |
143 finally: | |
144 logger.debug("Cleaning up.") | |
145 urllib.request.urlcleanup() |