Source code for schrodinger.application.jaguar.keywordDB

import xml.etree.ElementTree as ElementTree

keyword_types = {
    "IKEY": "integer",
    "RKEY": "real",
    "SKEY": "string",
    "IAKEY": "indexed_integer",
    "RAKEY": "indexed_real",
    "SAKEY": "indexed_string"
}

#---------------------------------------------------------------------------


[docs]class Keyword(object): """ A class to hold keyword info. """
[docs] def __init__(self, name, type, default, description=None, comment=None, index=0, boolean=None, qsite_allowed=None): self.name = name self.type = type self.default = default self.description = description self.comment = comment self.index = index self.settings = [] self.mopac = {} self.mopac["boolean"] = boolean self.mopac["qsite_allowed"] = qsite_allowed self.type_code = {} for code, type in keyword_types.items(): self.type_code[type] = code
def __str__(self): return "%s (type = %s, default = %s)" % (self.name, self.type, self.default) def __repr__(self): rep = "keyword('%s', '%s', '%s'" % (self.name, self.type, self.default) if self.description is None: return rep + ")" else: return "%s, '%s')" % (rep, self.description)
[docs] def get_macro(self): """ Return C-style macro string from Keyword attributes """ tmp = "MMJAG" + "_" + self.type_code[ self.type] + "_" + self.name.upper() return tmp
#---------------------------------------------------------------------------
[docs]class Setting(object): """ A class to hold keyword setting info. """
[docs] def __init__(self, name, code, value, description=None, comment=None): self.name = name self.code = code self.value = value self.description = description self.comment = comment
def __str__(self): return "%s [%s]" % (self.code, self.value) def __repr__(self): rep = "setting('%s', '%s'" % (self.code, self.value) if self.description is None: return rep + ")" else: return "%s, '%s')" % (rep, self.description)
[docs] def get_macro(self): """ Return C-style macro string from Setting attributes """ tmp = "MMJAG" + "_" + self.name.upper() + "_" + self.code return tmp
#---------------------------------------------------------------------------
[docs]class DTDTreeBuilder(ElementTree.TreeBuilder): """ We create this subclass and redefine doctype() to avoid: "DeprecationWarning: This method of XMLParser is deprecated. Define doctype() method on the TreeBuilder target" """
[docs] def doctype(self, name, pubid, system): pass
[docs]def load_keywords(filename): """ Load keywords from an XML file in jaguar_keywords.dtd format. Return a list and dictionary of Keyword objects. Keyword names and values are stored in lowercase. """ parser = ElementTree.XMLParser(encoding="utf-8", target=DTDTreeBuilder()) xml = ElementTree.parse(filename, parser=parser) root = xml.getroot() index = 0 jaguar_keywords_dict = {} jaguar_keywords_list = [] # Parse XML keyword attributes for child in list(root): name = child.find("name") if name is not None: name = name.text.strip().lower() else: raise TypeError("All keywords require a name") type = child.find("type") if type is not None: type = type.text.strip() else: raise TypeError("All keywords require a type") default = child.find("default") if default is not None: default = default.text.strip() else: raise TypeError("All keywords require a default") description = child.find("description") if description is not None: description = description.text.strip() comment = child.find("comment") if comment is not None: comment = comment.text.strip() # MOPAC only boolean = child.find("boolean") if boolean is not None: boolean = boolean.text.strip() # MOPAC only qsite_allowed = child.find("qsite_allowed") if qsite_allowed is not None: qsite_allowed = qsite_allowed.text.strip() kw = Keyword(name, type, default, description, comment, index, boolean, qsite_allowed) index += 1 # Parse XML keyword settings attributes for node in child.findall("setting"): code = node.find("code") if code is not None: code = code.text.strip() else: raise TypeError("All keyword settings require a code") value = node.find("value") if value is not None: value = value.text.strip().lower() else: raise TypeError("All keyword settings require a value") description = node.find("description") if description is not None: description = description.text.strip() comment = node.find("comment") if comment is not None: comment = comment.text.strip() kw.settings.append(Setting(name, code, value, description, comment)) jaguar_keywords_list.append(kw) jaguar_keywords_dict[kw.name] = kw return jaguar_keywords_list, jaguar_keywords_dict