Skip to content

Commit

Permalink
Create publish.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanerobert committed Nov 4, 2024
1 parent 4fa0603 commit 28865e1
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 84 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Upload Python Package

on:
release:
types: [published]
jobs:
build-and-publish:
runs-on: ubuntu-latest
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.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +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)](http://badge.fury.io/py/config-mixin)


Mission
Expand All @@ -12,7 +12,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 +25,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 +58,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>
8 changes: 7 additions & 1 deletion config_probe/__init__.py → config_mixin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__version__ = "1.0.0"

import collections
import glob
import json
Expand All @@ -6,12 +8,16 @@
import yaml
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
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.

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
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[tox]
envlist = py34,p312
envlist = py312

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

0 comments on commit 28865e1

Please sign in to comment.