Skip to content

Commit 566155e

Browse files
author
George K. Thiruvathukal
committed
fix for --filename (a non-zettel field that is only kept in the database)
1 parent dce952d commit 566155e

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

zettelgeist/zettel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def get_yaml(self, restrict_to_fields=ZettelFieldsOrdered):
407407
continue
408408
if key in ZettelStringFields:
409409
yaml_zettel[key] = literal(self.zettel[key])
410-
elif key not in ZettelExtraFields:
410+
elif key != 'document': # Only field not allowed is Markdown document
411411
try:
412412
yaml_zettel[key] = self.zettel[key].copy()
413413
except:

zettelgeist/zfind.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import argparse
33
from time import strftime
4+
import yaml
45

56
from . import zdb, zettel, zquery
67
from .zutils import *
@@ -50,6 +51,10 @@ def main():
5051
argsd = vars(args)
5152

5253
db = zdb.get(args.database)
54+
55+
# The --get-all-tags and --get-all-mentions stop processing of all other tags
56+
# TODO: Make each group of functions a function to make this more comprehensible.
57+
5358
if args.get_all_tags:
5459
print('\n'.join(db.get_tags_list()))
5560
exit(0)
@@ -58,6 +63,8 @@ def main():
5863
print('\n'.join(db.get_mentions_list()))
5964
exit(0)
6065

66+
# The --query-{prompt,file,string} options are for ZQL processing.
67+
6168
if args.query_prompt:
6269
input_line = input("zfind> ")
6370
elif args.query_file:
@@ -76,6 +83,8 @@ def main():
7683
print("Warning: Query could not be loaded via --query, --query-file, or --query-prompt")
7784
sys.exit(1)
7885

86+
# This is the actual logic to find and process results.
87+
7988
search_count = 0
8089
match_filenames = []
8190
for row in gen:
@@ -86,51 +95,53 @@ def main():
8695
zettels = loader.getZettels()
8796
z = next(zettels)
8897

89-
# TODO: This is not good. I think we should only output YAML now that we have Markdown.
90-
this_result_output = [YAML_HEADER]
98+
# Handle output of YAML here
99+
this_result_output = []
91100
for field in row.keys():
92101
show_field = "show_" + field
93-
if argsd.get(show_field, None):
102+
if show_field == 'show_filename':
103+
filename_yaml = yaml.dump({ 'filename' : row['filename']})
104+
this_result_output.append(filename_yaml.rstrip())
105+
elif show_field == 'show_document':
106+
continue
107+
elif argsd.get(show_field, None):
94108
if row[field]:
95109
if z:
96-
# TODO: Think more carefully about special fields.
97-
# Need to refactor this code to make it cleaner
98-
if field == 'document':
99-
continue
100-
if field == 'filename':
101-
this_result_output.append('filename: %s' % row['filename'])
102-
elif field in zettel.ZettelFields:
103-
this_result_output.append(z.get_yaml([field]).rstrip())
110+
this_result_output.append(z.get_yaml([field]).rstrip())
104111
else:
105112
this_result_output.append("%s:" % field)
106113
this_result_output.append(row[field])
107-
108-
109-
this_result_output.append(YAML_HEADER)
110114

111115
# No output if just --- and ---
112-
if len(this_result_output) > 2:
113-
print('\n'.join(this_result_output))
116+
if len(this_result_output) > 0:
117+
print('\n'.join([YAML_HEADER] + this_result_output + [YAML_HEADER]))
114118

115119
if argsd.get("show_document"):
116120
document = row['document']
117121
if len(document) > 0:
118122
print(row['document'])
119123
print()
120124

125+
# --count here / Should this be added to the YAML payload? I think the answer is yes.
126+
121127
if args.count:
122128
print("%d Zettels matched search" % search_count)
123129

130+
# --fileset handled here / Should --fileset actually suppress the --show options?
131+
# It's primary purpose is to *avoid* showing output for subsequent processing, say, in a shell loop
132+
124133
if args.fileset:
125134
if not os.path.exists(args.fileset):
126135
write_to_file(args.fileset, "\n".join(match_filenames), mode="w", newlines=1)
127136
else:
128137
print("Filename %s exists; will not overwrite." % args.fileset)
129138

139+
# --stats seems to duplicate the work of --count. yes, it goes to a file, but that would
140+
# allow for subsequent querying, say, with yq or equivalent.
141+
130142
if args.stats:
131143
if not os.path.exists(args.stats):
132-
doc = {'count': search_count,
133-
'query': input_line.strip()}
144+
doc = {'count': search_count, 'query': input_line.strip()}
134145
write_to_file(args.stats, zettel.dict_as_yaml(doc), mode="w", newlines=1)
135146
else:
136147
print("Filename %s exists; will not overwrite." % args.stats)

0 commit comments

Comments
 (0)