Exemple #1
0
    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]
Exemple #2
0
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))
Exemple #3
0
    def get_blog_metadata(self, cpu_thread_id, argv):
        # if exception is raised it will be automatically be catch by processor.
        try:
            return self.blog_configuration[argv[0]]

        except KeyError:
            raise PatternInvalidArgument(
                "GetBlogMetadata", argv[0],
                messages.blog_has_no_metadata_like.format(argv[0]))
Exemple #4
0
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
Exemple #5
0
    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]
Exemple #6
0
    def get_entry_metadata(self, cpu_thread_id, argv):
        # if exception is raised it will be automatically be catch by processor.
        try:
            return str(
                getattr(self.cpu_threads_requested_entry[cpu_thread_id],
                        argv[0]))

        except AttributeError:
            raise PatternInvalidArgument(
                "GetEntryMetadata", argv[0],
                messages.entry_has_no_metadata_like.format(argv[0]))
Exemple #7
0
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
Exemple #8
0
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)
Exemple #9
0
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)
Exemple #10
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)))
Exemple #11
0
def try_oembed(providers, url):
    try:
        key = [key for key in providers["oembed"].keys()
               if url.netloc in key][0]

    except IndexError:
        raise PatternInvalidArgument(
            "url", url.geturl(), messages.unknown_provider.format(url.netloc))

    try:
        r = requests.get(providers["oembed"][key][0],
                         params={
                             "url": url.geturl(),
                             "format": "json"
                         })

    except requests.exceptions.ConnectionError as e:
        raise GenericMessage(messages.connectivity_issue + '\n' + str(e))

    if r.status_code != 200:
        raise GenericMessage(
            messages.ressource_unavailable.format(url.geturl()))

    try:
        html = json.loads(r.text)["html"]

    except Exception as e:
        raise GenericMessage(messages.response_is_not_json.format(
            url.geturl()))

    try:
        cache_filename = hashlib.md5(url.geturl().encode('utf-8')).hexdigest()
        os.makedirs("caches/embed", exist_ok=True)
        f = open("caches/embed/" + cache_filename, "w")
        f.write(html)
        f.close()

    except PermissionError:
        notify(messages.wrong_permissions.format("caches/embed/" +
                                                 cache_filename),
               color="YELLOW")

    return html
Exemple #12
0
    def for_pages(self, argv):
        list_lenght = int(argv[0])
        string = argv[1]
        separator = argv[2]

        if self.pages_count <= 1:
            return str()

        output = str()
        page_number = 0
        for page in self.pages:
            if (not page_number < self.current_page - list_lenght) and (
                    not page_number > self.current_page + list_lenght):
                try:
                    output += string.format(
                        **{
                            "entry_id":
                            '',
                            "entry_title":
                            '',
                            "page_number":
                            str(page_number),
                            "path":
                            self.filename.format(
                                **{
                                    "page_number": (str() if page_number ==
                                                    0 else page_number)
                                })
                        }) + separator
                except KeyError as e:
                    print(e)
                    raise PatternInvalidArgument(name="string", value=string)

            page_number += 1

        return output[:-len(separator)]
def trigger_pattern_invalid_argument_exception(argv):
    raise PatternInvalidArgument("some field", "some value", "some message")