def read_file(file_path): Logger.d("Reading file: {0}".format(file_path)) with open(file_path, "r") as f: content = f.read() f.close() return content
def run_all_generators(options, model): package = pyginny.generators prefix = package.__name__ + "." for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix): Logger.d("Found submodule {0} (is a package: {1})".format(modname, ispkg)) module = __import__(modname + ".generator", fromlist=["dummy"]) Logger.d("Imported {0}".format(module)) obj = module.Generator(options=options, model=model) obj.run()
def run(self): if not self.can_run(): Logger.i("Generator '{0}' skipped".format(self.get_name())) return # create the output folder output_path = self.get_output_path() FileUtil.prepare_output_path(output_path) # process all records found self.process_record_list() # finished Logger.s("Generator '{0}' finished".format(self.get_name()))
def write_to_file(dir_path, filename, content): Logger.d("Creating file {0} in directory {1} with {2} bytes...".format( filename, dir_path, len(content))) full_file_path = os.path.join(dir_path, filename) FileUtil.remove_file(full_file_path) FileUtil.create_dir(dir_path) with open(full_file_path, "w") as f: f.write(content) f.close() Logger.d("Created file {0} in directory {1}".format( filename, dir_path))
def get_all_generators_options(): package = pyginny.generators prefix = package.__name__ + "." result = "" for importer, modname, ispkg in pkgutil.iter_modules(package.__path__, prefix): Logger.d("Found submodule {0} (is a package: {1})".format(modname, ispkg)) module = __import__(modname + ".generator", fromlist=["dummy"]) Logger.d("Imported {0}".format(module)) obj = module.Generator(options=[], model=None) result = result + obj.get_usage_options() return result
def run(self): meta_model = get_meta_model() model = meta_model.model_from_file(os.path.expanduser(self.options["<file>"])) GeneratorUtil.run_all_generators(options=self.options, model=model) Logger.done()
def remove_file(filename): Logger.d("Remove file: {0}".format(filename)) if os.path.isfile(filename): os.remove(filename)
def remove_dir(dir_path): Logger.d("Remove dir: {0}".format(dir_path)) if os.path.isdir(dir_path): shutil.rmtree(dir_path)
def test_clean(self): Logger.clean("test_clean") self.assertTrue(True)
def test_colored(self): Logger.colored("test_colored", Fore.GREEN) self.assertTrue(True)
def test_fatal(self): with pytest.raises(SystemExit) as error: Logger.f("test_fatal") self.assertEqual(error.type, SystemExit) self.assertEqual(error.value.code, 1)
def test_error_without_exit(self): Logger.e("test_error", False) self.assertTrue(True)
def test_information(self): Logger.i("test_information") self.assertTrue(True)
def test_warning(self): Logger.w("test_warning") self.assertTrue(True)
def test_debug_with_debug_turned_off(self): Constants.DEBUG = False Logger.d("test_debug_with_debug_turned_off") self.assertTrue(True)
def test_debug_with_debug_turned_on(self): Constants.DEBUG = True Logger.d("test_debug_with_debug_turned_on") self.assertTrue(True)
def main(): """Main CLI entrypoint.""" colorama_init() generators_options = GeneratorUtil.get_all_generators_options() commands_doc = __doc__ commands_doc = commands_doc.format(generators_options) options = docopt(commands_doc, version=__version__) # show all params for debug if ("--debug" in options and options["--debug"]) or ("-d" in options and options["-d"]): Constants.DEBUG = True Logger.clean("System information: ") Logger.clean("> PROCESS ID : {0}".format(os.getpid())) Logger.clean("> PYTHON : {0}".format(platform.python_version())) Logger.clean("") Logger.clean("You supplied the following options: ") Logger.clean("{0}".format(dumps(options, indent=2, sort_keys=False))) Logger.clean("") # dynamically match the command that user is trying to run for (option_key, option_value) in options.items(): if hasattr(pyginny.commands, option_key) and option_value: command_module = getattr(pyginny.commands, option_key) commands = getmembers(command_module, isclass) found_command = None for command in commands: if command[0] != "Base" and command[0].lower() == option_key: found_command = command[1](options) break if found_command: found_command.run()
def create_dir(dir_path): Logger.d("Create a new dir: {0}".format(dir_path)) if not os.path.isdir(dir_path): os.makedirs(dir_path)