import json import os import argparse # 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") FILE_NAME = "version.json" 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 def create_version_file(file_path) -> None: """Creates a version.json file with all versions set to 0. Args: file_path: (str): Path with file name. Example: >>>create_version_file(/path/to/file) file: { "minor": 0, "major": 0, "patch": 0 }""" # Defining the dictionary with the initial version numbers. initial_version = {"minor": 0, "major": 0, "patch": 0} # Opening/creating the file to write the dictionary as json to it. file = open(file_path, "x") file.write(json.dumps(initial_version, indent=4)) print("File written successfully.") return def load_version(input_path) -> dict[str, int]: """Opens the given file and returns its contents.""" file_path = open(input_path) prog_version = json.load(file_path) return prog_version # print(load_version()) # 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)) parser = argparse.ArgumentParser( prog="pyvers", description="xD", ) parser.add_argument( "config", help="The config file which to edit.", type=str, ) args = parser.parse_args() # Dispatching # 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) create_version_file(file_path) else: parser.print_help() elif check_for_file(args.config): file_path = os.path.join(args.config, FILE_NAME) print(load_version(file_path))