diff --git a/refit/src/modules/librefit.py b/refit/src/modules/librefit.py index a410f24..c001c2c 100644 --- a/refit/src/modules/librefit.py +++ b/refit/src/modules/librefit.py @@ -1,5 +1,6 @@ import os import sys +import json from .refit_logger import logger @@ -247,3 +248,30 @@ def create_parallel_directories(input_path: str, target_depth: int, width: int, current_depth=0, name=name, ) + + +def get_version_from_file(input_path) -> str: + """Returns the version of the program. + + The function accepts an path to a version file as a string and it + returns it version number formatted. + + Arg: + input_path (str): The absolute or relative path to the + version file. + + Example: + >>>get_version_from_file("/path/to/file.json") + '0.2.1'""" + # Expands the input path + expanded_path = os.path.expanduser(input_path) + + # Opening the file and reading its contents, saving as as an dict. + file_path = open(expanded_path) + prog_version = json.load(file_path) + + # Formatting the output before returning the string again + pretty_version = ( + f"{prog_version['major']}.{prog_version['minor']}.{prog_version['patch']}" + ) + return pretty_version diff --git a/refit/src/refit.py b/refit/src/refit.py index 8c66246..e53f757 100644 --- a/refit/src/refit.py +++ b/refit/src/refit.py @@ -1,43 +1,14 @@ import argparse import sys -import os -import configparser from modules.refit_logger import logger from modules.refit_create import Refit_Create +from modules.librefit import get_version_from_file - -CONFIG_FILE = "version.cfg" - - -def read_version_config(): - # logger.debug(f"Start read_version_config() with config file: {CONFIG_FILE}") - config = configparser.ConfigParser() - config.read(CONFIG_FILE) - - if not os.path.exists(CONFIG_FILE): - # logger.error( - # f"FUNC: read_version_config() MSG: Could not find config file '{CONFIG_FILE}'" - # ) - return "x.x.x" - - if "VERSION" not in config: - # logger.error(f"Could not find VERSION-variable in config file '{CONFIG_FILE}'") - return "x.x.x" - - try: - v = config["VERSION"] - major = v.get("major", "0") - minor = v.get("minor", "0") - patch = v.get("patch", "0") - # logger.debug(f"Config file read successfully. Version: {major}.{minor}.{patch}") - return f"{major}.{minor}.{patch}" - except Exception: - # logger.warning("Couldn not read version from config file") - return "x.x.x" - - -REFIT_VERSION = f"Refit Beta {read_version_config()}" +# NOTE: The final version file needs a dedicated place to live in +# so the version number is always readable, independent from where it +# is executed +REFIT_VERSION = get_version_from_file("~/Documents/git/python/refit/src/version.json") # ---------------------------ARGPARSE START--------------------------- diff --git a/refit/src/version.json b/refit/src/version.json new file mode 100644 index 0000000..aaf74f2 --- /dev/null +++ b/refit/src/version.json @@ -0,0 +1,5 @@ +{ + "minor": 3, + "major": 0, + "patch": 8 +}