Commit 47485eb9 authored by Berk Onat's avatar Berk Onat

Adding GromosTopoReader to MDDataAccess with updates at SmartParser

parent 8fd7c2a5
This diff is collapsed.
......@@ -299,6 +299,188 @@ def getList_MetaStrInDict(sourceDict):
"""
return [sourceDict[item].matchStr for item in sourceDict]
def copyMetaDictToDict(sourceObj, sourceDict, sourceType=None, targetObj=None,
targetDict=None, targetType=None, copyDict=None):
""" Copy values of a source dictionary to a target one
by matching the keys in bith dictionaries.
Dictionary types can be python or SmartParser
Inputs:
sourceObj (object) : python object that holds sourceDict
sourceDict (string) : the name of the source dictionary
sourceType (string) : type can be 'standard' or 'smartparser'
targetObj (object) : python object that holds targetDict
if None, sourceObj will be used
targetDict (string) : the name of the target dictionary
if None, sourceDict will be used
targetType (string) : type can be 'standard' or 'smartparser'
if targetDict is None, this equals to sourceType
copyDict (dict) : includes items for mapping
keys from source to target
Ex. : {"ener0" : "Energy", "step" : "Timestep"}
If target inputs are None, the copy will work on the same dictionary.
Outputs: [Success, Status]
Success : True if any copy else False
Status : -2 if both dictionaries are not accessible
-1 if only target dict is not accessible
0 can access dict but without any copy
1 at least one copy is made from sourceDict to targetDict
N number of N copies were made from sourceDict to targetDict
"""
rtn = [False,-2]
skip = False
sDict = None
tDict = None
if targetDict is None and copyList is None:
skip = True
if skip is False:
if sourceType is None:
sourceType = 'smartparser'
if targetObj is None:
targetObj = sourceObj
if targetDict is None:
targetDict = sourceDict
targetType = sourceType
if targetType is None:
targetType = sourceType
sDict = getattr(sourceObj, sourceDict)
tDict = getattr(targetObj, targetDict)
if sDict is not None:
rtn[1] += 1
if tDict is not None:
rtn[1] += 1
sp = False
if 'smartparser' in sourceType:
sp = True
tp = False
if 'smartparser' in targetType:
tp = True
for skey, sval in sDict.items():
sk = sval.matchStr if sp else skey
if copyDict:
if sk in copyDict:
target = copyDict[sk]
for tkey, tval in tDict.items():
tk = tval.matchStr if tp else tkey
if target == tk:
rtn[0] = True
rtn[1] += 1
if tp:
tDict[tkey] = sval.value if sp else sval
else:
tDict[tkey] = sval.value if sp else sval
else:
for tkey, tval in tDict.items():
tk = tval.matchStr if tp else tkey
if sk == tk:
rtn[0] = True
rtn[1] += 1
if tp:
tDict[tkey] = sval.value if sp else sval
else:
tDict[tkey] = sval.value if sp else sval
break
if rtn[1]>0:
setattr(targetObj, tDict, targetDict)
return rtn
def setMetaStrInDict(objectName, sourceDict, mStr, mVal, matchExact=False,
caseSensitive=False, multipleMatch=True, matchFirst=None, matchLast=None):
"""Find mStr in a given sourceDict and set it to mVal.
If the value is set in a dictionary item, activeInfo of the
item is also set to True
Inputs:
objectName (String) : Python object that holds this attribute
Ex. : self
sourceDict (String) : The name string of the SmartParser dictionary
Ex. : 'myDict' (this will be self.myDict)
mStr (String) : The string to be matched with the matchStr in dict.
mVal (Any type that is accepted by metaInfoStorage) : The value
Output
A list with [Success, MatchKey, Status]:
Success: True : there is at least one match
False: there is an error
MatchKey : a list with the matched
keys in the dict
Status : -1 : there is no dictionary
with the given name
0 : there is dict but there is no
item that matchs with mStr
1 : there is a match in the dict
with a successful value set
2-N : there are multiple matches
with successful value settings
(Only available if multipleMatch is True)
"""
rtnList = [False, None, -1]
theDict = getattr(objectName, sourceDict)
if theDict is not None:
rtnList[2] = 0
matchKeys = []
if matchFirst is not None:
if int(matchFirst)<1:
matchFirst = None
else:
macthExact = False
if matchLast is not None:
if int(matchLast)<1:
matchLast = None
else:
macthExact = False
for k,v in theDict.items():
assign = False
if matchExact is True:
if caseSensitive:
if mStr == v.matchStr:
assign = True
else:
if mStr.upper() == v.matchStr.upper():
assign = True
elif matchFirst is not None:
mFirst = int(matchFirst)
if mFirst >= len(v.matchStr):
mFirst=len(v.matchStr)
vStr = v.matchStr[0:mFirst]
if caseSensitive:
if mStr in vStr:
assign = True
else:
if mStr.upper() in vStr.upper():
assign = True
elif matchLast is not None:
mLast = int(matchLast)
if mLast >= len(v.matchStr):
mLast=len(v.matchStr)
vStr = v.matchStr[-mLast:-1]
if caseSensitive:
if mStr in vStr:
assign = True
else:
if mStr.upper() in vStr.upper():
assign = True
else:
if caseSensitive:
if mStr in v.matchStr:
assign = True
else:
if mStr.upper() in v.matchStr.upper():
assign = True
if assign:
theDict[k].value = mVal
theDict[k].activeInfo = True
matchKeys.append(k)
if multipleMatch is False:
break
if matchKeys:
rtnList[0] = True
rtnList[1] = matchKeys
rtnList[2] = len(matchKeys)
setattr(objectName, sourceDict, theDict)
return rtnList
def isMetaStrInDict(nameStr, sourceDict):
"""Returns a list that includes all meta name
strings for the given dictionary.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment