Commit 079d74a6 authored by Denis Salem's avatar Denis Salem
Browse files

improve readability in threads module

parent eb995175
......@@ -193,6 +193,7 @@
TODO | Make entry URL nicer when filename is index.html and has subfolder.
TODO | Catch "OSError: [Errno 98] Address already in use".
TODO | Improve sub folder management in Entry and EntriesThread.
TODO | Optimize objects dereferencing.
# version 3.x.x
TODO | Add support for plugin.
......
......@@ -37,51 +37,56 @@ class ArchivesThread(Thread):
def if_in_archives(self, argv):
return argv[0].strip()
def setup_archive_context(self, i, len_archives):
archive = self.datastore.entries_per_dates[i]
if archive.value in self.disable_threads:
return None
tree_special_char = '└' if i == len_archives-1 else '├'
notify("│\t "+tree_special_char+"─ "+archive.value+"...")
self.export_path = str("blog/"+self.sub_folders+'/'+archive.value+'/').replace(' ','-')
os.makedirs(self.export_path)
self.organize_entries([
entry for entry in self.datastore.get_entries_for_given_date(
archive.value,
self.datastore.blog_configuration["reverse_thread_order"]
)
])
return archive
def do(self):
len_archives = len(self.datastore.entries_per_dates)
for i in range(0, len_archives):
thread = self.datastore.entries_per_dates[i]
if thread.value in self.disable_threads:
archive = self.setup_archive_context(i, len_archives)
if archive == None:
continue
if i == len_archives-1:
tree_special_char = '└'
else:
tree_special_char = '├'
notify("│\t "+tree_special_char+"─ "+thread.value+"...")
self.export_path = str("blog/"+self.sub_folders+'/'+thread.value+'/').replace(' ','-')
os.makedirs(self.export_path)
self.organize_entries([
entry for entry in self.datastore.get_entries_for_given_date(
thread.value,
self.datastore.blog_configuration["reverse_thread_order"]
)
])
super().do()
if self.datastore.enable_jsonld:
from venc2.l10n import messages
notify("│\t "+('│' if i != len_archives-1 else ' ')+" └─ "+messages.generating_jsonld_doc)
blog_url = self.datastore.blog_configuration["blog_url"]
archive_as_jsonld = self.datastore.archives_as_jsonld[thread.value]
archive_as_jsonld = self.datastore.archives_as_jsonld[archive.value]
archive_as_jsonld["breadcrumb"]["itemListElement"].append({
"@type": "ListItem",
"position": 2,
"item": {
"@id": blog_url+'/'+self.sub_folders+thread.value+"/archives.jsonld",
"url": blog_url+'/'+self.sub_folders+thread.value,
"name": self.datastore.blog_configuration["blog_name"] +' | '+thread.value
"@id": blog_url+'/'+self.sub_folders+archive.value+"/archives.jsonld",
"url": blog_url+'/'+self.sub_folders+archive.value,
"name": self.datastore.blog_configuration["blog_name"] +' | '+archive.value
}
})
archive_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+thread.value+"/archives.jsonld"
archive_as_jsonld["url"] = blog_url+'/'+self.sub_folders+thread.value
archive_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+archive.value+"/archives.jsonld"
archive_as_jsonld["url"] = blog_url+'/'+self.sub_folders+archive.value
dump = json.dumps(archive_as_jsonld)
f = open("blog/"+self.sub_folders+'/'+thread.value+"/archives.jsonld", 'w')
f = open("blog/"+self.sub_folders+'/'+archive.value+"/archives.jsonld", 'w')
f.write(dump)
def JSONLD(self, argv):
def GetJSONLD(self, argv):
if self.current_page == 0:
return '<script type="application/ld+json" src="archives.jsonld"></script>'
......
......@@ -49,93 +49,96 @@ class CategoriesThread(Thread):
def if_in_categories(self, argv):
return argv[0].strip()
def do_feeds(self, entries, node, tree_special_char):
entries = sorted(entries, key = lambda entry : entry.id, reverse=True)[0:self.datastore.blog_configuration["feed_lenght"]]
if not self.disable_rss_feed:
self.rss_feed.do(entries, self.export_path, self.relative_origin,self.indentation_level+tree_special_char+' ', ' ├' if not self.disable_atom_feed or self.datastore.enable_jsonld or len(node.childs) else ' └')
if not self.disable_atom_feed:
self.atom_feed.do(entries, self.export_path, self.relative_origin,self.indentation_level+tree_special_char+' ', ' ├' if len(node.childs) or self.datastore.enable_jsonld else ' └')
def do_jsonld(self, node, tree_special_char):
from venc2.l10n import messages
import json
notify(self.indentation_level+tree_special_char+' '+ (' ├─ ' if len(node.childs) else ' └─ ')+messages.generating_jsonld_doc)
blog_url = self.datastore.blog_configuration["blog_url"]
category_as_jsonld = self.datastore.categories_as_jsonld[self.category_value]
position = 2
category_breadcrumb_path = ''
for sub_category in self.category_value.split('/'):
category_breadcrumb_path += sub_category+'/'
category_as_jsonld["breadcrumb"]["itemListElement"].append({
"@type": "ListItem",
"position": position,
"item": {
"@id": blog_url+'/'+self.sub_folders+category_breadcrumb_path+"categories.jsonld",
"url": blog_url+'/'+self.sub_folders+category_breadcrumb_path,
"name": self.datastore.blog_configuration["blog_name"] +' | '+ sub_category
}
})
position += 1
category_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+self.category_value+"categories.jsonld"
category_as_jsonld["url"] = blog_url+'/'+self.sub_folders+self.category_value
dump = json.dumps(category_as_jsonld)
f = open(("blog/"+self.sub_folders+self.category_value+"categories.jsonld").replace(' ','-'), 'w')
f.write(dump)
def setup_category_context(self, i, root, len_root):
node = root[i]
if node.value in self.disable_threads:
return (None, None, None)
if i == len_root-1:
tree_special_char = '└'
else:
tree_special_char = '├'
notify(self.indentation_level+tree_special_char+"─ "+node.value+"...")
export_path = self.export_path
category_value = self.category_value
self.category_value += node.value+'/'
self.export_path += str(node.value+'/').replace(' ','-')
self.relative_origin = ''.join([ '../' for f in self.export_path.split("/")[1:] if f != '' ]).replace("//",'/')
try:
os.makedirs(self.export_path)
except FileExistsError:
pass
return (node, export_path, category_value)
def do(self, root=None):
if root == None:
root = self.datastore.entries_per_categories
len_root = len(root)
for i in range(0, len_root):
node = root[i]
if node.value in self.disable_threads:
node, export_path, category_value = self.setup_category_context(i, root, len_root)
if node == None:
continue
if i == len_root-1:
tree_special_char = '└'
else:
tree_special_char = '├'
notify(self.indentation_level+tree_special_char+"─ "+node.value+"...")
export_path = self.export_path
category_value = self.category_value
self.category_value += node.value+'/'
self.export_path += str(node.value+'/').replace(' ','-')
self.relative_origin = ''.join([ '../' for f in self.export_path.split("/")[1:] if f != '' ]).replace("//",'/')
try:
os.makedirs(self.export_path)
except FileExistsError:
pass
# Get entries
# do actual context
entries = [self.datastore.entries[entry_index] for entry_index in node.related_to]
self.organize_entries( entries[::-1] if self.datastore.blog_configuration["reverse_thread_order"] else entries )
super().do()
entries = sorted(entries, key = lambda entry : entry.id, reverse=True)[0:self.datastore.blog_configuration["feed_lenght"]]
if i == len_root-1:
tree_special_char = ' '
else:
tree_special_char = '│'
if not self.disable_rss_feed:
self.rss_feed.do(entries, self.export_path, self.relative_origin,self.indentation_level+tree_special_char+' ', ' ├' if not self.disable_atom_feed or self.datastore.enable_jsonld or len(node.childs) else ' └')
if not self.disable_atom_feed:
self.atom_feed.do(entries, self.export_path, self.relative_origin,self.indentation_level+tree_special_char+' ', ' ├' if len(node.childs) or self.datastore.enable_jsonld else ' └')
tree_special_char = ' ' if i == len_root-1 else '│'
self.do_feeds(entries, node, tree_special_char)
if self.datastore.enable_jsonld:
from venc2.l10n import messages
notify(self.indentation_level+tree_special_char+' '+ (' ├─ ' if len(node.childs) else ' └─ ')+messages.generating_jsonld_doc)
import json
blog_url = self.datastore.blog_configuration["blog_url"]
category_as_jsonld = self.datastore.categories_as_jsonld[self.category_value]
position = 2
category_breadcrumb_path = ''
for sub_category in self.category_value.split('/'):
category_breadcrumb_path += sub_category+'/'
category_as_jsonld["breadcrumb"]["itemListElement"].append({
"@type": "ListItem",
"position": position,
"item": {
"@id": blog_url+'/'+self.sub_folders+category_breadcrumb_path+"categories.jsonld",
"url": blog_url+'/'+self.sub_folders+category_breadcrumb_path,
"name": self.datastore.blog_configuration["blog_name"] +' | '+ sub_category
}
})
position += 1
category_as_jsonld["@id"] = blog_url+'/'+self.sub_folders+self.category_value+"categories.jsonld"
category_as_jsonld["url"] = blog_url+'/'+self.sub_folders+self.category_value
dump = json.dumps(category_as_jsonld)
f = open(("blog/"+self.sub_folders+self.category_value+"categories.jsonld").replace(' ','-'), 'w')
f.write(dump)
if len_root - 1 == i:
self.indentation_level += " "
else:
self.indentation_level += "│ "
self.do_jsonld(node, tree_special_char)
# jump to branchs
self.indentation_level += " " if len_root - 1 == i else "│ "
self.do(root=node.childs)
self.indentation_level = self.indentation_level[:-3]
# Restore path
# Restore states
self.indentation_level = self.indentation_level[:-3]
self.export_path = export_path
self.category_value = category_value
def JSONLD(self, argv):
def GetJSONLD(self, argv):
if self.current_page == 0:
return '<script type="application/ld+json" src="categories.jsonld"></script>'
......
......@@ -124,7 +124,12 @@ class EntriesThread(Thread):
stream.write(output)
stream.close()
self.current_export_path = export_path
def do_jsonld(self, entry):
dump = json.dumps(self.datastore.entries_as_jsonld[self.current_entry.id])
f = open(self.current_export_path+"/entry"+str(entry.id)+".jsonld", 'w')
f.write(dump)
def do(self):
if self.datastore.enable_jsonld:
notify(self.indentation_level+'└─ '+messages.generating_jsonld_docs)
......@@ -139,11 +144,10 @@ class EntriesThread(Thread):
self.do_iteration(entry)
self.post_iteration()
if self.datastore.enable_jsonld:
dump = json.dumps(self.datastore.entries_as_jsonld[self.current_entry.id])
f = open(self.current_export_path+"/entry"+str(entry.id)+".jsonld", 'w')
f.write(dump)
self.do_jsonld(entry)
def JSONLD(self, argv):
def GetJSONLD(self, argv):
if self.datastore.enable_jsonld:
return '<script type="application/ld+json" src="entry'+str(self.current_entry.id)+'.jsonld"></script>'
......
......@@ -44,6 +44,9 @@ class FeedThread(Thread):
self.relative_origin = relative_origin
self.organize_entries(entries)
super().do()
def GetJSONLD(self, argv):
# Trigger UnknownPattern.
raise KeyError
......@@ -50,7 +50,7 @@ class MainThread(Thread):
if i == self.datastore.blog_configuration["feed_lenght"]:
return entries
def do_feed(self):
def do_feeds(self):
disable_rss_feed = self.datastore.blog_configuration["disable_rss_feed"]
disable_atom_feed = self.datastore.blog_configuration["disable_atom_feed"]
entries = []
......@@ -76,10 +76,10 @@ class MainThread(Thread):
def do(self):
super().do()
self.do_feed()
self.do_feeds()
self.do_jsonld()
def JSONLD(self, argv):
def GetJSONLD(self, argv):
if self.current_page == 0 and self.datastore.enable_jsonld:
return '<script type="application/ld+json" src="root.jsonld"></script>'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment