Simplify storing Tags in OrgNode object

- Use Set for Tags instead of dictionary with empty keys
- No Need to store First Tag separately
  - Remove properties methods associated with storing first tag separately
- Simplify extraction of tags string in org_to_jsonl
- Split notes_string creation into multiple f-string in separate line
  for code readability
This commit is contained in:
Debanjum Singh Solanky
2022-06-17 16:24:56 +03:00
parent 51a43245d3
commit 1c5754bf95
3 changed files with 22 additions and 40 deletions

View File

@@ -110,9 +110,9 @@ def convert_org_entries_to_jsonl(entries, verbose=0):
print(f"Title: {entry.Heading()}") print(f"Title: {entry.Heading()}")
if entry.Tags(): if entry.Tags():
tags_str = " ".join([tag for tag in entry.Tags()]) tags_str = " ".join(entry.Tags())
entry_dict["Tags"] = tags_str entry_dict["Tags"] = tags_str
if verbose > 1: if verbose > 2:
print(f"Tags: {tags_str}") print(f"Tags: {tags_str}")
if entry.Body(): if entry.Body():

View File

@@ -55,8 +55,7 @@ def makelist(filename):
level = 0 level = 0
heading = "" heading = ""
bodytext = "" bodytext = ""
tag1 = "" # The first tag enclosed in :: tags = set() # set of all tags in headline
alltags = [] # list of all tags in headline
sched_date = '' sched_date = ''
deadline_date = '' deadline_date = ''
nodelist = [] nodelist = []
@@ -69,7 +68,7 @@ def makelist(filename):
hdng = re.search(r'^(\*+)\s(.*?)\s*$', line) hdng = re.search(r'^(\*+)\s(.*?)\s*$', line)
if hdng: if hdng:
if heading: # we are processing a heading line if heading: # we are processing a heading line
thisNode = Orgnode(level, heading, bodytext, tag1, alltags) thisNode = Orgnode(level, heading, bodytext, tags)
if sched_date: if sched_date:
thisNode.setScheduled(sched_date) thisNode.setScheduled(sched_date)
sched_date = "" sched_date = ""
@@ -82,15 +81,14 @@ def makelist(filename):
level = hdng.group(1) level = hdng.group(1)
heading = hdng.group(2) heading = hdng.group(2)
bodytext = "" bodytext = ""
tag1 = "" tags = set() # set of all tags in headline
alltags = [] # list of all tags in headline
tagsrch = re.search(r'(.*?)\s*:([a-zA-Z0-9].*?):$',heading) tagsrch = re.search(r'(.*?)\s*:([a-zA-Z0-9].*?):$',heading)
if tagsrch: if tagsrch:
heading = tagsrch.group(1) heading = tagsrch.group(1)
tags = tagsrch.group(2) parsedtags = tagsrch.group(2)
if tags: if parsedtags:
for tag in tags.split(':'): for parsedtag in parsedtags.split(':'):
if tag != '': alltags.append(tag) if parsedtag != '': tags.add(parsedtag)
else: # we are processing a non-heading line else: # we are processing a non-heading line
if line[:10] == '#+SEQ_TODO': if line[:10] == '#+SEQ_TODO':
kwlist = re.findall(r'([A-Z]+)\(', line) kwlist = re.findall(r'([A-Z]+)\(', line)
@@ -146,7 +144,7 @@ def makelist(filename):
bodytext = bodytext + line bodytext = bodytext + line
# write out last node # write out last node
thisNode = Orgnode(level, heading, bodytext, tag1, alltags) thisNode = Orgnode(level, heading, bodytext, tags)
thisNode.setProperties(propdict) thisNode.setProperties(propdict)
if sched_date: if sched_date:
thisNode.setScheduled(sched_date) thisNode.setScheduled(sched_date)
@@ -185,7 +183,7 @@ class Orgnode(object):
Orgnode class represents a headline, tags and text associated Orgnode class represents a headline, tags and text associated
with the headline. with the headline.
""" """
def __init__(self, level, headline, body, tag, alltags): def __init__(self, level, headline, body, tags):
""" """
Create an Orgnode object given the parameters of level (as the Create an Orgnode object given the parameters of level (as the
raw asterisks), headline text (including the TODO tag), and raw asterisks), headline text (including the TODO tag), and
@@ -195,16 +193,13 @@ class Orgnode(object):
self.level = len(level) self.level = len(level)
self.headline = headline self.headline = headline
self.body = body self.body = body
self.tag = tag # The first tag in the list self.tags = set(tags) # All tags in the headline
self.tags = dict() # All tags in the headline
self.todo = "" self.todo = ""
self.prty = "" # empty of A, B or C self.prty = "" # empty of A, B or C
self.scheduled = "" # Scheduled date self.scheduled = "" # Scheduled date
self.deadline = "" # Deadline date self.deadline = "" # Deadline date
self.closed = "" # Closed date self.closed = "" # Closed date
self.properties = dict() self.properties = dict()
for t in alltags:
self.tags[t] = ''
# Look for priority in headline and transfer to prty field # Look for priority in headline and transfer to prty field
@@ -248,19 +243,12 @@ class Orgnode(object):
""" """
self.prty = newprty self.prty = newprty
def Tag(self):
"""
Returns the value of the first tag.
For example, :HOME:COMPUTER: would return HOME
"""
return self.tag
def Tags(self): def Tags(self):
""" """
Returns a list of all tags Returns the set of all tags
For example, :HOME:COMPUTER: would return ['HOME', 'COMPUTER'] For example, :HOME:COMPUTER: would return {'HOME', 'COMPUTER'}
""" """
return self.tags.keys() return self.tags
def hasTag(self, srch): def hasTag(self, srch):
""" """
@@ -270,19 +258,11 @@ class Orgnode(object):
""" """
return srch in self.tags return srch in self.tags
def setTag(self, newtag): def setTags(self, newtags):
""" """
Change the value of the first tag to the supplied string Store all the tags found in the headline.
""" """
self.tag = newtag self.tags = set(newtags)
def setTags(self, taglist):
"""
Store all the tags found in the headline. The first tag will
also be stored as if the setTag method was called.
"""
for t in taglist:
self.tags[t] = ''
def Todo(self): def Todo(self):
""" """
@@ -361,7 +341,7 @@ class Orgnode(object):
n = n + self.headline n = n + self.headline
n = "%-60s " % n # hack - tags will start in column 62 n = "%-60s " % n # hack - tags will start in column 62
closecolon = '' closecolon = ''
for t in self.tags.keys(): for t in self.tags:
n = n + ':' + t n = n + ':' + t
closecolon = ':' closecolon = ':'
n = n + closecolon n = n + closecolon

View File

@@ -60,7 +60,9 @@ def extract_entries(notesfile, verbose=0):
if not "Body" in note or note["Body"].strip(empty_escape_sequences) == "": if not "Body" in note or note["Body"].strip(empty_escape_sequences) == "":
continue continue
note_string = f'{note["Title"]}\t{note["Tags"] if "Tags" in note else ""}\n{note["Body"] if "Body" in note else ""}' note_string = f'{note["Title"]}' \
f'\t{note["Tags"] if "Tags" in note else ""}' \
f'\n{note["Body"] if "Body" in note else ""}'
entries.append([note_string, note["Raw"]]) entries.append([note_string, note["Raw"]])
# Close File # Close File