-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.py
170 lines (138 loc) · 4.38 KB
/
cli.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
"""
Main module: starts CLI version of Jd2chm.
"""
import sys
import os
import getopt
import re
import log as log_
import const
import core
import console
logging = log_.get_logging()
log = log_.get_logger()
@console.style(console.default().foreground, console.BRIGHT)
def usage():
"""Display Usage."""
print(const.USAGE)
@console.style(console.FOREGROUND_YELLOW)
def lic():
"""Display License."""
console.print_center_block(const.MIT_LICENSE, 70)
@console.style(console.FOREGROUND_GREEN)
def thanks():
"""Display thank you message."""
console.print_center_block(const.MSG_THANKS)
@console.style(console.FOREGROUND_GREEN)
def welcome():
"""Display welcome message."""
console.print_center_block(const.MSG_WELCOME % const.VERSION)
def get_title(index_html):
"""Extracts the default title from the root Javadoc index.html file."""
log.debug(os.path.abspath(index_html))
title = 'Javadoc Title'
re_title = re.compile(r'<title>\s*([^<]*)\s*</title>')
fo = open(index_html)
data = fo.read()
fo.close()
match = re_title.search(data)
if match:
title = match.group(1)
title = title.strip()
return title
def get_project_name(title):
"""Generate default title from the title."""
name = title.lower()
name = name.replace(' ', '_')
name = name.replace('.', '-')
return name
def get_index_html(javadoc_dir):
"""Checks and returns the index.html path if found. Otherwise returns None."""
if not os.path.exists(javadoc_dir):
console.print_error(const.NOT_DIR_MESSAGE.format(javadoc_dir))
return None
index_html = os.path.join(javadoc_dir, const.INDEX_HTML)
if not os.path.exists(index_html):
console.print_error(const.NOT_JAVADOC_DIR_MESSAGE.format(javadoc_dir, index_html))
return None
return index_html
def main(args):
welcome()
# Arguments processing
try:
opts, args = getopt.getopt(args, "hclvp:o:t:")
except getopt.GetoptError:
usage()
sys.exit(2)
project_name = None
project_title = None
start_dir = os.getcwd()
javadoc_dir = '.'
for o, a in opts:
if o == "-h":
usage()
sys.exit()
if o == "-c": # Check HHC compiler
env = core.ChmEnv()
hhc = env.get_html_compiler_path()
log.info("HTML Help Compiler installed and found: %s" % hhc)
sys.exit()
if o == "-l":
# Shows license
lic()
sys.exit()
if o == "-o":
# output: example 'beanshell' will give 'beanshell.chm'
project_name = a
if o == "-t":
# Title that will show up as the title of the CHM Window
project_title = a
if o == "-p":
# Path containing a Javadoc documentation (there should be an index.html in that path)
javadoc_dir = a
if o == "-v":
# verbose (debug = level)
logging.set_level(logging.DEBUG)
log.debug(args)
index_html = get_index_html(javadoc_dir)
if not index_html:
sys.exit(1)
if not project_title:
try:
title = get_title(index_html)
print("The title will be assign to the CHM window")
project_title = input("Enter the project title [%s]: " % title)
if not project_title:
project_title = title
except KeyboardInterrupt:
print()
print('Bye!')
sys.exit()
if not project_name:
try:
name = get_project_name(project_title)
project_name = input("Enter the project name [%s]: " % name)
if not project_name:
project_name = name
except KeyboardInterrupt:
print()
print('Bye!')
sys.exit()
# End arguments processing
log.info("Starts building the project")
log.info("Project: %s" % project_name)
log.info("Title: %s" % project_title)
log.debug("javadoc_dir: {}".format(javadoc_dir))
os.chdir(javadoc_dir)
# Prepare Environment
env = core.ChmEnv()
env.prepare_env(project_name, javadoc_dir)
# Create HTML help files
project = core.ChmProject()
project.create_project(project_name, project_title)
# Generate the CHM and clean-up
env.make()
# End
log.info('Compilation completed')
os.chdir(start_dir)
thanks()