1
1
import sys
2
2
import argparse
3
3
from time import strftime
4
+ import yaml
4
5
5
6
from . import zdb , zettel , zquery
6
7
from .zutils import *
@@ -50,6 +51,10 @@ def main():
50
51
argsd = vars (args )
51
52
52
53
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
+
53
58
if args .get_all_tags :
54
59
print ('\n ' .join (db .get_tags_list ()))
55
60
exit (0 )
@@ -58,6 +63,8 @@ def main():
58
63
print ('\n ' .join (db .get_mentions_list ()))
59
64
exit (0 )
60
65
66
+ # The --query-{prompt,file,string} options are for ZQL processing.
67
+
61
68
if args .query_prompt :
62
69
input_line = input ("zfind> " )
63
70
elif args .query_file :
@@ -76,6 +83,8 @@ def main():
76
83
print ("Warning: Query could not be loaded via --query, --query-file, or --query-prompt" )
77
84
sys .exit (1 )
78
85
86
+ # This is the actual logic to find and process results.
87
+
79
88
search_count = 0
80
89
match_filenames = []
81
90
for row in gen :
@@ -86,51 +95,53 @@ def main():
86
95
zettels = loader .getZettels ()
87
96
z = next (zettels )
88
97
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 = []
91
100
for field in row .keys ():
92
101
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 ):
94
108
if row [field ]:
95
109
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 ())
104
111
else :
105
112
this_result_output .append ("%s:" % field )
106
113
this_result_output .append (row [field ])
107
-
108
-
109
- this_result_output .append (YAML_HEADER )
110
114
111
115
# 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 ] ))
114
118
115
119
if argsd .get ("show_document" ):
116
120
document = row ['document' ]
117
121
if len (document ) > 0 :
118
122
print (row ['document' ])
119
123
print ()
120
124
125
+ # --count here / Should this be added to the YAML payload? I think the answer is yes.
126
+
121
127
if args .count :
122
128
print ("%d Zettels matched search" % search_count )
123
129
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
+
124
133
if args .fileset :
125
134
if not os .path .exists (args .fileset ):
126
135
write_to_file (args .fileset , "\n " .join (match_filenames ), mode = "w" , newlines = 1 )
127
136
else :
128
137
print ("Filename %s exists; will not overwrite." % args .fileset )
129
138
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
+
130
142
if args .stats :
131
143
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 ()}
134
145
write_to_file (args .stats , zettel .dict_as_yaml (doc ), mode = "w" , newlines = 1 )
135
146
else :
136
147
print ("Filename %s exists; will not overwrite." % args .stats )
0 commit comments