PK<|ND B3 3 safefs.py"""A thread-safe file operation package for python""" __version__ = '0.0.1' import os import tempfile from hashlib import md5 from filelock import FileLock TMPDIR = os.path.join(tempfile.gettempdir(), 'safefs.locks') GLOBAL_LOCK_NAME = '_SAFEFS_GLOBAL_LOCK_' if not os.path.exists(TMPDIR): os.makedirs(TMPDIR) def _get_lock_file(path): basename = md5(str(path).encode('utf-8')).hexdigest() return os.path.join(TMPDIR, basename + '.lock') class SafeFsContext(object): GLOCK = FileLock(_get_lock_file(GLOBAL_LOCK_NAME)) def __init__(self, func, argkeys, kwkeys, context): self.func = func self.argkeys = argkeys self.kwkeys = kwkeys self.context = context self.fs2lock = {} self.args = None self.kwargs = None self.default_context = context def __call__(self, *args, **kwargs): exfiles = kwargs.pop('_extra', []) if not isinstance(exfiles, (tuple, list)): exfiles = [exfiles] if kwargs.pop('_context', self.context): self.context = True self.args = args self.kwargs = kwargs self.fs2lock.update({arg:None for i, arg in enumerate(args) if i in self.argkeys}) self.fs2lock.update({kw:None for key, kw in kwargs.items() if key in self.kwkeys}) self.fs2lock.update({key:None for key in exfiles}) return self else: return self.func(*args, **kwargs) def __enter__(self): with SafeFsContext.GLOCK: for lfile, lock in self.fs2lock.items(): lock = lock or FileLock(_get_lock_file(lfile)) self.fs2lock[lfile] = lock lock.acquire() try: return self.func(*self.args, **self.kwargs) except Exception: from sys import exc_info self.__exit__(*exc_info()) raise def __exit__(self, type, value, traceback): with SafeFsContext.GLOCK: for lock in self.fs2lock.values(): if lock and lock.is_locked: lock.release() self.fs2lock = {} self.context = self.default_context def safefs(*args, **kwargs): def real_decorator(func): argkeys = [] kwkeys = [] for arg in args: if isinstance(arg, int): argkeys.append(arg) else: kwkeys.append(arg) context = kwargs.pop('_context', True) if not argkeys and not kwkeys: argkeys = [0] return SafeFsContext(func, argkeys, kwkeys, context) return real_decorator def register(*args, **kwargs): func = args[0] if not callable(func): raise TypeError('Expect a callable to be registered.') kwargs['_context'] = kwargs.get('_context', False) return safefs(*args[1:], **kwargs)(func) exists = register(os.path.exists, 0, 'path', _context = True) move = register(os.rename, 0, 1, 'src', 'dst' , _context = True) remove = register(os.remove, 0, 'path', _context = True) symlink = register(os.symlink, 0, 1, 'source', 'link_name', _context = True) # alias rename = move unlink = remove delete = remove PKY|N3o11safefs-0.0.1.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 pwwang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMuSasafefs-0.0.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!H^ǟ# safefs-0.0.1.dist-info/METADATA=;0D{b/')] J| ,`c6A>F@̼a'+X8Xlar!:X8hDO4t[o+zvEI 79tiˣad 66oz_╳dÊm2 z6vZ/[pEBQ_=f?PK!H| csafefs-0.0.1.dist-info/RECORDun0ҲRa %ȔU|z%}/Z i\*Gs{[rC]c:6o7\7Dd9+< -] yGe<=[=6P7hvf|H[c! ;{daA7Dnq"6Θy"i@R2RF"''o]G xM0Olm^{UKQ PK<|ND B3 3 safefs.pyPKY|N3o11Z safefs-0.0.1.dist-info/LICENSEPK!HMuSasafefs-0.0.1.dist-info/WHEELPK!H^ǟ# Tsafefs-0.0.1.dist-info/METADATAPK!H| cJsafefs-0.0.1.dist-info/RECORDPKey