Mercurial > libervia-backend
comparison sat/memory/cache.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT: a jabber client | 4 # SAT: a jabber client |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
21 | 21 |
22 log = getLogger(__name__) | 22 log = getLogger(__name__) |
23 from sat.tools.common import regex | 23 from sat.tools.common import regex |
24 from sat.core import exceptions | 24 from sat.core import exceptions |
25 from sat.core.constants import Const as C | 25 from sat.core.constants import Const as C |
26 import cPickle as pickle | 26 import pickle as pickle |
27 import mimetypes | 27 import mimetypes |
28 import os.path | 28 import os.path |
29 import time | 29 import time |
30 | 30 |
31 DEFAULT_EXT = ".raw" | 31 DEFAULT_EXT = ".raw" |
40 if None, the cache will be common for all profiles | 40 if None, the cache will be common for all profiles |
41 """ | 41 """ |
42 self.profile = profile | 42 self.profile = profile |
43 path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR] | 43 path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR] |
44 if profile: | 44 if profile: |
45 path_elts.extend([u"profiles", regex.pathEscape(profile)]) | 45 path_elts.extend(["profiles", regex.pathEscape(profile)]) |
46 else: | 46 else: |
47 path_elts.append(u"common") | 47 path_elts.append("common") |
48 self.cache_dir = os.path.join(*path_elts) | 48 self.cache_dir = os.path.join(*path_elts) |
49 | 49 |
50 if not os.path.exists(self.cache_dir): | 50 if not os.path.exists(self.cache_dir): |
51 os.makedirs(self.cache_dir) | 51 os.makedirs(self.cache_dir) |
52 | 52 |
53 def getPath(self, filename): | 53 def getPath(self, filename): |
54 """return cached file URL | 54 """return cached file URL |
55 | 55 |
56 @param filename(unicode): cached file name (cache data or actual file) | 56 @param filename(unicode): cached file name (cache data or actual file) |
57 """ | 57 """ |
58 if not filename or u"/" in filename: | 58 if not filename or "/" in filename: |
59 log.error( | 59 log.error( |
60 u"invalid char found in file name, hack attempt? name:{}".format(filename) | 60 "invalid char found in file name, hack attempt? name:{}".format(filename) |
61 ) | 61 ) |
62 raise exceptions.DataError(u"Invalid char found") | 62 raise exceptions.DataError("Invalid char found") |
63 return os.path.join(self.cache_dir, filename) | 63 return os.path.join(self.cache_dir, filename) |
64 | 64 |
65 def getMetadata(self, uid): | 65 def getMetadata(self, uid): |
66 """retrieve metadata for cached data | 66 """retrieve metadata for cached data |
67 | 67 |
71 None if file is not in cache (or cache is invalid) | 71 None if file is not in cache (or cache is invalid) |
72 """ | 72 """ |
73 | 73 |
74 uid = uid.strip() | 74 uid = uid.strip() |
75 if not uid: | 75 if not uid: |
76 raise exceptions.InternalError(u"uid must not be empty") | 76 raise exceptions.InternalError("uid must not be empty") |
77 cache_url = self.getPath(uid) | 77 cache_url = self.getPath(uid) |
78 if not os.path.exists(cache_url): | 78 if not os.path.exists(cache_url): |
79 return None | 79 return None |
80 | 80 |
81 try: | 81 try: |
82 with open(cache_url, "rb") as f: | 82 with open(cache_url, "rb") as f: |
83 cache_data = pickle.load(f) | 83 cache_data = pickle.load(f) |
84 except IOError: | 84 except IOError: |
85 log.warning(u"can't read cache at {}".format(cache_url)) | 85 log.warning("can't read cache at {}".format(cache_url)) |
86 return None | 86 return None |
87 except pickle.UnpicklingError: | 87 except pickle.UnpicklingError: |
88 log.warning(u"invalid cache found at {}".format(cache_url)) | 88 log.warning("invalid cache found at {}".format(cache_url)) |
89 return None | 89 return None |
90 | 90 |
91 try: | 91 try: |
92 eol = cache_data["eol"] | 92 eol = cache_data["eol"] |
93 except KeyError: | 93 except KeyError: |
94 log.warning(u"no End Of Life found for cached file {}".format(uid)) | 94 log.warning("no End Of Life found for cached file {}".format(uid)) |
95 eol = 0 | 95 eol = 0 |
96 if eol < time.time(): | 96 if eol < time.time(): |
97 log.debug( | 97 log.debug( |
98 u"removing expired cache (expired for {}s)".format(time.time() - eol) | 98 "removing expired cache (expired for {}s)".format(time.time() - eol) |
99 ) | 99 ) |
100 return None | 100 return None |
101 | 101 |
102 cache_data["path"] = self.getPath(cache_data["filename"]) | 102 cache_data["path"] = self.getPath(cache_data["filename"]) |
103 return cache_data | 103 return cache_data |
133 if filename is None: | 133 if filename is None: |
134 if mime_type: | 134 if mime_type: |
135 ext = mimetypes.guess_extension(mime_type, strict=False) | 135 ext = mimetypes.guess_extension(mime_type, strict=False) |
136 if ext is None: | 136 if ext is None: |
137 log.warning( | 137 log.warning( |
138 u"can't find extension for MIME type {}".format(mime_type) | 138 "can't find extension for MIME type {}".format(mime_type) |
139 ) | 139 ) |
140 ext = DEFAULT_EXT | 140 ext = DEFAULT_EXT |
141 elif ext == u".jpe": | 141 elif ext == ".jpe": |
142 ext = u".jpg" | 142 ext = ".jpg" |
143 else: | 143 else: |
144 ext = DEFAULT_EXT | 144 ext = DEFAULT_EXT |
145 mime_type = None | 145 mime_type = None |
146 filename = uid + ext | 146 filename = uid + ext |
147 if max_age is None: | 147 if max_age is None: |
148 max_age = C.DEFAULT_MAX_AGE | 148 max_age = C.DEFAULT_MAX_AGE |
149 cache_data = { | 149 cache_data = { |
150 u"source": source, | 150 "source": source, |
151 u"filename": filename, | 151 "filename": filename, |
152 u"eol": int(time.time()) + max_age, | 152 "eol": int(time.time()) + max_age, |
153 u"mime_type": mime_type, | 153 "mime_type": mime_type, |
154 } | 154 } |
155 file_path = self.getPath(filename) | 155 file_path = self.getPath(filename) |
156 | 156 |
157 with open(cache_url, "wb") as f: | 157 with open(cache_url, "wb") as f: |
158 pickle.dump(cache_data, f, protocol=2) | 158 pickle.dump(cache_data, f, protocol=2) |