Example #1
0
def init_theme(argv):
    theme_folder = "theme/"
    themes_folder = os.path.expanduser("~") + "/.local/share/VenC/themes/"
    if len(argv) == 1:
        if os.path.isdir(themes_folder + argv[0]):
            theme_folder = os.path.expanduser(
                "~") + "/.local/share/VenC/themes/" + argv[0] + "/"

        else:
            die(messages.theme_doesnt_exists.format(argv[0]))

    if not os.path.isdir(theme_folder):
        die(messages.file_not_found.format(theme_folder))

    if "config.yaml" in os.listdir(theme_folder) and not os.path.isdir(
            themes_folder + "/config.yaml"):
        import yaml
        config = yaml.load(open(theme_folder + "/config.yaml", 'r').read(),
                           Loader=yaml.FullLoader)
        if "override" in config.keys() and type(config["override"]) == dict:
            # TODO : Be explicit about which value are updated
            for param in config["override"].keys():
                if type(
                        config["override"][param]
                ) == dict and param in datastore.blog_configuration.keys(
                ) and type(datastore.blog_configuration[param]) == dict:
                    datastore.blog_configuration[param].update(
                        config["override"][param])

                else:
                    datastore.blog_configuration[param] = config["override"][
                        param]

        if "assets_dependencies" in config.keys() and type(
                config["assets_dependencies"]) == list:
            global theme_assets_dependencies
            theme_assets_dependencies = config["assets_dependencies"]

        if "includes_dependencies" in config.keys() and type(
                config["includes_dependencies"]) == list:
            global theme_includes_dependencies
            append = theme_includes_dependencies.append
            for include_file in config["includes_dependencies"]:
                append(include_file)

    try:
        return Theme(theme_folder), theme_folder

    except MalformedPatterns as e:
        from venc2.helpers import handle_malformed_patterns
        handle_malformed_patterns(e)
Example #2
0
def edit_and_export(argv):
    if len(argv) != 1:
        from venc2.helpers import die
        die(messages.missing_params.format("--edit-and-export"))
    
    try:
        proc = subprocess.Popen([datastore.blog_configuration["text_editor"], argv[0]])
        while proc.poll() == None:
            pass

    except TypeError:
        from venc2.helpers import die
        die(messages.unknown_text_editor.format(datastore.blog_configuration["text_editor"]))
    
    except:
        raise
    
    export_blog()
Example #3
0
def edit_and_export(argv):
    if len(argv) != 1:
        from venc2.helpers import die
        die(messages.missing_params.format("--edit-and-export"))
    
    try:
        proc = subprocess.Popen([datastore.blog_configuration["text_editor"], argv[0]])
        while proc.poll() == None:
            pass

    except TypeError:
        from venc2.helpers import die
        die(messages.unknown_text_editor.format(datastore.blog_configuration["text_editor"]))
    
    except:
        raise
    
    export_blog()
Example #4
0
    def update_chapters(self, entry):
        try:
            chapter = str(entry.chapter)
            [int(level) for level in chapter.split('.') if level != '']

        except ValueError as e:  # weak test to check attribute conformity
            return

        except AttributeError as e:  # does entry has chapter?
            return

        if chapter in self.raw_chapters.keys():
            from venc2.helpers import die
            die(
                messages.chapter_already_exists.format(
                    entry.title, entry.id, self.raw_chapters[chapter].title,
                    self.raw_chapters[chapter].id, chapter))
        else:
            self.raw_chapters[chapter] = entry
Example #5
0
def init_theme(argv):
    theme_folder = "theme/"

    if len(argv) == 1:
        if not argv[0] in themes_descriptor.keys():
            from venc2.helpers import die
            die(messages.theme_doesnt_exists.format(argv[0]))
        
        else:
            theme_folder = os.path.expanduser("~")+"/.local/share/VenC/themes/"+argv[0]+"/"
    
        for param in themes_descriptor[argv[0]].keys():
            if param[0] != "_": # marker to detect field names we do not want to replace
                datastore.blog_configuration[param] = themes_descriptor[argv[0]][param]

    try:
        return Theme(theme_folder), theme_folder
        
    except MalformedPatterns as e:
        from venc2.helpers import handle_malformed_patterns
        handle_malformed_patterns(e)
Example #6
0
def init_theme(argv):
    theme_folder = "theme/"

    if len(argv) == 1:
        if not argv[0] in themes_descriptor.keys():
            from venc2.helpers import die
            die(messages.theme_doesnt_exists.format(argv[0]))
        
        else:
            theme_folder = os.path.expanduser("~")+"/.local/share/VenC/themes/"+argv[0]+"/"
    
        for param in themes_descriptor[argv[0]].keys():
            if param[0] != "_": # marker to detect field names we do not want to replace
                datastore.blog_configuration[param] = themes_descriptor[argv[0]][param]

    try:
        return Theme(theme_folder), theme_folder
        
    except MalformedPatterns as e:
        from venc2.helpers import handle_malformed_patterns
        handle_malformed_patterns(e)
Example #7
0
    def update_chapters(self, entry):
        try:
            chapter = str(entry.chapter)
            [ int(level) for level in chapter.split('.') if level != '']

        except ValueError as e: # weak test to check attribute conformity
            return

        except AttributeError as e: # does entry has chapter?
            return

        if chapter in self.raw_chapters.keys():
            from venc2.helpers import die
            die(messages.chapter_already_exists.format(
                entry.title,
                entry.id,
                self.raw_chapters[chapter][1],
                self.raw_chapters[chapter][0],
                chapter
            ))
        else:
            self.raw_chapters[chapter] = (entry.id, entry.title, entry.url)
Example #8
0
def edit_and_export(argv):
    global datastore
    datastore = manager.DataStore()

    if len(argv) != 1:
        die(messages.missing_params.format("--edit-and-export"))

    try:
        proc = subprocess.Popen(
            [datastore.blog_configuration["text_editor"], argv[0]])
        while proc.poll() == None:
            pass

    except TypeError:
        die(
            messages.unknown_text_editor.format(
                datastore.blog_configuration["text_editor"]))

    except:
        raise

    export_blog()
Example #9
0
    def __init__(self):
        notify("┌─ " + messages.loading_data)
        self.root_page = None
        self.blog_configuration = get_blog_configuration()
        self.sort_by = self.blog_configuration["sort_by"]
        self.enable_jsonld = self.blog_configuration["enable_jsonld"]
        self.enable_jsonp = self.blog_configuration["enable_jsonp"]
        self.blog_url = self.blog_configuration["blog_url"]
        self.path_encoding = self.blog_configuration["path_encoding"]
        self.disable_threads = [
            thread_name.strip() for thread_name in
            self.blog_configuration["disable_threads"].split(',')
        ]
        self.entries = list()
        self.entries_per_archives = list()
        self.entries_per_categories = list()

        try:
            self.cpu_threads_requested_entry = [None] * cpu_count()

        except NotImplementedError:
            self.cpu_threads_requested_entry = [None]

        self.max_category_weight = 1
        self.categories_leaves = []
        self.embed_providers = {}
        self.html_categories_tree = {}
        self.html_categories_leaves = {}
        self.html_blog_archives = {}
        self.cache_get_entry_attribute_by_id = {}
        self.cache_get_chapter_attribute_by_index = {}
        self.generation_timestamp = datetime.datetime.now()
        self.raw_chapters = {}
        self.chapters_index = []
        self.html_chapters = {}

        # Build JSON-LD doc if any
        if self.enable_jsonld or self.enable_jsonp:
            if "https://schema.org" in self.blog_configuration.keys():
                self.optionals_schemadotorg = self.blog_configuration[
                    "https://schema.org"]

            else:
                self.optionals_schemadotorg = {}

            self.entries_as_jsonld = {}
            self.archives_as_jsonld = {}
            self.categories_as_jsonld = {}
            self.root_site_to_jsonld()

        # Build entries
        try:
            jsonld_callback = self.entry_to_jsonld_callback if (
                self.enable_jsonld or self.enable_jsonp) else None
            for filename in yield_entries_content():
                self.entries.append(
                    Entry(
                        filename, self.blog_configuration["path"],
                        jsonld_callback, self.blog_configuration["path"]
                        ["archives_directory_name"], self.path_encoding))

        # Might happen during Entry creation.
        except MalformedPatterns as e:
            from venc2.helpers import handle_malformed_patterns
            handle_malformed_patterns(e)

        self.entries = sorted(self.entries, key=lambda entry: self.sort(entry))

        path_categories_sub_folders = self.blog_configuration["path"][
            "categories_sub_folders"] + '/'
        path_archives_directory_name = self.blog_configuration["path"][
            "archives_directory_name"]

        for entry_index in range(0, len(self.entries)):
            current_entry = self.entries[entry_index]
            if entry_index > 0:
                self.entries[entry_index - 1].next_entry = current_entry
                current_entry.previous_entry = self.entries[entry_index - 1]

            # Update entriesPerDates
            if path_archives_directory_name != '':
                formatted_date = current_entry.formatted_date
                entries_index = self.get_entries_index_for_given_date(
                    formatted_date)
                if entries_index != None:
                    self.entries_per_archives[entries_index].count += 1
                    self.entries_per_archives[entries_index].related_to.append(
                        entry_index)

                else:
                    self.entries_per_archives.append(
                        MetadataNode(formatted_date, entry_index))

            # Update entriesPerCategories
            try:
                if self.path_encoding == '':
                    sub_folders = quirk_encoding(
                        unidecode.unidecode(path_categories_sub_folders))
                else:
                    sub_folders = urllib_parse_quote(
                        path_categories_sub_folders,
                        encoding=self.path_encoding)

            except UnicodeEncodeError as e:
                notify("\"{0}\": ".format(path_categories_sub_folders) +
                       str(e),
                       color="YELLOW")

            sub_folders = sub_folders if sub_folders != '/' else ''
            build_categories_tree(entry_index,
                                  current_entry.raw_categories,
                                  self.entries_per_categories,
                                  self.categories_leaves,
                                  self.max_category_weight,
                                  self.set_max_category_weight,
                                  encoding=self.path_encoding,
                                  sub_folders=sub_folders)
            self.update_chapters(current_entry)

        # build chapters index
        path_chapters_sub_folders = self.blog_configuration["path"][
            "chapters_sub_folders"]
        path_chapter_folder_name = self.blog_configuration["path"][
            "chapter_directory_name"]
        #TODO: Might be not safe, must test level if is actually an int. Test as well the whole sequence.

        for chapter in sorted(self.raw_chapters.keys(),
                              key=lambda x: int(x.replace('.', ''))):
            top = self.chapters_index
            index = ''
            levels = [
                str(level) for level in chapter.split('.') if level != ''
            ]
            len_levels = len(levels)

            for i in range(0, len_levels):
                l = levels[i]
                if index == '':
                    index = l

                else:
                    index += '.' + l

                f = filter(lambda c: c.index == index, top)
                try:
                    top = next(f).sub_chapters

                except StopIteration:
                    if index in self.raw_chapters.keys():
                        # TODO: Replace this shitty bloc by a function call building path
                        try:
                            path = "\x1a" + (
                                (path_chapters_sub_folders + '/'
                                 if path_chapters_sub_folders != '' else '') +
                                path_chapter_folder_name).format(
                                    **{
                                        "chapter_name":
                                        self.raw_chapters[index].title,
                                        "chapter_index": index
                                    })
                            try:
                                if self.path_encoding == '':
                                    path = quirk_encoding(
                                        unidecode.unidecode(path))

                                else:
                                    path = urllib_parse_quote(
                                        path, encoding=self.path_encoding)

                            except UnicodeEncodeError as e:
                                notify("\"{0}\": ".format(
                                    path_chapters_sub_folders) + str(e),
                                       color="YELLOW")

                        except KeyError as e:
                            from venc2.helpers import die
                            die(messages.variable_error_in_filename.format(e))

                        top.append(
                            Chapter(index, self.raw_chapters[index], path))
                        self.raw_chapters[index].chapter = top[-1]

                    else:
                        top.append(Chapter(index, None, ''))
                        top = top[-1].sub_chapters

        # Setup BlogArchives Data
        self.blog_archives = list()

        path_archives_sub_folders = self.blog_configuration["path"][
            "archives_sub_folders"] + '/'
        for node in self.entries_per_archives:
            try:
                if self.path_encoding == '':
                    sub_folders = quirk_encoding(
                        unidecode.unidecode(path_archives_sub_folders))
                else:
                    sub_folders = urllib_parse_quote(
                        path_archives_sub_folders, encoding=self.path_encoding)

            except UnicodeEncodeError as e:
                notify("\"{0}\": ".format(path_archives_sub_folders) + str(e),
                       color="YELLOW")

            sub_folders = sub_folders if sub_folders != '/' else ''

            self.blog_archives.append({
                "value": node.value,
                "path": "\x1a" + sub_folders + node.value,
                "count": node.count,
                "weight": node.weight
            })