def for_entry_metadata(self, cpu_thread_id, argv): if len(argv) != 3: raise PatternMissingArguments(expected=3, got=len(argv)) entry = self.cpu_threads_requested_entry[cpu_thread_id] key = ''.join(argv) if not key in entry.html_for_metadata: try: l = getattr(entry, argv[0]) if type(l) == dict: raise GenericMessage( messages.entry_metadata_is_not_a_list.format( argv[0], entry.id)) elif type(l) == str: l = l.split(",") except AttributeError as e: raise GenericMessage( messages.entry_has_no_metadata_like.format(argv[0])) if not len(argv[2]): argv[2] = ' ' try: entry.html_for_metadata[key] = argv[2].join( [argv[1].format(**{"value": item.strip()}) for item in l]) except KeyError as e: raise GenericMessage(messages.unknown_contextual.format(e)) return entry.html_for_metadata[key]
def get_entry_attribute_by_id(self, cpu_thread_id, argv=list()): if len(argv) < 2: raise PatternMissingArguments(2, len(argv)) key = ''.join(argv[:2]) if not key in self.cache_get_entry_attribute_by_id.keys(): try: entry = [ entry for entry in self.entries if entry.id == int(argv[1]) ][0] self.cache_get_entry_attribute_by_id[key] = getattr( entry, argv[0]) except ValueError: raise PatternInvalidArgument("id", argv[1], messages.id_must_be_an_integer) except AttributeError as e: raise PatternInvalidArgument( "attribute", argv[0], messages.entry_has_no_metadata_like.format(argv[0])) except IndexError: raise PatternInvalidArgument( "id", argv[1], messages.cannot_retrieve_entry_attribute_because_wrong_id) return self.cache_get_entry_attribute_by_id[key]
def for_entry_range(self, cpu_thread_id, argv): if len(argv) != 3: raise PatternMissingArguments(expected=2, got=len(argv)) try: start_from = int(argv[0]) except TypeError: raise GenericMessage( messages.wrong_pattern_argument.format("start_from", argv[0]) + ' ' + messages.pattern_argument_must_be_integer) try: end_from = int(argv[1]) except TypeError: raise GenericMessage( messages.wrong_pattern_argument.format("end_to", argv[0]) + ' ' + messages.pattern_argument_must_be_integer) if end_from <= start_from: raise GenericMessage( messages.invalid_range.format(start_from, end_to)) entry = self.cpu_threads_requested_entry[cpu_thread_id] output = "" #TODO: PREVENT CRASH IN CASE OF WRONG INPUTS for i in range(start_from, end_from): output += argv[2].replace("[index]}", '[' + str(i) + ']}').format(**entry.raw_metadata) return output
def upper(argv): try: a = argv[0] except: raise PatternMissingArguments() return a.upper()
def mul(argv): try: a, b = tuple(argv) except Exception as e: raise PatternMissingArguments(e) return str( int(a) * int(b) )
def greater(argv): try: a, b = tuple(argv) except Exception as e: raise PatternMissingArguments(e) return a if float(a) > float(b) else b
def get_embed_content(providers, argv): try: url = urlparse(argv[0]) except IndexError: raise PatternMissingArguments() return try_oembed(providers, url)
def merge(iterable, argv): if len(argv) != 2: raise PatternMissingArguments(expected=2, got=len(argv)) try: return argv[1].join( [argv[0].format(**something) for something in iterable]) except IndexError as e: if e.args == ('tuple index out of range', ): raise PatternInvalidArgument(name="string", value=argv[0]) raise e
def Latex2MathML(argv): try: tex_math_string = argv[0] return latex2mathml.converter.convert(tex_math_string) except IndexError: raise PatternMissingArguments() except Exception as e: print(e) raise PatternInvalidArgument("Tex math string", tex_math_string, messages.tex_math_error)
def Latex2MathML(cpu_thread_id, argv): try: tex_math_string = argv[0] except IndexError: raise PatternMissingArguments() try: return latex2mathml.converter.convert(tex_math_string) except: raise PatternInvalidArgument("LaTex math string", tex_math_string, messages.tex_math_error)
def include_file(argv): try: filename = argv[0] include_string = open("includes/" + filename, 'r').read() return include_string except IndexError: raise PatternMissingArguments() except PermissionError: raise PatternInvalidArgument( "path", filename, messages.wrong_permissions.format(argv[0])) except FileNotFoundError: raise PatternInvalidArgument("path", filename, messages.file_not_found.format(filename))
def if_infinite_scroll_enabled(self, argv): if not len(argv): raise PatternMissingArguments(expected=1, got=0) try: if self.blog_configuration["disable_infinite_scroll"]: try: return argv[1] except IndexError: return "" else: return argv[0] except KeyError: return argv[0]
def get_random_number(cpu_thread_id, in_argv): try: mn, mx, precision = in_argv except ValueError as e: raise PatternMissingArguments(e) argv = [] for i in range(0,3): try: argv.append(float(in_argv[i])) except ValueError: raise PatternInvalidArgument(arg_names[i],in_argv[i], messages.pattern_argument_must_be_integer) v = float(mn) + random.random() * (float(mx) - float(mn)) return str(int(v)) if int(precision) == 0 else str(round(v, int(precision)))
def include_file(cpu_thread_id, argv, raise_error=True): if not len(argv): raise PatternMissingArguments() filename = argv[0] if argv[0] == '': if not raise_error: return "" raise GenericMessage(messages.wrong_pattern_argument.format("path", argv[0], "include_file")) include_string = None paths = ("includes/"+filename, shutil.os.path.expanduser("~/.local/share/VenC/themes_includes/"+filename)) for path in paths: print(path, shutil.os.path.exists(path)); if shutil.os.path.exists(path): try: include_string = open(path, 'r').read() break except PermissionError: if not raise_error: return "" raise PatternInvalidArgument("path", filename, messages.wrong_permissions.format(path)) if include_string == None: if not raise_error: return "" raise PatternInvalidArgument( "path", filename, '\n' + '\n'.join( (messages.file_not_found.format(path) for path in paths) ) ) if len(argv) > 1: return include_string.format_map(SafeFormatDict(**{ "venc_arg_"+str(index) : argv[index] for index in range(1, len(argv)) })) else: return include_string
def get_chapter_attribute_by_index(self, cpu_thread_id, argv=list()): if len(argv) < 2: raise PatternMissingArguments(2, len(argv)) key = ''.join(argv[:2]) if not key in self.cache_get_chapter_attribute_by_index.keys(): try: self.cache_get_chapter_attribute_by_index[key] = getattr( self.raw_chapters[argv[1]].chapter, argv[0]) except KeyError as e: raise PatternInvalidArgument( "index", argv[1], messages.there_is_no_chapter_with_index.format(argv[1])) except AttributeError as e: raise PatternInvalidArgument( "attribute", argv[0], messages.chapter_has_no_attribute_like.format(argv[0])) return self.cache_get_chapter_attribute_by_index[key]
def set_color(cpu_thread_id, argv): if len(argv) < 2: raise PatternMissingArguments(expected=2, got=len(argv)) return "<span class=\"__VENC_TEXT_COLOR__\" style=\"color: "+argv[1]+";\">"+argv[0]+"</span>"