/
wizard.py
280 lines (230 loc) · 7.81 KB
/
wizard.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
# -*- coding: utf-8 -*-
#
# Copyright © 2015,2016 Mathieu Duponchelle <mathieu.duponchelle@opencreed.com>
# Copyright © 2015,2016 Collabora Ltd
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>.
"""
This module implements the core hotdoc wizard
"""
import argparse
import os
import sys
try:
from hotdoc.utils.git_interface import GitInterface
HAVE_GIT_INTERFACE = True
except ImportError:
HAVE_GIT_INTERFACE = False
from hotdoc.utils.loggable import TERMC
from hotdoc.utils.wizard import (QUICKSTART_HELP, QuickStartArgument,
QuickStartWizard, Skip)
HOTDOC_ASCII =\
r"""
/** __ __ ____ ______ ____ ____ ______
* / / / / / __ \ /_ __/ / __ \ / __ \ / ____/
* / /_/ / / / / / / / / / / / / / / / / /
* / __ / / /_/ / / / / /_/ / / /_/ / / /___
* /_/ /_/ \____/ /_/ /_____/ \____/ \____/
*
* The Tastiest Documentation Tool.
*/
"""
PROMPT_GIT_REPO =\
"""
If you use git, I can commit the files created or
modified during setup, you will be prompted for confirmation
when that is the case.
The author name and mail address will be 'hotdoc' and
'hotdoc@hotdoc.net'.
Note: here as everywhere else, you can answer None to
skip the question.
"""
PROMPT_ROOT_INDEX =\
"""
I can now create a root index to tie all your sub indexes
together, do you wish me to do that [y,n]? """
# pylint: disable=unused-argument
def validate_git_repo(wizard, path):
"""
Banana banana
"""
try:
GitInterface(path)
return True
# pylint: disable=broad-except
except Exception:
print "This does not look like a git repo : %s" % path
return False
class HotdocWizard(QuickStartWizard):
"""
Banana banana
"""
# pylint: disable=no-member
# pylint: disable=too-many-instance-attributes
def __init__(self, *args, **kwargs):
conf_path = kwargs.pop('conf_path', '')
QuickStartWizard.__init__(self, *args, **kwargs)
if self.parent == self:
self.doc_database = self
self.comments = {}
self.symbols = {}
self.term_controller = TERMC
if HAVE_GIT_INTERFACE:
self.git_interface = GitInterface()
else:
self.git_interface = None
self.config = {}
self.conf_path = conf_path
else:
self.comments = self.parent.comments
self.symbols = self.parent.symbols
self.doc_database = self.parent.doc_database
self.term_controller = self.parent.term_controller
self.git_interface = self.parent.git_interface
self.conf_path = self.parent.conf_path
self.tag_validators = {}
def add_comment(self, comment):
"""
Banana banana
"""
self.comments[comment.name] = comment
def get_comment(self, name):
"""
Banana banana
"""
return self.comments.get(name)
def get_or_create_symbol(self, type_, **kwargs):
"""
Banana banana
"""
unique_name = kwargs.get('unique_name')
if not unique_name:
unique_name = kwargs.get('display_name')
kwargs['unique_name'] = unique_name
filename = kwargs.get('filename')
if filename:
kwargs['filename'] = os.path.abspath(filename)
symbol = type_()
for key, value in kwargs.items():
setattr(symbol, key, value)
self.symbols[unique_name] = symbol
return symbol
def resolve_config_path(self, path):
"""
Banana banana
"""
if path is None:
return path
res = os.path.join(self.conf_path, path)
return os.path.abspath(res)
def before_prompt(self):
"""
Banana banana
"""
self.__clear_screen()
# pylint: disable=no-self-use
def get_index_path(self):
"""
Banana banana
"""
return None
# pylint: disable=no-self-use
def get_index_name(self):
"""
Banana banana
"""
return None
def __create_root_index(self):
contents = 'Welcome to our documentation!\n'
for obj in self._qs_objects:
if isinstance(obj, HotdocWizard):
index_path = obj.get_index_path()
index_name = obj.get_index_name()
if index_path:
contents += '\n#### [%s](%s)\n' % (index_name, index_path)
path = self.prompt_key(
'index_path',
prompt='Path to save the created index in',
store=False,
validate_function=QuickStartWizard.validate_folder)
path = os.path.join(path, 'index.markdown')
with open(path, 'w') as _:
_.write(contents)
self.config['index'] = path
def before_quick_start(self, obj):
"""
Banana banana
"""
# pylint: disable=unidiomatic-typecheck
if type(obj) == QuickStartArgument and obj.argument.dest == 'index':
if self.config.get('index'):
return
self.before_prompt()
if self.ask_confirmation(PROMPT_ROOT_INDEX):
self.__create_root_index()
raise Skip
def main_prompt(self):
"""
Banana banana
"""
prompt = self.term_controller.BOLD +\
"\nHotdoc started without arguments, starting setup\n" +\
self.term_controller.NORMAL
prompt += self.term_controller.CYAN + \
QUICKSTART_HELP + self.term_controller.NORMAL
prompt += '\nPress Enter to start setup '
if not self.wait_for_continue(prompt):
return False
if not HAVE_GIT_INTERFACE:
return True
repo_path = self.prompt_key(
'git_repo', prompt=">>> Path to the git repo ? ",
title="the path to the root of the git repository",
extra_prompt=PROMPT_GIT_REPO,
validate_function=validate_git_repo,
finalize_function=HotdocWizard.finalize_path)
self.git_interface.set_repo_path(self.resolve_config_path(repo_path))
return True
@staticmethod
def finalize_path(wizard, path):
"""
Banana banana
"""
if not path:
return path
return os.path.relpath(path, wizard.conf_path)
@staticmethod
def finalize_paths(wizard, paths):
"""
Banana banana
"""
if not paths:
return paths
res = []
for path in paths:
res.append(os.path.relpath(path, wizard.conf_path))
return res
def __clear_screen(self):
sys.stdout.write(self.term_controller.CLEAR_SCREEN)
sys.stdout.write(self.term_controller.RED +
self.term_controller.BOLD + HOTDOC_ASCII +
self.term_controller.NORMAL)
def _add_argument_override(self, group, *args, **kwargs):
set_default = kwargs.get('default')
kwargs['default'] = argparse.SUPPRESS
res = QuickStartWizard._add_argument_override(
self, group, *args, **kwargs)
if set_default:
self.config[res.dest] = set_default
return res