Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating code to python 3.12 #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Upload Python Package

on:
release:
types: [published]
jobs:
build-and-publish:
runs-on: ubuntu-latest
environment: pypi
permissions:
# IMPORTANT: this permission is mandatory for trusted publishing
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.release.tag_name }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
check-latest: true
- name: Install build dependencies
run: pip install -U setuptools wheel build
- name: Build
run: python -m build .
- name: Publish
uses: pypa/gh-action-pypi-publish@release/v1
18 changes: 18 additions & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python environment
uses: actions/setup-python@v3
with:
python-version: '3.12'
- name: tox
run: |
python -m pip install --upgrade pip
python -m pip install tox
tox
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[![Build Status](https://travis-ci.org/internap/python-config-probe.svg?branch=master)](https://travis-ci.org/internap/python-config-probe)
[![PyPI version](https://badge.fury.io/py/config-probe.svg)](http://badge.fury.io/py/config-probe)

![Build Status](https://github.com/stephanerobert/config-mixin/actions/workflows/tox.yml/badge.svg?branch=master)
[![PyPI version](https://badge.fury.io/py/config-mixin.svg?icon=si%3Apython)](https://badge.fury.io/py/config-mixin)

Mission
=======
Expand All @@ -12,7 +11,7 @@ the result config will be a shortcut to any config.

Setup:

config = probe(
config = mixin(
path="path/to/my/files",
patterns=["path/(*)/file.yaml"]
)
Expand All @@ -25,7 +24,7 @@ Use it:

- **path**

Initial path to probe. Patterns will be tested against the file structure underneath the path
Initial path to configs. Patterns will be tested against the file structure underneath the path,
and it will be ignored in determining the namespacing.

- **patterns**
Expand Down Expand Up @@ -58,8 +57,8 @@ Use it:

## Mocking the probing

Your unit test can have your code use fake_probe instead to which to give a dict and it will appear as if it
was just probed. Example:
Your unit test can have your code use fake_probe instead to which to give a dict, and it will appear as if it
was just mixed-in. Example:

config = fake_probe({
"ns1": {
Expand Down
9 changes: 9 additions & 0 deletions config-mixin.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
16 changes: 11 additions & 5 deletions config_probe/__init__.py → config_mixin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
__version__ = "1.0.0"

import collections
import glob
import json
import os

import yaml
from munch import Munch, iteritems
from munch import Munch

from config_probe.exceptions import ConfigNotFound
from config_mixin.exceptions import ConfigNotFound

NAMESPACE_PLACEHOLDER = "(*)"


def probe(path, patterns):
return mixin(path, patterns)


def mixin(path, patterns):
config = {}

for pattern in patterns:
Expand Down Expand Up @@ -46,7 +52,7 @@ def _deduce_namespaces(path_matchers, path_parts):
namespaces = []
path_parts, current_part = os.path.split(path_parts)
path_matchers, matcher = os.path.split(path_matchers)
while current_part is not "":
while current_part != "":
if matcher == NAMESPACE_PLACEHOLDER:
namespaces.append(current_part)

Expand All @@ -67,7 +73,7 @@ def _add_to_configuration(config, namespaces, new_values):

def _update(config, values):
for k, v in values.items():
if k in config and isinstance(v, collections.Mapping):
if k in config and isinstance(v, collections.abc.Mapping):
_update(config[k], v)
else:
config[k] = v
Expand All @@ -83,7 +89,7 @@ def __getattr__(self, k):

def _munchify(x):
if isinstance(x, dict):
return _Munch((k, _munchify(v)) for k,v in iteritems(x))
return _Munch((k, _munchify(v)) for k,v in x.items())
elif isinstance(x, (list, tuple)):
return type(x)(_munchify(v) for v in x)
else:
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[build-system]
requires = ["setuptools>=61.0", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "config_mixin"
dynamic = ["version"]
description = "Provide an auto-discovery process for configurations"
readme = "README.md"
requires-python = ">=3.10"
license = {text = "License :: OSI Approved :: Apache Software License"}
classifiers = [
"Intended Audience :: Developers",
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12"
]
dependencies = [
"munch",
"PyYAML"
]
authors = [
{name = "Stephane Robert", email = "stephane.robert@gmail.com"},
]

[project.optional-dependencies]
test = ["pytest", "pytest-cov", "pyhamcrest"]

[project.urls]
"Homepage" = "https://github.com/stephanerobert/config-mixin"

[tool.setuptools.dynamic]
version = {attr = "config_mixin.__version__"}
28 changes: 0 additions & 28 deletions setup.cfg

This file was deleted.

8 changes: 0 additions & 8 deletions setup.py

This file was deleted.

3 changes: 2 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nose
pytest
pytest-cov
pyhamcrest
36 changes: 18 additions & 18 deletions tests/test_config_probe.py → tests/test_config_mixin.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
import unittest

import os
from config_probe import probe, fake_probe
from config_probe.exceptions import ConfigNotFound
from config_mixin import mixin, fake_probe
from config_mixin.exceptions import ConfigNotFound
from hamcrest import is_, assert_that, has_key


class TestConfigProbe(unittest.TestCase):
def test_single_file(self):
config = probe(path=_dir("single-file"),
config = mixin(path=_dir("single-file"),
patterns=["stuff.yaml"])

assert_that(config.key, is_("stuff-value"))
assert_that(config.fruits, is_(["Apple", "Orange"]))

def test_single_file_with_namespace(self):
config = probe(path=_dir("single-file-with-namespace"),
config = mixin(path=_dir("single-file-with-namespace"),
patterns=["(*).json"])

assert_that(config.stuff.key, is_("stuff-value"))

def test_single_file_with_namespace_with_wrong_key(self):
config = probe(path=_dir("single-file-with-namespace"),
config = mixin(path=_dir("single-file-with-namespace"),
patterns=["(*).json"])

with self.assertRaises(ConfigNotFound):
print(config.stuff.key_inexistant)

def test_single_file_multiple_level_raising_or_not(self):
config = probe(path=_dir("multi-level-files"),
config = mixin(path=_dir("multi-level-files"),
patterns=["(*)/(*).yaml", "(*)/subdir/(*).yaml"])
assert_that(config.ns1.stuff.we.need.more.cowbell, is_("ok"))

with self.assertRaises(ConfigNotFound):
print(config.ns1.stuff.we.need.less.cowbell)

def test_two_files_with_subdir_namespace(self):
config = probe(path=_dir("two-files-with-subdir-namespace"),
config = mixin(path=_dir("two-files-with-subdir-namespace"),
patterns=["(*)/(*).yaml"])

assert_that(config.ns1.stuff.key1, is_("stuff from ns1"))
assert_that(config.ns2.stuff.key2, is_("stuff from ns2"))

def test_only_starred_parts_are_namespaced(self):
config = probe(path=_dir("two-files-with-subdir-namespace"),
config = mixin(path=_dir("two-files-with-subdir-namespace"),
patterns=["(*)/stuff.yaml"])

assert_that(config.ns1.key1, is_("stuff from ns1"))
assert_that(config.ns2.key2, is_("stuff from ns2"))

def test_using_only_a_star_does_not_count_toward_namespacing(self):
config = probe(path=_dir("two-files-with-subdir-namespace"),
config = mixin(path=_dir("two-files-with-subdir-namespace"),
patterns=["*/stuff.yaml"])

assert_that(config.key1, is_("stuff from ns1"))
assert_that(config.key2, is_("stuff from ns2"))

def test_multiple_patterns(self):
config = probe(path=_dir("two-files-with-subdir-namespace"),
config = mixin(path=_dir("two-files-with-subdir-namespace"),
patterns=["ns1/(*).yaml", "(*)/stuff.yaml"])

assert_that(config.stuff.key1, is_("stuff from ns1"))
assert_that(config.ns2.key2, is_("stuff from ns2"))

def test_multiple_patterns_on_same_namespaces_should_merge_recursively(self):
config = probe(path=_dir("multi-level-files"),
config = mixin(path=_dir("multi-level-files"),
patterns=["(*)/(*).yaml", "(*)/subdir/(*).yaml"])

assert_that(config.ns1.stuff.content1.key1, is_("value1"))
assert_that(config.ns1.stuff.content2.key2, is_("value2"))

def test_pattern_order_defines_which_files_have_the_authority(self):
config = probe(path=_dir("key-override"),
config = mixin(path=_dir("key-override"),
patterns=["file1.yaml", "file2.yaml"])
assert_that(config.key, is_("value2"))

config = probe(path=_dir("key-override"),
config = mixin(path=_dir("key-override"),
patterns=["file2.yaml", "file1.yaml"])
assert_that(config.key, is_("value1"))

def test_yaml_dict_are_merged_list_arent(self):
config = probe(path=_dir("dict-merge"),
config = mixin(path=_dir("dict-merge"),
patterns=["file1.yaml", "file2.yaml"])
assert_that(config.mydict.common_key, is_("value2"))
assert_that(config.mydict.only_in_1, is_("value1"))
Expand All @@ -89,7 +89,7 @@ def test_yaml_dict_are_merged_list_arent(self):
assert_that(config.mydict.subdict.only_in_1, is_("value1"))
assert_that(config.mydict.subdict.only_in_2, is_("value2"))

config = probe(path=_dir("dict-merge"),
config = mixin(path=_dir("dict-merge"),
patterns=["file2.yaml", "file1.yaml"])
assert_that(config.mydict.common_key, is_("value1"))
assert_that(config.mydict.only_in_1, is_("value1"))
Expand All @@ -99,7 +99,7 @@ def test_yaml_dict_are_merged_list_arent(self):
assert_that(config.mydict.subdict.only_in_2, is_("value2"))

def test_dict_should_behave_as_dict(self):
config = probe(path=_dir("single-file"),
config = mixin(path=_dir("single-file"),
patterns=["(*).yaml"])

assert_that(config["stuff"]["key"], is_("stuff-value"))
Expand All @@ -109,10 +109,10 @@ def test_dict_should_behave_as_dict(self):
assert_that(dict(**config.stuff), has_key('key'))

def test_support_for_empty_files(self):
probe(path=_dir("empty-files"), patterns=["*.*"])
mixin(path=_dir("empty-files"), patterns=["*.*"])

def test_support_absolute_paths_outside_base_dir(self):
config = probe(path=_dir("single-file-with-namespace"), patterns=[
config = mixin(path=_dir("single-file-with-namespace"), patterns=[
"{}/(*)/(*)/stuff.yaml".format(os.path.dirname(__file__))
])
assert_that(config['two-files-with-subdir-namespace'].ns1.key1, is_("stuff from ns1"))
Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[tox]
envlist = py27,py34
envlist = py312

[testenv]
deps = -r{toxinidir}/test-requirements.txt
commands = nosetests
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands = pytest --cov --cov-report term-missing