-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
155 lines (125 loc) · 4.16 KB
/
helper.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
"""methods.py:
"""
__author__ = "Dilawar Singh"
__copyright__ = "Copyright 2017-, Dilawar Singh"
__version__ = "1.0.0"
__maintainer__ = "Dilawar Singh"
__email__ = "dilawars@ncbs.res.in"
__status__ = "Development"
import sys
import os
import subprocess
import tempfile
delimiter_ = '@@'
def which(file):
for path in os.environ["PATH"].split(os.pathsep):
if os.path.exists(os.path.join(path, file)):
return os.path.join(path, file)
return None
latexFound_ = True
LATEX = which( 'lualatex' ) or which( 'pdflatex' )
if LATEX is None:
latexFound_ = False
def get_default_attribs( listofkeyval, **kwargs ):
''' listofkeyval: list 'key=val' or a ;-separated string e.g
'key1=val1;key2=val2''
'''
default = [ ]
if not isinstance( listofkeyval, list ):
listofkeyval = listofkeyval.split( ';' )
for a in listofkeyval:
if '=' in a:
key, val = a.split( '=', 1 )
if kwargs.get( key, '' ):
val = '%s' % kwargs[ key ]
default.append( '%s=%s' % (key, clean(val) ) )
else:
default.append( clean( a ) )
return ', '.join( default )
def _m( name ):
'''Generate macros '''
global delimiter_
return '%s%s%s' % (delimiter_, name, delimiter_ )
def _sub( name, value, text ):
return text.replace( _m( name ), value )
def clean( s ):
try:
s = s.replace( "\t", "\\t" )
s = s.replace( "\f", "\\f" )
s = s.replace( "\b", "\\b" )
s = s.replace( "\n", "\\n" )
except Exception as e:
print( 'WARN: Failed to cleanup %s of type %s' % (s, type(s)) )
raise e
return s
def keyvalToDict( listofkeyval, attr = { } ):
if not listofkeyval:
return attr
if not isinstance( listofkeyval, list ):
listofkeyval = listofkeyval.split( ';' )
for a in listofkeyval:
if '=' in a:
key, val = a.split( '=', 1 )
else:
key, val = a, ''
attr[ key.strip() ] = clean( val.strip( ) )
return attr
def attachExtraAttrib( listofkeyval, attr ):
keyvalToDict( listofkeyval, attr )
def remove_chars( text, chars ):
for c in chars:
text = text.replace( c, '' )
return text
def is_sequence(arg):
if type( arg ) == str:
return False
return hasattr(arg, "__getitem__") or hasattr(arg, "__iter__")
def merge_dict( dict1, dict2 ):
'''Merge two dictionaries.
'''
combined = dict( )
for k in dict1:
combined[k] = dict1[k]
for k in dict2:
combined[k] = dict2[k]
return combined
def savefile( text, filename ):
# definately save a tex file.
global LATEX
assert filename, "Empty filename"
assert filename, "Empty filename"
dirname = os.path.dirname( filename )
if not dirname:
dirname = '.'
assert dirname, "Could not determine directory of %s" % filename
auxD = tempfile.gettempdir( )
outD = dirname
basename = os.path.basename( filename )
nameWe = '.'.join( basename.split( '.' )[:-1] ) # drop extention.
texfile = os.path.join( dirname, nameWe + '.tex' )
with open( texfile, 'w' ) as f:
f.write( text )
ext = filename.split( '.' )[-1].strip( ).lower( )
# texargs = '-aux-directory=%s -output-directory=%s ' % ( auxD, outD )
# NOTE: Luatex does not have -aux-directory option.
texargs = '-output-directory=%s ' % outD
if ext in [ 'pdf' ]:
if latexFound_:
cmd = "%s -shell-escape %s %s" % (LATEX, texargs, texfile)
print( 'Executing %s' % cmd )
subprocess.call( cmd.split( ) )
else:
print( '[WARN] No lualatex/pdflatex found' )
elif ext in [ 'ps' ]:
if latexFound_:
cmd = "latex --shell-escape %s %s" % (texargs, texfile)
print( 'Executing %s' % cmd )
subprocess.call( cmd.split( ) )
else:
print( '[WARN] No latex found' )
else:
with open( filename, 'w' ) as f:
f.write( text )
## Remove tex-file if user specified name is different.
#if filename != texfile:
# os.unlink( texfile )