def diff(file_lhs, file_rhs): lhs_out = remove_space(open(file_lhs, encoding="utf-8").readlines()) rhs_out = remove_space(open(file_rhs, encoding="utf-8").readlines()) if operator.eq(lhs_out, rhs_out): print(Fore.GREEN + "same") else: print(Fore.RED + "different") length = min(len(lhs_out), len(rhs_out)) for k in range(length): if lhs_out[k] != rhs_out[k]: print(Fore.RED + "different line {}:{} != {}:{}".format( file_lhs, k + 1, file_rhs, k + 1)) print(Fore.RED + "\t{}".format(lhs_out[k])) print(Fore.RED + "\t{}".format(rhs_out[k]))
def remove_variable(self, variable_name) -> None: """ removes a variable :param variable_name: str name of the variable syntax: <variable name> example: "test_variable" :return: None :since: 0.1.0 """ try: from .utils import remove_space, replace_line except ImportError: from utils import remove_space, replace_line found = False if variable_name in self._user_variables: del self._user_variables[variable_name] found = True if self._aion_open_variable_file is not None: line_number = -1 for line in self._aion_open_variable_file: line_number = line_number + 1 if remove_space(line, "").split("=")[0] == variable_name: replace_line(_aion_variable_file, line_number, "") self._aion_open_variable_file = open( _aion_variable_file, "rw") return if found is False: raise KeyError("the variable " + variable_name + " doesn't exists")
def unit(dir_name, test_set): ncases = 0 npassed = 0 for test in get_test_names(test_set): ncases += 1 testfile_name = os.path.join(test_set, test, "testfile.txt") output_name = os.path.join(test_set, test, "output.txt") lhs_out = get_exe_output(dir_name, testfile_name, "error.txt") rhs_out = remove_space(open(output_name).readlines()) if lhs_out[0] == rhs_out[0]: is_correct = True for line in rhs_out: # print(line) if line not in lhs_out: is_correct = False if is_correct: # correct print(Fore.GREEN + "passed: {}".format(test)) continue # error print(Fore.LIGHTRED_EX + "failed: {}".format(test)) if lhs_out[0] != rhs_out[0]: print(Fore.LIGHTRED_EX + "first line error: expected '{}' while found '{}'".format(rhs_out[0], lhs_out[0])) continue for line in rhs_out: if line not in lhs_out: print(Fore.LIGHTRED_EX + "could not find '{}'".format(line))
def _main(self) -> None: """ checks which type of an 'aion' file is the given file :return: None :since: 0.1.0 """ try: from ._error_codes import init_no_file_type from .utils import remove_space except ImportError: from _error_codes import init_no_file_type from utils import remove_space for line in open(self.fname, "r"): if line.isspace(): continue elif line.startswith("#"): line = remove_space(line, "") if line.startswith("#type:"): line = line.replace("#type:", "") if line.strip() == "skill": self._skill() elif "plugin" in line.strip(): self._plugin() else: raise IndexError("Errno: " + init_no_file_type + " - Couldn't find file type in " + self.fname)
def run_unit(dir_name, test, test_set, option=None): try: testfile_name = os.path.join(test_set, test) output_name = os.path.join(test_set, test, "output.txt") # lhs_out = get_exe_output(dir_name, testfile_name, "error.txt") lhs_out, cycles = get_mips_output(dir_name, testfile_name) ref_cycles_path = os.path.join(test_set, test, "ref_cycles") lhs_out = remove_space(lhs_out) if not os.path.exists(output_name): with open(output_name, "w") as f: for line in lhs_out: f.write(line) f.write("\n") rhs_out = remove_space(open(output_name).readlines()) if operator.eq(lhs_out, rhs_out): if not os.path.exists(ref_cycles_path): os.system("echo {} > {}".format(cycles, ref_cycles_path)) ref_cycles = int(open(ref_cycles_path).readline()) score = ref_cycles / cycles global cycles_sum, score_sum cycles_sum += cycles score_sum += score * cycles print(Fore.GREEN + "passed: {}. Cycles: {}, Score: {:.2f}".format(test, cycles, score)) return True else: print(Fore.LIGHTRED_EX + "failed: {}".format(test)) length = min(len(lhs_out), len(rhs_out)) for i in range(length): if lhs_out[i] != rhs_out[i]: print(Fore.LIGHTRED_EX + "found {} while expected '{}' at line {}" .format("'{}'".format(lhs_out[i]), "'{}'".format(rhs_out[i]), i + 1)) return False if len(lhs_out) > len(rhs_out): print(Fore.LIGHTRED_EX + "found {} while expected {} at line {}" .format("'{}'".format(lhs_out[length]), "NOTHING", length + 1)) elif len(lhs_out) < len(rhs_out): print(Fore.LIGHTRED_EX + "found {} while expected {} at line {}" .format("NOTHING", "'{}'".format(rhs_out[length]), length + 1)) return False # exit(-1) except: print(Fore.LIGHTRED_EX + "failed: {}. unable to compile and run".format(test)) return False
def text2html(handler, text): '''Text to HTML.''' if not text: return '' text = utils.remove_space(text) html = '' paragraphs = text.split('\n') for paragraph in paragraphs: html += '<p>%s</p>\n' % paragraph return html
def pipeline(self, text): text = utils.remove_space(text) text = utils.remove_punct(text) text = utils.remove_contractions(text.lower(), contractions) text = utils.remove_url(text) text = utils.remove_html(text) text = utils.correct_spellings(text) return text
def render_string(self, template_name, **kwargs): '''Template render by Jinja2.''' default = { 'xsrf': self.xsrf_form_html, 'request': self.request, 'settings': self.settings, 'me': self.current_user, 'static': self.static_url, 'handler': self, } kwargs.update(default) kwargs.update(self.ui) # Enabled tornado UI methods. template = self.get_template(template_name) html = template.render(**kwargs) #Render template. html = utils.remove_space(html) #remove space in line head and end. return html
def set_value(self, variable_name: str, value: str) -> None: """ set a new value to a variable :param variable_name: str name of the variable you want to change the value syntax: "<variable name>" example: "test_variable" :param value: str new value syntax: "<new value>" example: "new_test_value" :return: None :since: 0.1.0 """ try: from ._error_codes import variable_set_value_variable_doesnt_exist from .utils import remove_space, replace_line except ImportError: from _error_codes import variable_set_value_variable_doesnt_exist from utils import remove_space, replace_line found = False if variable_name in self._user_variables: self._user_variables[variable_name] = value found = True if self._aion_open_variable_file is not None: line_number = -1 for line in self._aion_open_variable_file: line_number = line_number + 1 if remove_space(line, " ").split("=")[0] == variable_name: replace_line(_aion_variable_file, line_number, variable_name + "=" + value) self._aion_open_variable_file = open( _aion_variable_file, "w+") return if found is False: raise KeyError("Errno: " + variable_set_value_variable_doesnt_exist + " - The variable " + variable_name + " doesn't exists")
def cleanup(self): self.purpose = utils.remove_space(self.purpose) self.partner = utils.remove_space(self.partner)
def execute_aion_file_type_plugin(fname: str) -> None: """ installs custom plugin from '<file name>.aion' :param fname: str file name of the '.aion' file syntax: <file name> example: "/home/pi/plugin.aion" :return: None :since: 0.1.0 """ try: from ._errors import plugin_couldnt_find_key, plugin_couldnt_find_plugin_type, plugin_illegal_run_after_pseudonym, plugin_run_after_pseudonym_already_exist,\ plugin_illegal_run_before_pseudonym, plugin_run_before_pseudonym_already_exist, plugin_file_doesnt_exist_in_setup_dir, plugin_directory_doesnt_exist_in_setup_dir,\ plugin_run_after_additional_directories_already_exist, plugin_run_before_additional_directories_already_exist, plugin_python3_module_not_found from .acph import add_acph from .language import add_entry, language_directory from .utils import remove_space, BaseXMLReader, BaseXMLWriter except ImportError: from _error_codes import plugin_couldnt_find_key, plugin_couldnt_find_plugin_type, plugin_illegal_run_after_pseudonym, plugin_run_after_pseudonym_already_exist,\ plugin_illegal_run_before_pseudonym, plugin_run_before_pseudonym_already_exist, plugin_file_doesnt_exist_in_setup_dir, plugin_directory_doesnt_exist_in_setup_dir,\ plugin_run_after_additional_directories_already_exist, plugin_run_before_additional_directories_already_exist, plugin_python3_module_not_found from acph import add_acph from language import add_entry, language_directory from utils import remove_space, BaseXMLReader, BaseXMLWriter from ast import literal_eval from importlib import import_module from os import listdir, path from shutil import copy, copytree from subprocess import call setup_dict = {} setup_dir = path.dirname(path.abspath(fname)) plugin_type = "" is_skill = True for line in open(fname, "r"): if line.startswith("#"): no_space_line = remove_space(line, "") if no_space_line == "#type:run_after_plugin": plugin_type = RUN_AFTER elif no_space_line == "#type:run_before_plugin": plugin_type = RUN_BEFORE continue setup_dict[line.split("=")[0].strip()] = line.split("=")[1].strip() try: author = setup_dict["author"] main_file = setup_dict["main_file"] plugin_name = setup_dict["plugin_name"] skill = setup_dict["skill"] plugin_methods = literal_eval(setup_dict["plugin_methods"]) version = setup_dict["version"] additional_directories = literal_eval( setup_dict["additional_directories"]) description = setup_dict["description"] language_locales = literal_eval(setup_dict["language_locales"]) language_dict = literal_eval(setup_dict["language_dict"]) license = setup_dict["license"] required_python3_packages = literal_eval( setup_dict["required_python3_packages"]) except KeyError as error: raise KeyError("Errno: " + plugin_couldnt_find_key + " - Couldn't find key " + str(error) + " in " + fname) if plugin_type != RUN_AFTER or plugin_type != RUN_BEFORE: raise NameError( "Errno: " + plugin_couldnt_find_plugin_type + " - Couldn't find plugin type ('RUN_AFTER' or 'RUN_BEFORE') in " + fname) # ----- # if plugin_type == RUN_AFTER: for pseudonym in plugin_methods: if pseudonym == "run_after": raise NameError("Errno: " + plugin_illegal_run_after_pseudonym + " - Illegal name '" + pseudonym + "' in " + str(plugin_methods)) try: if pseudonym in get_all_run_after_plugins()[skill]: raise NameError("Errno: " + plugin_run_before_pseudonym_already_exist + " - The plugin pseudonym " + plugin_name + " already exist") except KeyError: is_skill = False pass elif plugin_type == RUN_BEFORE: for pseudonym in plugin_methods: if pseudonym == "run_before": raise NameError("Errno: " + plugin_illegal_run_before_pseudonym + " - Illegal name '" + pseudonym + "' in " + str(plugin_methods)) try: if pseudonym in get_all_run_before_plugins()[skill]: raise NameError("Errno: " + plugin_run_after_pseudonym_already_exist + " - The plugin pseudonym " + plugin_name + " already exist") except KeyError: is_skill = False pass if path.isfile(setup_dir + "/" + main_file) is False: raise FileNotFoundError("Errno: " + plugin_file_doesnt_exist_in_setup_dir + " - The file " + main_file + " doesn't exist in setup dir (" + setup_dir + ")") for directory in additional_directories: if path.isdir(setup_dir + "/" + directory) is False: raise NotADirectoryError( "Errno: " + plugin_directory_doesnt_exist_in_setup_dir + " - The directory " + directory + " doesn't exist in setup dir (" + setup_dir + ")") if plugin_type == RUN_AFTER: if directory in listdir(run_after_path): raise IsADirectoryError( "Errno: " + plugin_run_after_additional_directories_already_exist + " - The directory " + directory + " already exist in " + run_after_path) elif plugin_type == RUN_BEFORE: if directory in listdir(run_before_path): raise IsADirectoryError( "Errno: " + plugin_run_before_additional_directories_already_exist + " - The directory " + directory + " already exist in " + run_before_path) # ----- # for package in required_python3_packages: call("pip3 install " + package, shell=True) try: import_module(package) except ModuleNotFoundError: raise ModuleNotFoundError( "Errno: " + plugin_python3_module_not_found + " - Couldn't install the required python3 package '" + package + "'") dat_writer = None if plugin_type == RUN_AFTER: copy(main_file, run_after_path + "/" + main_file) for directory in additional_directories: copytree(directory, run_after_path + "/" + directory) for language in language_locales: add_entry(language, plugin_name, language_dict) dat_writer = BaseXMLWriter(run_after_file) if is_skill is False: dat_writer.add("run_after", skill, type="skill") dat_writer.write() elif plugin_type == RUN_BEFORE: copy(main_file, run_after_path + "/" + main_file) for directory in additional_directories: copytree(directory, run_after_path + "/" + directory) for language in language_locales: add_entry(language, plugin_name, language_dict) dat_writer = BaseXMLWriter(run_before_file) if is_skill is False: dat_writer.add("run_before", skill, type="skill") dat_writer.write() for pseudonym, method in plugin_methods.values(): dat_writer.add(skill, str(pseudonym), parent_attrib={"type": "skill"}, method=method, root_plugin=plugin_name) dat_writer.add(str(pseudonym), "additional_directories", str(additional_directories)) dat_writer.add(str(pseudonym), "author", str(author)) dat_writer.add(str(pseudonym), "description", str(description)) dat_writer.add(str(pseudonym), "language_dict", str(language_dict)) dat_writer.add(str(pseudonym), "language_locales", str(language_locales)) dat_writer.add(str(pseudonym), "license", str(license)) dat_writer.add(str(pseudonym), "main_file", str(main_file)) dat_writer.add(str(pseudonym), "required_python3_packages", str(required_python3_packages)) dat_writer.add(str(pseudonym), "version", str(version)) dat_writer.write()