def __create_default_config(exists_ok=False): if os.path.exists(const.PATH_FILE_CONFIG) and not exists_ok: try: with open(const.PATH_FILE_CONFIG, "r", encoding="utf-8") as file: config = json.load(file) for key in const.CONFIG_DEFAULT.keys(): if key not in config: config[key] = const.CONFIG_DEFAULT[key] with open(const.PATH_FILE_CONFIG, "w", encoding="utf-8") as file: json.dump(config, file, ensure_ascii=False, indent=const.JSON_INDENT) except (EnvironmentError, json.decoder.JSONDecodeError) as err: __logger.error(log.get_err_tb(err)) return err else: config = const.CONFIG_DEFAULT.copy() try: with open(const.PATH_FILE_CONFIG, "w", encoding="utf-8") as file: json.dump(config, file, ensure_ascii=False, indent=const.JSON_INDENT) except (EnvironmentError, TypeError) as err: __logger.error(log.get_err_tb(err)) return str(err)
def get_config(): """ Is not entry point of particular task. Serves to get configuration data for the running application. :return: configuration dict. """ try: with open(const.PATH_FILE_CONFIG, "r", encoding="utf-8") as file: config = json.load(file) return config except (EnvironmentError, json.decoder.JSONDecodeError) as err: # if corrupted => rewrite with defaults # util.print_err_msg(log.get_err_tb(err)) config = const.CONFIG_DEFAULT.copy() try: with open(const.PATH_FILE_CONFIG, "w", encoding="utf-8") as file: json.dump(config, file, ensure_ascii=False, indent=const.JSON_INDENT) except (EnvironmentError, TypeError) as err: util.print_err_msg(log.get_err_tb(err)) return return config
def make_dirs(dir_paths): """ Creates specified directories in file system. :param dir_paths: list of directories' paths to create. :return: None. """ for dir_path in dir_paths[:]: try: os.makedirs(dir_path, exist_ok=True) if os.name == "posix": os.chown(dir_path, os.getuid(), gid=-1) if not os.access(dir_path, os.R_OK | os.W_OK): raise PermissionError("no permissions to dir: %s" % dir_path) except PermissionError as err: __logger.error(log.get_err_tb(err)) continue except (ValueError, TypeError, EnvironmentError) as err: __logger.error(log.get_err_tb(err)) continue dir_paths.remove(dir_path)
def show(argv): """ Entry point of `pytux log show` task. Prints contents of log file. :param argv: unused. :return: None on success, str with error description on error. """ try: with open(const.PATH_FILE_LOG, "r") as file: print(file.read()) except EnvironmentError as err: return log.get_err_tb(err) return None
def clear(argv): """ Entry point of `pytux log clear` task. Prints contents of log file. :param argv: unused. :return: None on success, str with error description on error. """ try: with open(const.PATH_FILE_LOG, "w"): pass except EnvironmentError as err: return log.get_err_tb(err) return None
def __set_configs_attribute(key, value): try: with open(const.PATH_FILE_CONFIG, "r", encoding="utf-8") as file: config = json.load(file) config[key] = value with open(const.PATH_FILE_CONFIG, "w", encoding="utf-8") as file: json.dump(config, file, ensure_ascii=False, indent=const.JSON_INDENT) except (EnvironmentError, json.decoder.JSONDecodeError, KeyError, TypeError) as err: __logger.error(log.get_err_tb(err)) return str(err)
def main(argv): """ Entry point of `pytux log` command. :param argv: command line arguments passed to tasks. :return: 0 on success, -1 on error. """ try: msg = __tasks_log[argv.task](argv) if msg is not None: util.print_err_msg(msg) return -1 except Exception as err: util.print_err_msg(log.get_err_tb(err)) return -1 return 0
def show(argv): """ Entry point of `pytux config show` task. Prints contents of pytux config file. :param argv: unused. :return: None on success, str with error description on error. """ err = __create_default_config() if err: return err try: with open(const.PATH_FILE_CONFIG, "r", encoding="utf-8") as file: print(file.read()) except EnvironmentError as err: __logger.error(log.get_err_tb(err)) return str(err)
def main(): """ Entry point of `pytux` program. :return: return code of specified command. """ if os.name not in const.PATH_DIRS_LOG.keys(): util.print_err_msg("unable to run on %s platform" % os.name, False) return -1 working_dirs = [const.PATH_DIR_HOME, const.PATH_DIRS_LOG[os.name]] util.make_dirs(working_dirs) if len(working_dirs) != 0: util.print_err_msg("unable to make working dirs", False) return -1 try: const.LOG_FILE = open(const.PATH_FILE_LOG, "a") except EnvironmentError as err: util.print_err_msg(log.get_err_tb(err)) return -1 argv = args.parse_args() argv.config = get_config() if argv.config is None: util.print_err_msg("unable to get config", False) return -1 if argv.command == "log": const.LOG_FILE.close() const.LOG_FILE = sys.stdout if argv.command != "log": log.setup_logging(argv.config[const.CONFIG_KEY_LOG_LEVEL]) global __logger __logger = log.get_logger(__name__) __logger.info("starting...") __logger.info("executing <%s> command" % argv.command) code = __commands[argv.command](argv) __logger.info("exiting...") log.stop_logging() const.LOG_FILE.close()
def main(argv): """ Entry point of `pytux build` command. :param argv: command line arguments passed to tasks. :return: 0 on success, -1 on error. """ set_config(argv.config) if argv.file is not None: source_file_name = argv.file.name result_file_name = path.splitext(source_file_name)[0] + ".rpy" try: result = parse(argv.file) with open(result_file_name, "w") as result_file: result_file.write(result) print( f"Pytux successfully translated {source_file_name} to {result_file_name}" ) except Exception as err: __logger.error(log.get_err_tb(err)) util.print_err_msg(err) return -1 return 0