Skip to content

Commit

Permalink
Use polarion-tools-common
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Sep 13, 2019
1 parent d2d91ff commit d8402e4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 57 deletions.
45 changes: 6 additions & 39 deletions dump2polarion/configuration.py
Expand Up @@ -2,14 +2,13 @@
Configuration loading.
"""

import glob
import logging
import os

import yaml
from polarion_tools_common import configuration, utils

from dump2polarion.exceptions import Dump2PolarionException
from dump2polarion.utils import find_vcs_root

DEFAULT_CONF = os.path.join(os.path.dirname(os.path.abspath(__file__)), "polarion_tools.yaml")
PROJECT_CONF_DIRS = ("conf", ".")
Expand Down Expand Up @@ -123,47 +122,14 @@ def _get_user_conf(config_file):
return config_settings


def _get_project_conf():
"""Loads configuration from project config file."""
config_settings = {}

project_root = find_vcs_root(".")
if project_root is None:
return config_settings

for conf_dir in PROJECT_CONF_DIRS:
conf_dir = conf_dir.lstrip("./")
joined_dir = os.path.join(project_root, conf_dir) if conf_dir else project_root
joined_glob = os.path.join(joined_dir, PROJECT_CONF)
conf_files = glob.glob(joined_glob)
# config files found, not trying other directories
if conf_files:
break
else:
conf_files = []

for conf_file in conf_files:
try:
with open(conf_file, encoding="utf-8") as input_file:
loaded_settings = yaml.safe_load(input_file)
except OSError:
logger.warning("Failed to load config from %s", conf_file)
else:
logger.info("Config loaded from %s", conf_file)
config_settings.update(loaded_settings)

return config_settings


def get_config(config_file=None, config_values=None, load_project_conf=True):
"""Loads config file and returns its content."""
config_values = config_values or {}
config_settings = {}

default_conf = _get_default_conf()
user_conf = _get_user_conf(config_file) if config_file else {}
# load project configuration only when user configuration was not specified
project_conf = {} if user_conf or not load_project_conf else _get_project_conf()
project_conf = {} if user_conf or not load_project_conf else configuration.get_config()

if not (user_conf or project_conf or config_values):
if load_project_conf:
Expand All @@ -174,10 +140,11 @@ def get_config(config_file=None, config_values=None, load_project_conf=True):
raise Dump2PolarionException("No configuration file or values passed.")

# merge configuration
config_settings = {}
config_settings.update(default_conf)
config_settings.update(user_conf)
config_settings.update(project_conf)
config_settings.update(config_values)
utils.merge_dicts(config_settings, user_conf)
utils.merge_dicts(config_settings, project_conf)
utils.merge_dicts(config_settings, config_values)

_populate_urls(config_settings)
_set_legacy_project_id(config_settings)
Expand Down
19 changes: 2 additions & 17 deletions dump2polarion/utils.py
Expand Up @@ -13,6 +13,7 @@
import requests
import urllib3
from lxml import etree
from polarion_tools_common import utils

from dump2polarion.exceptions import Dump2PolarionException

Expand All @@ -28,13 +29,7 @@

def get_unicode_str(obj):
"""Makes sure obj is a valid XML unicode string."""
if isinstance(obj, str):
text = obj
elif isinstance(obj, bytes):
text = obj.decode("utf-8", errors="ignore")
else:
text = str(obj)
return VALID_XML_RE.sub("", text)
return VALID_XML_RE.sub("", utils.get_unicode_str(obj))


def init_log(log_level):
Expand Down Expand Up @@ -159,16 +154,6 @@ def get_session(credentials, config):
return session


def find_vcs_root(path, dirs=(".git",)):
"""Searches up from a given path to find the project root."""
prev, path = None, os.path.abspath(path)
while prev != path:
if any(os.path.exists(os.path.join(path, d)) for d in dirs):
return path
prev, path = path, os.path.abspath(os.path.join(path, os.pardir))
return None


def get_testrun_id_config(config):
"""Gets testrun ID defined in config file."""
config_testrun_id = config.get("xunit_import_properties") or {}
Expand Down
10 changes: 9 additions & 1 deletion setup.py
Expand Up @@ -23,7 +23,15 @@
]
},
setup_requires=["setuptools_scm"],
install_requires=["lxml", "pyyaml", "requests", "docutils", "packaging", "python-box"],
install_requires=[
"lxml",
"pyyaml",
"requests",
"docutils",
"packaging",
"python-box",
"polarion-tools-common",
],
keywords=["polarion", "testing"],
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Expand Up @@ -18,6 +18,9 @@ commands=
max_line_length = 100
max-complexity = 10

[tool:isort]
known_third_party=polarion_tools_common

[travis]
python =
3.7: py37, lint
Expand Down

0 comments on commit d8402e4

Please sign in to comment.