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
|
import sys
import os
HERE = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(HERE, '..', '..'))
from alot.commands import COMMANDS
from configobj import ConfigObj
from validate import Validator
import re
NOTE = """
.. CAUTION: THIS FILE IS AUTO-GENERATED
from the inline comments of specfile %s.
If you want to change its content make your changes
to that spec to ensure they woun't be overwritten later.
"""
def rewrite_entries(config, path, specpath, sec=None, sort=False):
file = open(path, 'w')
file.write(NOTE % specpath)
if sec is None:
sec = config
if sort:
sec.scalars.sort()
for entry in sec.scalars:
v = Validator()
#config.validate(v)
#print config[entry]
#etype = re.sub('\(.*\)','', config[entry])
##if etype == 'option':
etype, eargs, ekwargs, default = v._parse_check(sec[entry])
if default is not None:
default = config._quote(default)
if etype == 'gpg_key_hint':
etype = 'string'
description = '\n.. _%s:\n' % entry.replace('_', '-')
description += '\n.. describe:: %s\n\n' % entry
comments = [sec.inline_comments[entry]] + sec.comments[entry]
for c in comments:
if c:
description += ' '*4 + re.sub('^\s*#', '', c)
description = description.rstrip(' ') + '\n'
if etype == 'option':
description += '\n :type: option, one of %s\n' % eargs
else:
if etype == 'force_list':
etype = 'string list'
description += '\n :type: %s\n' % etype
if default is not None:
default = default.replace('*','\\*')
if etype in ['string', 'string_list', 'gpg_key_hint'] and default != 'None':
description += ' :default: "%s"\n\n' % (default)
else:
description += ' :default: %s\n\n' % (default)
file.write(description)
file.close()
if __name__ == "__main__":
specpath = os.path.join(HERE, '..','..', 'alot', 'defaults', 'alot.rc.spec')
config = ConfigObj(None, configspec=specpath, stringify=False, list_values=False)
config.validate(Validator())
alotrc_table_file = os.path.join(HERE, 'configuration', 'alotrc_table')
rewrite_entries(config.configspec, alotrc_table_file, 'defaults/alot.rc.spec', sort=True)
rewrite_entries(config, os.path.join(HERE, 'configuration', 'accounts_table'),
'defaults/alot.rc.spec',
sec=config.configspec['accounts']['__many__'])
|