diff --git a/build/lib/syenv/syenv.py b/build/lib/syenv/syenv.py index a6f0a02..8f62786 100644 --- a/build/lib/syenv/syenv.py +++ b/build/lib/syenv/syenv.py @@ -16,17 +16,33 @@ class Syenv: """ _INTERP_REGEX: str = r'{{(\w+)}}' - - def __init__(self, prefix: str = '', type_separator: str = '::') -> None: + _DEFAULT_TYPE_SEP: str = '::' + _DEFAULT_KEEP_PREFIX: bool = False + _SKIPED_ATTR: List[str] = ['_prefix', '_type_separator', '_keep_prefix'] + + def __init__( + self, + prefix: str = '', + *, + type_separator: str = _DEFAULT_TYPE_SEP, + keep_prefix: bool = _DEFAULT_KEEP_PREFIX, + ) -> None: """The Syenv class constructor. Hydrate the object with the variables retrieved. Args: - prefix (str, optional): The variables prefixe. Default to ''. + prefix (str, optional): The variables prefixe. + Default to Syenv._DEFAULT_TYPE_SEP. + type_separator (str, optional): The pattern that seperate the + type from the value. Default to Syenv._DEFAULT_TYPE_SEP. + keep_prefix (bool, optional): Indicate if Syenv should keep this + prefix for its attributes name. + Default to Syenv._DEFAULT_KEEP_PREFIX. """ self._prefix: str = prefix self._type_separator: str = type_separator + self._keep_prefix: bool = keep_prefix self._loadenv() @property @@ -39,6 +55,19 @@ def as_dict(self) -> Dict[str, Any]: return {k: v for k, v in self.__iter__()} + def from_pattern(self, pattern: str) -> Dict[str, Any]: + """Get the variables which names matches with the pattern + passed in parameter. + + Args: + pattern (str): The string to search in attributes. + + Returns: + Dict[str, Ant]: The attributes matched. + """ + + return {k: v for k, v in self.as_dict.items() if re.search(pattern, k)} + def _loadenv(self) -> None: """Hydrate the Syenv object with the environment variables retrieved. @@ -52,7 +81,9 @@ def _loadenv(self) -> None: if re.match(r'^%s' % self._prefix, env_key): setattr( self, - self._sub_prefix(env_key), + env_key + if self._keep_prefix + else self._sub_prefix(env_key), self._interpolate(os.environ[env_key]), ) @@ -84,7 +115,14 @@ def _interpolate(self, val: str) -> str: try: val = val.replace( '{{%s}}' % key, - str(getattr(self, self._sub_prefix(key))), + str( + getattr( + self, + key + if self._keep_prefix + else self._sub_prefix(key), + ) + ), ) except AttributeError: raise SysenvError( @@ -167,5 +205,5 @@ def __iter__(self) -> Generator: """Overload the __iter__ method for suppress useless attributes.""" for key, val in self.__dict__.items(): - if key not in ['_prefix', '_type_separator']: + if key not in self._SKIPED_ATTR: yield key, val \ No newline at end of file diff --git a/dist/syenv-1.2.0-py3-none-any.whl b/dist/syenv-1.2.0-py3-none-any.whl new file mode 100644 index 0000000..ed0a06f Binary files /dev/null and b/dist/syenv-1.2.0-py3-none-any.whl differ diff --git a/dist/syenv-1.2.0.tar.gz b/dist/syenv-1.2.0.tar.gz new file mode 100644 index 0000000..b4bc274 Binary files /dev/null and b/dist/syenv-1.2.0.tar.gz differ diff --git a/setup.py b/setup.py index 64a7f50..ae47c40 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,8 @@ long_description: str = pathlib.Path('README.md').read_text(encoding='utf-8') setuptools.setup( - name="syenv", # Replace with your own username - version="1.1.0", + name="syenv", + version="1.2.0", author="Arthuchaut", author_email="arthuchaut@gmail.com", description="A simple environment variables importer which some cool functionnalities", diff --git a/syenv.egg-info/PKG-INFO b/syenv.egg-info/PKG-INFO index 82119c7..6ee2340 100644 --- a/syenv.egg-info/PKG-INFO +++ b/syenv.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: syenv -Version: 1.1.0 +Version: 1.2.0 Summary: A simple environment variables importer which some cool functionnalities Home-page: https://github.com/Arthuchaut/syenv Author: Arthuchaut @@ -47,10 +47,10 @@ Description: # Syenv # .env MY_APP_FTPS_HOST=hostname MY_APP_FTPS_USER=username - MY_APP_FTPS_PORT=int:22 + MY_APP_FTPS_PORT=int::22 - MY_APP_STORAGE_DIR=pathlib.Path:storage - MY_APP_STUFF_STORAGE=pathlib.Path:{{MY_APP_STORAGE_DIR}}/stuffs + MY_APP_STORAGE_DIR=pathlib.Path::storage + MY_APP_STUFF_STORAGE=pathlib.Path::{{MY_APP_STORAGE_DIR}}/stuffs ``` We can observe that the syntax of the values is a bit special. Syenv supports `variable typing` and `interpolation` (see [Variables syntax](#variables-syntax)). diff --git a/tests/unit/test_syenv.py b/tests/unit/test_syenv.py index 873d936..ae5f913 100644 --- a/tests/unit/test_syenv.py +++ b/tests/unit/test_syenv.py @@ -1,5 +1,4 @@ from pathlib import Path -import pathlib from pydoc import locate from typing import Any, Callable, Dict import pytest