Skip to content

Commit

Permalink
Merge pull request #38 from t00m/issue-37
Browse files Browse the repository at this point in the history
Issue 37
👍
  • Loading branch information
t00m committed Nov 20, 2019
2 parents c9fc3b8 + 75b0d2f commit 10ecfde
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
31 changes: 20 additions & 11 deletions kb4it/resources/offline/templates/DOC_CARD.tpl
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<!-- DOC_CARD.tpl :: START -->
<div class="uk-card uk-border-rounded uk-card-small uk-card-hover uk-auto@m uk-padding-small" uk-scrollspy="target: > div; cls: uk-animation-fade; delay: 100" style="border:1px solid lightgray; background-color: ghostwhite">
<div class="uk-child-width-expand@s uk-text-center" uk-grid uk-height-match="target: > div > .uk-card">
<div>
<div class="uk-flex-middle" uk-grid>
<div class="uk-width-expand@m uk-padding-remove-top uk-padding-remove-bottom uk-padding-remove-right">
<span class="uk-text-small uk-text-bold uk-text-truncate uk-align-left">%s</span>
</div>
<div class="uk-width-auto@m uk-flex-first">
<img class="uk-border-circle" width="48" height="48" src="%s">
</div>
<div class="uk-card uk-card-default uk-width-1-1@m uk-card-hover" uk-scrollspy="target: > div; cls: uk-animation-fade; delay: 100" style="border:1px solid lightgray; background-color: ghostwhite">
<div class="uk-card-header">
<div class="uk-grid-small uk-flex-middle" uk-grid>
<div class="uk-width-auto">
<a href="%s"><img class="uk-border-circle" width="48" height="48" src="%s" uk-tooltip="%s"></a>
</div>
<div class="uk-width-expand">
<div class="uk-text-break uk-text-truncate uk-text-bold uk-margin-remove-bottom">%s</div>
<p class="uk-text-meta uk-margin-remove-top"><time datetime="%s">%s</time></p>
</div>
</div>
</div>
<div class="uk-card-footer uk-padding-remove-bottom">
<div class="uk-flex uk-flex-right">
<ul class="uk-breadcrumb">
<li>%s</li>
<li>%s</li>
</ul>
</div>
</div>
</div>
<!-- DOC_CARD.tpl :: END -->
<!-- DOC_CARD.tpl :: END -->


8 changes: 6 additions & 2 deletions kb4it/src/core/mod_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,21 @@ def last_dt_modification(filename):
return dt

def last_ts_modification(filename):
"""Return last modification human-readable datetime of a file """
"""Return last modification human-readable timestamp of a file """
t = os.path.getmtime(filename)
d = datetime.fromtimestamp(t)
return "%s" % d.strftime("%Y/%m/%d %H:%M:%S")

def get_human_datetime(dt):
"""Return datetime for humans."""
return "%s" % dt.strftime("%A, %B %m, %Y at %H:%M")

def last_ts_rss(date):
""" Converts a datetime into an RFC 2822 formatted date."""
return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][date.weekday()], date.day, ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][date.month-1], date.year, date.hour, date.minute, date.second)

def last_modification_date(filename):
"""Return last modification human-readable datetime of a file """
"""Return last modification human-readable timestamp of a file """
t = os.path.getmtime(filename)
d = datetime.fromtimestamp(t)
return "%s" % d.strftime("%Y/%m/%d %H:%M:%S")
Expand Down
24 changes: 16 additions & 8 deletions kb4it/src/services/srv_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_docs_by_timestamp(self):
"""Return a list of tuples (doc, timestamp) sorted by timestamp desc."""
adict = {}
for docname in self.kbdict_new['document']:
ts = self.kbdict_new['document'][docname]['timestamp']
ts = self.kbdict_new['document'][docname]['Timestamp']
adict[docname] = ts
return sorted(adict.items(), key=operator.itemgetter(1), reverse=True)

Expand Down Expand Up @@ -160,7 +160,9 @@ def stage_03_preprocessing(self):
self.log.debug("\t\tPreprocessing DOC[%s]", docname)

# Add a new document to the database
self.srvdtb.add_document(docname)
ts_dtm = last_dt_modification(source)
timestamp = last_modification_date(source)
self.srvdtb.add_document(docname, ts_dtm)

# Get content
with open(source) as source_adoc:
Expand All @@ -176,8 +178,8 @@ def stage_03_preprocessing(self):
# Get Document Content and Metadata Hash
self.kbdict_new['document'][docname]['content_hash'] = get_hash_from_dict({'content': srcadoc})
self.kbdict_new['document'][docname]['metadata_hash'] = get_hash_from_dict(keys)
self.kbdict_new['document'][docname]['timestamp'] = last_modification_date(source)
# ~ self.log.error("%s -> %s", self.kbdict_new['document'][docname]['timestamp'], docname)
self.kbdict_new['document'][docname]['Timestamp'] = timestamp
# ~ self.log.error("%s -> %s", self.kbdict_new['document'][docname]['Timestamp'], docname)
cached_document = os.path.join(self.runtime['dir']['cache'], docname.replace('.adoc', '.html'))
cached_document_exists = os.path.exists(cached_document)
# Get documents per [key, value] and add them to kbdict
Expand All @@ -202,8 +204,8 @@ def stage_03_preprocessing(self):
else:
try:
# Compare timestamps for source/cache documents
doc_ts_new = self.kbdict_new['document'][docname]['timestamp']
doc_ts_cur = self.kbdict_cur['document'][docname]['timestamp']
doc_ts_new = self.kbdict_new['document'][docname]['Timestamp']
doc_ts_cur = self.kbdict_cur['document'][docname]['Timestamp']
if doc_ts_new > doc_ts_cur:
FORCE_DOC_COMPILATION = True
else:
Expand Down Expand Up @@ -249,6 +251,7 @@ def stage_04_processing(self):


# Process
self.log.debug("All keys: %s", available_keys)
for key in available_keys:
FORCE_DOC_COMPILATION = False
self.log.debug("\t\t* Processing Key: %s", key)
Expand All @@ -258,7 +261,12 @@ def stage_04_processing(self):
cur_nodes = sorted(self.kbdict_cur['metadata'][key][value])
except:
cur_nodes = []
new_nodes = sorted(self.kbdict_new['metadata'][key][value])
try:
new_nodes = sorted(self.kbdict_new['metadata'][key][value])
except Exception as error:
self.log.error("Error in [%s][%s]: %s", key, value, error)
new_nodes = ['###ERROR###']

if cur_nodes != new_nodes:
FORCE_DOC_KEY_COMPILATION = True
filename = "%s_%s.adoc" % (valid_filename(key), valid_filename(value))
Expand Down Expand Up @@ -383,7 +391,7 @@ def stage_06_extras(self):
adict = {}
### RSS feeds
for docname in self.kbdict_new['document']:
ts = self.kbdict_new['document'][docname]['timestamp']
ts = self.kbdict_new['document'][docname]['Timestamp']
adict[docname] = ts
# ~ Sort docs by timestamp
lastdocs = sorted(adict.items(), key=operator.itemgetter(1), reverse=True)
Expand Down
19 changes: 13 additions & 6 deletions kb4it/src/services/srv_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from kb4it.src.core.mod_srv import Service
from kb4it.src.services.srv_db import HEADER_KEYS
from kb4it.src.core.mod_utils import template, valid_filename, get_labels
from kb4it.src.core.mod_utils import last_ts_rss, get_human_datetime
from kb4it.src.core.mod_utils import set_max_frequency, get_font_size
from kb4it.src.core.mod_utils import get_author_icon
from kb4it.src.core.mod_utils import last_modification
Expand Down Expand Up @@ -292,11 +293,13 @@ def create_metadata_section(self, doc):
custom_keys = self.srvdtb.get_custom_keys(doc)
custom_props = ''
for key in custom_keys:
values = self.srvdtb.get_html_values_from_key(doc, key)
labels = get_labels(values)
row_custom_prop = template('METADATA_ROW_CUSTOM_PROPERTY')
custom_props += row_custom_prop % (valid_filename(key), key, labels)

try:
values = self.srvdtb.get_html_values_from_key(doc, key)
labels = get_labels(values)
row_custom_prop = template('METADATA_ROW_CUSTOM_PROPERTY')
custom_props += row_custom_prop % (valid_filename(key), key, labels)
except Exception as error:
self.log.error("Key[%s]: %s", key, error)
num_custom_props = len(custom_props)
if num_custom_props > 0:
html += custom_props
Expand Down Expand Up @@ -341,6 +344,7 @@ def get_doc_card(self, doc):
scope = self.srvdtb.get_values(doc, 'Scope')[0]
team = self.srvdtb.get_values(doc, 'Team')[0] # Only first match?
author = self.srvdtb.get_values(doc, 'Author')[0]
authors = ', '.join(self.srvdtb.get_values(doc, 'Author'))
icon_path = get_author_icon(source_dir, author)
if icon_path == "resources/images/authors/author_unknown.png":
self.missing_icons[author] = os.path.join(source_dir, "%s.png" % valid_filename(author))
Expand All @@ -349,5 +353,8 @@ def get_doc_card(self, doc):
link_scope = DOC_CARD_LINK % ("Scope_%s" % valid_filename(scope), scope)
link_team = DOC_CARD_LINK % ("Team_%s" % valid_filename(team), team)
link_author = DOC_CARD_LINK % ("Author_%s" % valid_filename(author), author)
return DOC_CARD % (link_title, icon_path)
link_image = "Author_%s.html" % valid_filename(author)
timestamp = self.srvdtb.get_doc_timestamp(doc)
human_ts = get_human_datetime(timestamp)
return DOC_CARD % (link_image, icon_path, authors, link_title, timestamp, human_ts, link_category, link_scope)

13 changes: 9 additions & 4 deletions kb4it/src/services/srv_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

EOHMARK = """// END-OF-HEADER. DO NOT MODIFY OR DELETE THIS LINE"""
HEADER_KEYS = ['Author', 'Category', 'Scope', 'Status', 'Team', 'Priority']
IGNORE_KEYS = HEADER_KEYS + ['Title']
IGNORE_KEYS = HEADER_KEYS + ['Title', 'Timestamp']


class KB4ITDB(Service):
Expand All @@ -31,10 +31,11 @@ def get_database(self):
"""Get a pointer to the database."""
return self.db

def add_document(self, doc):
def add_document(self, doc, timestamp):
"""Add a new document node to the graph."""
self.db[doc] = {}
self.log.debug("\t\t\tCreated new document: %s", doc)
self.db[doc]['Timestamp'] = timestamp
self.log.debug("\t\t\tCreated new document: %s with timestamp %s", doc, timestamp)

def add_document_key(self, doc, key, value):
"""Add a new key node to a document."""
Expand All @@ -46,6 +47,10 @@ def add_document_key(self, doc, key, value):
self.db[doc][key] = [value]
self.log.debug("\t\t\tKey '%s' with value '%s' linked to document: %s", key, value, doc)

def get_doc_timestamp(self, doc):
"""Get timestamp for a given document."""
return self.db[doc]['Timestamp']

def get_html_values_from_key(self, doc, key):
"""Return the html link for a value."""
html = []
Expand Down Expand Up @@ -91,7 +96,7 @@ def get_all_keys(self):
keys = []
for doc in self.db:
for key in self.get_doc_keys(doc):
if key != 'Title':
if key not in ['Title', 'Timestamp']:
keys.append(key)
keys = list(set(keys))
keys.sort(key=lambda y: y.lower())
Expand Down

0 comments on commit 10ecfde

Please sign in to comment.