-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathanyeye.py
More file actions
executable file
·293 lines (248 loc) · 7.96 KB
/
anyeye.py
File metadata and controls
executable file
·293 lines (248 loc) · 7.96 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
##############################################################################
#
# diffpy.structure by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""
Anyeye view structure file in atomeye.
Usage: ``anyeye [options] strufile``
Anyeye understands more `Structure` formats than atomeye. It converts `strufile`
to a temporary XCFG file which is opened in atomeye. See supported file formats:
``inputFormats``
Options:
-f, --formula
Override chemical formula in `strufile`. The formula defines
elements in the same order as in `strufile`, e.g., ``Na4Cl4``.
-w, --watch
Watch input file for changes.
--viewer=VIEWER
The structure viewer program, by default "atomeye".
The program will be executed as "VIEWER structurefile".
--formats=FORMATS
Comma-separated list of file formats that are understood
by the VIEWER, by default ``"xcfg,pdb"``. Files of other
formats will be converted to the first listed format.
-h, --help
Display this message and exit.
-V, --version
Show script version and exit.
"""
from __future__ import print_function
import os
import re
import signal
import sys
from diffpy.structure.structureerrors import StructureFormatError
# parameter dictionary
pd = {
"formula": None,
"watch": False,
"viewer": "atomeye",
"formats": ["xcfg", "pdb"],
}
def usage(style=None):
"""Show usage info, for ``style=="brief"`` show only first 2 lines."""
import os.path
myname = os.path.basename(sys.argv[0])
msg = __doc__.replace("anyeye", myname)
if style == "brief":
msg = (
msg.split("\n")[1]
+ "\n"
+ "Try `%s --help' for more information." % myname
)
else:
from diffpy.structure.parsers import inputFormats
fmts = [f for f in inputFormats() if f != "auto"]
msg = msg.replace("inputFormats", " ".join(fmts))
print(msg)
return
def version():
from diffpy.structure import __version__
print("anyeye", __version__)
return
def loadStructureFile(filename, format="auto"):
"""Load structure from specified file.
Parameters
----------
filename : str
Path to the structure file.
format : str, Optional
File format, by default "auto".
Returns
-------
tuple
A tuple of (Structure, fileformat).
"""
from diffpy.structure import Structure
stru = Structure()
p = stru.read(filename, format)
fileformat = p.format
return (stru, fileformat)
def convertStructureFile(pd):
# make temporary directory on the first pass
if "tmpdir" not in pd:
from tempfile import mkdtemp
pd["tmpdir"] = mkdtemp()
strufile = pd["strufile"]
tmpfile = os.path.join(pd["tmpdir"], os.path.basename(strufile))
pd["tmpfile"] = tmpfile
# speed up file processing in the watch mode
fmt = pd.get("format", "auto")
stru = None
if fmt == "auto":
stru, fmt = loadStructureFile(strufile)
pd["fmt"] = fmt
# if fmt is recognized by the viewer, use as is
if fmt in pd["formats"] and pd["formula"] is None:
import shutil
shutil.copyfile(strufile, tmpfile + ".tmp")
os.rename(tmpfile + ".tmp", tmpfile)
return
# otherwise convert to the first recognized viewer format
if stru is None:
stru = loadStructureFile(strufile, fmt)[0]
if pd["formula"]:
formula = pd["formula"]
if len(formula) != len(stru):
emsg = "Formula has %i atoms while structure %i" % (
len(formula),
len(stru),
)
raise RuntimeError(emsg)
for a, el in zip(stru, formula):
a.element = el
elif format == "rawxyz":
for a in stru:
if a.element == "":
a.element = "C"
stru.write(tmpfile + ".tmp", pd["formats"][0])
os.rename(tmpfile + ".tmp", tmpfile)
return
def watchStructureFile(pd):
from time import sleep
strufile = pd["strufile"]
tmpfile = pd["tmpfile"]
while pd["watch"]:
if os.path.getmtime(tmpfile) < os.path.getmtime(strufile):
convertStructureFile(pd)
sleep(1)
return
def cleanUp(pd):
if "tmpfile" in pd:
os.remove(pd["tmpfile"])
del pd["tmpfile"]
if "tmpdir" in pd:
os.rmdir(pd["tmpdir"])
del pd["tmpdir"]
return
def parseFormula(formula):
"""Parse chemical formula and return a list of elements"""
# remove all blanks
formula = re.sub(r"\s", "", formula)
if not re.match("^[A-Z]", formula):
raise RuntimeError("InvalidFormula '%s'" % formula)
elcnt = re.split("([A-Z][a-z]?)", formula)[1:]
ellst = []
try:
for i in range(0, len(elcnt), 2):
el = elcnt[i]
cnt = elcnt[i + 1]
cnt = (cnt == "") and 1 or int(cnt)
ellst.extend(cnt * [el])
except ValueError:
emsg = "Invalid formula, %r is not valid count" % elcnt[i + 1]
raise RuntimeError(emsg)
return ellst
def die(exit_status=0, pd={}):
cleanUp(pd)
sys.exit(exit_status)
def signalHandler(signum, stackframe):
# revert to default handler
signal.signal(signum, signal.SIG_DFL)
if signum == signal.SIGCHLD:
pid, exit_status = os.wait()
exit_status = (exit_status >> 8) + (exit_status & 0x00FF)
die(exit_status, pd)
else:
die(1, pd)
return
def main():
import getopt
# default parameters
pd["watch"] = False
try:
opts, args = getopt.getopt(
sys.argv[1:],
"f:whV",
["formula=", "watch", "viewer=", "formats=", "help", "version"],
)
except getopt.GetoptError as errmsg:
print(errmsg, file=sys.stderr)
die(2)
# process options
for o, a in opts:
if o in ("-f", "--formula"):
try:
pd["formula"] = parseFormula(a)
except RuntimeError as msg:
print(msg, file=sys.stderr)
die(2)
elif o in ("-w", "--watch"):
pd["watch"] = True
elif o == "--viewer":
pd["viewer"] = a
elif o == "--formats":
pd["formats"] = [w.strip() for w in a.split(",")]
elif o in ("-h", "--help"):
usage()
die()
elif o in ("-V", "--version"):
version()
die()
if len(args) < 1:
usage("brief")
die()
elif len(args) > 1:
print("too many structure files", file=sys.stderr)
die(2)
pd["strufile"] = args[0]
# trap the following signals
signal.signal(signal.SIGHUP, signalHandler)
signal.signal(signal.SIGQUIT, signalHandler)
signal.signal(signal.SIGSEGV, signalHandler)
signal.signal(signal.SIGTERM, signalHandler)
signal.signal(signal.SIGINT, signalHandler)
env = os.environ.copy()
if os.path.basename(pd["viewer"]).startswith("atomeye"):
env["XLIB_SKIP_ARGB_VISUALS"] = "1"
# try to run the thing:
try:
convertStructureFile(pd)
spawnargs = (pd["viewer"], pd["viewer"], pd["tmpfile"], env)
# load strufile in atomeye
if pd["watch"]:
signal.signal(signal.SIGCHLD, signalHandler)
os.spawnlpe(os.P_NOWAIT, *spawnargs)
watchStructureFile(pd)
else:
status = os.spawnlpe(os.P_WAIT, *spawnargs)
die(status, pd)
except IOError as e:
print("%s: %s" % (args[0], e.strerror), file=sys.stderr)
die(1, pd)
except StructureFormatError as e:
print("%s: %s" % (args[0], e), file=sys.stderr)
die(1, pd)
return
if __name__ == "__main__":
main()