Skip to content

Commit

Permalink
Added published dist
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthuchaut committed Nov 20, 2020
1 parent 52299a2 commit c349cb8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
50 changes: 44 additions & 6 deletions build/lib/syenv/syenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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]),
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Binary file added dist/syenv-1.2.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/syenv-1.2.0.tar.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions syenv.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)).
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_syenv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
import pathlib
from pydoc import locate
from typing import Any, Callable, Dict
import pytest
Expand Down

0 comments on commit c349cb8

Please sign in to comment.