From d50b8bd95b1feef96e375e800564e80dcc1c6e7b Mon Sep 17 00:00:00 2001 From: bom Date: Thu, 9 Feb 2023 12:38:04 +0100 Subject: [PATCH] Move parse and write code to file handler --- devops_test.py | 71 ++---------------------------------------- file_handlers.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 68 deletions(-) create mode 100644 file_handlers.py diff --git a/devops_test.py b/devops_test.py index 8adcb5f..1a2806b 100644 --- a/devops_test.py +++ b/devops_test.py @@ -1,6 +1,5 @@ -import json from enum import Enum -import re +from file_handlers import FileHandler def init_project(): # validate_values() @@ -28,47 +27,8 @@ class Version(): @classmethod def from_file(cls, config_file_path): - ret_cls = cls(None, False) - ret_cls.config_file_path = config_file_path - ret_cls.config_file_type = config_file_path.split('.')[-1] - ret_cls.parse() - return ret_cls - - def parse(self): - match self.config_file_type: - case 'json': - self.__parse_json() - case 'gradle': - self.__parse_gradle() - case _: - raise Exception(f'The file type "{self.config_file_type}" is not implemented') - - def __parse_json(self): - with open(self.config_file_path, 'r') as json_file: - json_version = json.load(json_file)['version'] - if '-SNAPSHOT' in json_version: - self.is_snapshot = True - json_version = json_version.replace('-SNAPSHOT', '') - self.version = [int(x) for x in json_version.split('.')] - - def __parse_gradle(self): - with open(self.config_file_path, 'r') as gradle_file: - contents = gradle_file.read() - version_line = re.search("\nversion = .*\n", contents) - if version_line is None: - raise Exception("Version not found in gradle file") - version_line = version_line.group() - version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line) - if version_string is None: - raise Exception("Version not found in gradle file") - - version_string = version_string.group() - if '-SNAPSHOT' in version_string: - self.is_snapshot = True - version_string = version_string.replace('-SNAPSHOT', '') - - self.version = [int(x) for x in version_string.split('.')] - + file_handler = FileHandler.from_file_path(config_file_path) + return cls(file_handler.parse()) def increment(self, level: ReleaseLevel): self.is_snapshot = False @@ -85,31 +45,6 @@ class Version(): self.version[ReleaseLevel.MINOR.value] = 0 self.version[ReleaseLevel.MAJOR.value] += 1 - def write(self): - match self.config_file_type: - case 'json': - self.__write_json() - case 'gradle': - self.__write_gradle() - case _: - raise Exception(f'The file type "{self.config_file_type}" is not implemented') - - def __write_json(self): - with open(self.config_file_path, 'r+') as json_file: - json_data = json.load(json_file) - json_data['version'] = self.get() - json_file.seek(0) - json.dump(json_data, json_file, indent=4) - json_file.truncate() - - def __write_gradle(self): - with open(self.config_file_path, 'r+') as gradle_file: - gradle_contents = gradle_file.read() - version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{self.get()}"\n', gradle_contents) - gradle_file.seek(0) - gradle_file.write(version_substitute) - gradle_file.truncate() - def get(self) -> str: version_string = ".".join([str(x) for x in self.version]) if self.is_snapshot: diff --git a/file_handlers.py b/file_handlers.py new file mode 100644 index 0000000..c7fec26 --- /dev/null +++ b/file_handlers.py @@ -0,0 +1,81 @@ +from abc import ABC, abstractmethod +import json +import re + +class FileHandler(ABC): + + @classmethod + def from_file_path(cls, file_path): + config_file_type = file_path.split('.')[-1] + match config_file_type: + case 'json': + file_handler = JsonFileHandler() + case 'gradle': + file_handler = GradleFileHandler() + case _: + raise Exception(f'The file type "{config_file_type}" is not implemented') + + file_handler.config_file_path = file_path + file_handler.config_file_type = config_file_type + return file_handler + + @abstractmethod + def parse(self) -> tuple[list[int], bool]: + pass + + @abstractmethod + def write(self): + pass + +class JsonFileHandler(FileHandler): + + def parse(self) -> tuple[list[int], bool]: + with open(self.config_file_path, 'r') as json_file: + json_version = json.load(json_file)['version'] + is_snapshot = False + if '-SNAPSHOT' in json_version: + is_snapshot = True + json_version = json_version.replace('-SNAPSHOT', '') + version = [int(x) for x in json_version.split('.')] + return version, is_snapshot + + def write(self): + with open(self.config_file_path, 'r+') as json_file: + json_data = json.load(json_file) + json_data['version'] = self.get() + json_file.seek(0) + json.dump(json_data, json_file, indent=4) + json_file.truncate() + + +class GradleFileHandler(FileHandler): + + def parse(self) -> tuple[list[int], bool]: + with open(self.config_file_path, 'r') as gradle_file: + contents = gradle_file.read() + version_line = re.search("\nversion = .*\n", contents) + if version_line is None: + raise Exception("Version not found in gradle file") + + version_line = version_line.group() + version_string = re.search('[0-9]*\\.[0-9]*\\.[0-9]*(-SNAPSHOT)?', version_line) + if version_string is None: + raise Exception("Version not found in gradle file") + + version_string = version_string.group() + if '-SNAPSHOT' in version_string: + self.is_snapshot = True + version_string = version_string.replace('-SNAPSHOT', '') + + self.version = [int(x) for x in version_string.split('.')] + + def write(self): + with open(self.config_file_path, 'r+') as gradle_file: + gradle_contents = gradle_file.read() + version_substitute = re.sub("\nversion = .*\n", f'\nversion = "{self.get()}"\n', gradle_contents) + gradle_file.seek(0) + gradle_file.write(version_substitute) + gradle_file.truncate() + +a = FileHandler.from_file_path('build.gradle') +print(type(a)) \ No newline at end of file