-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessor.py
executable file
·216 lines (141 loc) · 7.65 KB
/
processor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
import git
from gooey import Gooey, GooeyParser
from tagged_document import TaggedDocument, TagQuery
from source_document import SourceDocument
import logging
from argparse import ArgumentParser
import sys
import os
from source_document import WORKSPACE_REF
SOURCE_FILE_EXTENSIONS = [
"swift",
"txt",
"cs",
"py"
]
class Processor(object):
def __init__(self, source_path, tagged_path, source_extensions=["txt"], tagged_extensions=["swift"], language=None, clean=False, expand_images=False, show_query=False, as_inline_list_items=False):
assert isinstance(source_path, str)
assert isinstance(tagged_path, str)
self.repo = git.Repo(tagged_path)
self.tagged_documents = TaggedDocument.find(self.repo, tagged_extensions)
self.source_documents = SourceDocument.find(source_path, source_extensions)
self.clean = clean
self.language = language
self.expand_images = expand_images
self.show_query = show_query
self.as_inline_list_items = as_inline_list_items
def get_file_contents(self, name):
for root, dirs, files in os.walk(self.repo.working_dir):
for file in files:
if file == name:
path = os.path.join(root, file)
return open(path).read()
logging.error("Failed to find %s", name)
def process(self, dry_run=False, suffix=""):
file_getter = lambda name: self.get_file_contents(name)
for doc in self.source_documents:
assert isinstance(doc, SourceDocument)
rendered_source, dirty = doc.render(
self.tagged_documents,
language=self.language,
clean=self.clean,
file_getter=file_getter,
show_query=self.show_query,
as_inline_list_items=self.as_inline_list_items
)
if dirty:
if dry_run:
logging.info("Would write %s", doc.path)
else:
with open(doc.path + suffix, "w") as output_file:
output_file.write(rendered_source)
logging.info("Writing %s", doc.path)
def extract_snippets(self, extract_dir):
if os.path.isdir(extract_dir) == False:
logging.error("%s is not a directory.", extract_dir)
return
use_file_prefix = len(self.source_documents) > 1
for doc in self.source_documents:
for (count, snippet) in enumerate(doc.snippets):
assert isinstance(snippet, TagQuery)
filename = "{}-{}".format(count, snippet.as_filename)
if use_file_prefix:
filename = "{}_{}".format(doc.filename, filename)
dest_path = os.path.join(extract_dir,filename)
output = doc.render_snippet(snippet, self.tagged_documents)
with open(dest_path, "w") as f:
f.write(output)
def find_overlong_lines(self, limit):
import itertools
all_long_lines = itertools.chain(*[doc[WORKSPACE_REF].lines_over_limit(limit) for doc in self.tagged_documents])
for line in all_long_lines:
logging.info("Line too long: %s:%i (%i > %i)", line.source_name, line.line_number, len(line.text), limit)
def find_multiply_defined_tags(self):
from itertools import combinations
from collections import defaultdict
duplicate_tags = defaultdict(set)
docs_at_head = filter(lambda d: d[WORKSPACE_REF], self.tagged_documents)
tag_groups = {doc.path: doc[WORKSPACE_REF].tags for doc in self.tagged_documents}
for group_pair_keys in combinations(tag_groups, 2):
tags_in_both = tag_groups[group_pair_keys[0]] & tag_groups[group_pair_keys[1]]
for tag in tags_in_both:
duplicate_tags[tag].add(group_pair_keys[0])
duplicate_tags[tag].add(group_pair_keys[1])
logging.debug("\nChecking for multiple tag definitions.")
for tag in duplicate_tags:
doc_list = []
for doc in duplicate_tags[tag]:
doc_list.append(" - {0}\n".format(doc))
logging.warn("Tag '{0}' is defined in multiple files:\n{1}".format(tag, "".join(doc_list)))
referenced_doc = [source_document for source_document in self.source_documents if tag in source_document.tags_used]
ref_list = []
for ref in referenced_doc:
ref_list.append("\t - {0}\n".format(ref.path))
logging.warn("\t'{0}' is used in documents:\n{1}".format(tag, "".join(ref_list)))
@Gooey(
program_name="Snippet Processor",
tabbed_groups=True
)
def main():
options = GooeyParser()
options.add_argument("source_dir", help="Path to the directory containing your book's source text.", widget="DirChooser")
options.add_argument("code_dir", help="Path to the git repo containing source code.", widget="DirChooser")
options.add_argument("-l", "--lang", dest="language", help="Indicate that the source code is in this language when syntax highlighting", default="swift")
options.add_argument("-n", "--dry-run", action="store_true", help="Don't actually modify any files")
options.add_argument("--clean", action="store_true", help="Remove snippets from files, instead of adding their contents to files")
options.add_argument("--length", default=75, help="The maximum length permitted for snippet lines. Lines longer than this will be warned about.")
advanced_options = options.add_argument_group("Advanced Options")
advanced_options.add_argument("--suffix", default="", help="Append this to the file name of written files (default=none)")
advanced_options.add_argument("-x", "--extract-snippets", dest="extract_dir", default=None, help="Render each snippet to a file, and store it in this directory.", widget="DirChooser")
advanced_options.add_argument("-v", "--verbose", action="store_true", help="Verbose logging.")
advanced_options.add_argument("-q", "--show_query", action="store_true", help="Include the query in rendered snippets.")
advanced_options.add_argument("--as_inline_list_items", action="store_true", help="Add a + after the snippet tag, to make the snippets format properly when being used as inline blocks in list items")
#options.add_argument("-i", "--expand-images", action="store_true", help="Expand img: shortcuts (CURRENTLY BROKEN!)")
opts = options.parse_args()
logging.getLogger().setLevel(logging.INFO)
if opts.verbose:
logging.getLogger().setLevel(logging.DEBUG)
processor = Processor(
opts.source_dir,
opts.code_dir,
source_extensions=["txt","asciidoc"],
tagged_extensions=SOURCE_FILE_EXTENSIONS,
language=opts.language,
clean=opts.clean,
show_query=opts.show_query,
as_inline_list_items=opts.as_inline_list_items)
logging.debug("Found %i source files:", len(processor.source_documents))
for doc in processor.source_documents:
logging.debug(" - %s", doc.path)
logging.debug("Found %i code files:", len(processor.tagged_documents))
for doc in processor.tagged_documents:
logging.debug(" - %s", doc.path)
processor.find_multiply_defined_tags()
processor.find_overlong_lines(opts.length)
processor.process(dry_run=opts.dry_run, suffix=opts.suffix)
if opts.extract_dir:
processor.extract_snippets(opts.extract_dir)
if __name__ == '__main__':
main()