From 9c3a3fb6ec525cb9a4777d65cb6c1a694c5fcf43 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 26 Jul 2020 08:15:21 +0100 Subject: [PATCH] Allow user to override output file path --- MikesServoMapper.py | 25 +++++++++++++++++-------- main.py | 10 +++++++++- output/.gitignore | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/MikesServoMapper.py b/MikesServoMapper.py index e8de71b..4cbedd5 100644 --- a/MikesServoMapper.py +++ b/MikesServoMapper.py @@ -25,14 +25,17 @@ class MikesServoMapper: __DEFAULT_OUTPUT_FILE_NAME = "servo-mappings.yml" - def __init__(self, config_file: str, names): + def __init__(self, config_file: str = None, names=None, output_file: str = None): # noinspection PyTypeChecker self.__logger: logging.Logger = None self.__logger_formatter = None self.init_logging() - self.__names = list(names) + if names is None: + self.__names = list() + else: + self.__names = list(names) self.__names.sort() self.__config = None @@ -40,6 +43,10 @@ class MikesServoMapper: self.pull_config_names() self.__logger.info("Names: %s" % (pprint.pformat(self.__names))) + self.__output_file_path = output_file + if self.__output_file_path is None: + self.__output_file_path = self.make_default_mappings_output_file_path() + self.__mappings = {} def init_logging(self): @@ -112,7 +119,7 @@ class MikesServoMapper: self.__logger.info("") self.__logger.info("Please choose a mode: ") - self.__logger.info("1. Create mappings") + self.__logger.info("1. Edit mappings") self.__logger.info("2. Test current mappings") self.__logger.info("3. Write mappings to file") self.__logger.info("4. Load previously saved mappings") @@ -124,7 +131,7 @@ class MikesServoMapper: break if user_choice == "1": - self.do_mappings() + self.edit_mappings() elif user_choice == "2": self.test_mappings() elif user_choice == "3": @@ -134,7 +141,7 @@ class MikesServoMapper: else: self.__logger.warning("Invalid choice: %s" % user_choice) - def do_mappings(self): + def edit_mappings(self): self.__logger.info("Begin mapping mode !") @@ -250,7 +257,7 @@ class MikesServoMapper: # Center servo.angle = 90 - def make_mappings_output_file_path(self): + def make_default_mappings_output_file_path(self): output_file_path = os.path.join( "output", @@ -261,7 +268,9 @@ class MikesServoMapper: def write_mappings(self): - output_file_path = self.make_mappings_output_file_path() + output_file_path = self.__output_file_path + + self.__logger.info("Writing mappings to output file: %s" % (output_file_path,)) data = { "servo-mappings": self.__mappings @@ -273,7 +282,7 @@ class MikesServoMapper: def load_mappings(self, file_path=None): if file_path is None: - file_path = self.make_mappings_output_file_path() + file_path = self.__output_file_path self.__logger.info("Attempting to load mappings from: %s" % (file_path,)) diff --git a/main.py b/main.py index e8f8e2b..0e40be5 100644 --- a/main.py +++ b/main.py @@ -26,12 +26,20 @@ def main(): action="append", dest="names" ) + parser.add_argument( + "--output", "--output-path", + help="Override the default mapping output path", + required=False, + default=None, + dest="output_file" + ) args = parser.parse_args() mapper = MikesServoMapper( config_file=args.config_file, - names=args.names + names=args.names, + output_file=args.output_file ) mapper.run() diff --git a/output/.gitignore b/output/.gitignore index 21d3095..d4800b2 100644 --- a/output/.gitignore +++ b/output/.gitignore @@ -1,6 +1,6 @@ # -*.yml +*.*