forked from SINGROUP/pycp2k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputparser.py
354 lines (304 loc) · 14 KB
/
inputparser.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Provides functions for creating a python classes from the cp2k_input.xml
file.
"""
import xml.etree.cElementTree as cElementTree
import utilities
import textwrap
#===============================================================================
def validify_section(string):
"""Modifies the section name so that it can be used as a valid attribute
name in a python class.
"""
original = string
changed = False
if "-" in string:
changed = True
string = string.replace("-", "_")
if "+" in string:
changed = True
string = string.replace("+", "PLUS")
if string[0].isdigit():
changed = True
string = "NUM" + string
if changed:
print(" Section {} replaced with {}".format(original, string))
return string
#===============================================================================
def validify_keyword(string):
"""Modifies the keyword name so that it can be used as a valid attribute
name in a python class.
"""
original = string
changed = False
if "-" in string:
changed = True
string = string.replace("-", "_")
if "+" in string:
changed = True
string = string.replace("+", "PLUS")
if string[0].isdigit():
changed = True
string = "NUM" + string
if changed:
print(" Keyword {} replaced with {}".format(original, string))
string = string.capitalize()
return string
#===============================================================================
def create_docstring(item):
description = item.find("DESCRIPTION")
default_value = item.find("DEFAULT_VALUE")
default_unit = item.find("DEFAULT_UNIT")
# Description
output = " \"\"\"\n"
if description is not None:
if description.text is not None:
for line in textwrap.wrap(description.text):
output += " " + line + "\n"
# If the values are enumerated, document the possible values
data_type = item.find("DATA_TYPE")
if data_type.get("kind") == "keyword":
output += "\n Available values:\n"
enumerations = data_type.find("ENUMERATION")
for enum in enumerations.findall("ITEM"):
output += " " + enum.find("NAME").text + "\n"
enum_description = enum.find("DESCRIPTION").text
if enum_description is not None:
for line in textwrap.wrap(enum_description):
output += " " + line + "\n"
# Default value
if default_value is not None:
if default_value.text is not None:
output += "\n Default value:\n"
for line in textwrap.wrap(default_value.text):
output += " " + line + "\n"
# Default unit
if default_unit is not None:
output += "\n Default unit:\n"
if default_unit.text is not None:
for line in textwrap.wrap(default_unit.text):
output += " " + line + "\n"
output += " \"\"\"\n"
return output
#===============================================================================
def recursive_class_creation(section, level, class_dictionary, version_dictionary):
"""Recursively goes throught the .xml file created by cp2k --xml command
and creates a python class for each section. Keywords, default keywords,
section parameters and subsections are stored as attributes.
"""
default_keywords = {}
repeated_default_keywords = {}
keywords = {}
repeated_keywords = {}
subsections = {}
repeated_subsections = {}
repeated_aliases = {}
aliases = {}
attributes = []
inp_name = ""
# Initial string for each section of the class
imports = ["from pycp2k.inputsection import InputSection"]
docstring = ""
properties = ""
setters = ""
public = (
" def __init__(self):\n"
" InputSection.__init__(self)\n"
)
private = ""
class_subsections = ""
functions = "\n"
# The root with tag CP2K_INPUT doesn't have a name. It is hardcoded here.
sectionname = section.find("NAME")
if sectionname is None:
class_name = "_CP2K_INPUT"
inp_name = "CP2K_INPUT"
else:
class_name = sectionname.text
inp_name = class_name
class_name = validify_section(class_name)
class_name = "_" + class_name.lower()
# Start writing class body
section_description = section.find("DESCRIPTION")
# if section_description is not None and section_description.text is not None:
# docstring += " \"\"\"\n"
# for line in textwrap.wrap(section_description.text):
# docstring += " " + line + "\n"
# docstring += " \"\"\"\n"
# else:
# docstring += " \"\"\"\"\"\"\n"
#---------------------------------------------------------------------------
# Create attribute for section parameter
section_parameters = section.find("SECTION_PARAMETERS")
if section_parameters is not None:
attributes.append("Section_parameters")
public += " self.Section_parameters = None\n"
# Write the description for the section parameter
# public += create_docstring(section_parameters)
#---------------------------------------------------------------------------
# Create attribute for all the keywords
for keyword in section.findall("KEYWORD"):
# First find out the default name and whether the attribute is visible or not
default_name = ""
visible = True
for keyname in keyword.findall("NAME"):
keytype = keyname.get("type")
name = keyname.text
newname = validify_keyword(name)
if keytype == "default":
default_name = newname
if name.startswith("__"):
visible = False
# Now store the keywords as class attributes
if visible:
for keyname in keyword.findall("NAME"):
name = keyname.text
newname = validify_keyword(name)
# Create original attribute for the default keyname
if newname == default_name:
# Special case for repeateable keywords.
if keyword.get("repeats") == "yes":
public += " self." + newname + " = []\n"
# public += create_docstring(keyword)
repeated_keywords[newname] = name
else:
public += " self." + newname + " = None\n"
# public += create_docstring(keyword)
keywords[newname] = name
# Create properties for aliases
else:
if keyword.get("repeats") == "yes":
public += " self." + newname + " = self." + default_name + "\n"
repeated_aliases[newname] = default_name
else:
aliases[newname] = default_name
properties += ("\n @property\n"
" def " + newname + "(self):\n"
" \"\"\"\n"
" See documentation for " + default_name + "\n"
" \"\"\"\n"
" return self." + default_name + "\n")
setters += ("\n @" + newname + ".setter\n"
" def " + newname + "(self, value):\n"
" self." + default_name + " = value\n")
#---------------------------------------------------------------------------
# Create a class attribute for all DEFAULT_KEYWORDS
default_keyword = section.find("DEFAULT_KEYWORD")
if default_keyword is not None:
attributes.append("Default_keyword")
# Special case for repeateable default_keywords. Create a dictionary of the
# keyword and add a function for creating them.
name = default_keyword.find("NAME").text
newname = validify_keyword(name)
if default_keyword.get("repeats") == "yes":
public += " self." + newname + " = []\n"
# public += create_docstring(default_keyword)
repeated_default_keywords[newname] = name
else:
public += " self." + newname + " = None\n"
# public += create_docstring(default_keyword)
default_keywords[newname] = name
#---------------------------------------------------------------------------
# Create attribute for each subsection
for subsection in section.findall("SECTION"):
member_class_name = recursive_class_creation(subsection, level+1, class_dictionary, version_dictionary)
member_name = subsection.find("NAME").text
member_name = member_name.replace("-", "_")
member_name = member_name.replace("+", "PLUS")
if member_name[0].isdigit():
member_name = "_" + member_name
imports.append("from .{0} import {0}".format(member_class_name))
# Special case for repeateable sections. Create a dictionary of the
# subsections and add a function for creating them.
if subsection.get("repeats") == "yes":
class_subsections += " self." + member_name + "_list = []\n"
repeated_subsections[member_name] = member_class_name
attributes.append(member_name + "_list")
else:
class_subsections += " self." + member_name + " = " + member_class_name + "()\n"
subsections[member_name] = subsection.find("NAME").text
#---------------------------------------------------------------------------
# Write a list of the stored variable names
private += " self._name = \"" + str(inp_name) + "\"\n"
if len(keywords) != 0:
private += " self._keywords = " + str(keywords) + "\n"
if len(repeated_keywords) != 0:
private += " self._repeated_keywords = " + str(repeated_keywords) + "\n"
if len(default_keywords) != 0:
private += " self._default_keywords = " + str(default_keywords) + "\n"
if len(repeated_default_keywords) != 0:
private += " self._repeated_default_keywords = " + str(repeated_default_keywords) + "\n"
if len(subsections) != 0:
private += " self._subsections = " + str(subsections) + "\n"
if len(repeated_subsections) != 0:
private += " self._repeated_subsections = " + str(repeated_subsections) + "\n"
if len(aliases) != 0:
private += " self._aliases = " + str(aliases) + "\n"
if len(repeated_aliases) != 0:
private += " self._repeated_aliases = " + str(repeated_aliases) + "\n"
if len(attributes) != 0:
private += " self._attributes = " + str(attributes) + "\n"
#---------------------------------------------------------------------------
# Write a function for adding repeateable sections
for repeated in repeated_subsections.items():
attribute_name = repeated[0]
attribute_class_name = repeated[1]
functions += (" def " + attribute_name + "_add(self, section_parameters=None):\n"
" new_section = " + attribute_class_name + "()\n"
" if section_parameters is not None:\n"
" if hasattr(new_section, 'Section_parameters'):\n"
" new_section.Section_parameters = section_parameters\n"
" self." + attribute_name + "_list.append(new_section)\n"
" return new_section\n\n")
#---------------------------------------------------------------------------
# The class names are not unique. Use numbering to identify classes.
exists = False
import_string = "\n".join(imports)
class_string = docstring + public + class_subsections + private + functions + properties + setters
version_number = version_dictionary.get(class_name)
if version_number is None:
version_dictionary[class_name] = 1
class_dictionary[class_name+str(1)] = (import_string, class_string)
return class_name+str(1)
for version in range(version_number):
old_class_body = class_dictionary[class_name+str(version + 1)]
if old_class_body == class_string:
exists = True
version_number = version + 1
break
if not exists:
version_dictionary[class_name] = version_number + 1
class_dictionary[class_name+str(version_number + 1)] = (import_string, class_string)
return class_name + str(version_number + 1)
else:
return class_name + str(version_number)
#===============================================================================
def main(xml_path):
"""Parses the classes and saves them to the package directory as
parsedclasses.py.
"""
# Start parsing here
utilities.print_subtitle("CREATING INPUT STRUCTURE...")
tree = cElementTree.parse(xml_path)
root = tree.getroot()
# Extract the cp2k version and revision
version = root.find("CP2K_VERSION").text
revision = root.find("COMPILE_REVISION").text
class_dictionary = {}
version_dictionary = {}
recursive_class_creation(root, 0, class_dictionary, version_dictionary)
# Put each class into its own module. This produces manu small files, but
# this way it it easier for autocompletion to handle everything
for class_name, class_body in class_dictionary.items():
with open('pycp2k/classes/{}.py'.format(class_name), 'w') as file:
file.write(class_body[0] + "\n\n\n")
class_body_header = (
"class " + class_name + "(InputSection):\n"
)
file.write(class_body_header)
file.write(class_body[1])
return (version, revision)
# Run main function by default
if __name__ == "__main__":
main()