-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmd2rst.py
executable file
·137 lines (116 loc) · 3.98 KB
/
md2rst.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# PYTHON_ARGCOMPLETE_OK
import argparse
import argcomplete
import os
import subprocess
class MdtoRst(object):
"""
This script will search for all the README.md files for the
given path and change it to rst files.'
"""
epilog = '\n'.join([
"Odoo Developer Comunity Tool",
"Development by Katherine Zaoral (github: @zaoral)",
"Coded by Katherine Zaoral <[email protected]>.",
"Source code at git:vauxoo-dev/gist-vauxoo."
])
description = """
This script will search for all the README.md files for the
given path and change it to rst files.
NOTE: This script will edit the principal addond path README.md
so if you want to not please check to remove this change.
"""
def __init__(self):
"""
Initialization of the class.
@return: None
"""
self.args = self.argument_parser()
self.path = self.args['path']
if self.args.get('no_confirm', False):
pass
else:
self.confirm_run(self.args)
return None
def argument_parser(self):
"""
This function create the help command line, manage and filter the
parameters of this script (default values, choices values).
@return dictionary of the arguments.
"""
parser = argparse.ArgumentParser(
prog='md2rst',
description=self.description,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=self.epilog)
parser.add_argument(
'--no-confirm',
action='store_true',
help=('Ask user for confirmation to the user. Default is True'))
parser.add_argument(
'-p', '--path',
metavar='PATH',
type=str,
required=True,
help=('The module Path you want to apply the transformation'))
argcomplete.autocomplete(parser)
return parser.parse_args().__dict__
def confirm_run(self, args):
"""
Manual confirmation before runing the script. Very usefull.
@param args: dictionary of arguments.
@return True or exit the program in the confirm is no.
"""
print'\n... Configuration of Parameters Set'
for (parameter, value) in args.iteritems():
print '%s = %s' % (parameter, value)
question = 'Confirm the run with the above parameters?'
answer = 'The script parameters were confirmed by the user'
self.confirmation(question, answer)
return True
def confirmation(self, question, answer):
"""
Manual confirmation for the user.
@return True or exit the program in the confirmation in negative.
"""
confirm_flag = False
while confirm_flag not in ['y', 'n']:
confirm_flag = raw_input(question + ' [y/n]: ')
if confirm_flag == 'y':
print answer
elif confirm_flag == 'n':
print 'The user cancel the operation'
exit()
else:
print 'The entry is not valid, please enter y or n.'
return True
def run(self):
"""
run the given command in the command line.
@return True
"""
files = []
search_files = ['README.md']
for sfile in search_files:
files += subprocess.Popen(
['find', self.path, '-name', sfile],
stdout=subprocess.PIPE).stdout.readlines()
files.sort()
files = [item.strip() for item in files]
module_list = list(set([
os.path.dirname(fname)
for fname in files
]))
for module in module_list:
print 'Module: ' + module
os.system('cd {path} && git mv README.md README.rst'.format(
path=module))
return True
def main():
obj = MdtoRst()
obj.run()
return True
if __name__ == '__main__':
main()