@@ -43,21 +43,21 @@ def do_t(self, line):
43
43
"""t <template> <text> - test selection text"""
44
44
template_id , criteria = line .split (' ' , 1 )
45
45
t = self ._load_template (template_id )
46
- criteria = parse_criteria (criteria )
46
+ criteria = self . _parse_criteria (criteria )
47
47
tm = TemplateMaker (t )
48
48
selection = apply_criteria (criteria , tm )
49
49
for n , i in enumerate (selection ):
50
50
print "[%d] %r" % (n , remove_annotation (tm .selected_data (i )))
51
51
52
52
def do_a (self , line ):
53
53
"""a <template> <data> [-n number] [-f field]- add or test annotation
54
-
54
+
55
55
Add a new annotation (if -f is passed) or test what would be annotated
56
56
otherwise
57
57
"""
58
58
template_id , criteria = line .split (' ' , 1 )
59
59
t = self ._load_template (template_id )
60
- criteria = parse_criteria (criteria )
60
+ criteria = self . _parse_criteria (criteria )
61
61
tm = TemplateMaker (t )
62
62
selection = apply_criteria (criteria , tm )
63
63
if criteria .field :
@@ -78,7 +78,7 @@ def do_al(self, template_id):
78
78
t = self ._load_template (template_id )
79
79
tm = TemplateMaker (t )
80
80
for n , (a , i ) in enumerate (tm .annotations ()):
81
- print "[%s-%d] (%s) %r" % (template_id , n , a ['annotations' ]['content' ],
81
+ print "[%s-%d] (%s) %r" % (template_id , n , a ['annotations' ]['content' ],
82
82
remove_annotation (tm .selected_data (i )))
83
83
84
84
def do_s (self , url ):
@@ -126,21 +126,24 @@ def _save_templates(self, templates):
126
126
with open (self .filename , 'w' ) as f :
127
127
templates = [page_to_dict (t ) for t in templates ]
128
128
return json .dump ({'templates' : templates }, f )
129
-
129
+
130
+ def _parse_criteria (self , criteria_str ):
131
+ """Parse the given criteria string and returns a criteria object"""
132
+ p = optparse .OptionParser ()
133
+ p .add_option ('-f' , '--field' , help = 'field to annotate' )
134
+ p .add_option ('-n' , '--number' , type = "int" , help = 'number of result to select' )
135
+ o , a = p .parse_args (shlex .split (criteria_str ))
136
+
137
+ encoding = getattr (self .stdin , 'encoding' , None ) or sys .stdin .encoding
138
+ o .text = ' ' .join (a ).decode (encoding or 'ascii' )
139
+ return o
140
+
141
+
130
142
def parse_at (ta_line ):
131
143
p = optparse .OptionParser ()
132
144
p .add_option ('-e' , '--encoding' , help = 'page encoding' )
133
145
return p .parse_args (shlex .split (ta_line ))
134
146
135
- def parse_criteria (criteria_str ):
136
- """Parse the given criteria string and returns a criteria object"""
137
- p = optparse .OptionParser ()
138
- p .add_option ('-f' , '--field' , help = 'field to annotate' )
139
- p .add_option ('-n' , '--number' , type = "int" , help = 'number of result to select' )
140
- o , a = p .parse_args (shlex .split (criteria_str ))
141
- o .text = ' ' .join (a )
142
- return o
143
-
144
147
def apply_criteria (criteria , tm ):
145
148
"""Apply the given criteria object to the given template"""
146
149
func = best_match (criteria .text ) if criteria .text else lambda x , y : False
0 commit comments