Файловый менеджер - Редактировать - /opt/imh-python/lib/python3.9/site-packages/dogpile/cache/backends/__pycache__/file.cpython-39.pyc
Ðазад
a t�h�5 � @ s� d Z ddlmZ ddlZddlZddlZddlmZ ddlmZ ddl m Z g d �ZG d d� de�ZG dd � d �Z G dd� de �ZdS )z^ File Backends ------------------ Provides backends that deal with local filesystem access. � )�contextmanagerN� )�BytesBackend)�NO_VALUE� )�util)� DBMBackend�FileLock�AbstractFileLockc @ sz e Zd ZdZdd� Zddd�Zdd� Zd d � Zedd� �Z ed d� �Z dd� Zdd� Zdd� Z dd� Zdd� Zdd� ZdS )r a� A file-backend using a dbm file to store keys. Basic usage:: from dogpile.cache import make_region region = make_region().configure( 'dogpile.cache.dbm', expiration_time = 3600, arguments = { "filename":"/path/to/cachefile.dbm" } ) DBM access is provided using the Python ``anydbm`` module, which selects a platform-specific dbm module to use. This may be made to be more configurable in a future release. Note that different dbm modules have different behaviors. Some dbm implementations handle their own locking, while others don't. The :class:`.DBMBackend` uses a read/write lockfile by default, which is compatible even with those DBM implementations for which this is unnecessary, though the behavior can be disabled. The DBM backend by default makes use of two lockfiles. One is in order to protect the DBM file itself from concurrent writes, the other is to coordinate value creation (i.e. the dogpile lock). By default, these lockfiles use the ``flock()`` system call for locking; this is **only available on Unix platforms**. An alternative lock implementation, such as one which is based on threads or uses a third-party system such as `portalocker <https://pypi.python.org/pypi/portalocker>`_, can be dropped in using the ``lock_factory`` argument in conjunction with the :class:`.AbstractFileLock` base class. Currently, the dogpile lock is against the entire DBM file, not per key. This means there can only be one "creator" job running at a time per dbm file. A future improvement might be to have the dogpile lock using a filename that's based on a modulus of the key. Locking on a filename that uniquely corresponds to the key is problematic, since it's not generally safe to delete lockfiles as the application runs, implying an unlimited number of key-based files would need to be created and never deleted. Parameters to the ``arguments`` dictionary are below. :param filename: path of the filename in which to create the DBM file. Note that some dbm backends will change this name to have additional suffixes. :param rw_lockfile: the name of the file to use for read/write locking. If omitted, a default name is used by appending the suffix ".rw.lock" to the DBM filename. If False, then no lock is used. :param dogpile_lockfile: the name of the file to use for value creation, i.e. the dogpile lock. If omitted, a default name is used by appending the suffix ".dogpile.lock" to the DBM filename. If False, then dogpile.cache uses the default dogpile lock, a plain thread-based mutex. :param lock_factory: a function or class which provides for a read/write lock. Defaults to :class:`.FileLock`. Custom implementations need to implement context-manager based ``read()`` and ``write()`` functions - the :class:`.AbstractFileLock` class is provided as a base class which provides these methods based on individual read/write lock functions. E.g. to replace the lock with the dogpile.core :class:`.ReadWriteMutex`:: from dogpile.core.readwrite_lock import ReadWriteMutex from dogpile.cache.backends.file import AbstractFileLock class MutexLock(AbstractFileLock): def __init__(self, filename): self.mutex = ReadWriteMutex() def acquire_read_lock(self, wait): ret = self.mutex.acquire_read_lock(wait) return wait or ret def acquire_write_lock(self, wait): ret = self.mutex.acquire_write_lock(wait) return wait or ret def release_read_lock(self): return self.mutex.release_read_lock() def release_write_lock(self): return self.mutex.release_write_lock() from dogpile.cache import make_region region = make_region().configure( "dogpile.cache.dbm", expiration_time=300, arguments={ "filename": "file.dbm", "lock_factory": MutexLock } ) While the included :class:`.FileLock` uses ``os.flock()``, a windows-compatible implementation can be built using a library such as `portalocker <https://pypi.python.org/pypi/portalocker>`_. .. versionadded:: 0.5.2 c C s| t j�t j�|d ��| _t j�| j�\}}|�dt�| _| � |�d�d||�| _ | � |�d�d||tjj �| _| �� d S )N�filename�lock_factoryZrw_lockfilez.rw.lockZdogpile_lockfilez .dogpile.lock)�os�path�abspath�normpathr �split�getr r � _init_lock�_rw_lockr ZKeyReentrantMutex�factory� _dogpile_lock�_init_dbm_file)�self� argumentsZdir_r � r ��/root/rpmbuild/BUILDROOT/imh-python39-modules-3.9.7-92.el8.x86_64/opt/imh-python/lib/python3.9/site-packages/dogpile/cache/backends/file.py�__init__� s ���zDBMBackend.__init__Nc C sZ |d u r"| � tj�||| ��}n(|durF| � tj�tj�|���}nd S |rV||�}|S )NF)r r r �joinr r )r �argument�suffixZbasedirZbasefile�wrapper�lockr r r r � s �zDBMBackend._init_lockc C s^ t �| jt j�}|s@dD ]&}t �| jt j | t j�rd} q@q|sZt�| jd�}|�� d S )N)�dbZdatZpag�dirT�c)r �accessr �F_OK�extsep�dbm�open�close)r �exists�ext�fhr r r r � s zDBMBackend._init_dbm_filec C s | j r| � |�S d S d S �N)r )r �keyr r r � get_mutex� s zDBMBackend.get_mutexc c s| | j d u rd V nf|rH| j �� � d V W d � qx1 s<0 Y n0| j �� � d V W d � n1 sn0 Y d S r. )r �write�read)r r1 r r r �_use_rw_lock� s &zDBMBackend._use_rw_lockc c sj | � |��L t�| j|rdnd��}|V W d � n1 s>0 Y W d � n1 s\0 Y d S )N�w�r)r3 r( r) r )r r1 �dbm_objr r r � _dbm_file� s zDBMBackend._dbm_filec C sn | � d��P}t|d�r$|�|t�}n$z|| }W n tyF t}Y n0 |W d � S 1 s`0 Y d S )NFr )r7 �hasattrr r �KeyError)r r/ r6 �valuer r r �get_serialized� s zDBMBackend.get_serializedc s � fdd�|D �S )Nc s g | ]}� � |��qS r )r; )�.0r/ �r r r � <listcomp>� � z3DBMBackend.get_serialized_multi.<locals>.<listcomp>r )r �keysr r= r �get_serialized_multi� s zDBMBackend.get_serialized_multic C s6 | � d��}|||<