Skip to content

Commit

Permalink
Added pre-commit, reformatted all code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Knucklessg1 committed May 9, 2024
1 parent 229bd91 commit 578614d
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 68 deletions.
69 changes: 69 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
default_language_version:
python: python3
exclude: 'dotnet'
ci:
autofix_prs: true
autoupdate_commit_msg: '[pre-commit.ci] pre-commit suggestions'
autoupdate_schedule: 'monthly'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-yaml
- id: check-toml
- id: check-json
- id: check-byte-order-marker
exclude: .gitignore
- id: check-merge-conflict
- id: detect-private-key
- id: trailing-whitespace
- id: end-of-file-fixer
- id: no-commit-to-branch
- repo: https://github.com/psf/black
rev: 24.3.0
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: ["--fix", "--ignore=E402"]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
args: ["-L", "ans,linar,nam,tread,ot,"]
exclude: |
(?x)^(
pyproject.toml |
website/static/img/ag.svg |
website/yarn.lock |
website/docs/tutorial/code-executors.ipynb |
website/docs/topics/code-execution/custom-executor.ipynb |
website/docs/topics/non-openai-models/cloud-gemini.ipynb |
notebook/.*
)$
# See https://jaredkhan.com/blog/mypy-pre-commit
- repo: local
hooks:
- id: mypy
name: mypy
entry: "./scripts/pre-commit-mypy-run.sh"
language: python
# use your preferred Python version
# language_version: python3.8
additional_dependencies: []
types: [python]
# use require_serial so that script
# is only called once per commit
require_serial: true
# Print the number of files as a sanity-check
verbose: true
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-black
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions autoversioner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# coding: utf-8
from autoversioner.version import __version__, __author__, __credits__
from autoversioner.autoversioner import autoversioner, main

"""
autoversioner
Expand All @@ -11,3 +12,5 @@
__version__ = __version__
__author__ = __author__
__credits__ = __credits__

__all__ = ["autoversioner", "main"]
105 changes: 62 additions & 43 deletions autoversioner/autoversioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,33 @@


def usage():
print(f"Usage: \n"
f"-h | --help [ See usage for script ]\n"
f"-e | --env [ Export version metadata to environment file (/directory/.env) ]\n"
f"-j | --json [ Export version metadata to JSON file (/directory/version.json) ]\n"
f"-d | --directory [ Directory to save .env and JSON files ]\n"
f"--major [ Increment major version ]\n"
f"--minor [ Increment minor version ]\n"
f"--patch [ Patch version ]\n"
f"\n"
f"autoversioner -v 'v2024.1.4'\n"
f"autoversioner -v '2024.1.4'\n"
f"autoversioner -v '1.10.4' --json --directory '~/Downloads' --env\n")
print(
"Usage: \n"
"-h | --help [ See usage for script ]\n"
"-e | --env [ Export version metadata to environment file (/directory/.env) ]\n"
"-j | --json [ Export version metadata to JSON file (/directory/version.json) ]\n"
"-d | --directory [ Directory to save .env and JSON files ]\n"
"--major [ Increment major version ]\n"
"--minor [ Increment minor version ]\n"
"--patch [ Patch version ]\n"
"\n"
"autoversioner -v 'v2024.1.4'\n"
"autoversioner -v '2024.1.4'\n"
"autoversioner -v '1.10.4' --json --directory '~/Downloads' --env\n"
)


def version(current_version="", major=False, minor=False, patch=False):
long_date_pattern = re.compile(r'20[1-2][0-9]')
short_date_pattern = re.compile(r'^[1-2][0-9]')
long_date_pattern = re.compile(r"20[1-2][0-9]")
short_date_pattern = re.compile(r"^[1-2][0-9]")
today = datetime.date.today()
long_year = today.strftime("%Y")
short_year = today.strftime("%y")
month = int(today.strftime("%m"))
long_date = f"{long_year}.{month}"
short_date = f"{short_year}.{month}"
if current_version == "":
new_version = f'{short_date}.0'
new_version = f"{short_date}.0"
return new_version
cleaned_current_version = current_version.split(".")
cleaned_current_version = cleaned_current_version[:3]
Expand Down Expand Up @@ -66,63 +68,79 @@ def version(current_version="", major=False, minor=False, patch=False):
return new_version
if long_date_pattern.search(current_version):
current_version = semantic_version.Version(current_version)
s = semantic_version.SimpleSpec(f'<{long_date}.0')
new_version = f'{long_date}.0'
s = semantic_version.SimpleSpec(f"<{long_date}.0")
new_version = f"{long_date}.0"
if s.match(current_version):
new_version = f'{long_date}.0'
s = semantic_version.SimpleSpec(f'=={long_date}.{current_version.patch}')
new_version = f"{long_date}.0"
s = semantic_version.SimpleSpec(f"=={long_date}.{current_version.patch}")
if s.match(current_version):
new_version = current_version.next_patch()
new_version = f'{str(new_version)}'
new_version = f"{str(new_version)}"
return new_version
elif short_date_pattern.search(current_version):
current_version = semantic_version.Version(current_version)
s = semantic_version.SimpleSpec(f'<{short_date}.0')
new_version = f'{short_date}.0'
s = semantic_version.SimpleSpec(f"<{short_date}.0")
new_version = f"{short_date}.0"
if s.match(current_version):
new_version = f'{short_date}.0'
s = semantic_version.SimpleSpec(f'=={short_date}.{current_version.patch}')
new_version = f"{short_date}.0"
s = semantic_version.SimpleSpec(f"=={short_date}.{current_version.patch}")
if s.match(current_version):
new_version = current_version.next_patch()
new_version = f'{str(new_version)}'
new_version = f"{str(new_version)}"
return new_version
else:
current_version = semantic_version.Version(current_version)
new_version = current_version.next_patch()
return new_version


def output(metadata=None, json_output=False, env_output=False, print_output=False, directory=""):
def output(
metadata=None, json_output=False, env_output=False, print_output=False, directory=""
):
if not metadata:
print("No metadata supplied to output")
return
if json_output:
json_file_path = os.path.join(directory, 'version.json')
json_file_path = os.path.join(directory, "version.json")
# Write the dictionary to the JSON file
with open(json_file_path, 'w') as json_file:
with open(json_file_path, "w") as json_file:
json.dump(metadata, json_file, indent=2)
if env_output:
env_metadata = {k.upper(): v for k, v in metadata.items()}
env_content = "\n".join([f"{key}={value}" for key, value in env_metadata.items()])
env_file_path = os.path.join(directory, '.env')
env_content = "\n".join(
[f"{key}={value}" for key, value in env_metadata.items()]
)
env_file_path = os.path.join(directory, ".env")
# Save the content to the .env file
with open(env_file_path, 'w') as env_file:
with open(env_file_path, "w") as env_file:
env_file.write(env_content)
if print_output:
print(f"{metadata['new_version']}")


def autoversioner(argv):
current_version = '1.0.0'
current_version = "1.0.0"
directory = ""
environment_output = False
json_output = False
major = False
minor = False
patch = False
try:
opts, args = getopt.getopt(argv, "hejd:v:", ["help", "env", "json", "directory=", "version=",
"major", "minor", "patch"])
opts, args = getopt.getopt(
argv,
"hejd:v:",
[
"help",
"env",
"json",
"directory=",
"version=",
"major",
"minor",
"patch",
],
)
except getopt.GetoptError:
usage()
sys.exit(2)
Expand All @@ -149,10 +167,9 @@ def autoversioner(argv):
current_version = ""
current_version = re.sub("v", "", current_version)

new_version = version(current_version=current_version,
major=major,
minor=minor,
patch=patch)
new_version = version(
current_version=current_version, major=major, minor=minor, patch=patch
)

version_metadata = {
"current_version": current_version,
Expand All @@ -163,11 +180,13 @@ def autoversioner(argv):
"tag": f"v{new_version}",
}

output(metadata=version_metadata,
json_output=json_output,
env_output=environment_output,
print_output=True,
directory=directory)
output(
metadata=version_metadata,
json_output=json_output,
env_output=environment_output,
print_output=True,
directory=directory,
)


def main():
Expand Down
6 changes: 3 additions & 3 deletions autoversioner/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# coding: utf-8

__version__ = '24.1.6'
__author__ = 'Audel Rouhi'
__credits__ = 'Audel Rouhi'
__version__ = "24.1.6"
__author__ = "Audel Rouhi"
__credits__ = "Audel Rouhi"
15 changes: 15 additions & 0 deletions scripts/pre-commit-mypy-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# taken from: https://jaredkhan.com/blog/mypy-pre-commit

# A script for running mypy,
# with all its dependencies installed.

set -o errexit

# Change directory to the project root directory.
cd "$(dirname "$0")"/..

pip install -q -e .[types]

mypy
46 changes: 25 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,42 @@
from pip._internal.req import parse_requirements


readme = Path('README.md').read_text()
readme = Path("README.md").read_text()
version = __version__
requirements = parse_requirements(os.path.join(os.path.dirname(__file__), 'requirements.txt'), session=PipSession())
requirements = parse_requirements(
os.path.join(os.path.dirname(__file__), "requirements.txt"), session=PipSession()
)
readme = re.sub(r"Version: [0-9]*\.[0-9]*\.[0-9][0-9]*", f"Version: {version}", readme)
with open("README.md", "w") as readme_file:
readme_file.write(readme)
description = 'Synchronize your subtitle files by shifting the subtitle time (+/-)'
description = "Synchronize your subtitle files by shifting the subtitle time (+/-)"

setup(
name='autoversioner',
name="autoversioner",
version=f"{version}",
description=description,
long_description=f'{readme}',
long_description_content_type='text/markdown',
url='https://github.com/Knuckles-Team/subsync',
long_description=f"{readme}",
long_description_content_type="text/markdown",
url="https://github.com/Knuckles-Team/subsync",
author=__author__,
author_email='knucklessg1@gmail.com',
license='MIT',
packages=['autoversioner'],
author_email="knucklessg1@gmail.com",
license="MIT",
packages=["autoversioner"],
include_package_data=True,
install_requires=[str(requirement.requirement) for requirement in requirements],
py_modules=['autoversioner'],
package_data={'autoversioner': ['autoversioner']},
py_modules=["autoversioner"],
package_data={"autoversioner": ["autoversioner"]},
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: Public Domain',
'Environment :: Console',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
"Development Status :: 5 - Production/Stable",
"License :: Public Domain",
"Environment :: Console",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
],
entry_points={'console_scripts': ['autoversioner = autoversioner.autoversioner:main']},
entry_points={
"console_scripts": ["autoversioner = autoversioner.autoversioner:main"]
},
)

0 comments on commit 578614d

Please sign in to comment.