-
Notifications
You must be signed in to change notification settings - Fork 3
/
globals.py
executable file
·256 lines (202 loc) · 7.61 KB
/
globals.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
# -*- coding: utf-8 -*-
"""
Global Variables and global functions
"""
__author__ = "Dulip Withanage"
import json
import os
import sys
import requests
from lxml import etree
from debug import Debuggable, Debug
numeral_map = tuple(zip(
(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
))
class GV(object):
''' Global variables '''
def __init__(self, settings):
# GLOBAL VARIABLES
self.settings = settings
#application paths
self.apps = {'fop': 'fop/fop/fop',
'saxon': 'tools/meTypeset/runtime/saxon9.jar',
'ah': '/usr/AHFormatterV65_64/run.sh',
'xep': '/usr/local/xep/bin/xep/xep'
}
# projects
self.PROJECT_INPUT_FILE_JSON_IS_NOT_VALID = 'project input file json is not valid'
self.PROJECT_INPUT_FILE_TYPE_IS_NOT_SPECIFIED = 'project input file type is not specified'
self.PROJECT_INPUT_FILE_HAS_MORE_THAN_TWO_DOTS = 'project input file has more than two dots'
self.PROJECT_INPUT_FILE_DOES_NOT_EXIST = 'project input_file does not exist'
self.PROJECT_IS_NOT_ACTIVE = 'project is not active'
self.PROJECT_OUTPUT_FILE_IS_NOT_DEFINED = 'project output file is not defined'
self.PROJECT_OUTPUT_FILE_TYPE_IS_NOT_SPECIFIED = 'project output file type is not defined'
self.PROJECT_OUTPUT_FILE_WAS_NOT_CREATED = 'project output file was not created'
self.PROJECT_TYPESETTER_IS_NOT_AVAILABLE = 'project typesetter is not available'
self.PROJECT_TYPESETTER_IS_NOT_SPECIFIED = 'project typesetter is not specified'
self.PROJECT_TYPESETTER_NAME_IS_NOT_SPECIFIED = 'project typesetter name is not specified'
self.PROJECT_TYPESETTER_VAR_IS_NOT_SPECIFIED = 'project typesetter varaible is not specified'
self.PROJECT_TYPESETTERS_ARE_NOT_SPECIFIED = 'project typesetters are not specified'
self.PROJECTS_VAR_IS_NOT_SPECIFIED = 'project variable is not specified'
self.PROJECT_TYPESETTER_PROCESS_METHOD_NOT_SPECIFIED='project typesetter process method not specified'
self.PROJECTS_TYPESETTER_RUNS_WITH_NO_ARGUMENTS = 'projects typesetter runs with no arguments'
# typesetter errors
self.TYPESETTER_ARGUMENTS_NOT_DEFINED = "typesetter arguments not defined"
self.TYPESETTER_EXECUTABLE_VARIABLE_IS_UNDEFINED = 'typesetter executable variable is undefined'
self.TYPESETTER_FILE_OUTPUT_TYPE_IS_UNDEFINED = 'typesetter file output type is undefined'
self.TYPESETTER_METADATA_FILE_WAS_NOT_SPECIFIED = 'Metadata file wasn\'t specified '
self.TYPESETTER_METYPESET_RUNS_WITH_DEFAULT_METADATA_FILE = 'typesetter metypeset runs with default metadata file'
self.TYPESETTER_IS_NOT_SPECIFIED = 'typesetter is not specified '
self.TYPESETTER_PATH_IS_NOT_SPECIFIED = 'typesetter path is not specified '
self.TYPESETTER_BINARY_IS_UNAVAILABLE = 'typesetter binary is unavailable '
self.TYPESETTER_RUNS_WITH_NO_ARGUMENTS = 'typesetter runs with no arguments'
# xml
self.RUNNING_FO_CONVERSION = 'running FO conversion'
self.RUNNING_PDF_CONVERSION = 'running PDF conversion'
self.XML_ELEMENT_NOT_FOUND = 'xml element not found'
self.XML_FILE_NOT_CREATED = 'xml file not created'
self.XML_INPUT_FILE_IS_NOT_FOUND = 'xml input file is not found'
self.XML_INPUT_FILE_IS_NOT_VALID = 'xml input file is not valid'
self.SAXON_IS_NOT_AVAILABLE = 'saxon is not available'
self.FOP_PATH_IS_NOT_AVAILABLE='fop path is not available'
# WORDS
self.OUTPUT = 'Output'
self.debug = Debug()
self.numeral_map = numeral_map
#LOG Object
self.log= []
self.uuid = 'mpt'
self.version = '0.0.1'
@staticmethod
def fatal_error(module, message):
"""
Prints a formatted error message and exits
Parameters
----------
module: python module
Returns the name of the module
message: str
Error message
See Also
--------
module.get_module_name()
"""
print(('[FATAL ERROR] [{0}] {1}'.format(
module.get_module_name(), message)))
sys.exit(1)
def is_json(self, s):
"""
Checks whether a string is valid json string
Parameters
----------
s : str
JSON data as string
Raises
------
ValueError error
Inappropriate json string
"""
try:
return json.loads(s)
except ValueError as e:
return False
return True
def read_json(self, pth):
"""
Reads a json file from system path or exits
Parameters
----------
pth: str
path of the file in the folder structure
Returns
-------
json : json
json object
"""
if os.path.isfile(pth):
with open(pth) as j:
return json.load(j)
else:
try:
r = requests.get(pth, verify=False, stream=True)
if r.status_code==200:
return r.json()
else:
self.debug.print_debug(self, self.PROJECT_INPUT_FILE_JSON_IS_NOT_VALID)
sys.exit(1)
except requests.exceptions.ConnectionError as ce:
self.debug.print_debug(self, str(ce.message))
sys.exit(1)
def create_dirs_recursive(self, pth):
"""
Recursively create directories for a system path or exists if folder exists
Parameters
----------
pth : str
system path to be created
"""
p = ''
for path in pth:
p = p + os.path.sep + path.strip('/').strip('/')
if not os.path.exists(p):
try:
os.makedirs(p)
except OSError as o:
print(o)
sys.exit(1)
return p
def set_numbering_tags(self, tags, tr):
"""
Automatic numbering of the list of elements
Parameters
----------
tags: list
list of elements
Returns
-------
tr : elementtree
"""
for tag in tags:
sh = tr.findall('.//' + tag)
sid = 1
for i in sh:
i.set('id', tag.replace('-', '') + str(sid))
sid += 1
return tr
def check_program(self, p):
"""
Checks whether a the program or typesetter is installed and executable
Parameters
---------
p: str
Program path
Returns
--------
None: bool
Returns None , if program exists
"""
def is_exe(f_path):
"""
Checks whether path is available and executable
Parameters
---------
f_path: str
File path
Returns
--------
boolean: bool
True or False
"""
return os.path.isfile(f_path) and os.access(f_path, os.X_OK)
fpath, fname = os.path.split(p)
if fpath:
if is_exe(p):
return p
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, p)
if is_exe(exe_file):
return exe_file
return None