Parcourir la source

feat(config): can read json

runhey il y a 3 ans
Parent
commit
4776b9af3b

+ 2 - 0
.gitignore

@@ -24,6 +24,8 @@ test/
 *.user
 !module/gui/FluentUI/fluentuiplugin.dll
 build-fluentui-Desktop_*/
+!console.bat
+!oas.exe
 
 
 

+ 236 - 0
module/config/atomicwrites.py

@@ -0,0 +1,236 @@
+"""
+Copy-pasted from
+https://github.com/untitaker/python-atomicwrites
+"""
+import contextlib
+import io
+import os
+import sys
+import tempfile
+
+try:
+    import fcntl
+except ImportError:
+    fcntl = None
+
+# `fspath` was added in Python 3.6
+try:
+    from os import fspath
+except ImportError:
+    fspath = None
+
+__version__ = '1.4.1'
+
+PY2 = sys.version_info[0] == 2
+
+text_type = unicode if PY2 else str  # noqa
+
+
+def _path_to_unicode(x):
+    if not isinstance(x, text_type):
+        return x.decode(sys.getfilesystemencoding())
+    return x
+
+
+DEFAULT_MODE = "wb" if PY2 else "w"
+
+_proper_fsync = os.fsync
+
+if sys.platform != 'win32':
+    if hasattr(fcntl, 'F_FULLFSYNC'):
+        def _proper_fsync(fd):
+            # https://lists.apple.com/archives/darwin-dev/2005/Feb/msg00072.html
+            # https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man2/fsync.2.html
+            # https://github.com/untitaker/python-atomicwrites/issues/6
+            fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
+
+
+    def _sync_directory(directory):
+        # Ensure that filenames are written to disk
+        fd = os.open(directory, 0)
+        try:
+            _proper_fsync(fd)
+        finally:
+            os.close(fd)
+
+
+    def _replace_atomic(src, dst):
+        os.rename(src, dst)
+        _sync_directory(os.path.normpath(os.path.dirname(dst)))
+
+
+    def _move_atomic(src, dst):
+        os.link(src, dst)
+        os.unlink(src)
+
+        src_dir = os.path.normpath(os.path.dirname(src))
+        dst_dir = os.path.normpath(os.path.dirname(dst))
+        _sync_directory(dst_dir)
+        if src_dir != dst_dir:
+            _sync_directory(src_dir)
+else:
+    from ctypes import windll, WinError
+
+    _MOVEFILE_REPLACE_EXISTING = 0x1
+    _MOVEFILE_WRITE_THROUGH = 0x8
+    _windows_default_flags = _MOVEFILE_WRITE_THROUGH
+
+
+    def _handle_errors(rv):
+        if not rv:
+            raise WinError()
+
+
+    def _replace_atomic(src, dst):
+        _handle_errors(windll.kernel32.MoveFileExW(
+            _path_to_unicode(src), _path_to_unicode(dst),
+            _windows_default_flags | _MOVEFILE_REPLACE_EXISTING
+        ))
+
+
+    def _move_atomic(src, dst):
+        _handle_errors(windll.kernel32.MoveFileExW(
+            _path_to_unicode(src), _path_to_unicode(dst),
+            _windows_default_flags
+        ))
+
+
+def replace_atomic(src, dst):
+    '''
+    Move ``src`` to ``dst``. If ``dst`` exists, it will be silently
+    overwritten.
+
+    Both paths must reside on the same filesystem for the operation to be
+    atomic.
+    '''
+    return _replace_atomic(src, dst)
+
+
+def move_atomic(src, dst):
+    '''
+    Move ``src`` to ``dst``. There might a timewindow where both filesystem
+    entries exist. If ``dst`` already exists, :py:exc:`FileExistsError` will be
+    raised.
+
+    Both paths must reside on the same filesystem for the operation to be
+    atomic.
+    '''
+    return _move_atomic(src, dst)
+
+
+class AtomicWriter(object):
+    '''
+    A helper class for performing atomic writes. Usage::
+
+        with AtomicWriter(path).open() as f:
+            f.write(...)
+
+    :param path: The destination filepath. May or may not exist.
+    :param mode: The filemode for the temporary file. This defaults to `wb` in
+        Python 2 and `w` in Python 3.
+    :param overwrite: If set to false, an error is raised if ``path`` exists.
+        Errors are only raised after the file has been written to.  Either way,
+        the operation is atomic.
+    :param open_kwargs: Keyword-arguments to pass to the underlying
+        :py:func:`open` call. This can be used to set the encoding when opening
+        files in text-mode.
+
+    If you need further control over the exact behavior, you are encouraged to
+    subclass.
+    '''
+
+    def __init__(self, path, mode=DEFAULT_MODE, overwrite=False,
+                 **open_kwargs):
+        if 'a' in mode:
+            raise ValueError(
+                'Appending to an existing file is not supported, because that '
+                'would involve an expensive `copy`-operation to a temporary '
+                'file. Open the file in normal `w`-mode and copy explicitly '
+                'if that\'s what you\'re after.'
+            )
+        if 'x' in mode:
+            raise ValueError('Use the `overwrite`-parameter instead.')
+        if 'w' not in mode:
+            raise ValueError('AtomicWriters can only be written to.')
+
+        # Attempt to convert `path` to `str` or `bytes`
+        if fspath is not None:
+            path = fspath(path)
+
+        self._path = path
+        self._mode = mode
+        self._overwrite = overwrite
+        self._open_kwargs = open_kwargs
+
+    def open(self):
+        '''
+        Open the temporary file.
+        '''
+        return self._open(self.get_fileobject)
+
+    @contextlib.contextmanager
+    def _open(self, get_fileobject):
+        f = None  # make sure f exists even if get_fileobject() fails
+        try:
+            success = False
+            with get_fileobject(**self._open_kwargs) as f:
+                yield f
+                self.sync(f)
+            self.commit(f)
+            success = True
+        finally:
+            if not success:
+                try:
+                    self.rollback(f)
+                except Exception:
+                    pass
+
+    def get_fileobject(self, suffix="", prefix=tempfile.gettempprefix(),
+                       dir=None, **kwargs):
+        '''Return the temporary file to use.'''
+        if dir is None:
+            dir = os.path.normpath(os.path.dirname(self._path))
+        descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
+                                            dir=dir)
+        # io.open() will take either the descriptor or the name, but we need
+        # the name later for commit()/replace_atomic() and couldn't find a way
+        # to get the filename from the descriptor.
+        os.close(descriptor)
+        kwargs['mode'] = self._mode
+        kwargs['file'] = name
+        return io.open(**kwargs)
+
+    def sync(self, f):
+        '''responsible for clearing as many file caches as possible before
+        commit'''
+        f.flush()
+        _proper_fsync(f.fileno())
+
+    def commit(self, f):
+        '''Move the temporary file to the target location.'''
+        if self._overwrite:
+            replace_atomic(f.name, self._path)
+        else:
+            move_atomic(f.name, self._path)
+
+    def rollback(self, f):
+        '''Clean up all temporary resources.'''
+        os.unlink(f.name)
+
+
+def atomic_write(path, writer_cls=AtomicWriter, **cls_kwargs):
+    '''
+    Simple atomic writes. This wraps :py:class:`AtomicWriter`::
+
+        with atomic_write(path) as f:
+            f.write(...)
+
+    :param path: The target path to write to.
+    :param writer_cls: The writer class to use. This parameter is useful if you
+        subclassed :py:class:`AtomicWriter` to change some behavior and want to
+        use that new subclass.
+
+    Additional keyword arguments are passed to the writer class. See
+    :py:class:`AtomicWriter`.
+    '''
+    return writer_cls(path, **cls_kwargs).open()

+ 20 - 1
module/config/config.py

@@ -17,4 +17,23 @@ class Config(ConfigManual, ConfigWatcher, ConfigUpdater):
     """
     一个配置文件的集成类
     """
-    pass
+    def __init__(self, config_name: str, task=None) -> None:
+        """
+
+        :param config_name:
+        :param task:
+        """
+        self.config_name = config_name
+        self.data: dict = {}
+
+        self.load()
+
+
+    def load(self) -> None:
+        self.data = self.read_file(self.config_name)
+
+
+
+if __name__ == '__main__':
+    config = Config(config_name='oas1')
+    logger.info(config.data)

+ 32 - 2
module/config/config_updater.py

@@ -22,10 +22,40 @@ class ConfigUpdater:
         pass
 
     @timer
-    def update_config(self, config_name: str ) -> None:
+    def update_config(self, config_name: str) -> None:
         """
         更新配置文件.从template更新
         :param config_name:
         :return:
         """
-        pass
+        pass
+
+    def read_file(self, config_name, is_template=False):
+        """
+        Read and update config file.
+
+        Args:
+            config_name (str): ./config/{file}.json
+            is_template (bool):
+
+        Returns:
+            dict:
+        """
+        old = read_file(filepath_config(config_name))
+        # new = self.config_update(old, is_template=is_template)
+        # The updated config did not write into file, although it doesn't matters.
+        # Commented for performance issue
+        # self.write_file(config_name, new)
+        return old
+
+    @staticmethod
+    def write_file(config_name, data, mod_name='alas'):
+        """
+        Write config file.
+
+        Args:
+            config_name (str): ./config/{file}.json
+            data (dict):
+            mod_name (str):
+        """
+        write_file(filepath_config(config_name, mod_name), data)

+ 77 - 0
module/config/utils.py

@@ -2,9 +2,14 @@
 # @author runhey
 # github https://github.com/runhey
 import os
+import json
+import yaml
 
+from filelock import FileLock
 from datetime import datetime, timedelta, timezone
 
+from module.config.atomicwrites import atomic_write
+
 DEFAULT_TIME = datetime(2023, 1, 1, 0, 0)
 
 def filepath_config(filename, mod_name='script') -> str:
@@ -15,3 +20,75 @@ def filepath_config(filename, mod_name='script') -> str:
         return os.path.join('./config', f'{filename}.json')
     else:
         return os.path.join('./config', f'{filename}.{mod_name}.json')
+
+
+def read_file(file: str):
+    """
+    Read a file, support both .yaml and .json format.
+    Return empty dict if file not exists.
+
+    Args:
+        file (str):
+
+    Returns:
+        dict, list:
+    """
+    folder = os.path.dirname(file)
+    if not os.path.exists(folder):
+        os.mkdir(folder)
+
+    if not os.path.exists(file):
+        return {}
+
+    _, ext = os.path.splitext(file)
+    lock = FileLock(f"{file}.lock")
+    with lock:
+        print(f'read: {file}')
+        if ext == '.yaml':
+            with open(file, mode='r', encoding='utf-8') as f:
+                s = f.read()
+                data = list(yaml.safe_load_all(s))
+                if len(data) == 1:
+                    data = data[0]
+                if not data:
+                    data = {}
+                return data
+        elif ext == '.json':
+            with open(file, mode='r', encoding='utf-8') as f:
+                s = f.read()
+                return json.loads(s)
+        else:
+            print(f'Unsupported config file extension: {ext}')
+            return {}
+
+def write_file(file: str, data):
+    """
+    Write data into a file, supports both .yaml and .json format.
+
+    Args:
+        file (str):
+        data (dict, list):
+    """
+    folder = os.path.dirname(file)
+    if not os.path.exists(folder):
+        os.mkdir(folder)
+
+    _, ext = os.path.splitext(file)
+    lock = FileLock(f"{file}.lock")
+    with lock:
+        print(f'write: {file}')
+        if ext == '.yaml':
+            with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
+                if isinstance(data, list):
+                    yaml.safe_dump_all(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
+                                       sort_keys=False)
+                else:
+                    yaml.safe_dump(data, f, default_flow_style=False, encoding='utf-8', allow_unicode=True,
+                                   sort_keys=False)
+        elif ext == '.json':
+            with atomic_write(file, overwrite=True, encoding='utf-8', newline='') as f:
+                s = json.dumps(data, indent=2, ensure_ascii=False, sort_keys=False, default=str)
+                f.write(s)
+        else:
+            print(f'Unsupported config file extension: {ext}')
+

+ 2 - 1
requirements-in.txt

@@ -2,4 +2,5 @@ pyside6==6.4.3
 adbutils==1.2.9
 uiautomator2==2.16.23
 
-rich==13.3.5
+rich==13.3.5
+pyyaml==6.0