Skip to content

Commit

Permalink
REFACTOR : Improvements to insert.
Browse files Browse the repository at this point in the history
  • Loading branch information
crdoconnor committed Dec 28, 2023
1 parent c518f5c commit 0dfd356
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
16 changes: 8 additions & 8 deletions hitch/story/in.story
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,36 @@ Insert file:
- orji:
env:
ORJITMP: ./tmp
cmd: in notetemplate.jinja2 above org/simple.org/0/1 --text speech.txt
cmd: in notetemplate.jinja2 above org/simple.org//0/1 --text speech.txt
output: |
Written note successfully
Written note(s) successfully

- file contents:
filename: org/simple.org
contents: |
* TODO New note
This is a note generated by speech to text.
* Top note
** Subnote
** TODO Insert above, under or below, replace this
* TODO New note
This is a note generated by speech to text.
* DONE Done item

Below:
steps:
- orji:
env:
ORJITMP: ./tmp
cmd: in notetemplate.jinja2 below org/simple.org/0/1 --text speech.txt
cmd: in notetemplate.jinja2 below org/simple.org//0/1 --text speech.txt
output: |
Written note successfully
Written note(s) successfully

- file contents:
filename: org/simple.org
contents: |
* TODO New note
This is a note generated by speech to text.
* Top note
** Subnote
** TODO Insert above, under or below, replace this
* TODO New note
This is a note generated by speech to text.
* DONE Done item

20 changes: 7 additions & 13 deletions orji/insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import orgmunge
from .tempdir import TempDir
from .loader import Loader
from .lookup import Lookup


@click.command()
Expand All @@ -20,6 +21,7 @@ def insert(jinjafile, relative, location, textfile):
temp_dir = TempDir()
temp_dir.create()
loader = Loader(temp_dir)
lookup = Lookup(location)
template_text = Path(jinjafile).read_text()
output_text = Template(template_text, jinjafile).render(
text=Path(textfile).read_text()
Expand All @@ -30,23 +32,15 @@ def insert(jinjafile, relative, location, textfile):
todos={"todo_states": {"todo": "TODO"}, "done_states": {"done": "DONE"}},
)

write_file = location.split(".org/")[0] + ".org"
_ = location.split(".org/")[1]

write_parsed = orgmunge.Org(
Path(write_file).read_text(),
from_file=False,
todos={"todo_states": {"todo": "TODO"}, "done_states": {"done": "DONE"}},
)
write_notes = Note(write_parsed.root, loader=loader)
write_note = lookup.load(loader)
if relative == "above":
for note in chunk_to_insert.root.children:
write_notes._node.add_child(note)
write_note._node.add_child(note)
elif relative == "below":
for note in chunk_to_insert.root.children:
write_notes._node.add_child(note)
write_note._node.add_child(note)
else:
raise NotImplementedError("f{relative} not implemented")
write_parsed.write(write_file)
click.echo("Written note successfully")
Path(lookup.filepath).write_text(str(write_note))
click.echo("Written note(s) successfully")
temp_dir.destroy()
2 changes: 1 addition & 1 deletion orji/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def load(self, filepath):
from_file=False,
todos={"todo_states": {"todo": "TODO"}, "done_states": {"done": "DONE"}},
)
return Note(munge_parsed.root, loader=self)
return Note(munge_parsed.root, loader=self, org=munge_parsed)
12 changes: 8 additions & 4 deletions orji/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def paragraphs(self):


class Note:
def __init__(self, node, loader):
def __init__(self, node, loader, org):
self._node = node
self._loader = loader
self._org = org

@property
def name(self):
Expand Down Expand Up @@ -111,18 +112,21 @@ def from_indexlookup(self, indexlookup):
for index in split:
node = node.children[index]

return Note(node, self._loader)
return Note(node, self._loader, self._rg)

def has(self, lookup):
return Lookup(lookup, relative_to=self).exists(loader=self._loader)

def at(self, lookup):
return Lookup(lookup, relative_to=self).load(loader=self._loader)

def __str__(self):
return str(self._org)

@property
def children(self):
return [Note(node, self._loader) for node in self._node.children]
return [Note(node, self._loader, self._org) for node in self._node.children]

def __iter__(self):
for node in self._node.children:
yield Note(node, self._loader)
yield Note(node, self._loader, self._org)
2 changes: 1 addition & 1 deletion orji/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run(orgdir, rundir, out, multiple):
todos={"todo_states": {"todo": "TODO"}, "done_states": {"done": "DONE"}},
)

for note in Note(parsed_munge.root, loader=loader):
for note in Note(parsed_munge.root, loader=loader, org=parsed_munge):
if note.state == "TODO":
for tag in note.tags:
if tag in scripts.keys():
Expand Down

0 comments on commit 0dfd356

Please sign in to comment.