i guess it works
This commit is contained in:
@@ -1,54 +1,50 @@
|
||||
import json
|
||||
import os
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
# TODO:
|
||||
# - argparser with input file as required -> default output without
|
||||
# args is version
|
||||
# - `--patch`,'--minor' and '--major' raise the version numbers by
|
||||
# one respectively
|
||||
# - add a check for the input path if a file with the name
|
||||
# 'version.json' exists, if not it asks for user input and creates
|
||||
# one. if a file exists it opens the file and continues with either
|
||||
# the given flag or returning the version read from the file.
|
||||
# - create functions to raise versions
|
||||
# - add an path as input for the create_version_file function
|
||||
|
||||
# Path to the file containing the version which to edit.
|
||||
FILE_PATH = os.path.expanduser("~/Documents/git/python/pyvers/src/version.json")
|
||||
# ======================================================================
|
||||
# Constantes
|
||||
FILE_NAME = "version.json"
|
||||
# ======================================================================
|
||||
|
||||
# ======================================================================
|
||||
# Parser
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="pyvers",
|
||||
description="xD",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"config",
|
||||
help="The location of the config file to edit.",
|
||||
type=str,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--patch",
|
||||
help="Bumps the patch version by one.",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--minor",
|
||||
help="Bumps the minor version by one.",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--major",
|
||||
help="Bumps the major version by one.",
|
||||
action="store_true",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
# ======================================================================
|
||||
|
||||
|
||||
def check_for_file(input_path: str):
|
||||
"""Checks if the file 'version.json' exists at the given path.
|
||||
|
||||
Args:
|
||||
input_path ( str ): Path to check for the file.
|
||||
|
||||
Example:
|
||||
>>>check_for_file(".")
|
||||
'False'
|
||||
>>>check_for_file("version.json")
|
||||
'True'
|
||||
>>>check_for_file("./version.json")
|
||||
'True'
|
||||
"""
|
||||
if FILE_NAME in input_path:
|
||||
file_path = os.path.expanduser(input_path)
|
||||
if os.path.exists(file_path):
|
||||
print("True1")
|
||||
return True
|
||||
else:
|
||||
print("False")
|
||||
return False
|
||||
elif os.path.exists(os.path.join(input_path, FILE_NAME)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# ======================================================================
|
||||
# Functions
|
||||
def create_version_file(file_path) -> None:
|
||||
"""Creates a version.json file with all versions set to 0.
|
||||
"""Creates a version.json file with all versions set to 0 at the
|
||||
input location.
|
||||
|
||||
Args:
|
||||
file_path: (str): Path with file name.
|
||||
@@ -74,64 +70,133 @@ def create_version_file(file_path) -> None:
|
||||
|
||||
def load_version(input_path) -> dict[str, int]:
|
||||
"""Opens the given file and returns its contents."""
|
||||
# Open given path and loading the files contents into a variable
|
||||
file_path = open(input_path)
|
||||
prog_version = json.load(file_path)
|
||||
return prog_version
|
||||
|
||||
|
||||
# print(load_version())
|
||||
def bump_patch(version_file) -> None:
|
||||
"""Bumps the patch number of the given file by one."""
|
||||
# Get the version as dictionary
|
||||
version = load_version(version_file)
|
||||
|
||||
# Adding one to the version from the file and updating
|
||||
# the dictionary
|
||||
new_version = version["patch"] + 1
|
||||
version.update({"patch": new_version})
|
||||
|
||||
# Opening the file and overwriting its contents.
|
||||
with open(version_file, "w") as f:
|
||||
f.write(json.dumps(version, indent=4))
|
||||
|
||||
|
||||
# def update_patch() -> None:
|
||||
# version = load_version(args.config)
|
||||
#
|
||||
# new_version = version["patch"] + 1
|
||||
#
|
||||
# version.update({"patch": new_version})
|
||||
# print(version)
|
||||
# with open(FILE_PATH, "w") as f:
|
||||
# f.write(json.dumps(version, indent=4))
|
||||
#
|
||||
#
|
||||
# def update_minor() -> None:
|
||||
# version = load_version()
|
||||
#
|
||||
# new_version = version["minor"] + 1
|
||||
#
|
||||
# version.update({"minor": new_version})
|
||||
# print(version)
|
||||
# with open(FILE_PATH, "w") as f:
|
||||
# f.write(json.dumps(version, indent=4))
|
||||
def bump_minor(version_file) -> None:
|
||||
"""Bumps the minor version number of the given file by one."""
|
||||
# Get the version as dictionary
|
||||
version = load_version(version_file)
|
||||
|
||||
# Adding one to the version from the file and updating
|
||||
# the dictionary
|
||||
new_version = version["minor"] + 1
|
||||
version.update({"minor": new_version})
|
||||
|
||||
# Opening the file and overwriting its contents.
|
||||
with open(version_file, "w") as f:
|
||||
f.write(json.dumps(version, indent=4))
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="pyvers",
|
||||
description="xD",
|
||||
)
|
||||
def bump_major(version_file) -> None:
|
||||
"""Bumps the major version number of the given file by one."""
|
||||
# Get the version as dictionary
|
||||
version = load_version(version_file)
|
||||
|
||||
parser.add_argument(
|
||||
"config",
|
||||
help="The config file which to edit.",
|
||||
type=str,
|
||||
)
|
||||
# Adding one to the version from the file and updating
|
||||
# the dictionary
|
||||
new_version = version["major"] + 1
|
||||
version.update({"major": new_version})
|
||||
|
||||
args = parser.parse_args()
|
||||
# Opening the file and overwriting its contents.
|
||||
with open(version_file, "w") as f:
|
||||
f.write(json.dumps(version, indent=4))
|
||||
|
||||
|
||||
# Dispatching
|
||||
def version_bumper(version_file, major_version, minor_version, patch) -> None:
|
||||
"""Decides what version to bump, based on the users passed flags"""
|
||||
if patch:
|
||||
bump_patch(version_file)
|
||||
if minor_version:
|
||||
bump_minor(version_file)
|
||||
if major_version:
|
||||
bump_major(version_file)
|
||||
|
||||
|
||||
def check_for_file(input_path: str) -> bool:
|
||||
"""Checks if the file 'version.json' exists at the given path.
|
||||
|
||||
Args:
|
||||
input_path ( str ): Path to check for the file.
|
||||
|
||||
Example:
|
||||
>>>check_for_file(".")
|
||||
'False'
|
||||
>>>check_for_file("version.json")
|
||||
'True'
|
||||
>>>check_for_file("./version.json")
|
||||
'True'
|
||||
"""
|
||||
if FILE_NAME in input_path:
|
||||
file_path = os.path.expanduser(input_path)
|
||||
if os.path.exists(file_path):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
elif os.path.exists(os.path.join(input_path, FILE_NAME)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def request_to_create():
|
||||
"""Prompts the user if he wants to create the file and exits if the
|
||||
user declines or presses any other character instead of 'y/Y'"""
|
||||
|
||||
# print(args.config)
|
||||
print(check_for_file(args.config))
|
||||
if check_for_file(args.config) is False:
|
||||
PROMPT = "The file does not exist, do you want to create it? [y/n]"
|
||||
PROMPT += "\n>>>"
|
||||
|
||||
response = input(PROMPT)
|
||||
if "y" in response or "Y" in response:
|
||||
file_path = os.path.join(os.path.expanduser(args.config), FILE_NAME)
|
||||
file_path = os.path.join(
|
||||
os.path.expanduser(args.config),
|
||||
FILE_NAME,
|
||||
)
|
||||
create_version_file(file_path)
|
||||
return
|
||||
else:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
elif check_for_file(args.config):
|
||||
file_path = os.path.join(args.config, FILE_NAME)
|
||||
|
||||
# ======================================================================
|
||||
# Dispatching
|
||||
if check_for_file(args.config):
|
||||
file_path = os.path.join(os.path.expanduser(args.config), FILE_NAME)
|
||||
if args.patch or args.minor or args.major:
|
||||
version_bumper(
|
||||
version_file=file_path,
|
||||
major_version=args.major,
|
||||
minor_version=args.minor,
|
||||
patch=args.patch,
|
||||
)
|
||||
else:
|
||||
print(load_version(file_path))
|
||||
|
||||
if check_for_file(args.config) is False:
|
||||
request_to_create()
|
||||
file_path = os.path.join(os.path.expanduser(args.config), FILE_NAME)
|
||||
version_bumper(
|
||||
version_file=file_path,
|
||||
major_version=args.major,
|
||||
minor_version=args.minor,
|
||||
patch=args.patch,
|
||||
)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"minor": 6,
|
||||
"major": 0,
|
||||
"patch": 7
|
||||
}
|
||||
Reference in New Issue
Block a user