Skip to content

Commit

Permalink
Done refactorings and tests and minor fixes for v.1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ed-gusev committed Nov 27, 2022
1 parent 582dacf commit b6098aa
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 88 deletions.
5 changes: 3 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# - https://pytest-cov.readthedocs.io/en/latest/index.html
#
# Created: Dmitrii Gusev, 09.10.2022
# Modified:
# Modified: Dmitrii Gusev, 27.11.2022
#
###############################################################################

Expand All @@ -16,8 +16,9 @@ branch = True
data_file = .coverage/.coverage
omit =
*__init__*
*temp*
*/temp/*
*/tests/*
*/.vscode/*

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

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

42 changes: 14 additions & 28 deletions _build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# system shell) and from the pipenv environment as well (pipenv shell).
#
# Created: Dmitrii Gusev, 30.11.2021
# Modified: Dmitrii Gusev, 22.11.2022
# Modified: Dmitrii Gusev, 27.11.2022
#
###############################################################################

Expand All @@ -24,15 +24,6 @@ VERBOSE="--verbose"
BUILD_DIR='build/'
DIST_DIR='dist/'

# -- pypi user/password
PYPI_USER="<specify PyPi user>"
PYPI_PASSWORD="<specify PyPi password>"

# -- upload mode: PROD (pypi.org), TEST (test.pypi.org), DRY-RUN (no upload, default)
UPLOAD_MODE="DRY-RUN"
# -- setup proxy for twine (comment/uncomment - if needed)
#export ALL_PROXY=<proxy host>:<port>

clear
printf "Build of [PyUtilities] library is starting...\n"
sleep 2
Expand All @@ -45,15 +36,19 @@ printf "\nDeleting [%s]...\n" ${DIST_DIR}
rm -r ${DIST_DIR} || printf "%s doesn't exist!\n" ${DIST_DIR}

# -- clean caches and sync + lock pipenv dependencies (update from the file Pipfile.lock)
# todo: do we need this clean/update for build?
# todo: we can use key --outdated - ?
printf "\nCleaning pipenv cache and update dependencies.\n"
# todo: enable below lines
# pipenv clean ${VERBOSE}
# pipenv update ${VERBOSE}
# todo: make this update optional - by cmd line key
printf "\n\nCleaning pipenv cache and update dependencies.\n\n"
pipenv clean ${VERBOSE}
pipenv update --outdated ${VERBOSE}
pipenv update ${VERBOSE}

# -- 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

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

# -- run mypy - types checker
Expand All @@ -66,18 +61,9 @@ 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"
# -- (option -s -> tar.gz, option -w -> whl (binary) distribution)
printf "\n\nBuilding distribution for [PyUtilities] library.\n\n"
pipenv run python -m build -s -w

# -- upload new library to Test PyPi (TEST)
# twine upload --repository-url https://test.pypi.org/legacy/ dist/*
# -- upload new library dist to real PyPi (PROD)
# twine upload -u vinnypuhh dist/*

printf "\nBuild finished.\n\n"
26 changes: 26 additions & 0 deletions _deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

###############################################################################
#
# Deployment script for [pyutilities] project.
# Script can be run from outside of virtual (pipenv) environment (from the
# system shell) and from the pipenv environment as well (pipenv shell).
#
# Created: Dmitrii Gusev, 27.11.2022
# Modified:
#
###############################################################################

# -- safe bash scripting - fail-fast pattern (google for more info)
set -euf -o pipefail

# -- set up encoding/language
export LANG="en_US.UTF-8"

# -- upload new library to Test PyPi (TEST)
# printf "\n\nUploading new library dist to test pypi repo\n\n"
# twine upload --repository-url https://test.pypi.org/legacy/ dist/*

# -- upload new library dist to real PyPi (PROD)
printf "\n\nUploading library [pyutilities] to PyPi repository\n\n"
twine upload -u "$1" -p "$2" dist/*
6 changes: 4 additions & 2 deletions _env_virtual_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# environment (pipenv shell).
#
# Created: Dmitrii Gusev, 09.10.2022
# Modified: Dmitrii Gusev, 22.11.2022
# Modified: Dmitrii Gusev, 27.11.2022
#
###############################################################################

Expand Down Expand Up @@ -65,9 +65,11 @@ 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
printf "\n\nOutdated packages list (pip list):\n"
pipenv run pip list --outdated

printf "\n\n"
13 changes: 8 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ description = PyUtilities :: Useful python 3.x utilities library.
url = https://pypi.org/project/pyutilities
author = Dmitrii Gusev
author_email = dmitry.ed.gusev@gmail.com
long_description = file: README.md, LICENCE
long_description = file: README.md, LICENSE
long_description_content_type = text/markdown
# license_files = LICENSE
keywords = python, toolset, utilities, library
license = MIT
classifiers =
Expand Down Expand Up @@ -71,7 +72,9 @@ install_requires =
[options.packages.find]
where = src

# -- integration with setuptools - cmd line - see link to click library
# [options.entry_points]
# console_scripts =
# mypackage = mypackage.myscript:main
# -- integration with setuptools - cmd line entry points
[options.entry_points]
console_scripts =
pyftp = pyutilities.network.pyftp:pyftp
pyssh = pyutilities.network.pyssh:pyssh
fedit = pyutilities.io.fedit:fedit
81 changes: 43 additions & 38 deletions src/pyutilities/io/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
Inplace/inline file editing utility. Executable from cmd line.
Created: Gusev Dmitrii, 13.04.2017
Modified: Gusev Dmitrii, 11.10.2022
Modified: Gusev Dmitrii, 27.11.2022
"""

# todo: implement: add line mode (if not found needed line)

import sys
import fileinput
import argparse
from pyutilities.logging import init_logging

# - string checking types
CHECK_TYPE_STARTS = "starts"
Expand All @@ -21,7 +22,7 @@
CHECK_TYPES = (CHECK_TYPE_STARTS, CHECK_TYPE_ENDS, CHECK_TYPE_CONTAINS)


def check_str(check_type, source_str, test_str):
def check_str(args, check_type, source_str, test_str):
"""
Check relation between string and test string, according to test type
:param check_type: type of matching
Expand All @@ -37,43 +38,47 @@ def check_str(check_type, source_str, test_str):
return test_str in source_str


# create arguments parser
parser = argparse.ArgumentParser(description="File editing tool: replace inline values.")
def fedit():
# create arguments parser
parser = argparse.ArgumentParser(description="File editing tool: replace inline values.")

# add mandatory arguments to parser
parser.add_argument(
"-f", "--file", dest="infile", action="store", required=True, help="file to change inline"
)
parser.add_argument(
"-s", "--sourceStr", dest="sourceStr", action="store", required=True, help="source string for change"
)
parser.add_argument(
"-d", "--destStr", dest="destStr", action="store", required=True, help="target string for change"
)
# add mandatory arguments to parser
parser.add_argument(
"-f", "--file", dest="infile", action="store", required=True, help="file to change inline"
)
parser.add_argument(
"-s", "--sourceStr", dest="sourceStr", action="store", required=True, help="source string for change"
)
parser.add_argument(
"-d", "--destStr", dest="destStr", action="store", required=True, help="target string for change"
)

# add optional arguments to parser
parser.add_argument(
"-t",
"--type",
dest="edit_type",
action="store",
choices=CHECK_TYPES,
default=CHECK_TYPE_STARTS,
help="type of inline edit",
)
# add optional arguments to parser
parser.add_argument(
"-t",
"--type",
dest="edit_type",
action="store",
choices=CHECK_TYPES,
default=CHECK_TYPE_STARTS,
help="type of inline edit",
)

# parse cmd line parameters
args = parser.parse_args()
# parse cmd line parameters
args = parser.parse_args()

# inplace file processing
# todo: refactor edit logic into function
# todo: add some checks - file existence, etc
for line in fileinput.input(files=[args.infile], inplace=True, backup=".original"):
# if we found string - we will replace it
if check_str(args.edit_type, line, args.sourceStr):
sys.stderr.write("Found: {}\n".format(args.sourceStr))
print(args.destStr)
else:
print(
line,
)
# inplace file processing
# todo: refactor edit logic into function
# todo: add some checks - file existence, etc
for line in fileinput.input(files=[args.infile], inplace=True, backup=".original"):
# if we found string - we will replace it
if check_str(args, args.edit_type, line, args.sourceStr):
sys.stderr.write("Found: {}\n".format(args.sourceStr))
print(args.destStr)
else:
print(line)


if __name__ == "__main__":
init_logging()
fedit()
Loading

0 comments on commit b6098aa

Please sign in to comment.