Skip to content

Commit

Permalink
pypi package 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed Dec 27, 2023
1 parent 16d99b0 commit 3f5aa85
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,5 @@ cython_debug/
examples/tmp

tmp

pypi.sh
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ pytest:
venv/bin/python -m pytest ./tests

autotest: doctest pytest


wheel:
venv/bin/python setup.py develop
venv/bin/python setup.py sdist
venv/bin/python setup.py bdist_wheel

wheel-push:
bash wheel-push.sh

pypi: wheel wheel-push
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[![PyPI version](https://badge.fury.io/py/toml_union.svg)](https://pypi.org/project/toml_union/)
[![Downloads](https://pepy.tech/badge/toml_union)](https://pepy.tech/project/toml_union)
[![Downloads](https://pepy.tech/badge/toml_union/month)](https://pepy.tech/project/toml_union)
[![Downloads](https://pepy.tech/badge/toml_union/week)](https://pepy.tech/project/toml_union)


- [About](#about)
- [Python usage example](#python-usage-example)
Expand All @@ -7,7 +12,11 @@

# About

This script combines several `*.toml` files (usially `pyproject.toml`) into one. If it finds some conflict between items, they are kept for manual review.
```sh
pip install toml-union
```

This PyPI package is located in one script and combines several `*.toml` files (usially `pyproject.toml`) into one. If it finds some conflict between items, they are kept for manual review.

It has a docker image (`docker pull pasaopasen/toml-union`) and an [example](/examples/docker-test.sh) of its usage.

Expand Down Expand Up @@ -197,13 +206,14 @@ version = "12"

Equivalent CLI command:
```sh
python toml_union.py examples/input/file1.toml examples/input/file2.toml examples/input/file3.toml -o output.toml -r report.json -k tool.poetry.name=union -k tool.poetry.version=12
toml-union examples/input/file1.toml examples/input/file2.toml examples/input/file3.toml -o output.toml -r report.json -k tool.poetry.name=union -k tool.poetry.version=12
```

Help message:

```sh
venv/bin/python toml_union.py -h
toml-union -h

usage: toml_union.py [-h] [--output OUTFILE] [--unicode-escape] [--report REPORT] [--remove-field [REMOVE_FIELDS [REMOVE_FIELDS ...]]] [--key-value KEY=VALUE] [--ckey-value KEY=VALUE] INPUT [INPUT ...]

Combines several toml files to one with conflicts showing
Expand All @@ -214,7 +224,7 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
--output OUTFILE, -o OUTFILE
output toml file path (default: None)
output toml file path, empty value means to print to console (default: None)
--unicode-escape, -u whether to try to escape unicode sequences in the outfile, useful when outfile has many slashes and codes (default: False)
--report REPORT, -r REPORT
path to report json on failure (default: None)
Expand Down
4 changes: 4 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@

pytest
ipython

setuptools
wheel
twine
39 changes: 39 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import setuptools


def parse_requirements(requirements: str):
with open(requirements) as f:
return [
l.strip('\n') for l in f if l.strip('\n') and not l.startswith('#')
]


with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="toml_union",
version="1.0.0",
author="Demetry Pascal",
author_email="qtckpuhdsa@gmail.com",
maintainer='Demetry Pascal',
description="Utils to merge *.toml files with conflicts highlighting",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/PasaOpasen/toml-union",
license='MIT',
keywords=['toml', 'merge'],
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3.8",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=parse_requirements('./requirements.txt'),
entry_points = {
'console_scripts': ['toml-union=toml_union.toml_union:main'],
},
)

2 changes: 1 addition & 1 deletion toml_union/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

from .toml_union import toml_union_process, override_param, remove_field, read_toml, write_toml, write_json
from .toml_union import toml_union_process, override_param, remove_field, read_toml, write_toml, write_json, read_text, write_text
29 changes: 23 additions & 6 deletions toml_union/toml_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from collections import defaultdict
from dataclasses import dataclass
from functools import reduce
import tempfile

import argparse

Expand Down Expand Up @@ -273,6 +274,15 @@ def process(data: TOML_DICT) -> TOML_DICT:
return process(dct)


def read_text(file_name: Union[str, os.PathLike]) -> str:
return Path(file_name).read_text(encoding='utf-8')


def write_text(file_name: Union[str, os.PathLike], text: str):
mkdir_of_file(file_name)
Path(file_name).write_text(text, encoding='utf-8')


def read_toml(file_name: Union[str, os.PathLike]) -> TOML_DICT:
"""reads dict from toml with some preprocessing"""
with open(file_name, 'r', encoding='utf-8') as f:
Expand All @@ -292,9 +302,9 @@ def write_toml(file_name: Union[str, os.PathLike], data: TOML_DICT, unicode_esca
toml.dump(data, f)

if unicode_escape:
Path(file_name).write_text(
Path(file_name).read_text(encoding='utf-8').encode().decode('unicode_escape').replace('"', "'"),
encoding='utf-8'
write_text(
file_name,
read_text(file_name).encode().decode('unicode_escape').replace('"', "'")
)


Expand Down Expand Up @@ -476,7 +486,7 @@ def remove_field(dct: Dict, route: str):

def toml_union_process(
files: Iterable[Union[str, os.PathLike]],
outfile: Union[str, os.PathLike],
outfile: Optional[Union[str, os.PathLike]] = None,
report: Optional[Union[str, os.PathLike]] = None,
remove_fields: Optional[Iterable[str]] = None,
overrides: Dict[str, Any] = None,
Expand Down Expand Up @@ -536,7 +546,14 @@ def toml_union_process(

outdict = to_dict(datas)
"""result shortened dict"""
write_toml(outfile, outdict, unicode_escape=unicode_escape)

if outfile is None:
_, f = tempfile.mkstemp(prefix='toml-union', text=True)
write_toml(f, outdict, unicode_escape=unicode_escape)
print(read_text(f))
os.unlink(f)
else:
write_toml(outfile, outdict, unicode_escape=unicode_escape)

if report:
index_file_map: List[str] = [
Expand Down Expand Up @@ -599,7 +616,7 @@ def __call__(self, parser, args, values, option_string=None):
)
parser.add_argument(
'--output', '-o', action='store', type=str,
help='output toml file path',
help='output toml file path, empty value means to print to console',
dest='outfile'
)

Expand Down
20 changes: 20 additions & 0 deletions wheel-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# pushes to pypi with credentials reading
#

if [ -f pypi.sh ]
then
source pypi.sh
fi

U=${PYPI_USERNAME}
P=${PYPI_PASSWORD}

if [ -n "$U" ] && [ -n "$P" ]
then
venv/bin/python -m twine upload -u $U -p $P dist/* --skip-existing
else
echo "credentials not found"
venv/bin/python -m twine upload dist/* --skip-existing
fi

0 comments on commit 3f5aa85

Please sign in to comment.