comparison sat/memory/cache.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 2863345c9bbb
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
43 """ 43 """
44 @param profile(unicode, None): name of the profile to set the cache for 44 @param profile(unicode, None): name of the profile to set the cache for
45 if None, the cache will be common for all profiles 45 if None, the cache will be common for all profiles
46 """ 46 """
47 self.profile = profile 47 self.profile = profile
48 path_elts = [host.memory.getConfig("", "local_dir"), C.CACHE_DIR] 48 path_elts = [host.memory.config_get("", "local_dir"), C.CACHE_DIR]
49 if profile: 49 if profile:
50 path_elts.extend(["profiles", regex.pathEscape(profile)]) 50 path_elts.extend(["profiles", regex.path_escape(profile)])
51 else: 51 else:
52 path_elts.append("common") 52 path_elts.append("common")
53 self.cache_dir = Path(*path_elts) 53 self.cache_dir = Path(*path_elts)
54 54
55 self.cache_dir.mkdir(0o700, parents=True, exist_ok=True) 55 self.cache_dir.mkdir(0o700, parents=True, exist_ok=True)
119 "invalid char found in file name, hack attempt? name:{}".format(filename) 119 "invalid char found in file name, hack attempt? name:{}".format(filename)
120 ) 120 )
121 raise exceptions.DataError("Invalid char found") 121 raise exceptions.DataError("Invalid char found")
122 return self.cache_dir / filename 122 return self.cache_dir / filename
123 123
124 def getMetadata(self, uid: str, update_eol: bool = True) -> Optional[Dict[str, Any]]: 124 def get_metadata(self, uid: str, update_eol: bool = True) -> Optional[Dict[str, Any]]:
125 """Retrieve metadata for cached data 125 """Retrieve metadata for cached data
126 126
127 @param uid(unicode): unique identifier of file 127 @param uid(unicode): unique identifier of file
128 @param update_eol(bool): True if eol must extended 128 @param update_eol(bool): True if eol must extended
129 if True, max_age will be added to eol (only if it is not already expired) 129 if True, max_age will be added to eol (only if it is not already expired)
130 @return (dict, None): metadata with following keys: 130 @return (dict, None): metadata with following keys:
131 see [cacheData] for data details, an additional "path" key is the full path to 131 see [cache_data] for data details, an additional "path" key is the full path to
132 cached file. 132 cached file.
133 None if file is not in cache (or cache is invalid) 133 None if file is not in cache (or cache is invalid)
134 """ 134 """
135 135
136 uid = uid.strip() 136 uid = uid.strip()
174 pickle.dump(cache_data, f, protocol=2) 174 pickle.dump(cache_data, f, protocol=2)
175 175
176 cache_data["path"] = self.getPath(cache_data["filename"]) 176 cache_data["path"] = self.getPath(cache_data["filename"])
177 return cache_data 177 return cache_data
178 178
179 def getFilePath(self, uid: str) -> Path: 179 def get_file_path(self, uid: str) -> Path:
180 """Retrieve absolute path to file 180 """Retrieve absolute path to file
181 181
182 @param uid(unicode): unique identifier of file 182 @param uid(unicode): unique identifier of file
183 @return (unicode, None): absolute path to cached file 183 @return (unicode, None): absolute path to cached file
184 None if file is not in cache (or cache is invalid) 184 None if file is not in cache (or cache is invalid)
185 """ 185 """
186 metadata = self.getMetadata(uid) 186 metadata = self.get_metadata(uid)
187 if metadata is not None: 187 if metadata is not None:
188 return metadata["path"] 188 return metadata["path"]
189 189
190 def removeFromCache(self, uid, metadata=None): 190 def remove_from_cache(self, uid, metadata=None):
191 """Remove data from cache 191 """Remove data from cache
192 192
193 @param uid(unicode): unique identifier cache file 193 @param uid(unicode): unique identifier cache file
194 """ 194 """
195 cache_data = self.getMetadata(uid, update_eol=False) 195 cache_data = self.get_metadata(uid, update_eol=False)
196 if cache_data is None: 196 if cache_data is None:
197 log.debug(f"cache with uid {uid!r} has already expired or been removed") 197 log.debug(f"cache with uid {uid!r} has already expired or been removed")
198 return 198 return
199 199
200 try: 200 try:
213 213
214 cache_file = self.getPath(uid) 214 cache_file = self.getPath(uid)
215 cache_file.unlink() 215 cache_file.unlink()
216 log.debug(f"cache with uid {uid!r} has been removed") 216 log.debug(f"cache with uid {uid!r} has been removed")
217 217
218 def cacheData( 218 def cache_data(
219 self, 219 self,
220 source: str, 220 source: str,
221 uid: str, 221 uid: str,
222 mime_type: Optional[str] = None, 222 mime_type: Optional[str] = None,
223 max_age: Optional[int] = None, 223 max_age: Optional[int] = None,