def __run_command(self, command, params): command = DataProvider.get_service_command(command) if ModuleManager.module_was_loaded('CORE'): for idx, param in enumerate(params): try: CommonValidator.validate_name_variable(param) CommonValidator.check_if_variable_exists(param) variable = param[1:] params[idx] = DataProvider.get_variable_value(variable) except Exception: pass try: self.__validator.validate('command', (command.get_name(), params)) except (Validator.ValidationError, CommonValidator.ValidationError) as exception: raise Interpreter.RunTimeError( f'Command: `{command.get_name()}`. ' + str(exception)) param_types = command.get_param_types() for idx, param in enumerate(params): if inspect.isclass(param_types[idx]): params[idx] = param_types[idx](params[idx]) else: break try: self.__validator.validate('command', (command.get_name(), params)) except (Validator.ValidationError, CommonValidator.ValidationError) as exception: raise Interpreter.RunTimeError( f'Command: `{command.get_name()}`. ' + str(exception)) if command.get_name() == Semantic.get_symbol('var'): new_command, *new_params = params[2:] self.__run_command(new_command, new_params) params = params[:2] params.append(DataProvider.get_returned_value()) DataProvider.return_value(None) if command.get_name() == Semantic.get_symbol('import'): return try: command.get_exec_func()(*params) except CommonLogic.RunTimeError as exception: raise Interpreter.RunTimeError(f'Command: {command.get_name()}. ' f'{str(exception)}')
def get_element_from_html(filename, traverse_tags, cmd, cmd_value): with open(filename, 'r') as file: content = file.read() soup = BeautifulSoup(content, 'lxml') tags = traverse_tags.split('@') if tags[0] == "": tags = tags[1:] current = soup for tag in tags: tag_name, *tag_cnts = tag.split('!') tag_cnts.append(1) tag_cnt = int(tag_cnts[0]) next_child = None children = [e for e in current.children if e.name is not None] for child in children: if child.name == tag_name: tag_cnt -= 1 if tag_cnt == 0: next_child = child break if not next_child: raise CommonLogic.RunTimeError() current = next_child if cmd == 'attr': try: # DataProvider.return_value("http:" + current[cmd_value]) DataProvider.return_value('https:' + current[cmd_value]) except: raise CommonLogic.RunTimeError( f'Wrong attribute value `{cmd_value}`') elif cmd == 'field': parts = current.text.split() parts = [part.strip() for part in parts] DataProvider.return_value(" ".join(parts)) else: raise CommonLogic.RunTimeError(f'Wrong attribute name `{cmd}`')
def load_page(url): session = requests.Session() response = session.get(url) content = response.text DataProvider.return_value(content)
def do_inc(value): DataProvider.return_value(value + 1)
def format_length(string, length, symbol): res = symbol * (length - len(string)) + string DataProvider.return_value(res)
def get_month_name(month): x = calendar.month_name[month] DataProvider.return_value(x)
def days_count_by_month(year, month): days = [ 31, 28 + (year % 4 == 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] DataProvider.return_value(days[month - 1])
def do_replace(s, t1, t2): DataProvider.return_value(s.replace(t1, t2))
def do_concat(s, t): DataProvider.return_value(s + t)
def do_const(value): DataProvider.return_value(value)