Skip to content

Commit

Permalink
WIP. Passes all tests. Build is working with issues on code formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ed-gusev committed Nov 24, 2022
1 parent dca0e17 commit 87cf38f
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data_file = .coverage/.coverage
omit =
*__init__*
*temp*
*tests/*
*/tests/*

[report]
show_missing = True
Expand Down
46 changes: 23 additions & 23 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions _build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,29 @@ rm -r ${DIST_DIR} || printf "%s doesn't exist!\n" ${DIST_DIR}
# todo: do we need this clean/update for build?
# todo: we can use key --outdated - ?
printf "\nCleaning pipenv cache and update dependencies.\n"
pipenv clean ${VERBOSE}
pipenv update ${VERBOSE}
# todo: enable below lines
# pipenv clean ${VERBOSE}
# pipenv update ${VERBOSE}

# -- run pytest with pytest-cov (see pytest.ini/setup.cfg - additional parameters)
printf "\nExecuting tests.\n"
pipenv run pytest tests/

# -- run mypy - types checker
printf "\nExecuting mypy.\n"
printf "\n\nExecuting [mypy] types checker\n\n"
pipenv run mypy src/
pipenv run mypy tests/

# -- run black code formatter
printf "\nExecuting black code formatter.\n"
pipenv run black src/ ${VERBOSE} --line-length 110
pipenv run black tests/ ${VERBOSE} --line-length 110

# -- run flake8 for checking code formatting
printf "\nExecuting flake8.\n"
printf "\n\nExecuting [flake8] code format checker\n\n"
pipenv run flake8 src/
pipenv run flake8 tests/

# -- run black code formatter
printf "\n\nExecuting [black] automatic code formatter.\n"
pipenv run black src/ ${VERBOSE} --line-length 110
pipenv run black tests/ ${VERBOSE} --line-length 110

# -- build library distribution (binary whl and source (tar.gz)
printf "\nBuilding distribution for [PyUtilities] library.\n"
pipenv run python -m build -s -w
Expand Down
2 changes: 1 addition & 1 deletion _env_virtual_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sleep 5

# - check for vulnerabilities and show dependencies graph
printf "\nChecking virtual environment for vulnerabilities.\n"
pipenv check
# pipenv check
pipenv graph

# - outdated packages report
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ keywords = python, toolset, utilities, library
license = MIT
classifiers =
Programming Language :: Python :: 3
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
License :: OSI Approved :: MIT License
Operating System :: OS Independent
Expand Down
6 changes: 3 additions & 3 deletions src/pyutilities/commands/pymaven.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
Functions are incapsulated in PyMaven class.
Created: Dmitrii Gusev, 02.05.2019
Modified: Dmitrii Gusev, 22.11.2022
Modified: Dmitrii Gusev, 24.11.2022
"""

import os
import platform
import logging
import pyutilities.utils.string_utils as string_utils

from subprocess import Popen
from pyutilities.utils.common_utils import myself
Expand All @@ -32,8 +31,9 @@ def __init__(self, mvn_settings: str | None = None):
self.__mvn_exec = self.get_mvn_executable()
log.info(f"Selected maven executable [{self.__mvn_exec}].")

self.__mvn_settings: str | None
# init special maven settings - calculate path
if not string_utils.is_str_empty(mvn_settings):
if mvn_settings:
abs_settings_path = os.path.abspath(mvn_settings)
if not os.path.exists(abs_settings_path): # fail-fast for non-existent settings
raise FileNotFoundError
Expand Down
27 changes: 16 additions & 11 deletions src/pyutilities/config/configuration.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/env python
# coding=utf-8
# -*- coding: utf-8 -*-

"""
Utility class for holding configuration. Can merge configuration with environment variables.
Can load configuration from YAML files. See docstring for Configuration class.
Utility class for holding configuration.
Key properties:
- ability to merge configuration with environment variables
- ability to load configuration from YAML files
For more data - see docstring for Configuration class.
18.11.2018
Added child config class that is able to load config (as dictionary) from xls file (from specified sheet).
18.11.2018 Added config class that is able to load config from xls file.
24.11.2022 Various refactorings, added some typing.
Created: Gusev Dmitrii, XX.08.2017
Modified: Gusev Dmitrii, 12.10.2022
Modified: Gusev Dmitrii, 24.11.2022
"""

import os
Expand Down Expand Up @@ -41,9 +43,11 @@ def __init__(self, path_to_config=None, dict_to_merge=None, is_override_config=T
self.log = logging.getLogger(__name__)
self.log.addHandler(logging.NullHandler())
self.log.debug("Initializing Configuration() instance...")
self.log.debug(f"Load configuration:\n\tpath -> {path_to_config}"
f"\n\tdict -> {dict_to_merge}\n\toverride config -> {is_override_config}"
f"\n\tmerge env -> {is_merge_env}")
self.log.debug(
f"Load configuration:\n\tpath -> {path_to_config}"
f"\n\tdict -> {dict_to_merge}\n\toverride config -> {is_override_config}"
f"\n\tmerge env -> {is_merge_env}"
)

# init internal dictionary
self.config_dict = {}
Expand Down Expand Up @@ -83,7 +87,7 @@ def append_dict(self, dictionary, is_override):
if value:
self.set(key, value)

def load(self, path, is_merge_env=True):
def load(self, path: str | None, is_merge_env=True):
"""Parses YAML file(s) from the given directory/file to add content into this configuration instance
:param is_merge_env: merge parameters with environment (True) or not (False)
:param path: directory/file to load files from
Expand Down Expand Up @@ -252,6 +256,7 @@ def __str__(self):

class ConfigError(Exception):
"""Invalid configuration error"""

pass


Expand Down
6 changes: 3 additions & 3 deletions src/pyutilities/utils/string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
to module String in java library Apache Commons).
Created: Dmitrii Gusev, 15.04.2019
Modified: Dmitrii Gusev, 22.11.2022
Modified: Dmitrii Gusev, 24.11.2022
"""

import logging
from typing import Tuple, Dict
from typing import Tuple, Dict, AnyStr
from pyutilities.exception import PyUtilitiesException
from pyutilities.defaults import MSG_MODULE_ISNT_RUNNABLE

Expand All @@ -21,7 +21,7 @@
log.addHandler(logging.NullHandler())


def is_str_empty(string: str | None) -> bool:
def is_str_empty(string: AnyStr | None) -> bool:
"""Check is string empty/NoNe or not.
:param string:
:return:
Expand Down
5 changes: 4 additions & 1 deletion src/pyutilities/web/web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ def assert_status_hook(response, *args, **kwargs):
self.__session.max_redirects = redirects_count
if not user_agent: # set User Agent header
user_agent = WebClient.__ua.random
self.__session.headers.update({"user-agent": user_agent}) # header may be "User-Agent"

# header may be "User-Agent" or "user-agent" (usually camel case)
self.__session.headers.update({"User-Agent": f"{user_agent}"})

if auth: # add authorization to session
self.__session.auth = auth

Expand Down
32 changes: 18 additions & 14 deletions tests/config/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
#!/usr/bin/env python
# coding=utf-8
# -*- coding: utf-8 -*-

"""
Unit tests for Configuration class.
Created: Gusev Dmitrii, XX.08.2017
Modified: Gusev Dmitrii, 12.10.2022
Modified: Gusev Dmitrii, 24.11.2022
"""

import os
import unittest
from mock import patch
from pyutilities.config.configuration import Configuration, ConfigError

CONFIG_PATH = "test_configs"
CONFIG_MODULE_MOCK_YAML = "pyutilities.config.configuration.parse_yaml"
CONFIG_PATH = "tests/config/test_configs"
CONFIG_MODULE_MOCK_YAML = "pyutilities.config.configuration.read_yaml"
CONFIG_MODULE_MOCK_OS = "pyutilities.config.configuration.os"

KEY1 = "section1.key1"
KEY2 = "section2.key3"


class ConfigurationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# method just for the demo purpose
pass

def setUp(self):
def setUp(self) -> None:
# init config instance before each test, don't merge with environment
self.config = Configuration(is_merge_env=False)
self.config: Configuration = Configuration(is_merge_env=False)

def tearDown(self):
def tearDown(self) -> None:
# dereference config instance after each test
self.config = None
# self.config = None
pass

def test_load_invalid_path(self):
for invalid_path in ["", "", " ", " ", "non-exist path"]:
Expand All @@ -39,8 +43,8 @@ def test_load_invalid_path(self):

def test_merge_config_files(self):
self.config.load(CONFIG_PATH) # <- load all yaml configs from specified location
self.assertEqual(self.config.get("section1.key1"), "value1")
self.assertEqual(self.config.get("section2.key3"), "value3")
self.assertEqual(self.config.get(KEY1), "value1")
self.assertEqual(self.config.get(KEY2), "value3")

def test_merge_env_variables(self):
os.environ["simple_key"] = "env_value"
Expand All @@ -53,8 +57,8 @@ def test_merge_env_variables(self):
def test_merge_dict_single_on_init(self):
dict_to_merge = {"a": "b", "c": "d", "aa.bb": "eee"}
config = Configuration(path_to_config=CONFIG_PATH, dict_to_merge=dict_to_merge, is_merge_env=True)
self.assertEqual(config.get("section1.key1"), "value1")
self.assertEqual(config.get("section2.key3"), "value3")
self.assertEqual(config.get(KEY1), "value1")
self.assertEqual(config.get(KEY2), "value3")
self.assertEqual(config.get("a"), "b")
self.assertEqual(config.get("c"), "d")
self.assertEqual(config.get("aa.bb"), "eee")
Expand All @@ -65,8 +69,8 @@ def test_merge_dict_list_on_init(self):
config = Configuration(
path_to_config=CONFIG_PATH, dict_to_merge=dict_list_to_merge, is_merge_env=True
)
self.assertEqual(config.get("section1.key1"), "value1")
self.assertEqual(config.get("section2.key3"), "value3")
self.assertEqual(config.get(KEY1), "value1")
self.assertEqual(config.get(KEY2), "value3")
self.assertEqual(config.get("a"), "b")
self.assertEqual(config.get("c"), "d")
self.assertEqual(config.get("aa.bb"), "eee")
Expand Down
8 changes: 4 additions & 4 deletions tests/config/test_configuration_xls.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env python
# coding=utf-8
# -*- coding: utf-8 -*-

"""
Unit tests for ConfigurationXls class.
Created: Gusev Dmitrii, XX.12.2018
Modified: Gusev Dmitrii, 12.10.2022
Modified: Gusev Dmitrii, 24.11.2022
"""

import unittest
from pyutilities.config.configuration import ConfigurationXls, ConfigError

XLSX_CONFIG_FILE = "test_configs/xlsx_config.xlsx" # xlsx format (Excel 2010)
XLS_CONFIG_FILE = "test_configs/xls_config.xls" # xls format (old Excel)
XLSX_CONFIG_FILE = "tests/config/test_configs/xlsx_config.xlsx" # xlsx format (Excel 2010)
XLS_CONFIG_FILE = "tests/config/test_configs/xls_config.xls" # xls format (old Excel)
CONFIG_SHEET = "config_sheet"


Expand Down
Loading

0 comments on commit 87cf38f

Please sign in to comment.