Package fabmetheus_utilities :: Package geometry :: Package geometry_utilities :: Module evaluate
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.geometry.geometry_utilities.evaluate

   1  """ 
   2  Boolean geometry utilities. 
   3   
   4  """ 
   5   
   6  from fabmetheus_utilities.geometry.geometry_utilities.evaluate_elements import setting 
   7  from fabmetheus_utilities.vector3 import Vector3 
   8  from fabmetheus_utilities import archive 
   9  from fabmetheus_utilities import euclidean 
  10  import math 
  11  import os 
  12  import sys 
  13  import traceback 
  14   
  15   
  16  __author__ = 'Enrique Perez (perez_enrique@yahoo.com)' 
  17  __credits__ = 'Art of Illusion <http://www.artofillusion.org/>' 
  18  __date__ = '$Date: 2008/02/05 $' 
  19  __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' 
  20   
  21   
  22  globalModuleFunctionsDictionary = {} 
  23   
  24   
25 -def addQuoteWord(evaluatorWords, word):
26 'Add quote word and remainder if the word starts with a quote character or dollar sign, otherwise add the word.' 27 if len(word) < 2: 28 evaluatorWords.append(word) 29 return 30 firstCharacter = word[0] 31 if firstCharacter == '$': 32 dotIndex = word.find('.', 1) 33 if dotIndex > -1: 34 evaluatorWords.append(word[: dotIndex]) 35 evaluatorWords.append(word[dotIndex :]) 36 return 37 if firstCharacter != '"' and firstCharacter != "'": 38 evaluatorWords.append(word) 39 return 40 nextQuoteIndex = word.find(firstCharacter, 1) 41 if nextQuoteIndex < 0 or nextQuoteIndex == len(word) - 1: 42 evaluatorWords.append(word) 43 return 44 nextQuoteIndex += 1 45 evaluatorWords.append(word[: nextQuoteIndex]) 46 evaluatorWords.append(word[nextQuoteIndex :])
47
48 -def addPrefixDictionary(dictionary, keys, value):
49 'Add prefixed key values to dictionary.' 50 for key in keys: 51 dictionary[key.lstrip('_')] = value
52
53 -def addToPathsRecursively(paths, vector3Lists):
54 'Add to vector3 paths recursively.' 55 if vector3Lists.__class__ == Vector3 or vector3Lists.__class__ .__name__ == 'Vector3Index': 56 paths.append([ vector3Lists ]) 57 return 58 path = [] 59 for vector3List in vector3Lists: 60 if vector3List.__class__ == list: 61 addToPathsRecursively(paths, vector3List) 62 elif vector3List.__class__ == Vector3: 63 path.append(vector3List) 64 if len(path) > 0: 65 paths.append(path)
66
67 -def addValueToEvaluatedDictionary(evaluatedDictionary, key, xmlElement):
68 'Get the evaluated dictionary.' 69 value = getEvaluatedValueObliviously(key, xmlElement) 70 if value == None: 71 valueString = str(xmlElement.attributeDictionary[key]) 72 print('Warning, addValueToEvaluatedDictionary in evaluate can not get a value for:') 73 print(valueString) 74 evaluatedDictionary[key + '__Warning__'] = 'Can not evaluate: ' + valueString.replace('"', ' ').replace( "'", ' ') 75 else: 76 evaluatedDictionary[key] = value
77
78 -def addVector3ToXMLElement(key, vector3, xmlElement):
79 'Add vector3 to xml element.' 80 xmlElement.attributeDictionary[key] = '[%s,%s,%s]' % (vector3.x, vector3.y, vector3.z)
81
82 -def compareExecutionOrderAscending(module, otherModule):
83 'Get comparison in order to sort modules in ascending execution order.' 84 if module.globalExecutionOrder < otherModule.globalExecutionOrder: 85 return -1 86 if module.globalExecutionOrder > otherModule.globalExecutionOrder: 87 return 1 88 if module.__name__ < otherModule.__name__: 89 return -1 90 return int(module.__name__ > otherModule.__name__)
91
92 -def convertToPaths(dictionary):
93 'Recursively convert any XMLElements to paths.' 94 if dictionary.__class__ == Vector3 or dictionary.__class__.__name__ == 'Vector3Index': 95 return 96 keys = getKeys(dictionary) 97 if keys == None: 98 return 99 for key in keys: 100 value = dictionary[key] 101 if value.__class__.__name__ == 'XMLElement': 102 if value.xmlObject != None: 103 dictionary[key] = getFloatListListsByPaths(value.xmlObject.getPaths()) 104 else: 105 convertToPaths(dictionary[key])
106
107 -def convertToTransformedPaths(dictionary):
108 'Recursively convert any XMLElements to paths.' 109 if dictionary.__class__ == Vector3 or dictionary.__class__.__name__ == 'Vector3Index': 110 return 111 keys = getKeys(dictionary) 112 if keys == None: 113 return 114 for key in keys: 115 value = dictionary[key] 116 if value.__class__.__name__ == 'XMLElement': 117 if value.xmlObject != None: 118 dictionary[key] = value.xmlObject.getTransformedPaths() 119 else: 120 convertToTransformedPaths(dictionary[key])
121
122 -def executeLeftOperations( evaluators, operationLevel ):
123 'Evaluate the expression value from the numeric and operation evaluators.' 124 for negativeIndex in xrange( - len(evaluators), - 1 ): 125 evaluatorIndex = negativeIndex + len(evaluators) 126 evaluators[evaluatorIndex].executeLeftOperation( evaluators, evaluatorIndex, operationLevel )
127
128 -def executeNextEvaluatorArguments(evaluator, evaluators, evaluatorIndex, nextEvaluator):
129 'Execute the nextEvaluator arguments.' 130 if evaluator.value == None: 131 print('Warning, executeNextEvaluatorArguments in evaluate can not get a evaluator.value for:') 132 print(evaluatorIndex) 133 print(evaluators) 134 print(evaluator) 135 return 136 nextEvaluator.value = evaluator.value(*nextEvaluator.arguments) 137 del evaluators[evaluatorIndex]
138
139 -def executePairOperations(evaluators, operationLevel):
140 'Evaluate the expression value from the numeric and operation evaluators.' 141 for negativeIndex in xrange(1 - len(evaluators), - 1): 142 evaluatorIndex = negativeIndex + len(evaluators) 143 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, operationLevel)
144
145 -def getBracketEvaluators(bracketBeginIndex, bracketEndIndex, evaluators):
146 'Get the bracket evaluators.' 147 return getEvaluatedExpressionValueEvaluators(evaluators[bracketBeginIndex + 1 : bracketEndIndex])
148
149 -def getBracketsExist(evaluators):
150 'Evaluate the expression value.' 151 bracketBeginIndex = None 152 for negativeIndex in xrange( - len(evaluators), 0 ): 153 bracketEndIndex = negativeIndex + len(evaluators) 154 evaluatorEnd = evaluators[ bracketEndIndex ] 155 evaluatorWord = evaluatorEnd.word 156 if evaluatorWord in ['(', '[', '{']: 157 bracketBeginIndex = bracketEndIndex 158 elif evaluatorWord in [')', ']', '}']: 159 if bracketBeginIndex == None: 160 print('Warning, bracketBeginIndex in evaluateBrackets in evaluate is None.') 161 print('This may be because the brackets are not balanced.') 162 print(evaluators) 163 del evaluators[ bracketEndIndex ] 164 return 165 evaluators[ bracketBeginIndex ].executeBracket(bracketBeginIndex, bracketEndIndex, evaluators) 166 evaluators[ bracketBeginIndex ].word = None 167 return True 168 return False
169
170 -def getBracketValuesDeleteEvaluator(bracketBeginIndex, bracketEndIndex, evaluators):
171 'Get the bracket values and delete the evaluator.' 172 evaluatedExpressionValueEvaluators = getBracketEvaluators(bracketBeginIndex, bracketEndIndex, evaluators) 173 bracketValues = [] 174 for evaluatedExpressionValueEvaluator in evaluatedExpressionValueEvaluators: 175 bracketValues.append( evaluatedExpressionValueEvaluator.value ) 176 del evaluators[ bracketBeginIndex + 1: bracketEndIndex + 1 ] 177 return bracketValues
178
179 -def getCapitalizedSuffixKey(prefix, suffix):
180 'Get key with capitalized suffix.' 181 if prefix == '' or prefix.endswith('.'): 182 return prefix + suffix 183 return prefix + suffix[:1].upper()+suffix[1:]
184
185 -def getDictionarySplitWords(dictionary, value):
186 'Get split line for evaluators.' 187 if getIsQuoted(value): 188 return [value] 189 for dictionaryKey in dictionary.keys(): 190 value = value.replace(dictionaryKey, ' ' + dictionaryKey + ' ') 191 dictionarySplitWords = [] 192 for word in value.split(): 193 dictionarySplitWords.append(word) 194 return dictionarySplitWords
195
196 -def getEndIndexConvertEquationValue( bracketEndIndex, evaluatorIndex, evaluators ):
197 'Get the bracket end index and convert the equation value evaluators into a string.' 198 evaluator = evaluators[evaluatorIndex] 199 if evaluator.__class__ != EvaluatorValue: 200 return bracketEndIndex 201 if not evaluator.word.startswith('equation.'): 202 return bracketEndIndex 203 if evaluators[ evaluatorIndex + 1 ].word != ':': 204 return bracketEndIndex 205 valueBeginIndex = evaluatorIndex + 2 206 equationValueString = '' 207 for valueEvaluatorIndex in xrange( valueBeginIndex, len(evaluators) ): 208 valueEvaluator = evaluators[ valueEvaluatorIndex ] 209 if valueEvaluator.word == ',' or valueEvaluator.word == '}': 210 if equationValueString == '': 211 return bracketEndIndex 212 else: 213 evaluators[ valueBeginIndex ] = EvaluatorValue( equationValueString ) 214 valueDeleteIndex = valueBeginIndex + 1 215 del evaluators[ valueDeleteIndex : valueEvaluatorIndex ] 216 return bracketEndIndex - valueEvaluatorIndex + valueDeleteIndex 217 equationValueString += valueEvaluator.word 218 return bracketEndIndex
219
220 -def getEvaluatedBoolean(defaultValue, key, xmlElement):
221 'Get the evaluated boolean.' 222 if xmlElement == None: 223 return defaultValue 224 if key in xmlElement.attributeDictionary: 225 return euclidean.getBooleanFromValue(getEvaluatedValueObliviously(key, xmlElement)) 226 return defaultValue
227
228 -def getEvaluatedDictionaryByCopyKeys(copyKeys, xmlElement):
229 'Get the evaluated dictionary by copyKeys.' 230 evaluatedDictionary = {} 231 for key in xmlElement.attributeDictionary.keys(): 232 if key in copyKeys: 233 evaluatedDictionary[key] = xmlElement.attributeDictionary[key] 234 else: 235 addValueToEvaluatedDictionary(evaluatedDictionary, key, xmlElement) 236 return evaluatedDictionary
237
238 -def getEvaluatedDictionaryByEvaluationKeys(evaluationKeys, xmlElement):
239 'Get the evaluated dictionary.' 240 evaluatedDictionary = {} 241 for key in xmlElement.attributeDictionary.keys(): 242 if key in evaluationKeys: 243 addValueToEvaluatedDictionary(evaluatedDictionary, key, xmlElement) 244 return evaluatedDictionary
245
246 -def getEvaluatedExpressionValue(value, xmlElement):
247 'Evaluate the expression value.' 248 try: 249 return getEvaluatedExpressionValueBySplitLine( getEvaluatorSplitWords(value), xmlElement ) 250 except: 251 print('Warning, in getEvaluatedExpressionValue in evaluate could not get a value for:') 252 print(value) 253 traceback.print_exc(file=sys.stdout) 254 return None
255
256 -def getEvaluatedExpressionValueBySplitLine(words, xmlElement):
257 'Evaluate the expression value.' 258 evaluators = [] 259 for wordIndex, word in enumerate(words): 260 nextWord = '' 261 nextWordIndex = wordIndex + 1 262 if nextWordIndex < len(words): 263 nextWord = words[nextWordIndex] 264 evaluator = getEvaluator(evaluators, nextWord, word, xmlElement) 265 if evaluator != None: 266 evaluators.append(evaluator) 267 while getBracketsExist(evaluators): 268 pass 269 evaluatedExpressionValueEvaluators = getEvaluatedExpressionValueEvaluators(evaluators) 270 if len( evaluatedExpressionValueEvaluators ) > 0: 271 return evaluatedExpressionValueEvaluators[0].value 272 return None
273
274 -def getEvaluatedExpressionValueEvaluators(evaluators):
275 'Evaluate the expression value from the numeric and operation evaluators.' 276 for evaluatorIndex, evaluator in enumerate(evaluators): 277 evaluator.executeCenterOperation(evaluators, evaluatorIndex) 278 for negativeIndex in xrange(1 - len(evaluators), 0): 279 evaluatorIndex = negativeIndex + len(evaluators) 280 evaluators[evaluatorIndex].executeRightOperation(evaluators, evaluatorIndex) 281 executeLeftOperations(evaluators, 200) 282 for operationLevel in [80, 60, 40, 20, 15]: 283 executePairOperations(evaluators, operationLevel) 284 executeLeftOperations(evaluators, 13) 285 executePairOperations(evaluators, 12) 286 for negativeIndex in xrange(-len(evaluators), 0): 287 evaluatorIndex = negativeIndex + len(evaluators) 288 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, 10) 289 for evaluatorIndex in xrange(len(evaluators) - 1, -1, -1): 290 evaluators[evaluatorIndex].executePairOperation(evaluators, evaluatorIndex, 0) 291 return evaluators
292
293 -def getEvaluatedFloat(defaultValue, key, xmlElement):
294 'Get the evaluated float.' 295 if xmlElement == None: 296 return defaultValue 297 if key in xmlElement.attributeDictionary: 298 return euclidean.getFloatFromValue(getEvaluatedValueObliviously(key, xmlElement)) 299 return defaultValue
300
301 -def getEvaluatedFloatByKeys(defaultValue, keys, xmlElement):
302 'Get the evaluated float by keys.' 303 for key in keys: 304 defaultValue = getEvaluatedFloat(defaultValue, key, xmlElement) 305 return defaultValue
306
307 -def getEvaluatedInt(defaultValue, key, xmlElement):
308 'Get the evaluated int.' 309 if xmlElement == None: 310 return None 311 if key in xmlElement.attributeDictionary: 312 try: 313 return getIntFromFloatString(getEvaluatedValueObliviously(key, xmlElement)) 314 except: 315 print('Warning, could not evaluate the int.') 316 print(key) 317 print(xmlElement.attributeDictionary[key]) 318 return defaultValue
319
320 -def getEvaluatedIntByKeys(defaultValue, keys, xmlElement):
321 'Get the evaluated int by keys.' 322 for key in keys: 323 defaultValue = getEvaluatedInt(defaultValue, key, xmlElement) 324 return defaultValue
325
326 -def getEvaluatedLinkValue(word, xmlElement):
327 'Get the evaluated link value.' 328 if word == '': 329 return '' 330 if getStartsWithCurlyEqualRoundSquare(word): 331 return getEvaluatedExpressionValue(word, xmlElement) 332 return word
333
334 -def getEvaluatedString(defaultValue, key, xmlElement):
335 'Get the evaluated string.' 336 if xmlElement == None: 337 return defaultValue 338 if key in xmlElement.attributeDictionary: 339 return str(getEvaluatedValueObliviously(key, xmlElement)) 340 return defaultValue
341
342 -def getEvaluatedValue(defaultValue, key, xmlElement):
343 'Get the evaluated value.' 344 if xmlElement == None: 345 return defaultValue 346 if key in xmlElement.attributeDictionary: 347 return getEvaluatedValueObliviously(key, xmlElement) 348 return defaultValue
349
350 -def getEvaluatedValueObliviously(key, xmlElement):
351 'Get the evaluated value.' 352 value = str(xmlElement.attributeDictionary[key]).strip() 353 if key == 'id' or key == 'name' or key == 'tags': 354 return value 355 return getEvaluatedLinkValue(value, xmlElement)
356
357 -def getEvaluator(evaluators, nextWord, word, xmlElement):
358 'Get the evaluator.' 359 if word in globalSplitDictionary: 360 return globalSplitDictionary[word](word, xmlElement) 361 firstCharacter = word[: 1] 362 if firstCharacter == "'" or firstCharacter == '"': 363 if len(word) > 1: 364 if firstCharacter == word[-1]: 365 return EvaluatorValue(word[1 : -1]) 366 if firstCharacter == '$': 367 return EvaluatorValue(word[1 :]) 368 dotIndex = word.find('.') 369 functions = xmlElement.getXMLProcessor().functions 370 if dotIndex > -1 and len(word) > 1: 371 if dotIndex == 0 and word[1].isalpha(): 372 return EvaluatorAttribute(word, xmlElement) 373 if dotIndex > 0: 374 untilDot = word[: dotIndex] 375 if untilDot in globalModuleEvaluatorDictionary: 376 return globalModuleEvaluatorDictionary[untilDot](word, xmlElement) 377 if len(functions) > 0: 378 if untilDot in functions[-1].localDictionary: 379 return EvaluatorLocal(word, xmlElement) 380 if firstCharacter.isalpha() or firstCharacter == '_': 381 if len(functions) > 0: 382 if word in functions[-1].localDictionary: 383 return EvaluatorLocal(word, xmlElement) 384 wordElement = xmlElement.getXMLElementByImportID(word) 385 if wordElement != None: 386 if wordElement.localName == 'class': 387 return EvaluatorClass(word, wordElement) 388 if wordElement.localName == 'function': 389 return EvaluatorFunction(word, wordElement) 390 return EvaluatorValue(word) 391 return EvaluatorNumeric(word, xmlElement)
392
393 -def getEvaluatorSplitWords(value):
394 'Get split words for evaluators.' 395 if value.startswith('='): 396 value = value[len('=') :] 397 if len(value) < 1: 398 return [] 399 global globalDictionaryOperatorBegin 400 uniqueQuoteIndex = 0 401 word = '' 402 quoteString = None 403 quoteDictionary = {} 404 for characterIndex in xrange(len(value)): 405 character = value[characterIndex] 406 if character == '"' or character == "'": 407 if quoteString == None: 408 quoteString = '' 409 elif quoteString != None: 410 if character == quoteString[: 1]: 411 uniqueQuoteIndex = getUniqueQuoteIndex(uniqueQuoteIndex, value) 412 uniqueToken = getTokenByNumber(uniqueQuoteIndex) 413 quoteDictionary[uniqueToken] = quoteString + character 414 character = uniqueToken 415 quoteString = None 416 if quoteString == None: 417 word += character 418 else: 419 quoteString += character 420 beginSplitWords = getDictionarySplitWords(globalDictionaryOperatorBegin, word) 421 global globalSplitDictionaryOperator 422 evaluatorSplitWords = [] 423 for beginSplitWord in beginSplitWords: 424 if beginSplitWord in globalDictionaryOperatorBegin: 425 evaluatorSplitWords.append(beginSplitWord) 426 else: 427 evaluatorSplitWords += getDictionarySplitWords(globalSplitDictionaryOperator, beginSplitWord) 428 for evaluatorSplitWordIndex, evaluatorSplitWord in enumerate(evaluatorSplitWords): 429 for quoteDictionaryKey in quoteDictionary.keys(): 430 if quoteDictionaryKey in evaluatorSplitWord: 431 evaluatorSplitWords[evaluatorSplitWordIndex] = evaluatorSplitWord.replace(quoteDictionaryKey, quoteDictionary[quoteDictionaryKey]) 432 evaluatorTransitionWords = [] 433 for evaluatorSplitWord in evaluatorSplitWords: 434 addQuoteWord(evaluatorTransitionWords, evaluatorSplitWord) 435 return evaluatorTransitionWords
436
437 -def getFloatListFromBracketedString( bracketedString ):
438 'Get list from a bracketed string.' 439 if not getIsBracketed( bracketedString ): 440 return None 441 bracketedString = bracketedString.strip().replace('[', '').replace(']', '').replace('(', '').replace(')', '') 442 if len( bracketedString ) < 1: 443 return [] 444 splitLine = bracketedString.split(',') 445 floatList = [] 446 for word in splitLine: 447 evaluatedFloat = euclidean.getFloatFromValue(word) 448 if evaluatedFloat != None: 449 floatList.append( evaluatedFloat ) 450 return floatList
451
452 -def getFloatListListsByPaths(paths):
453 'Get float lists by paths.' 454 floatListLists = [] 455 for path in paths: 456 floatListList = [] 457 for point in path: 458 floatListList.append( point.getFloatList() ) 459 return floatListLists
460
461 -def getFromCreationEvaluatorPlugins( namePathDictionary, xmlElement ):
462 'Get the creation evaluator plugins if the xmlElement is from the creation evaluator.' 463 if getEvaluatedBoolean( False, '_fromCreationEvaluator', xmlElement ): 464 return getMatchingPlugins( namePathDictionary, xmlElement ) 465 return []
466
467 -def getKeys(repository):
468 'Get keys for repository.' 469 repositoryClass = repository.__class__ 470 if repositoryClass == list or repositoryClass == tuple: 471 return range(len(repository)) 472 if repositoryClass == dict: 473 return repository.keys() 474 return None
475
476 -def getIntFromFloatString(value):
477 'Get the int from the string.' 478 floatString = str(value).strip() 479 if floatString == '': 480 return None 481 dotIndex = floatString.find('.') 482 if dotIndex < 0: 483 return int(value) 484 return int( round( float(floatString) ) )
485
486 -def getIsBracketed(word):
487 'Determine if the word is bracketed.' 488 if len(word) < 2: 489 return False 490 firstCharacter = word[0] 491 lastCharacter = word[-1] 492 if firstCharacter == '(' and lastCharacter == ')': 493 return True 494 return firstCharacter == '[' and lastCharacter == ']'
495
496 -def getIsQuoted(word):
497 'Determine if the word is quoted.' 498 if len(word) < 2: 499 return False 500 firstCharacter = word[0] 501 lastCharacter = word[-1] 502 if firstCharacter == '"' and lastCharacter == '"': 503 return True 504 return firstCharacter == "'" and lastCharacter == "'"
505
506 -def getLocalAttributeValueString(key, valueString):
507 'Get the local attribute value string with augmented assignment.' 508 augmentedStatements = '+= -= *= /= %= **='.split() 509 for augmentedStatement in augmentedStatements: 510 if valueString.startswith(augmentedStatement): 511 return key + augmentedStatement[: -1] + valueString[len(augmentedStatement) :] 512 return valueString
513
514 -def getMatchingPlugins( namePathDictionary, xmlElement ):
515 'Get the plugins whose names are in the attribute dictionary.' 516 matchingPlugins = [] 517 namePathDictionaryCopy = namePathDictionary.copy() 518 for key in xmlElement.attributeDictionary: 519 dotIndex = key.find('.') 520 if dotIndex > - 1: 521 keyUntilDot = key[: dotIndex] 522 if keyUntilDot in namePathDictionaryCopy: 523 pluginModule = archive.getModuleWithPath( namePathDictionaryCopy[ keyUntilDot ] ) 524 del namePathDictionaryCopy[ keyUntilDot ] 525 if pluginModule != None: 526 matchingPlugins.append( pluginModule ) 527 return matchingPlugins
528
529 -def getNextChildIndex(xmlElement):
530 'Get the next childNode index.' 531 for childNodeIndex, childNode in enumerate( xmlElement.parentNode.childNodes ): 532 if childNode == xmlElement: 533 return childNodeIndex + 1 534 return len( xmlElement.parentNode.childNodes )
535
536 -def getPathByKey(defaultPath, key, xmlElement):
537 'Get path from prefix and xml element.' 538 if key not in xmlElement.attributeDictionary: 539 return defaultPath 540 word = str(xmlElement.attributeDictionary[key]).strip() 541 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 542 if evaluatedLinkValue.__class__ == list: 543 return getPathByList(evaluatedLinkValue) 544 xmlElementObject = getXMLElementObject(evaluatedLinkValue) 545 if xmlElementObject == None: 546 return defaultPath 547 return xmlElementObject.getPaths()[0]
548
549 -def getPathByList(vertexList):
550 'Get the paths by list.' 551 if len(vertexList) < 1: 552 return Vector3() 553 if vertexList[0].__class__ != list: 554 vertexList = [vertexList] 555 path = [] 556 for floatList in vertexList: 557 vector3 = getVector3ByFloatList(floatList, Vector3()) 558 path.append(vector3) 559 return path
560
561 -def getPathByPrefix(path, prefix, xmlElement):
562 'Get path from prefix and xml element.' 563 if len(path) < 2: 564 print('Warning, bug, path is too small in evaluate in setPathByPrefix.') 565 return 566 pathByKey = getPathByKey([], getCapitalizedSuffixKey(prefix, 'path'), xmlElement) 567 if len( pathByKey ) < len(path): 568 for pointIndex in xrange( len( pathByKey ) ): 569 path[pointIndex] = pathByKey[pointIndex] 570 else: 571 path = pathByKey 572 path[0] = getVector3ByPrefix(path[0], getCapitalizedSuffixKey(prefix, 'pathStart'), xmlElement) 573 path[-1] = getVector3ByPrefix(path[-1], getCapitalizedSuffixKey(prefix, 'pathEnd'), xmlElement) 574 return path
575
576 -def getPathsByKey(defaultPaths, key, xmlElement):
577 'Get paths by key.' 578 if key not in xmlElement.attributeDictionary: 579 return defaultPaths 580 word = str(xmlElement.attributeDictionary[key]).strip() 581 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 582 if evaluatedLinkValue.__class__ == dict or evaluatedLinkValue.__class__ == list: 583 convertToPaths(evaluatedLinkValue) 584 return getPathsByLists(evaluatedLinkValue) 585 xmlElementObject = getXMLElementObject(evaluatedLinkValue) 586 if xmlElementObject == None: 587 return defaultPaths 588 return xmlElementObject.getPaths()
589
590 -def getPathsByLists(vertexLists):
591 'Get paths by lists.' 592 vector3Lists = getVector3ListsRecursively(vertexLists) 593 paths = [] 594 addToPathsRecursively(paths, vector3Lists) 595 return paths
596
597 -def getSidesBasedOnPrecision(radius, xmlElement):
598 'Get the number of poygon sides.' 599 return int(math.ceil(math.sqrt(0.5 * radius * math.pi * math.pi / setting.getPrecision(xmlElement))))
600
601 -def getSidesMinimumThreeBasedOnPrecision(radius, xmlElement):
602 'Get the number of poygon sides, with a minimum of three.' 603 return max(getSidesBasedOnPrecision(radius, xmlElement), 3)
604
605 -def getSidesMinimumThreeBasedOnPrecisionSides(radius, xmlElement):
606 'Get the number of poygon sides, with a minimum of three.' 607 sides = getSidesMinimumThreeBasedOnPrecision(radius, xmlElement) 608 return getEvaluatedFloat(sides, 'sides', xmlElement)
609
610 -def getSplitDictionary():
611 'Get split dictionary.' 612 global globalSplitDictionaryOperator 613 splitDictionary = globalSplitDictionaryOperator.copy() 614 global globalDictionaryOperatorBegin 615 splitDictionary.update( globalDictionaryOperatorBegin ) 616 splitDictionary['and'] = EvaluatorAnd 617 splitDictionary['false'] = EvaluatorFalse 618 splitDictionary['False'] = EvaluatorFalse 619 splitDictionary['or'] = EvaluatorOr 620 splitDictionary['not'] = EvaluatorNot 621 splitDictionary['true'] = EvaluatorTrue 622 splitDictionary['True'] = EvaluatorTrue 623 splitDictionary['none'] = EvaluatorNone 624 splitDictionary['None'] = EvaluatorNone 625 return splitDictionary
626
627 -def getStartsWithCurlyEqualRoundSquare(word):
628 'Determine if the word starts with round or square brackets.' 629 return word.startswith('{') or word.startswith('=') or word.startswith('(') or word.startswith('[')
630
631 -def getTokenByNumber(number):
632 'Get token by number.' 633 return '_%s_' % number
634
635 -def getTransformedPathByKey(defaultTransformedPath, key, xmlElement):
636 'Get transformed path from prefix and xml element.' 637 if key not in xmlElement.attributeDictionary: 638 return defaultTransformedPath 639 value = xmlElement.attributeDictionary[key] 640 if value.__class__ == list: 641 return value 642 word = str(value).strip() 643 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 644 if evaluatedLinkValue.__class__ == list: 645 return getPathByList(evaluatedLinkValue) 646 xmlElementObject = getXMLElementObject(evaluatedLinkValueClass) 647 if xmlElementObject == None: 648 return defaultTransformedPath 649 return xmlElementObject.getTransformedPaths()[0]
650
651 -def getTransformedPathByPrefix(path, prefix, xmlElement):
652 'Get path from prefix and xml element.' 653 if len(path) < 2: 654 print('Warning, bug, path is too small in evaluate in setPathByPrefix.') 655 return 656 pathByKey = getTransformedPathByKey([], getCapitalizedSuffixKey(prefix, 'path'), xmlElement) 657 if len( pathByKey ) < len(path): 658 for pointIndex in xrange( len( pathByKey ) ): 659 path[pointIndex] = pathByKey[pointIndex] 660 else: 661 path = pathByKey 662 path[0] = getVector3ByPrefix(path[0], getCapitalizedSuffixKey(prefix, 'pathStart'), xmlElement) 663 path[-1] = getVector3ByPrefix(path[-1], getCapitalizedSuffixKey(prefix, 'pathEnd'), xmlElement) 664 return path
665
666 -def getTransformedPathsByKey(defaultTransformedPaths, key, xmlElement):
667 'Get transformed paths by key.' 668 if key not in xmlElement.attributeDictionary: 669 return defaultTransformedPaths 670 value = xmlElement.attributeDictionary[key] 671 if value.__class__ == list: 672 return getPathsByLists(value) 673 word = str(value).strip() 674 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 675 if evaluatedLinkValue.__class__ == dict or evaluatedLinkValue.__class__ == list: 676 convertToTransformedPaths(evaluatedLinkValue) 677 return getPathsByLists(evaluatedLinkValue) 678 xmlElementObject = getXMLElementObject(evaluatedLinkValue) 679 if xmlElementObject == None: 680 return defaultTransformedPaths 681 return xmlElementObject.getTransformedPaths()
682
683 -def getUniqueQuoteIndex( uniqueQuoteIndex, word ):
684 'Get uniqueQuoteIndex.' 685 uniqueQuoteIndex += 1 686 while getTokenByNumber(uniqueQuoteIndex) in word: 687 uniqueQuoteIndex += 1 688 return uniqueQuoteIndex
689
690 -def getUniqueToken(word):
691 'Get unique token.' 692 uniqueString = '@#!' 693 for character in uniqueString: 694 if character not in word: 695 return character 696 uniqueNumber = 0 697 while True: 698 for character in uniqueString: 699 uniqueToken = character + str(uniqueNumber) 700 if uniqueToken not in word: 701 return uniqueToken 702 uniqueNumber += 1
703
704 -def getVector3ByDictionary( dictionary, vector3 ):
705 'Get vector3 by dictionary.' 706 if 'x' in dictionary: 707 vector3 = getVector3IfNone(vector3) 708 vector3.x = euclidean.getFloatFromValue(dictionary['x']) 709 if 'y' in dictionary: 710 vector3 = getVector3IfNone(vector3) 711 vector3.y = euclidean.getFloatFromValue(dictionary['y']) 712 if 'z' in dictionary: 713 vector3 = getVector3IfNone(vector3) 714 vector3.z = euclidean.getFloatFromValue( dictionary['z'] ) 715 return vector3
716
717 -def getVector3ByDictionaryListValue(value, vector3):
718 'Get vector3 by dictionary, list or value.' 719 if value.__class__ == Vector3 or value.__class__.__name__ == 'Vector3Index': 720 return value 721 if value.__class__ == dict: 722 return getVector3ByDictionary(value, vector3) 723 if value.__class__ == list: 724 return getVector3ByFloatList(value, vector3) 725 floatFromValue = euclidean.getFloatFromValue(value) 726 if floatFromValue == None: 727 return vector3 728 vector3.setToXYZ(floatFromValue, floatFromValue, floatFromValue) 729 return vector3
730
731 -def getVector3ByFloatList(floatList, vector3):
732 'Get vector3 by float list.' 733 if len(floatList) > 0: 734 vector3 = getVector3IfNone(vector3) 735 vector3.x = euclidean.getFloatFromValue(floatList[0]) 736 if len(floatList) > 1: 737 vector3 = getVector3IfNone(vector3) 738 vector3.y = euclidean.getFloatFromValue(floatList[1]) 739 if len(floatList) > 2: 740 vector3 = getVector3IfNone(vector3) 741 vector3.z = euclidean.getFloatFromValue(floatList[2]) 742 return vector3
743
744 -def getVector3ByMultiplierPrefix( multiplier, prefix, vector3, xmlElement ):
745 'Get vector3 from multiplier, prefix and xml element.' 746 if multiplier == 0.0: 747 return vector3 748 oldMultipliedValueVector3 = vector3 * multiplier 749 vector3ByPrefix = getVector3ByPrefix(oldMultipliedValueVector3.copy(), prefix, xmlElement) 750 if vector3ByPrefix == oldMultipliedValueVector3: 751 return vector3 752 return vector3ByPrefix / multiplier
753
754 -def getVector3ByMultiplierPrefixes( multiplier, prefixes, vector3, xmlElement ):
755 'Get vector3 from multiplier, prefixes and xml element.' 756 for prefix in prefixes: 757 vector3 = getVector3ByMultiplierPrefix( multiplier, prefix, vector3, xmlElement ) 758 return vector3
759
760 -def getVector3ByPrefix(defaultVector3, prefix, xmlElement):
761 'Get vector3 from prefix and xml element.' 762 value = getEvaluatedValue(None, prefix, xmlElement) 763 if value != None: 764 defaultVector3 = getVector3ByDictionaryListValue(value, defaultVector3) 765 prefix = archive.getUntilDot(prefix) 766 x = getEvaluatedFloat(None, prefix + '.x', xmlElement) 767 if x != None: 768 defaultVector3 = getVector3IfNone(defaultVector3) 769 defaultVector3.x = x 770 y = getEvaluatedFloat(None, prefix + '.y', xmlElement) 771 if y != None: 772 defaultVector3 = getVector3IfNone(defaultVector3) 773 defaultVector3.y = y 774 z = getEvaluatedFloat(None, prefix + '.z', xmlElement) 775 if z != None: 776 defaultVector3 = getVector3IfNone(defaultVector3) 777 defaultVector3.z = z 778 return defaultVector3
779
780 -def getVector3ByPrefixes( prefixes, vector3, xmlElement ):
781 'Get vector3 from prefixes and xml element.' 782 for prefix in prefixes: 783 vector3 = getVector3ByPrefix(vector3, prefix, xmlElement) 784 return vector3
785
786 -def getVector3FromXMLElement(xmlElement):
787 'Get vector3 from xml element.' 788 vector3 = Vector3( 789 getEvaluatedFloat(0.0, 'x', xmlElement), 790 getEvaluatedFloat(0.0, 'y', xmlElement), 791 getEvaluatedFloat(0.0, 'z', xmlElement)) 792 return getVector3ByPrefix(vector3, 'cartesian', xmlElement)
793
794 -def getVector3IfNone(vector3):
795 'Get new vector3 if the original vector3 is none.' 796 if vector3 == None: 797 return Vector3() 798 return vector3
799
800 -def getVector3ListsRecursively(floatLists):
801 'Get vector3 lists recursively.' 802 if len(floatLists) < 1: 803 return Vector3() 804 firstElement = floatLists[0] 805 if firstElement.__class__ == Vector3: 806 return floatLists 807 if firstElement.__class__ != list: 808 return getVector3ByFloatList(floatLists, Vector3()) 809 vector3ListsRecursively = [] 810 for floatList in floatLists: 811 vector3ListsRecursively.append(getVector3ListsRecursively(floatList)) 812 return vector3ListsRecursively
813
814 -def getVisibleObjects(archivableObjects):
815 'Get the visible objects.' 816 visibleObjects = [] 817 for archivableObject in archivableObjects: 818 if archivableObject.getVisible(): 819 visibleObjects.append(archivableObject) 820 return visibleObjects
821
822 -def getXMLElementByKey(key, xmlElement):
823 'Get the xml element by key.' 824 if key not in xmlElement.attributeDictionary: 825 return None 826 word = str(xmlElement.attributeDictionary[key]).strip() 827 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 828 if evaluatedLinkValue.__class__.__name__ == 'XMLElement': 829 return evaluatedLinkValue 830 print('Warning, could not get XMLElement in getXMLElementByKey in evaluate for:') 831 print(key) 832 print(evaluatedLinkValue) 833 print(xmlElement) 834 return None
835
836 -def getXMLElementObject(evaluatedLinkValue):
837 'Get XMLElementObject.' 838 if evaluatedLinkValue.__class__.__name__ != 'XMLElement': 839 print('Warning, could not get XMLElement in getXMLElementObject in evaluate for:') 840 print(evaluatedLinkValue) 841 return None 842 if evaluatedLinkValue.xmlObject == None: 843 print('Warning, evaluatedLinkValue.xmlObject is None in getXMLElementObject in evaluate for:') 844 print(evaluatedLinkValue) 845 return None 846 return evaluatedLinkValue.xmlObject
847
848 -def getXMLElementsByKey(key, xmlElement):
849 'Get the xml elements by key.' 850 if key not in xmlElement.attributeDictionary: 851 return [] 852 word = str(xmlElement.attributeDictionary[key]).strip() 853 evaluatedLinkValue = getEvaluatedLinkValue(word, xmlElement) 854 if evaluatedLinkValue.__class__.__name__ == 'XMLElement': 855 return [evaluatedLinkValue] 856 if evaluatedLinkValue.__class__ == list: 857 return evaluatedLinkValue 858 print('Warning, could not get XMLElements in getXMLElementsByKey in evaluate for:') 859 print(key) 860 print(evaluatedLinkValue) 861 print(xmlElement) 862 return []
863
864 -def processArchivable(archivableClass, xmlElement):
865 'Get any new elements and process the archivable.' 866 if xmlElement == None: 867 return 868 xmlElement.xmlObject = archivableClass() 869 xmlElement.xmlObject.setToXMLElement(xmlElement) 870 xmlElement.getXMLProcessor().processChildNodes(xmlElement)
871
872 -def processCondition(xmlElement):
873 'Process the xml element condition.' 874 xmlProcessor = xmlElement.getXMLProcessor() 875 if xmlElement.xmlObject == None: 876 xmlElement.xmlObject = ModuleXMLElement(xmlElement) 877 if xmlElement.xmlObject.conditionSplitWords == None: 878 return 879 if len(xmlProcessor.functions ) < 1: 880 print('Warning, the (in) element is not in a function in processCondition in evaluate for:') 881 print(xmlElement) 882 return 883 if int( getEvaluatedExpressionValueBySplitLine( xmlElement.xmlObject.conditionSplitWords, xmlElement ) ) > 0: 884 xmlProcessor.functions[-1].processChildNodes(xmlElement) 885 else: 886 xmlElement.xmlObject.processElse(xmlElement)
887
888 -def removeIdentifiersFromDictionary(dictionary):
889 'Remove the identifier elements from a dictionary.' 890 euclidean.removeElementsFromDictionary(dictionary, ['id', 'name', 'tags'])
891
892 -def setAttributeDictionaryByArguments(argumentNames, arguments, xmlElement):
893 'Set the attribute dictionary to the arguments.' 894 for argumentIndex, argument in enumerate(arguments): 895 xmlElement.attributeDictionary[argumentNames[argumentIndex]] = argument
896
897 -def setFunctionLocalDictionary(arguments, function):
898 'Evaluate the function statement and delete the evaluators.' 899 function.localDictionary = {'_arguments' : arguments} 900 if len(arguments) > 0: 901 firstArgument = arguments[0] 902 if firstArgument.__class__ == dict: 903 function.localDictionary = firstArgument 904 return 905 if 'parameters' not in function.xmlElement.attributeDictionary: 906 return 907 parameters = function.xmlElement.attributeDictionary['parameters'].strip() 908 if parameters == '': 909 return 910 parameterWords = parameters.split(',') 911 for parameterWordIndex, parameterWord in enumerate(parameterWords): 912 strippedWord = parameterWord.strip() 913 keyValue = KeyValue().getByEqual(strippedWord) 914 if parameterWordIndex < len(arguments): 915 function.localDictionary[keyValue.key] = arguments[parameterWordIndex] 916 else: 917 strippedValue = keyValue.value 918 if strippedValue == None: 919 print('Warning there is no default parameter in getParameterValue for:') 920 print(strippedWord) 921 print(parameterWords) 922 print(arguments) 923 print( function.xmlElement.attributeDictionary ) 924 else: 925 strippedValue = strippedValue.strip() 926 function.localDictionary[keyValue.key.strip()] = strippedValue 927 if len(arguments) > len(parameterWords): 928 print('Warning there are too many initializeFunction parameters for:') 929 print( function.xmlElement.attributeDictionary ) 930 print(parameterWords) 931 print(arguments)
932
933 -def setLocalAttribute(xmlElement):
934 'Set the local attribute if any.' 935 if xmlElement.xmlObject != None: 936 return 937 for key in xmlElement.attributeDictionary: 938 if key[: 1].isalpha(): 939 value = getEvaluatorSplitWords(getLocalAttributeValueString(key, xmlElement.attributeDictionary[key].strip())) 940 xmlElement.xmlObject = KeyValue(key, value) 941 return 942 xmlElement.xmlObject = KeyValue()
943 944
945 -class BaseFunction:
946 'Class to get equation results.'
947 - def __init__(self, xmlElement):
948 'Initialize.' 949 self.localDictionary = {} 950 self.xmlElement = xmlElement 951 self.xmlProcessor = xmlElement.getXMLProcessor()
952
953 - def __repr__(self):
954 'Get the string representation of this Class.' 955 return str(self.__dict__)
956
957 - def getReturnValue(self):
958 'Get return value.' 959 self.getReturnValueWithoutDeletion() 960 del self.xmlProcessor.functions[-1] 961 return self.returnValue
962
963 - def processChildNodes(self, xmlElement):
964 'Process childNodes if shouldReturn is false.' 965 for childNode in xmlElement.childNodes: 966 if self.shouldReturn: 967 return 968 self.xmlProcessor.processXMLElement(childNode)
969 970
971 -class ClassFunction(BaseFunction):
972 'Class to get class results.'
973 - def getReturnValueByArguments(self, *arguments):
974 'Get return value by arguments.' 975 setFunctionLocalDictionary(arguments, self) 976 return self.getReturnValue()
977
979 'Get return value without deleting last function.' 980 self.returnValue = None 981 self.shouldReturn = False 982 self.xmlProcessor.functions.append(self) 983 self.processChildNodes(self.xmlElement) 984 return self.returnValue
985 986
987 -class ClassObject:
988 'Class to hold class attributes and functions.'
989 - def __init__(self, xmlElement):
990 'Initialize.' 991 self.functionDictionary = xmlElement.xmlObject.functionDictionary 992 self.selfDictionary = {} 993 for variable in xmlElement.xmlObject.variables: 994 self.selfDictionary[variable] = None
995
996 - def _getAccessibleAttribute(self, attributeName):
997 'Get the accessible attribute.' 998 if attributeName in self.selfDictionary: 999 return self.selfDictionary[attributeName] 1000 if attributeName in self.functionDictionary: 1001 function = self.functionDictionary[attributeName] 1002 function.classObject = self 1003 return function.getReturnValueByArguments 1004 return None
1005
1006 - def __repr__(self):
1007 'Get the string representation of this Class.' 1008 return str(self.__dict__)
1009
1010 - def _setAccessibleAttribute(self, attributeName, value):
1011 'Set the accessible attribute.' 1012 if attributeName in self.selfDictionary: 1013 self.selfDictionary[attributeName] = value
1014 1015
1016 -class Evaluator:
1017 'Base evaluator class.'
1018 - def __init__(self, word, xmlElement):
1019 'Set value to none.' 1020 self.value = None 1021 self.word = word
1022
1023 - def __repr__(self):
1024 'Get the string representation of this Class.' 1025 return str(self.__dict__)
1026
1027 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1028 'Execute the bracket.' 1029 pass
1030
1031 - def executeCenterOperation(self, evaluators, evaluatorIndex):
1032 'Execute operator which acts on the center.' 1033 pass
1034
1035 - def executeDictionary(self, dictionary, evaluators, keys, evaluatorIndex, nextEvaluator):
1036 'Execute the dictionary.' 1037 del evaluators[evaluatorIndex] 1038 enumeratorKeys = euclidean.getEnumeratorKeys(dictionary, keys) 1039 if enumeratorKeys.__class__ == list: 1040 nextEvaluator.value = [] 1041 for enumeratorKey in enumeratorKeys: 1042 if enumeratorKey in dictionary: 1043 nextEvaluator.value.append(dictionary[enumeratorKey]) 1044 else: 1045 print('Warning, key in executeKey in Evaluator in evaluate is not in for:') 1046 print(enumeratorKey) 1047 print(dictionary) 1048 return 1049 if enumeratorKeys in dictionary: 1050 nextEvaluator.value = dictionary[enumeratorKeys] 1051 else: 1052 print('Warning, key in executeKey in Evaluator in evaluate is not in for:') 1053 print(enumeratorKeys) 1054 print(dictionary)
1055
1056 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1057 'Execute the function.' 1058 pass
1059
1060 - def executeKey(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1061 'Execute the key index.' 1062 if self.value.__class__ == str: 1063 self.executeString(evaluators, keys, evaluatorIndex, nextEvaluator) 1064 return 1065 if self.value.__class__ == list: 1066 self.executeList(evaluators, keys, evaluatorIndex, nextEvaluator) 1067 return 1068 if self.value.__class__ == dict: 1069 self.executeDictionary(self.value, evaluators, keys, evaluatorIndex, nextEvaluator) 1070 return 1071 getAccessibleDictionaryFunction = getattr(self.value, '_getAccessibleDictionary', None) 1072 if getAccessibleDictionaryFunction != None: 1073 self.executeDictionary(getAccessibleDictionaryFunction(), evaluators, keys, evaluatorIndex, nextEvaluator) 1074 return 1075 if self.value.__class__.__name__ != 'XMLElement': 1076 return 1077 del evaluators[evaluatorIndex] 1078 enumeratorKeys = euclidean.getEnumeratorKeys(self.value.attributeDictionary, keys) 1079 if enumeratorKeys.__class__ == list: 1080 nextEvaluator.value = [] 1081 for enumeratorKey in enumeratorKeys: 1082 if enumeratorKey in self.value.attributeDictionary: 1083 nextEvaluator.value.append(getEvaluatedExpressionValue(self.value.attributeDictionary[enumeratorKey], self.value)) 1084 else: 1085 print('Warning, key in executeKey in Evaluator in evaluate is not in for:') 1086 print(enumeratorKey) 1087 print(self.value.attributeDictionary) 1088 return 1089 if enumeratorKeys in self.value.attributeDictionary: 1090 nextEvaluator.value = getEvaluatedExpressionValue(self.value.attributeDictionary[enumeratorKeys], self.value) 1091 else: 1092 print('Warning, key in executeKey in Evaluator in evaluate is not in for:') 1093 print(enumeratorKeys) 1094 print(self.value.attributeDictionary)
1095
1096 - def executeLeftOperation(self, evaluators, evaluatorIndex, operationLevel):
1097 'Execute operator which acts from the left.' 1098 pass
1099
1100 - def executeList(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1101 'Execute the key index.' 1102 del evaluators[evaluatorIndex] 1103 enumeratorKeys = euclidean.getEnumeratorKeys(self.value, keys) 1104 if enumeratorKeys.__class__ == list: 1105 nextEvaluator.value = [] 1106 for enumeratorKey in enumeratorKeys: 1107 intKey = euclidean.getIntFromValue(enumeratorKey) 1108 if self.getIsInRange(intKey): 1109 nextEvaluator.value.append(self.value[intKey]) 1110 else: 1111 print('Warning, key in executeList in Evaluator in evaluate is not in for:') 1112 print(enumeratorKey) 1113 print(self.value) 1114 return 1115 intKey = euclidean.getIntFromValue(enumeratorKeys) 1116 if self.getIsInRange(intKey): 1117 nextEvaluator.value = self.value[intKey] 1118 else: 1119 print('Warning, key in executeList in Evaluator in evaluate is not in for:') 1120 print(enumeratorKeys) 1121 print(self.value)
1122
1123 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1124 'Operate on two evaluators.' 1125 pass
1126
1127 - def executeRightOperation( self, evaluators, evaluatorIndex ):
1128 'Execute operator which acts from the right.' 1129 pass
1130
1131 - def executeString(self, evaluators, keys, evaluatorIndex, nextEvaluator):
1132 'Execute the string.' 1133 del evaluators[evaluatorIndex] 1134 enumeratorKeys = euclidean.getEnumeratorKeys(self.value, keys) 1135 if enumeratorKeys.__class__ == list: 1136 nextEvaluator.value = '' 1137 for enumeratorKey in enumeratorKeys: 1138 intKey = euclidean.getIntFromValue(enumeratorKey) 1139 if self.getIsInRange(intKey): 1140 nextEvaluator.value += self.value[intKey] 1141 else: 1142 print('Warning, key in executeString in Evaluator in evaluate is not in for:') 1143 print(enumeratorKey) 1144 print(self.value) 1145 return 1146 intKey = euclidean.getIntFromValue(enumeratorKeys) 1147 if self.getIsInRange(intKey): 1148 nextEvaluator.value = self.value[intKey] 1149 else: 1150 print('Warning, key in executeString in Evaluator in evaluate is not in for:') 1151 print(enumeratorKeys) 1152 print(self.value)
1153
1154 - def getIsInRange(self, keyIndex):
1155 'Determine if the keyIndex is in range.' 1156 if keyIndex == None: 1157 return False 1158 return keyIndex >= -len(self.value) and keyIndex < len(self.value)
1159 1160
1161 -class EvaluatorAddition(Evaluator):
1162 'Class to add two evaluators.'
1163 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1164 'Operate on two evaluators.' 1165 if operationLevel == 20: 1166 self.executePair(evaluators, evaluatorIndex)
1167
1168 - def executePair( self, evaluators, evaluatorIndex ):
1169 'Add two evaluators.' 1170 leftIndex = evaluatorIndex - 1 1171 rightIndex = evaluatorIndex + 1 1172 if leftIndex < 0: 1173 print('Warning, no leftKey in executePair in EvaluatorAddition for:') 1174 print(evaluators) 1175 print(evaluatorIndex) 1176 print(self) 1177 del evaluators[evaluatorIndex] 1178 return 1179 if rightIndex >= len(evaluators): 1180 print('Warning, no rightKey in executePair in EvaluatorAddition for:') 1181 print(evaluators) 1182 print(evaluatorIndex) 1183 print(self) 1184 del evaluators[evaluatorIndex] 1185 return 1186 rightValue = evaluators[rightIndex].value 1187 evaluators[leftIndex].value = self.getOperationValue(evaluators[leftIndex].value, evaluators[rightIndex].value) 1188 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ]
1189
1190 - def getEvaluatedValues(self, enumerable, keys, value):
1191 'Get evaluatedValues.' 1192 if enumerable.__class__ == dict: 1193 evaluatedValues = {} 1194 for key in keys: 1195 evaluatedValues[key] = self.getOperationValue(value, enumerable[key]) 1196 return evaluatedValues 1197 evaluatedValues = [] 1198 for key in keys: 1199 evaluatedValues.append(self.getOperationValue(value, enumerable[key])) 1200 return evaluatedValues
1201
1202 - def getOperationValue(self, leftValue, rightValue):
1203 'Get operation value.' 1204 leftKeys = getKeys(leftValue) 1205 rightKeys = getKeys(rightValue) 1206 if leftKeys == None and rightKeys == None: 1207 return self.getValueFromValuePair(leftValue, rightValue) 1208 if leftKeys == None: 1209 return self.getEvaluatedValues(rightValue, rightKeys, leftValue) 1210 if rightKeys == None: 1211 return self.getEvaluatedValues(leftValue, leftKeys, rightValue) 1212 leftKeys.sort(reverse=True) 1213 rightKeys.sort(reverse=True) 1214 if leftKeys != rightKeys: 1215 print('Warning, the leftKeys are different from the rightKeys in getOperationValue in EvaluatorAddition for:') 1216 print('leftValue') 1217 print(leftValue) 1218 print(leftKeys) 1219 print('rightValue') 1220 print(rightValue) 1221 print(rightKeys) 1222 print(self) 1223 return None 1224 if leftValue.__class__ == dict or rightValue.__class__ == dict: 1225 evaluatedValues = {} 1226 for leftKey in leftKeys: 1227 evaluatedValues[leftKey] = self.getOperationValue(leftValue[leftKey], rightValue[leftKey]) 1228 return evaluatedValues 1229 evaluatedValues = [] 1230 for leftKey in leftKeys: 1231 evaluatedValues.append(self.getOperationValue(leftValue[leftKey], rightValue[leftKey])) 1232 return evaluatedValues
1233
1234 - def getValueFromValuePair(self, leftValue, rightValue):
1235 'Add two values.' 1236 return leftValue + rightValue
1237 1238
1239 -class EvaluatorEqual(EvaluatorAddition):
1240 'Class to compare two evaluators.'
1241 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1242 'Operate on two evaluators.' 1243 if operationLevel == 15: 1244 self.executePair(evaluators, evaluatorIndex)
1245
1246 - def getBooleanFromValuePair(self, leftValue, rightValue):
1247 'Compare two values.' 1248 return leftValue == rightValue
1249
1250 - def getValueFromValuePair(self, leftValue, rightValue):
1251 'Get value from comparison.' 1252 return self.getBooleanFromValuePair(leftValue, rightValue)
1253 1254
1255 -class EvaluatorSubtraction(EvaluatorAddition):
1256 'Class to subtract two evaluators.'
1257 - def executeLeft( self, evaluators, evaluatorIndex ):
1258 'Minus the value to the right.' 1259 leftIndex = evaluatorIndex - 1 1260 rightIndex = evaluatorIndex + 1 1261 leftValue = None 1262 if leftIndex >= 0: 1263 leftValue = evaluators[leftIndex].value 1264 if leftValue != None: 1265 return 1266 rightValue = evaluators[rightIndex].value 1267 if rightValue == None: 1268 print('Warning, can not minus.') 1269 print( evaluators[rightIndex].word ) 1270 else: 1271 evaluators[rightIndex].value = self.getNegativeValue(rightValue) 1272 del evaluators[evaluatorIndex]
1273
1274 - def executeLeftOperation(self, evaluators, evaluatorIndex, operationLevel):
1275 'Minus the value to the right.' 1276 if operationLevel == 200: 1277 self.executeLeft(evaluators, evaluatorIndex)
1278
1279 - def getNegativeValue( self, value ):
1280 'Get the negative value.' 1281 keys = getKeys(value) 1282 if keys == None: 1283 return self.getValueFromSingleValue(value) 1284 for key in keys: 1285 value[key] = self.getNegativeValue(value[key]) 1286 return value
1287
1288 - def getValueFromSingleValue( self, value ):
1289 'Minus value.' 1290 return -value
1291
1292 - def getValueFromValuePair(self, leftValue, rightValue):
1293 'Subtract two values.' 1294 return leftValue - rightValue
1295 1296
1297 -class EvaluatorAnd(EvaluatorAddition):
1298 'Class to compare two evaluators.'
1299 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1300 'Operate on two evaluators.' 1301 if operationLevel == 12: 1302 self.executePair(evaluators, evaluatorIndex)
1303
1304 - def getBooleanFromValuePair(self, leftValue, rightValue):
1305 'And two values.' 1306 return leftValue and rightValue
1307
1308 - def getValueFromValuePair(self, leftValue, rightValue):
1309 'Get value from comparison.' 1310 return self.getBooleanFromValuePair(leftValue, rightValue)
1311 1312
1313 -class EvaluatorAttribute(Evaluator):
1314 'Class to handle an attribute.'
1315 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1316 'Execute the function.' 1317 executeNextEvaluatorArguments(self, evaluators, evaluatorIndex, nextEvaluator)
1318
1319 - def executeRightOperation( self, evaluators, evaluatorIndex ):
1320 'Execute operator which acts from the right.' 1321 attributeName = self.word[1 :] 1322 previousIndex = evaluatorIndex - 1 1323 previousEvaluator = evaluators[previousIndex] 1324 if previousEvaluator.value.__class__ == dict: 1325 from fabmetheus_utilities.geometry.geometry_utilities.evaluate_enumerables import dictionary_attribute 1326 self.value = dictionary_attribute._getAccessibleAttribute(attributeName, previousEvaluator.value) 1327 elif previousEvaluator.value.__class__ == list: 1328 from fabmetheus_utilities.geometry.geometry_utilities.evaluate_enumerables import list_attribute 1329 self.value = list_attribute._getAccessibleAttribute(attributeName, previousEvaluator.value) 1330 elif previousEvaluator.value.__class__ == str: 1331 from fabmetheus_utilities.geometry.geometry_utilities.evaluate_enumerables import string_attribute 1332 self.value = string_attribute._getAccessibleAttribute(attributeName, previousEvaluator.value) 1333 else: 1334 attributeKeywords = attributeName.split('.') 1335 self.value = previousEvaluator.value 1336 for attributeKeyword in attributeKeywords: 1337 self.value = getattr(self.value, '_getAccessibleAttribute', None)(attributeKeyword) 1338 if self.value == None: 1339 print('Warning, EvaluatorAttribute in evaluate can not get a getAccessibleAttributeFunction for:') 1340 print(attributeName) 1341 print(previousEvaluator.value) 1342 print(self) 1343 return 1344 del evaluators[previousIndex]
1345 1346
1347 -class EvaluatorBracketCurly(Evaluator):
1348 'Class to evaluate a string.'
1349 - def executeBracket(self, bracketBeginIndex, bracketEndIndex, evaluators):
1350 'Execute the bracket.' 1351 for evaluatorIndex in xrange(bracketEndIndex - 3, bracketBeginIndex, - 1): 1352 bracketEndIndex = getEndIndexConvertEquationValue(bracketEndIndex, evaluatorIndex, evaluators) 1353 evaluatedExpressionValueEvaluators = getBracketEvaluators(bracketBeginIndex, bracketEndIndex, evaluators) 1354 self.value = {} 1355 for evaluatedExpressionValueEvaluator in evaluatedExpressionValueEvaluators: 1356 keyValue = evaluatedExpressionValueEvaluator.value 1357 self.value[keyValue.key] = keyValue.value 1358 del evaluators[bracketBeginIndex + 1: bracketEndIndex + 1]
1359 1360
1361 -class EvaluatorBracketRound(Evaluator):
1362 'Class to evaluate a string.'
1363 - def __init__(self, word, xmlElement):
1364 'Set value to none.' 1365 self.arguments = [] 1366 self.value = None 1367 self.word = word
1368
1369 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1370 'Execute the bracket.' 1371 self.arguments = getBracketValuesDeleteEvaluator(bracketBeginIndex, bracketEndIndex, evaluators) 1372 if len( self.arguments ) < 1: 1373 return 1374 if len( self.arguments ) > 1: 1375 self.value = self.arguments 1376 else: 1377 self.value = self.arguments[0]
1378
1379 - def executeRightOperation( self, evaluators, evaluatorIndex ):
1380 'Evaluate the statement and delete the evaluators.' 1381 previousIndex = evaluatorIndex - 1 1382 if previousIndex < 0: 1383 return 1384 evaluators[ previousIndex ].executeFunction( evaluators, previousIndex, self )
1385 1386
1387 -class EvaluatorBracketSquare(Evaluator):
1388 'Class to evaluate a string.'
1389 - def executeBracket( self, bracketBeginIndex, bracketEndIndex, evaluators ):
1390 'Execute the bracket.' 1391 self.value = getBracketValuesDeleteEvaluator(bracketBeginIndex, bracketEndIndex, evaluators)
1392
1393 - def executeRightOperation( self, evaluators, evaluatorIndex ):
1394 'Evaluate the statement and delete the evaluators.' 1395 previousIndex = evaluatorIndex - 1 1396 if previousIndex < 0: 1397 return 1398 if self.value.__class__ != list: 1399 return 1400 evaluators[ previousIndex ].executeKey( evaluators, self.value, previousIndex, self )
1401 1402
1403 -class EvaluatorClass(Evaluator):
1404 'Class evaluator class.'
1405 - def __init__(self, word, xmlElement):
1406 'Set value to none.' 1407 self.value = None 1408 self.word = word 1409 self.xmlElement = xmlElement
1410
1411 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1412 'Execute the function.' 1413 if self.xmlElement.xmlObject == None: 1414 self.xmlElement.xmlObject = FunctionVariable(self.xmlElement) 1415 nextEvaluator.value = ClassObject(self.xmlElement) 1416 initializeFunction = None 1417 if '_init' in self.xmlElement.xmlObject.functionDictionary: 1418 function = self.xmlElement.xmlObject.functionDictionary['_init'] 1419 function.classObject = nextEvaluator.value 1420 setFunctionLocalDictionary(nextEvaluator.arguments, function) 1421 function.getReturnValue() 1422 del evaluators[evaluatorIndex]
1423 1424
1425 -class EvaluatorComma(Evaluator):
1426 'Class to join two evaluators.'
1427 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1428 'Operate on two evaluators.' 1429 if operationLevel != 0: 1430 return 1431 previousIndex = evaluatorIndex - 1 1432 if previousIndex < 0: 1433 evaluators[evaluatorIndex].value = None 1434 return 1435 if evaluators[previousIndex].word == ',': 1436 evaluators[evaluatorIndex].value = None 1437 return 1438 del evaluators[evaluatorIndex]
1439 1440
1441 -class EvaluatorConcatenate(Evaluator):
1442 'Class to join two evaluators.'
1443 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1444 'Operate on two evaluators.' 1445 if operationLevel != 80: 1446 return 1447 leftIndex = evaluatorIndex - 1 1448 if leftIndex < 0: 1449 del evaluators[evaluatorIndex] 1450 return 1451 rightIndex = evaluatorIndex + 1 1452 if rightIndex >= len(evaluators): 1453 del evaluators[ leftIndex : rightIndex ] 1454 return 1455 leftValue = evaluators[leftIndex].value 1456 rightValue = evaluators[rightIndex].value 1457 if leftValue.__class__ == rightValue.__class__ and (leftValue.__class__ == list or rightValue.__class__ == str): 1458 evaluators[leftIndex].value = leftValue + rightValue 1459 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ] 1460 return 1461 if leftValue.__class__ == list and rightValue.__class__ == int: 1462 if rightValue > 0: 1463 originalList = leftValue[:] 1464 for copyIndex in xrange( rightValue - 1 ): 1465 leftValue += originalList 1466 evaluators[leftIndex].value = leftValue 1467 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ] 1468 return 1469 if leftValue.__class__ == dict and rightValue.__class__ == dict: 1470 leftValue.update(rightValue) 1471 evaluators[leftIndex].value = leftValue 1472 del evaluators[ evaluatorIndex : evaluatorIndex + 2 ] 1473 return 1474 del evaluators[ leftIndex : evaluatorIndex + 2 ]
1475 1476
1477 -class EvaluatorDictionary(Evaluator):
1478 'Class to join two evaluators.'
1479 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1480 'Operate on two evaluators.' 1481 if operationLevel != 10: 1482 return 1483 leftEvaluatorIndex = evaluatorIndex - 1 1484 if leftEvaluatorIndex < 0: 1485 print('Warning, leftEvaluatorIndex is less than zero in EvaluatorDictionary for:') 1486 print(self) 1487 print(evaluators) 1488 return 1489 rightEvaluatorIndex = evaluatorIndex + 1 1490 if rightEvaluatorIndex >= len(evaluators): 1491 print('Warning, rightEvaluatorIndex too high in EvaluatorDictionary for:') 1492 print(rightEvaluatorIndex) 1493 print(self) 1494 print(evaluators) 1495 return 1496 evaluators[rightEvaluatorIndex].value = KeyValue(evaluators[leftEvaluatorIndex].value, evaluators[rightEvaluatorIndex].value) 1497 del evaluators[ leftEvaluatorIndex : rightEvaluatorIndex ]
1498 1499
1500 -class EvaluatorDivision(EvaluatorAddition):
1501 'Class to divide two evaluators.'
1502 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1503 'Operate on two evaluators.' 1504 if operationLevel == 40: 1505 self.executePair(evaluators, evaluatorIndex)
1506
1507 - def getValueFromValuePair(self, leftValue, rightValue):
1508 'Divide two values.' 1509 return leftValue / rightValue
1510 1511
1512 -class EvaluatorElement(Evaluator):
1513 'Element evaluator class.'
1514 - def __init__(self, word, xmlElement):
1515 'Set value to none.' 1516 self.value = None 1517 self.word = word 1518 self.xmlElement = xmlElement
1519
1520 - def executeCenterOperation(self, evaluators, evaluatorIndex):
1521 'Execute operator which acts on the center.' 1522 dotIndex = self.word.find('.') 1523 if dotIndex < 0: 1524 print('Warning, EvaluatorElement in evaluate can not find the dot for:') 1525 print(functionName) 1526 print(self) 1527 return 1528 attributeName = self.word[dotIndex + 1 :] 1529 moduleName = self.word[: dotIndex] 1530 if moduleName in globalModuleFunctionsDictionary: 1531 self.value = globalModuleFunctionsDictionary[moduleName](attributeName, self.xmlElement) 1532 return 1533 pluginModule = None 1534 if moduleName in globalElementNameSet: 1535 pluginModule = archive.getModuleWithPath(archive.getElementsPath(moduleName)) 1536 if pluginModule == None: 1537 print('Warning, EvaluatorElement in evaluate can not get a pluginModule for:') 1538 print(moduleName) 1539 print(self) 1540 return 1541 getAccessibleAttributeFunction = pluginModule._getAccessibleAttribute 1542 globalModuleFunctionsDictionary[moduleName] = getAccessibleAttributeFunction 1543 self.value = getAccessibleAttributeFunction(attributeName, self.xmlElement)
1544
1545 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1546 'Execute the function.' 1547 executeNextEvaluatorArguments(self, evaluators, evaluatorIndex, nextEvaluator)
1548 1549
1550 -class EvaluatorFalse(Evaluator):
1551 'Class to evaluate a string.'
1552 - def __init__(self, word, xmlElement):
1553 'Set value to zero.' 1554 self.value = False 1555 self.word = word
1556 1557
1558 -class EvaluatorFunction(Evaluator):
1559 'Function evaluator class.'
1560 - def __init__(self, word, xmlElement):
1561 'Set value to none.' 1562 self.value = None 1563 self.word = word 1564 self.xmlElement = xmlElement
1565
1566 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1567 'Execute the function.' 1568 if self.xmlElement.xmlObject == None: 1569 if 'return' in self.xmlElement.attributeDictionary: 1570 value = self.xmlElement.attributeDictionary['return'] 1571 self.xmlElement.xmlObject = getEvaluatorSplitWords(value) 1572 else: 1573 self.xmlElement.xmlObject = [] 1574 self.function = Function(self.xmlElement ) 1575 setFunctionLocalDictionary(nextEvaluator.arguments, self.function) 1576 nextEvaluator.value = self.function.getReturnValue() 1577 del evaluators[evaluatorIndex]
1578 1579
1580 -class EvaluatorFundamental(Evaluator):
1581 'Fundamental evaluator class.'
1582 - def executeCenterOperation(self, evaluators, evaluatorIndex):
1583 'Execute operator which acts on the center.' 1584 dotIndex = self.word.find('.') 1585 if dotIndex < 0: 1586 print('Warning, EvaluatorFundamental in evaluate can not find the dot for:') 1587 print(functionName) 1588 print(self) 1589 return 1590 attributeName = self.word[dotIndex + 1 :] 1591 moduleName = self.word[: dotIndex] 1592 if moduleName in globalModuleFunctionsDictionary: 1593 self.value = globalModuleFunctionsDictionary[moduleName](attributeName) 1594 return 1595 pluginModule = None 1596 if moduleName in globalFundamentalNameSet: 1597 pluginModule = archive.getModuleWithPath(archive.getFundamentalsPath(moduleName)) 1598 else: 1599 underscoredName = '_' + moduleName 1600 if underscoredName in globalFundamentalNameSet: 1601 pluginModule = archive.getModuleWithPath(archive.getFundamentalsPath(underscoredName)) 1602 if pluginModule == None: 1603 print('Warning, EvaluatorFundamental in evaluate can not get a pluginModule for:') 1604 print(moduleName) 1605 print(self) 1606 return 1607 getAccessibleAttributeFunction = pluginModule._getAccessibleAttribute 1608 globalModuleFunctionsDictionary[moduleName] = getAccessibleAttributeFunction 1609 self.value = getAccessibleAttributeFunction(attributeName)
1610
1611 - def executeFunction(self, evaluators, evaluatorIndex, nextEvaluator):
1612 'Execute the function.' 1613 executeNextEvaluatorArguments(self, evaluators, evaluatorIndex, nextEvaluator)
1614 1615
1616 -class EvaluatorGreaterEqual( EvaluatorEqual ):
1617 'Class to compare two evaluators.'
1618 - def getBooleanFromValuePair(self, leftValue, rightValue):
1619 'Compare two values.' 1620 return leftValue >= rightValue
1621 1622
1623 -class EvaluatorGreater( EvaluatorEqual ):
1624 'Class to compare two evaluators.'
1625 - def getBooleanFromValuePair(self, leftValue, rightValue):
1626 'Compare two values.' 1627 return leftValue > rightValue
1628 1629
1630 -class EvaluatorLessEqual( EvaluatorEqual ):
1631 'Class to compare two evaluators.'
1632 - def getBooleanFromValuePair(self, leftValue, rightValue):
1633 'Compare two values.' 1634 return leftValue <= rightValue
1635 1636
1637 -class EvaluatorLess( EvaluatorEqual ):
1638 'Class to compare two evaluators.'
1639 - def getBooleanFromValuePair(self, leftValue, rightValue):
1640 'Compare two values.' 1641 return leftValue < rightValue
1642 1643
1644 -class EvaluatorLocal(EvaluatorElement):
1645 'Class to get a local variable.'
1646 - def executeCenterOperation(self, evaluators, evaluatorIndex):
1647 'Execute operator which acts on the center.' 1648 functions = self.xmlElement.getXMLProcessor().functions 1649 if len(functions) < 1: 1650 print('Warning, there are no functions in EvaluatorLocal in evaluate for:') 1651 print(self.word) 1652 return 1653 attributeKeywords = self.word.split('.') 1654 self.value = functions[-1].localDictionary[attributeKeywords[0]] 1655 for attributeKeyword in attributeKeywords[1 :]: 1656 self.value = self.value._getAccessibleAttribute(attributeKeyword)
1657 1658
1659 -class EvaluatorModulo( EvaluatorDivision ):
1660 'Class to modulo two evaluators.'
1661 - def getValueFromValuePair(self, leftValue, rightValue):
1662 'Modulo two values.' 1663 return leftValue % rightValue
1664 1665
1666 -class EvaluatorMultiplication( EvaluatorDivision ):
1667 'Class to multiply two evaluators.'
1668 - def getValueFromValuePair(self, leftValue, rightValue):
1669 'Multiply two values.' 1670 return leftValue * rightValue
1671 1672
1673 -class EvaluatorNone(Evaluator):
1674 'Class to evaluate None.'
1675 - def __init__(self, word, xmlElement):
1676 'Set value to none.' 1677 self.value = None 1678 self.word = str(word)
1679 1680
1681 -class EvaluatorNot(EvaluatorSubtraction):
1682 'Class to compare two evaluators.'
1683 - def executeLeftOperation(self, evaluators, evaluatorIndex, operationLevel):
1684 'Minus the value to the right.' 1685 if operationLevel == 13: 1686 self.executeLeft(evaluators, evaluatorIndex)
1687
1688 - def getValueFromSingleValue( self, value ):
1689 'Minus value.' 1690 return not value
1691 1692
1693 -class EvaluatorNotEqual( EvaluatorEqual ):
1694 'Class to compare two evaluators.'
1695 - def getBooleanFromValuePair(self, leftValue, rightValue):
1696 'Compare two values.' 1697 return leftValue != rightValue
1698 1699
1700 -class EvaluatorNumeric(Evaluator):
1701 'Class to evaluate a string.'
1702 - def __init__(self, word, xmlElement):
1703 'Set value.' 1704 self.value = None 1705 self.word = word 1706 try: 1707 if '.' in word: 1708 self.value = float(word) 1709 else: 1710 self.value = int(word) 1711 except: 1712 print('Warning, EvaluatorNumeric in evaluate could not get a numeric value for:') 1713 print(word) 1714 print(xmlElement)
1715 1716
1717 -class EvaluatorOr( EvaluatorAnd ):
1718 'Class to compare two evaluators.'
1719 - def getBooleanFromValuePair(self, leftValue, rightValue):
1720 'Or two values.' 1721 return leftValue or rightValue
1722 1723
1724 -class EvaluatorPower(EvaluatorAddition):
1725 'Class to power two evaluators.'
1726 - def executePairOperation(self, evaluators, evaluatorIndex, operationLevel):
1727 'Operate on two evaluators.' 1728 if operationLevel == 60: 1729 self.executePair(evaluators, evaluatorIndex)
1730
1731 - def getValueFromValuePair(self, leftValue, rightValue):
1732 'Power of two values.' 1733 return leftValue ** rightValue
1734 1735
1736 -class EvaluatorSelf(EvaluatorElement):
1737 'Class to handle self.'
1738 - def executeCenterOperation(self, evaluators, evaluatorIndex):
1739 'Execute operator which acts on the center.' 1740 functions = self.xmlElement.getXMLProcessor().functions 1741 if len(functions) < 1: 1742 print('Warning, there are no functions in executeCenterOperation in EvaluatorSelf in evaluate for:') 1743 print(self.xmlElement) 1744 return 1745 function = functions[-1] 1746 attributeKeywords = self.word.split('.') 1747 self.value = function.classObject 1748 for attributeKeyword in attributeKeywords[1 :]: 1749 self.value = self.value._getAccessibleAttribute(attributeKeyword)
1750 1751
1752 -class EvaluatorTrue(Evaluator):
1753 'Class to evaluate a string.'
1754 - def __init__(self, word, xmlElement):
1755 'Set value to true.' 1756 self.value = True 1757 self.word = word
1758 1759
1760 -class EvaluatorValue(Evaluator):
1761 'Class to evaluate a string.'
1762 - def __init__(self, word):
1763 'Set value to none.' 1764 self.value = word 1765 self.word = str(word)
1766 1767
1768 -class Function(BaseFunction):
1769 'Class to get equation results.'
1770 - def __init__(self, xmlElement):
1771 'Initialize.' 1772 self.localDictionary = {} 1773 self.evaluatorSplitLine = xmlElement.xmlObject 1774 self.xmlElement = xmlElement 1775 self.xmlProcessor = xmlElement.getXMLProcessor()
1776
1778 'Get return value without deleting last function.' 1779 self.returnValue = None 1780 self.xmlProcessor.functions.append(self) 1781 if len(self.evaluatorSplitLine) < 1: 1782 self.shouldReturn = False 1783 self.processChildNodes(self.xmlElement) 1784 else: 1785 self.returnValue = getEvaluatedExpressionValueBySplitLine(self.evaluatorSplitLine, self.xmlElement) 1786 return self.returnValue
1787 1788
1789 -class FunctionVariable:
1790 'Class to hold class functions and variable set.'
1791 - def __init__(self, xmlElement):
1792 'Initialize.' 1793 self.functionDictionary = {} 1794 self.variables = [] 1795 self.processClass(xmlElement)
1796
1797 - def addToVariableSet(self, xmlElement):
1798 'Add to variables.' 1799 setLocalAttribute(xmlElement) 1800 keySplitLine = xmlElement.xmlObject.key.split('.') 1801 if len(keySplitLine) == 2: 1802 if keySplitLine[0] == 'self': 1803 variable = keySplitLine[1] 1804 if variable not in self.variables: 1805 self.variables.append(variable)
1806
1807 - def processClass(self, xmlElement):
1808 'Add class to FunctionVariable.' 1809 for childNode in xmlElement.childNodes: 1810 self.processFunction(childNode) 1811 if 'parentNode' in xmlElement.attributeDictionary: 1812 self.processClass(xmlElement.getXMLElementByImportID(xmlElement.attributeDictionary['parentNode']))
1813
1814 - def processFunction(self, xmlElement):
1815 'Add function to function dictionary.' 1816 if xmlElement.localName != 'function': 1817 return 1818 idKey = xmlElement.attributeDictionary['id'] 1819 if idKey in self.functionDictionary: 1820 return 1821 self.functionDictionary[idKey] = ClassFunction(xmlElement) 1822 for childNode in xmlElement.childNodes: 1823 self.processStatement(childNode)
1824
1825 - def processStatement(self, xmlElement):
1826 'Add self statement to variables.' 1827 if xmlElement.localName == 'statement': 1828 self.addToVariableSet(xmlElement) 1829 for childNode in xmlElement.childNodes: 1830 self.processStatement(childNode)
1831 1832
1833 -class KeyValue:
1834 'Class to hold a key value.'
1835 - def __init__(self, key=None, value=None):
1836 'Get key value.' 1837 self.key = key 1838 self.value = value
1839
1840 - def __repr__(self):
1841 'Get the string representation of this KeyValue.' 1842 return str(self.__dict__)
1843
1844 - def getByCharacter( self, character, line ):
1845 'Get by character.' 1846 dotIndex = line.find( character ) 1847 if dotIndex < 0: 1848 self.key = line 1849 self.value = None 1850 return self 1851 self.key = line[: dotIndex] 1852 self.value = line[dotIndex + 1 :] 1853 return self
1854
1855 - def getByDot(self, line):
1856 'Get by dot.' 1857 return self.getByCharacter('.', line )
1858
1859 - def getByEqual(self, line):
1860 'Get by dot.' 1861 return self.getByCharacter('=', line )
1862 1863
1864 -class ModuleXMLElement:
1865 'Class to get the in attribute, the index name and the value name.'
1866 - def __init__( self, xmlElement):
1867 'Initialize.' 1868 self.conditionSplitWords = None 1869 self.elseElement = None 1870 if 'condition' in xmlElement.attributeDictionary: 1871 self.conditionSplitWords = getEvaluatorSplitWords( xmlElement.attributeDictionary['condition'] ) 1872 else: 1873 print('Warning, could not find the condition attribute in ModuleXMLElement in evaluate for:') 1874 print(xmlElement) 1875 return 1876 if len( self.conditionSplitWords ) < 1: 1877 self.conditionSplitWords = None 1878 print('Warning, could not get split words for the condition attribute in ModuleXMLElement in evaluate for:') 1879 print(xmlElement) 1880 nextIndex = getNextChildIndex(xmlElement) 1881 if nextIndex >= len( xmlElement.parentNode.childNodes ): 1882 return 1883 nextXMLElement = xmlElement.parentNode.childNodes[ nextIndex ] 1884 lowerLocalName = nextXMLElement.localName.lower() 1885 if lowerLocalName != 'else' and lowerLocalName != 'elif': 1886 return 1887 xmlProcessor = xmlElement.getXMLProcessor() 1888 if lowerLocalName not in xmlProcessor.namePathDictionary: 1889 return 1890 self.pluginModule = archive.getModuleWithPath( xmlProcessor.namePathDictionary[ lowerLocalName ] ) 1891 if self.pluginModule == None: 1892 return 1893 self.elseElement = nextXMLElement
1894
1895 - def processElse( self, xmlElement):
1896 'Process the else statement.' 1897 if self.elseElement != None: 1898 self.pluginModule.processElse( self.elseElement)
1899 1900 1901 globalCreationDictionary = archive.getGeometryDictionary('creation') 1902 globalDictionaryOperatorBegin = { 1903 '||' : EvaluatorConcatenate, 1904 '==' : EvaluatorEqual, 1905 '>=' : EvaluatorGreaterEqual, 1906 '<=' : EvaluatorLessEqual, 1907 '!=' : EvaluatorNotEqual, 1908 '**' : EvaluatorPower } 1909 globalModuleEvaluatorDictionary = {} 1910 globalFundamentalNameSet = set(archive.getPluginFileNamesFromDirectoryPath(archive.getFundamentalsPath())) 1911 addPrefixDictionary(globalModuleEvaluatorDictionary, globalFundamentalNameSet, EvaluatorFundamental) 1912 globalElementNameSet = set(archive.getPluginFileNamesFromDirectoryPath(archive.getElementsPath())) 1913 addPrefixDictionary(globalModuleEvaluatorDictionary, globalElementNameSet, EvaluatorElement) 1914 globalModuleEvaluatorDictionary['self'] = EvaluatorSelf 1915 globalSplitDictionaryOperator = { 1916 '+' : EvaluatorAddition, 1917 '{' : EvaluatorBracketCurly, 1918 '}' : Evaluator, 1919 '(' : EvaluatorBracketRound, 1920 ')' : Evaluator, 1921 '[' : EvaluatorBracketSquare, 1922 ']' : Evaluator, 1923 ',' : EvaluatorComma, 1924 ':' : EvaluatorDictionary, 1925 '/' : EvaluatorDivision, 1926 '>' : EvaluatorGreater, 1927 '<' : EvaluatorLess, 1928 '%' : EvaluatorModulo, 1929 '*' : EvaluatorMultiplication, 1930 '-' : EvaluatorSubtraction } 1931 globalSplitDictionary = getSplitDictionary() # must be after globalSplitDictionaryOperator 1932