-
Notifications
You must be signed in to change notification settings - Fork 0
/
.updatedoc.py
executable file
·328 lines (270 loc) · 9.98 KB
/
.updatedoc.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
# pass import - Passwords importer swiss army knife
# Copyright (C) 2017-2020 Alexandre PUJOL <alexandre@pujol.io>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script updates the readme file in this repo.
import io
from dominate import tags
from dominate.util import raw
import pass_import
from pass_import.__main__ import ArgParser
from pass_import.core import Cap
MANAGERS = pass_import.Managers()
# Internal tools
# ---------------
def replace(marker_begin, marker_end, string, newcontent):
"""Replace data inside string markers."""
begin = string.find(marker_begin)
end = string.find(marker_end)
return string.replace(string[begin + len(marker_begin):end], newcontent)
class ManagerMeta():
"""Generate curated password managers metadata."""
guide = 'this guide: '
def __init__(self, pm, ext=True, mode='md'):
self.pm = pm
self.man = mode == 'man'
self.markdown = mode == 'md'
self.html = mode == 'html'
self.prog = 'pass import' if ext else 'pimport'
def urlto_markdown(self, string):
"""Markdonize link."""
if self.guide in string:
msg = string.split(self.guide)
url = msg.pop()
string = ''.join(msg) + '[this guide](%s)' % url
return string
def urlto_man(self, string):
"""Remove markdown link from a string."""
if self.guide in string:
string = string.replace(self.guide, 'this guide: \\fI')
string += '\\fP'
return string
def urlto_html(self, string):
"""Convert to html link."""
if self.guide in string:
msg = string.split(self.guide)
url = msg.pop()
string = ''.join(msg) + "<a href=\"%s\">this guide</a>" % url
return string
def genrow(self):
"""Generate the html row."""
tags.td(tags.code(self.format, self.version),
align='center', __pretty=False)
tags.td(tags.i(raw(self.hexport)), align='center', __pretty=False)
tags.td(tags.code(self.himport), align='center', __pretty=False)
@property
def format(self):
"""Get format formated."""
res = ''
if self.pm.format != '':
if self.man:
res = '(%s)' % self.pm.format
else:
res = self.pm.format
return res
@property
def version(self):
"""Get version formated."""
res = ''
if self.pm.version != '':
res = ' v%s' % self.pm.version
return res
@property
def url(self):
"""Get url formated."""
return self.pm.url
@property
def hexport(self):
"""Get export help formated."""
res = ''
if self.markdown:
res = self.urlto_markdown(self.pm.hexport)
elif self.man:
res = self.urlto_man(self.pm.hexport)
elif self.html:
res = self.urlto_html(self.pm.hexport)
if res == '':
res = 'Nothing to do'
return res
@property
def himport(self):
"""Get import help formated."""
res = self.pm.himport
if res == '':
res = '%s %s file.%s' % (self.prog, self.pm.name, self.pm.format)
return res
@property
def usage(self):
"""Get usage formated."""
res = self.pm.usage()
if res != '':
res = '%s\n\n' % res
return res
@property
def description(self):
"""Get description formated."""
return self.pm.description()
# Generator functions
# --------------------
# README.md
def table_importer():
"""Generate the new importer table."""
matrix = MANAGERS.matrix()
with tags.table() as table:
with tags.thead():
tags.th('Password Manager', align="center")
tags.th('Formats', align="center")
tags.th('How to export Data', align="center")
tags.th('Command line', align="center")
with tags.tbody():
for name in sorted(matrix):
size = len(matrix[name])
for idx, pm in enumerate(matrix[name]):
with tags.tr():
mm = ManagerMeta(pm, mode='html')
if idx == 0:
tags.td(tags.a(name, href=mm.url), rowspan=size,
align="center", __pretty=False)
mm.genrow()
return "\n%s\n" % table.render()
def table_exporter():
"""Generate the new exporter table."""
res = (
'| **Exporters Password Manager** | **Format** | **Command line** |\n'
'|:------------------------------:|:----------:|:----------------:|\n'
)
matrix = MANAGERS.matrix(Cap.EXPORT)
for name in sorted(matrix):
for pm in matrix[name]:
mm = ManagerMeta(pm, mode='md')
res += "| [%s](%s) | %s | `pimport %s src [src]` |\n" % (
name, mm.url, mm.format, name)
return "\n%s\n" % res
def usage():
"""Generate the new pass-import usage."""
string = io.StringIO()
parser = ArgParser(True)
parser.print_help(string)
return "\n```\n%s```\n" % string.getvalue()
# Manual pages
def usage_importer(ext=True):
"""Generate the usage for importer."""
res = ''
matrix = MANAGERS.matrix()
for name in sorted(matrix):
for pm in matrix[name]:
mm = ManagerMeta(pm, ext, mode='man')
res += "\n.TP\n\\fB%s %s%s\\fP\nWebsite: \\fI%s\\fP\n\n" % (
name, mm.format, mm.version, mm.url)
res += mm.usage
res += ("Export: %s\n\n"
"Command: %s\n" % (mm.hexport, mm.himport))
return "\n%s" % res
def usage_exporter():
"""Generate the usage for exporter."""
res = ''
matrix = MANAGERS.matrix(Cap.EXPORT)
for name in sorted(matrix):
for pm in matrix[name]:
mm = ManagerMeta(pm, mode='man')
res += "\n.TP\n\\fB%s %s %s\\fP\nWebsite: \\fI%s\\fP\n\n" % (
name, mm.format, mm.version, mm.url)
res += mm.usage
res += "Command: pimport %s src [src]\n" % name
return "\n%s" % res
# Shell Completion
def bash_cmd(var, cap):
"""Re-generate command for bash completion."""
res = "\n\tlocal %s=(" % var
for name in sorted(MANAGERS.names(cap)):
if len(res.split('\n').pop()) + len(name) + 1 < 74:
res += "%s " % name
else:
res += '\n\t\t%s ' % name
return res[:-1] + ')\n\t'
def bash_importer():
"""Re-generate importer command for bash completion."""
return bash_cmd('importers', Cap.IMPORT)
def bash_exporter():
"""Re-generate exporter command for bash completion."""
return bash_cmd('exporters', Cap.EXPORT)
def zsh_cmd(cap):
"""Re-generate command for zsh completion."""
res = "\n\t\tsubcommands=(\n"
matrix = MANAGERS.matrix(cap)
for name in sorted(matrix):
formats = []
capability = 'Importer'
for pm in matrix[name]:
if cap is Cap.EXPORT:
capability = 'Exporter'
frmt = pm.format.upper()
if pm.version:
frmt += ' v%s' % pm.version
formats.append(frmt)
desc = '%s for %s in %s' % (capability, name, ', '.join(formats))
res += "\t\t\t'%s:%s'\n" % (name, desc)
return res + '\t\t)\n\t\t'
def zsh_importer():
"""Re-generate importer command for zsh completion."""
return zsh_cmd(Cap.IMPORT)
def zsh_exporter():
"""Re-generate exporter command for zsh completion."""
return zsh_cmd(Cap.EXPORT)
UPDATE = {
'README.md': [
('<!-- NB BEGIN -->', '<!-- NB END -->', '%d' % len(MANAGERS)),
('<!-- LIST BEGIN -->', '<!-- LIST END -->', table_importer()),
('<!-- LIST DST BEGIN -->', '<!-- LIST DST END -->', table_exporter()),
('<!-- USAGE BEGIN -->', '<!-- USAGE END -->', usage()),
],
'share/man/man1/pass-import.1': [
(r'\# NB BEGIN', r'\# NB END', '\n%d\n' % len(MANAGERS)),
(r'\# LIST BEGIN', r'\# LIST END', usage_importer()),
],
'share/man/man1/pimport.1': [
(r'\# NB BEGIN', r'\# NB END', '\n%d\n' % len(MANAGERS)),
(r'\# NB EXPORT BEGIN', r'\# NB EXPORT END',
'\n%d\n' % len(MANAGERS.names(Cap.EXPORT))),
(r'\# LIST BEGIN', r'\# LIST END', usage_importer()),
(r'\# LIST DST BEGIN', r'\# LIST DST END', usage_exporter()),
],
'share/bash-completion/completions/pass-import': [
('# importers begin', '# importers end', bash_importer()),
],
'share/zsh/site-functions/_pass-import': [
('# importers begin', '# importers end', zsh_importer()),
],
'share/bash-completion/completions/pimport': [
('# importers begin', '# importers end', bash_importer()),
('# exporter begin', '# importers begin', bash_exporter()),
],
'share/zsh/site-functions/_pimport': [
('# importers begin', '# importers end', zsh_importer()),
('# exporter begin', '# exporter end', zsh_exporter()),
],
}
def main():
"""Update the documentation files last usage and manager lists."""
for path, pattern in UPDATE.items():
with open(path, 'r') as file:
data = file.read()
for begin, end, res in pattern:
data = replace(begin, end, data, res)
with open(path, 'w') as file:
file.write(data)
if __name__ == "__main__":
main()