def _parse_entry(self, lines): r""" Internal method for parsing a card of the case control deck Parses a single case control deck card into 4 sections 1. param_name - obvious 2. Value - still kind of obvious 3. options - rarely used data 4. param_type - STRESS-type, SUBCASE-type, PARAM-type, SET-type, BEGIN_BULK-type It's easier with examples: param_type = SUBCASE-type SUBCASE 1 -> paramName=SUBCASE value=1 options=[] param_type = STRESS-type STRESS = ALL -> paramName=STRESS value=ALL options=[] STRAIN(PLOT) = 5 -> paramName=STRAIN value=5 options=[PLOT] TITLE = stuff -> paramName=TITLE value=stuff options=[] param_type = SET-type SET 1 = 10,20,30 -> paramName=SET value=[10,20,30] options = 1 param_type = BEGIN_BULK-type BEGIN BULK -> paramName=BEGIN value=BULK options = [] param_type = CSV-type PARAM,FIXEDB,-1 -> paramName=PARAM value=FIXEDB options = [-1] The param_type is the "macro" form of the data (similar to integer, float, string). The value is generally whats on the RHS of the equals sign (assuming it's there). Options are modifiers on the data. Form things like the PARAM card or the SET card they arent as clear, but the param_type lets the program know how to format it when writing it out. Parameters ---------- lines : List[str, str, ...] list of lines Returns ------- paramName : str see brief value : List[...] see brief options : List[str/int/float] see brief param_type : str/int/float/List see brief """ i = 0 options = [] value = None key = None param_type = None line = lines[i] #print(line) #print("*****lines = ", lines) equals_count = 0 for letter in line: if letter == '=': equals_count += 1 line_upper = line.upper().expandtabs() #print("line_upper = %r" % line) #print(' equals_count = %s' % equals_count) if line_upper.startswith('SUBCASE'): param_type = split_equal_space(line_upper, 'SUBCASE', 'SUBCASE = 5') key = 'SUBCASE' #print("key=%r isubcase=%r" % (key, isubcase)) value = integer(param_type, line_upper) #self.isubcase = int(isubcase) param_type = 'SUBCASE-type' assert key.upper() == key, key elif line_upper.startswith( ('LABEL', 'SUBT', 'TITL')): # SUBTITLE/TITLE try: eindex = line.index('=') except ValueError: msg = "cannot find an = sign in LABEL/SUBTITLE/TITLE line\n" msg += "line = %r" % line_upper.strip() raise RuntimeError(msg) key = line_upper[0:eindex].strip() value = line[eindex + 1:].strip() options = [] param_type = 'STRING-type' elif line_upper.startswith('SET ') and equals_count == 1: obj = SET.add_from_case_control(line_upper, lines, i) #if 0: #key = obj.key #options = None #value = obj #param_type = 'OBJ-type' #else: key = obj.key options = obj.set_id value = obj.value param_type = 'SET-type' elif line_upper.startswith('SETMC ') and equals_count == 1: obj = SETMC.add_from_case_control(line_upper, lines, i) #if 0: #key = obj.key #options = None #value = obj #param_type = 'OBJ-type' #else: key = value.key # type: str options = obj.set_id # type: List[int] value = obj.value # type: int param_type = 'SET-type' #elif line_upper.startswith(CHECK_CARD_NAMES): #if '(' in line: #key = line_upper.strip().split('(', 1)[0].strip() #elif '=' in line: #key = line_upper.strip().split('=', 1)[0].strip() #else: #msg = 'expected item of form "name = value" line=%r' % line.strip() #raise RuntimeError(msg) #key = update_param_name(key) #obj = CHECK_CARD_DICT[key].add_from_case_control(line, line_upper, lines, i) #value = obj.value #options = obj.options #param_type = 'STRESS-type' #key = obj.type #elif line_upper.startswith(INT_CARD_NAMES): #if '=' in line: #(name, value) = line_upper.strip().split('=') #else: #msg = 'expected item of form "name = value" line=%r' % line.strip() #raise RuntimeError(msg) #name = update_param_name(name) #obj = INT_CARD_DICT[name].add_from_case_control(line, line_upper, lines, i) #key = obj.type ##if 0: ##value = obj.value ##options = [] ##param_type = 'STRESS-type' ##else: #value = obj #options = None #param_type = 'OBJ-type' #key = obj.type #elif line_upper.startswith(INTSTR_CARD_NAMES): #if '=' in line: #(name, value) = line_upper.strip().split('=') #else: #msg = 'expected item of form "name = value" line=%r' % line.strip() #raise RuntimeError(msg) #name = name.strip() #obj = INTSTR_CARD_DICT[name].add_from_case_control(line, line_upper, lines, i) #key = obj.type ##if 0: ##value = obj.value ##options = [] ##param_type = 'STRESS-type' ##else: #value = obj #options = None #param_type = 'OBJ-type' #key = obj.type elif line_upper.startswith('EXTSEOUT'): options = None param_type = 'OBJ-type' obj = EXTSEOUT.add_from_case_control(line_upper.strip()) value = obj key = obj.type elif line_upper.startswith('WEIGHTCHECK'): options = None param_type = 'OBJ-type' obj = WEIGHTCHECK.add_from_case_control(line, line_upper, lines, i) value = obj key = obj.type elif line_upper.startswith('GROUNDCHECK'): options = None param_type = 'OBJ-type' obj = GROUNDCHECK.add_from_case_control(line, line_upper, lines, i) value = obj key = obj.type elif line_upper.startswith('MODCON'): options = None param_type = 'OBJ-type' obj = MODCON.add_from_case_control(line, line_upper, lines, i) value = obj key = obj.type #elif line_upper.startswith('AUXMODEL'): #options = None #param_type = 'OBJ-type' #value = AUXMODEL.add_from_case_control(line, line_upper, lines, i) #key = value.type #elif line_upper.startswith(STR_CARD_NAMES): #if '=' in line: #(name, value) = line_upper.strip().split('=') #else: #msg = 'expected item of form "name = value" line=%r' % line.strip() #raise RuntimeError(msg) #name = name.strip() #obj = STR_CARD_DICT[name].add_from_case_control(line, line_upper, lines, i) #value = obj #options = None #param_type = 'OBJ-type' #key = obj.type elif line_upper.startswith('TEMP'): if '=' in line: (key, value) = line_upper.strip().split('=') else: msg = 'expected item of form "name = value" line=%r' % line.strip( ) raise RuntimeError(msg) assert equals_count == 1, line_upper if '(' in line_upper: options = None param_type = 'STRESS-type' key = key.strip().upper() value = value.strip() if self.debug: self.log.debug("key=%r value=%r" % (key, value)) param_type = 'STRESS-type' assert key.upper() == key, key #param_type = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') assert len(options) == 1, line_upper # handle TEMPERATURE(INITIAL) and TEMPERATURE(LOAD) cards key = 'TEMPERATURE(%s)' % options[0] value = value.strip() options = [] else: key = 'TEMPERATURE(BOTH)' options = [] param_type = 'STRESS-type' value = int(value) elif line_upper.startswith('RIGID'): if '=' in line: (key, value) = line_upper.strip().split('=') key = key.strip() value = value.strip() else: msg = 'expected item of form "name = value" line=%r' % line.strip( ) raise RuntimeError(msg) #print('line_upper=%r' % line_upper) assert key == 'RIGID', 'key=%r value=%r line=%r' % (key, value, line) param_type = 'STRESS-type' options = [] #RIGID = LAGR, LGELIM, LAGRANGE, STIFF, LINEAR if value in ['LAGR', 'LAGRAN']: value = 'LAGRANGE' elif value in ['LGELIM', 'LAGRANGE', 'STIFF', 'LINEAR', 'AUTO']: pass else: raise NotImplementedError('key=%r value=%r line=%r' % (key, value, line)) elif equals_count == 1: # STRESS if '=' in line: (key, value) = line_upper.strip().split('=') else: msg = 'expected item of form "name = value" line=%r' % line.strip( ) raise RuntimeError(msg) key = key.strip().upper() value = value.strip() if self.debug: self.log.debug("key=%r value=%r" % (key, value)) param_type = 'STRESS-type' assert key.upper() == key, key if '(' in key: # comma may be in line - STRESS-type #param_type = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') elif ',' in value: # STRESS-type; special TITLE = stuffA,stuffB #print('A ??? line = ',line) #raise RuntimeError(line) pass else: # STRESS-type; TITLE = stuff #print('B ??? line = ',line) pass key = update_param_name(key) verify_card(key, value, options, line) assert key.upper() == key, key elif equals_count > 2 and '(' in line and 'FLSPOUT' not in line: #GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES #print('****', lines) assert len(lines) == 1, lines line = lines[0] try: key, value_options = line.split('(', 1) except ValueError: msg = 'Expected a "(", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += '%r' % line raise ValueError(msg) try: options_paren, value = value_options.rsplit('=', 1) except ValueError: msg = 'Expected a "=", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += 'value_options=%r\n' % value_options msg += '%r' % line raise ValueError(msg) options_paren = options_paren.strip() value = value.strip() if value.isdigit(): value = int(value) if not options_paren.endswith(')'): raise RuntimeError(line) str_options = options_paren[:-1] if '(' in str_options: options = split_by_mixed_commas_parentheses(str_options) else: options = str_options.split(',') param_type = 'STRESS-type' key = key.upper() elif line_upper.startswith('BEGIN'): # begin bulk try: (key, value) = line_upper.split(' ') except ValueError: msg = 'excepted "BEGIN BULK" found=%r' % line raise RuntimeError(msg) key = key.upper() param_type = 'BEGIN_BULK-type' assert key.upper() == key, key elif 'PARAM' in line_upper: # param key, value, options, param_type = _split_param(line, line_upper) elif ' ' not in line: key = line.strip().upper() value = line.strip() options = None param_type = 'KEY-type' assert key.upper() == key, key else: msg = 'generic catch all...line=%r' % line key = '' value = line options = None param_type = 'KEY-type' assert key.upper() == key, key i += 1 assert key.upper() == key, 'key=%s param_type=%s' % (key, param_type) return (i, key, value, options, param_type)
def _parse_entry(self, lines): r""" Internal method for parsing a card of the case control deck Parses a single case control deck card into 4 sections 1. paramName - obvious 2. Value - still kind of obvious 3. options - rarely used data 4. paramType - STRESS-type, SUBCASE-type, PARAM-type, SET-type, BEGIN_BULK-type It's easier with examples: paramType = SUBCASE-type SUBCASE 1 -> paramName=SUBCASE value=1 options=[] paramType = STRESS-type STRESS = ALL -> paramName=STRESS value=ALL options=[] STRAIN(PLOT) = 5 -> paramName=STRAIN value=5 options=[PLOT] TITLE = stuff -> paramName=TITLE value=stuff options=[] paramType = SET-type SET 1 = 10,20,30 -> paramName=SET value=[10,20,30] options = 1 paramType = BEGIN_BULK-type BEGIN BULK -> paramName=BEGIN value=BULK options = [] paramType = CSV-type PARAM,FIXEDB,-1 -> paramName=PARAM value=FIXEDB options = [-1] The paramType is the "macro" form of the data (similar to integer, float, string). The value is generally whats on the RHS of the equals sign (assuming it's there). Options are modifiers on the data. Form things like the PARAM card or the SET card they arent as clear, but the paramType lets the program know how to format it when writing it out. :param self: the CaseControlDeck object :param lines: list of lines :return: paramName see brief :return: value see brief :return: options see brief :return: paramType see brief """ i = 0 options = [] value = None key = None param_type = None line = lines[i] #print(line) #print("*****lines = ", lines) equals_count = 0 for letter in line: if letter == '=': equals_count += 1 line_upper = line.upper() #print("line_upper = %r" % line) if line_upper.startswith('SUBCASE'): #print("line = %r" % line) line2 = line.replace('=', '') sline = line2.split() if len(sline) != 2: msg = "trying to parse %r..." % line raise RuntimeError(msg) (key, param_type) = sline key = key.upper() #print("key=%r isubcase=%r" % (key, isubcase)) value = int(param_type) #self.isubcase = int(isubcase) param_type = 'SUBCASE-type' assert key.upper() == key, key elif (line_upper.startswith('LABEL') or line_upper.startswith('SUBT') or # SUBTITLE line_upper.startswith('TITL')): # TITLE try: eIndex = line.index('=') except: msg = "cannot find an = sign in LABEL/SUBTITLE/TITLE line\n" msg += "line = %r" % line_upper.strip() raise RuntimeError(msg) key = line_upper[0:eIndex].strip() value = line[eIndex + 1:].strip() options = [] param_type = 'STRING-type' elif line_upper.startswith('SET ') and equals_count == 1: # would have been caught by STRESS-type sline = line_upper.split('=') assert len(sline) == 2, sline key, value = sline try: (key, ID) = key.split() except: raise RuntimeError(key) key = key + ' ' + ID assert key.upper() == key, key options = int(ID) if self.debug: self.log.debug('SET-type key=%r ID=%r' % (key, ID)) fivalues = value.rstrip(' ,').split(',') # float/int values #: .. todo:: should be more efficient multiline reader... # read more lines.... if line[-1].strip() == ',': i += 1 #print("rawSETLine = %r" % (lines[i])) while 1: if ',' == lines[i].strip()[-1]: fivalues += lines[i][:-1].split(',') else: # last case fivalues += lines[i].split(',') #print("fivalues last = i=%s %r" % (i, lines[i])) i += 1 break i += 1 #print("len(fivalues) = ",len(fivalues)) value = fivalues param_type = 'SET-type' elif equals_count == 1: # STRESS if '=' in line: (key, value) = line_upper.strip().split('=') else: msg = 'expected item of form "name = value" line=%r' % line.strip() raise RuntimeError(msg) key = key.strip().upper() value = value.strip() if self.debug: self.log.debug("key=%r value=%r" % (key, value)) param_type = 'STRESS-type' assert key.upper() == key, key if '(' in key: # comma may be in line - STRESS-type #paramType = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') # handle TEMPERATURE(INITIAL) and TEMPERATURE(LOAD) cards if key == 'TEMPERATURE' or key == 'TEMP': key = 'TEMPERATURE(%s)' % (options[0]) options = [] #print("key=%r options=%s" %(key,options)) #elif ' ' in key and ',' in value: # SET-type #(key, ID) = key.split() #key = key + ' ' + ID #if self.debug: #self.log.debug('SET-type key=%r ID=%r' % (key, ID)) #fivalues = value.rstrip(' ,').split(',') # float/int values ##: .. todo:: should be more efficient multiline reader... ## read more lines.... #if line[-1].strip() == ',': #i += 1 ##print("rawSETLine = %r" % (lines[i])) #while 1: #if ',' == lines[i].strip()[-1]: #fivalues += lines[i][:-1].split(',') #else: # last case #fivalues += lines[i].split(',') ##print("fivalues last = i=%s %r" % (i, lines[i])) #i += 1 #break #i += 1 ##print("len(fivalues) = ",len(fivalues)) #value = fivalues #options = ID # needed a place to put it... #param_type = 'SET-type' elif ',' in value: # STRESS-type; special TITLE = stuffA,stuffB #print('A ??? line = ',line) #raise RuntimeError(line) pass else: # STRESS-type; TITLE = stuff #print('B ??? line = ',line) pass key = update_param_name(key.strip().upper()) verify_card(key, value, options, line) assert key.upper() == key, key elif line_upper.startswith('BEGIN'): # begin bulk try: (key, value) = line_upper.split(' ') except: msg = 'excepted "BEGIN BULK" found=%r' % line raise RuntimeError(msg) key = key.upper() param_type = 'BEGIN_BULK-type' assert key.upper() == key, key elif 'PARAM' in line_upper: # param sline = line_upper.split(',') if len(sline) != 3: raise SyntaxError("trying to parse %r..." % line) (key, value, options) = sline param_type = 'CSV-type' assert key.upper() == key, key elif ' ' not in line: key = line.strip().upper() value = line.strip() options = None param_type = 'KEY-type' assert key.upper() == key, key else: msg = 'generic catch all...line=|%r|' % line key = '' value = line options = None param_type = 'KEY-type' assert key.upper() == key, key i += 1 assert key.upper() == key, key return (i, key, value, options, param_type)
def _parse_entry(self, lines): r""" Internal method for parsing a card of the case control deck Parses a single case control deck card into 4 sections 1. paramName - obvious 2. Value - still kind of obvious 3. options - rarely used data 4. paramType - STRESS-type, SUBCASE-type, PARAM-type, SET-type, BEGIN_BULK-type It's easier with examples: paramType = SUBCASE-type SUBCASE 1 -> paramName=SUBCASE value=1 options=[] paramType = STRESS-type STRESS = ALL -> paramName=STRESS value=ALL options=[] STRAIN(PLOT) = 5 -> paramName=STRAIN value=5 options=[PLOT] TITLE = stuff -> paramName=TITLE value=stuff options=[] paramType = SET-type SET 1 = 10,20,30 -> paramName=SET value=[10,20,30] options = 1 paramType = BEGIN_BULK-type BEGIN BULK -> paramName=BEGIN value=BULK options = [] paramType = CSV-type PARAM,FIXEDB,-1 -> paramName=PARAM value=FIXEDB options = [-1] The paramType is the "macro" form of the data (similar to integer, float, string). The value is generally whats on the RHS of the equals sign (assuming it's there). Options are modifiers on the data. Form things like the PARAM card or the SET card they arent as clear, but the paramType lets the program know how to format it when writing it out. :param self: the CaseControlDeck object :param lines: list of lines :return: paramName see brief :return: value see brief :return: options see brief :return: paramType see brief """ i = 0 options = [] value = None key = None param_type = None line = lines[i] #print line #print "*****lines = ", lines equals_count = 0 for letter in line: if letter == '=': equals_count += 1 line_upper = line.upper() #print("line_upper = %r" % line) if line_upper.startswith('SUBCASE'): #print "line = |%r|" %(line) line2 = line.replace('=', '') sline = line2.split() if len(sline) != 2: msg = "trying to parse |%s|..." % line raise RuntimeError(msg) (key, param_type) = sline #print("key=|%s| isubcase=|%s|" % (key,isubcase)) value = int(param_type) #self.isubcase = int(isubcase) param_type = 'SUBCASE-type' elif (line_upper.startswith('LABEL') or line_upper.startswith('SUBT') or # SUBTITLE line_upper.startswith('TITL')): # TITLE try: eIndex = line.index('=') except: msg = "cannot find an = sign in LABEL/SUBTITLE/TITLE line\n" msg += "line = |%s|" % (line_upper.strip()) raise RuntimeError(msg) key = line[0:eIndex].strip() value = line[eIndex + 1:].strip() options = [] param_type = 'STRING-type' elif equals_count == 1: # STRESS if '=' in line: (key, value) = line.strip().split('=') else: msg = 'expected item of form "name = value" line=|%r|' % ( line.strip()) raise RuntimeError(msg) key = key.strip() value = value.strip() if self.debug: self.log.debug("key=|%s| value=|%s|" % (key, value)) param_type = 'STRESS-type' if '(' in key: # comma may be in line - STRESS-type #paramType = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') # handle TEMPERATURE(INITIAL) and TEMPERATURE(LOAD) cards if key == 'TEMPERATURE' or key == 'TEMP': key = 'TEMPERATURE(%s)' % (options[0]) options = [] #print "key=|%s| options=%s" %(key,options) elif ' ' in key and ',' in value: # SET-type (key, ID) = key.split() key = key + ' ' + ID if self.debug: self.log.debug('SET-type key=%s ID=%s' % (key, ID)) fivalues = value.rstrip(' ,').split(',') # float/int values #: .. todo:: should be more efficient multiline reader... # read more lines.... if line[-1].strip() == ',': i += 1 #print "rawSETLine = |%r|" %(lines[i]) while 1: if ',' == lines[i].strip()[-1]: fivalues += lines[i][:-1].split(',') else: # last case fivalues += lines[i].split(',') #print "fivalues last = i=%s |%r|" %(i,lines[i]) i += 1 break i += 1 #print "len(fivalues) = ",len(fivalues) value = fivalues options = ID # needed a place to put it... param_type = 'SET-type' elif ',' in value: # STRESS-type; special TITLE = stuffA,stuffB #print 'A ??? line = ',line #raise RuntimeError(line) pass else: # STRESS-type; TITLE = stuff #print 'B ??? line = ',line pass key = update_param_name(key.strip()) verify_card(key, value, options, line) elif line_upper.startswith('BEGIN'): # begin bulk try: (key, value) = line_upper.split(' ') except: msg = 'excepted "BEGIN BULK" found=|%r|' % line raise RuntimeError(msg) param_type = 'BEGIN_BULK-type' elif 'PARAM' in line_upper: # param sline = line.split(',') if len(sline) != 3: raise SyntaxError("trying to parse |%s|..." % line) (key, value, options) = sline param_type = 'CSV-type' elif ' ' not in line: key = line.strip() value = line.strip() options = None param_type = 'KEY-type' else: msg = 'generic catch all...line=|%r|' % (line) key = '' value = line options = None param_type = 'KEY-type' i += 1 return (i, key, value, options, param_type)
def _parse_entry(self, lines): r""" Internal method for parsing a card of the case control deck Parses a single case control deck card into 4 sections 1. paramName - obvious 2. Value - still kind of obvious 3. options - rarely used data 4. paramType - STRESS-type, SUBCASE-type, PARAM-type, SET-type, BEGIN_BULK-type It's easier with examples: paramType = SUBCASE-type SUBCASE 1 -> paramName=SUBCASE value=1 options=[] paramType = STRESS-type STRESS = ALL -> paramName=STRESS value=ALL options=[] STRAIN(PLOT) = 5 -> paramName=STRAIN value=5 options=[PLOT] TITLE = stuff -> paramName=TITLE value=stuff options=[] paramType = SET-type SET 1 = 10,20,30 -> paramName=SET value=[10,20,30] options = 1 paramType = BEGIN_BULK-type BEGIN BULK -> paramName=BEGIN value=BULK options = [] paramType = CSV-type PARAM,FIXEDB,-1 -> paramName=PARAM value=FIXEDB options = [-1] The paramType is the "macro" form of the data (similar to integer, float, string). The value is generally whats on the RHS of the equals sign (assuming it's there). Options are modifiers on the data. Form things like the PARAM card or the SET card they arent as clear, but the paramType lets the program know how to format it when writing it out. Parameters ---------- lines : List[str, str, ...] list of lines Returns ------- paramName : str see brief value : List[...] see brief options : List[str/int/float] see brief paramType : str/int/float/List see brief """ i = 0 options = [] value = None key = None param_type = None line = lines[i] #print(line) #print("*****lines = ", lines) equals_count = 0 for letter in line: if letter == '=': equals_count += 1 line_upper = line.upper() #print("line_upper = %r" % line) #print(' equals_count = %s' % equals_count) if line_upper.startswith('SUBCASE'): #print("line = %r" % line) line2 = line.replace('=', '') sline = line2.split() if len(sline) != 2: msg = "trying to parse %r..." % line raise RuntimeError(msg) (key, param_type) = sline key = key.upper() #print("key=%r isubcase=%r" % (key, isubcase)) value = int(param_type) #self.isubcase = int(isubcase) param_type = 'SUBCASE-type' assert key.upper() == key, key elif line_upper.startswith(('LABEL', 'SUBT', 'TITL')): # SUBTITLE/TITLE try: eindex = line.index('=') except: msg = "cannot find an = sign in LABEL/SUBTITLE/TITLE line\n" msg += "line = %r" % line_upper.strip() raise RuntimeError(msg) key = line_upper[0:eindex].strip() value = line[eindex + 1:].strip() options = [] param_type = 'STRING-type' elif (line_upper.startswith('SET ') or line_upper.startswith('SETMC ') and equals_count == 1): # would have been caught by STRESS-type sline = line_upper.split('=') assert len(sline) == 2, sline key, value = sline try: (key, ID) = key.split() except: raise RuntimeError(key) key = key + ' ' + ID assert key.upper() == key, key options = int(ID) if self.debug: self.log.debug('SET-type key=%r ID=%r' % (key, ID)) fivalues = value.rstrip(' ,').split(',') # float/int values #: .. todo:: should be more efficient multiline reader... # read more lines.... if line[-1].strip() == ',': i += 1 #print("rawSETLine = %r" % (lines[i])) while 1: if lines[i].strip()[-1] == ',': fivalues += lines[i][:-1].split(',') else: # last case fivalues += lines[i].split(',') #print("fivalues last = i=%s %r" % (i, lines[i])) i += 1 break i += 1 #print("len(fivalues) = ",len(fivalues)) value = fivalues param_type = 'SET-type' elif equals_count == 1: # STRESS if '=' in line: (key, value) = line_upper.strip().split('=') else: msg = 'expected item of form "name = value" line=%r' % line.strip() raise RuntimeError(msg) key = key.strip().upper() value = value.strip() if self.debug: self.log.debug("key=%r value=%r" % (key, value)) param_type = 'STRESS-type' assert key.upper() == key, key if '(' in key: # comma may be in line - STRESS-type #paramType = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') # handle TEMPERATURE(INITIAL) and TEMPERATURE(LOAD) cards if key in ['TEMPERATURE', 'TEMP']: option = options[0] if option == '': option = 'BOTH' key = 'TEMPERATURE' options = [option] #print("key=%r options=%s" %(key,options)) #elif ' ' in key and ',' in value: # SET-type #(key, ID) = key.split() #key = key + ' ' + ID #if self.debug: #self.log.debug('SET-type key=%r ID=%r' % (key, ID)) #fivalues = value.rstrip(' ,').split(',') # float/int values ##: .. todo:: should be more efficient multiline reader... ## read more lines.... #if line[-1].strip() == ',': #i += 1 ##print("rawSETLine = %r" % (lines[i])) #while 1: #if ',' == lines[i].strip()[-1]: #fivalues += lines[i][:-1].split(',') #else: # last case #fivalues += lines[i].split(',') ##print("fivalues last = i=%s %r" % (i, lines[i])) #i += 1 #break #i += 1 ##print("len(fivalues) = ",len(fivalues)) #value = fivalues #options = ID # needed a place to put it... #param_type = 'SET-type' elif ',' in value: # STRESS-type; special TITLE = stuffA,stuffB #print('A ??? line = ',line) #raise RuntimeError(line) pass else: # STRESS-type; TITLE = stuff #print('B ??? line = ',line) if key in ['TEMPERATURE', 'TEMP']: assert len(options) == 0, options key = 'TEMPERATURE' options = ['BOTH'] key = update_param_name(key.strip().upper()) verify_card(key, value, options, line) assert key.upper() == key, key elif equals_count > 2 and '(' in line and 'FLSPOUT' not in line: #GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES #print('****', lines) assert len(lines) == 1, lines line = lines[0] try: key, value_options = line.split('(', 1) except ValueError: msg = 'Expected a "(", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += '%r' % line raise ValueError(msg) try: options_paren, value = value_options.rsplit('=', 1) except ValueError: msg = 'Expected a "=", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += 'value_options=%r\n' % value_options msg += '%r' % line raise ValueError(msg) options_paren = options_paren.strip() value = value.strip() if value.isdigit(): value = int(value) if not options_paren.endswith(')'): raise RuntimeError(line) str_options = options_paren[:-1] if '(' in str_options: options_start = [] options_end = [] icomma = str_options.index(',') iparen = str_options.index('(') #print('icomma=%s iparen=%s' % (icomma, iparen)) while icomma < iparen: base, str_options = str_options.split(',', 1) str_options = str_options.strip() icomma = str_options.index(',') iparen = str_options.index('(') options_start.append(base.strip()) #print(' icomma=%s iparen=%s' % (icomma, iparen)) #print(' options_start=%s' % options_start) icomma = str_options.rindex(',') iparen = str_options.rindex(')') #print('icomma=%s iparen=%s' % (icomma, iparen)) while icomma > iparen: str_options, end = str_options.rsplit(')', 1) str_options = str_options.strip() + ')' #print(' str_options = %r' % str_options) icomma = str_options.rindex(',') iparen = str_options.rindex(')') options_end.append(end.strip(' ,')) #print(' icomma=%s iparen=%s' % (icomma, iparen)) #print(' options_end=%s' % options_end[::-1]) #print() #print('options_start=%s' % options_start) #print('options_end=%s' % options_end) #print('leftover = %r' % str_options) options = options_start + [str_options] + options_end[::-1] else: options = str_options.split(',') param_type = 'STRESS-type' key = key.upper() #print('options =', options) #asdf elif line_upper.startswith('BEGIN'): # begin bulk try: (key, value) = line_upper.split(' ') except: msg = 'excepted "BEGIN BULK" found=%r' % line raise RuntimeError(msg) key = key.upper() param_type = 'BEGIN_BULK-type' assert key.upper() == key, key elif 'PARAM' in line_upper: # param if ',' in line_upper: sline = line_upper.split(',') elif '\t' in line_upper: sline = line_upper.split('\t') else: raise SyntaxError("trying to parse %r..." % line) if len(sline) != 3: raise SyntaxError("trying to parse %r..." % line) (key, value, options) = sline param_type = 'CSV-type' assert key.upper() == key, key elif ' ' not in line: key = line.strip().upper() value = line.strip() options = None param_type = 'KEY-type' assert key.upper() == key, key else: msg = 'generic catch all...line=%r' % line key = '' value = line options = None param_type = 'KEY-type' assert key.upper() == key, key i += 1 assert key.upper() == key, 'key=%s param_type=%s' % (key, param_type) return (i, key, value, options, param_type)
def _parse_entry(self, lines): r""" Internal method for parsing a card of the case control deck Parses a single case control deck card into 4 sections 1. paramName - obvious 2. Value - still kind of obvious 3. options - rarely used data 4. paramType - STRESS-type, SUBCASE-type, PARAM-type, SET-type, BEGIN_BULK-type It's easier with examples: paramType = SUBCASE-type SUBCASE 1 -> paramName=SUBCASE value=1 options=[] paramType = STRESS-type STRESS = ALL -> paramName=STRESS value=ALL options=[] STRAIN(PLOT) = 5 -> paramName=STRAIN value=5 options=[PLOT] TITLE = stuff -> paramName=TITLE value=stuff options=[] paramType = SET-type SET 1 = 10,20,30 -> paramName=SET value=[10,20,30] options = 1 paramType = BEGIN_BULK-type BEGIN BULK -> paramName=BEGIN value=BULK options = [] paramType = CSV-type PARAM,FIXEDB,-1 -> paramName=PARAM value=FIXEDB options = [-1] The paramType is the "macro" form of the data (similar to integer, float, string). The value is generally whats on the RHS of the equals sign (assuming it's there). Options are modifiers on the data. Form things like the PARAM card or the SET card they arent as clear, but the paramType lets the program know how to format it when writing it out. Parameters ---------- lines : List[str, str, ...] list of lines Returns ------- paramName : str see brief value : List[...] see brief options : List[str/int/float] see brief paramType : str/int/float/List see brief """ i = 0 options = [] value = None key = None param_type = None line = lines[i] #print(line) #print("*****lines = ", lines) equals_count = 0 for letter in line: if letter == '=': equals_count += 1 line_upper = line.upper() #print("line_upper = %r" % line) #print(' equals_count = %s' % equals_count) if line_upper.startswith('SUBCASE'): #print("line = %r" % line) line2 = line.replace('=', '') sline = line2.split() if len(sline) != 2: msg = "trying to parse %r..." % line raise RuntimeError(msg) (key, param_type) = sline key = key.upper() #print("key=%r isubcase=%r" % (key, isubcase)) value = int(param_type) #self.isubcase = int(isubcase) param_type = 'SUBCASE-type' assert key.upper() == key, key elif line_upper.startswith( ('LABEL', 'SUBT', 'TITL')): # SUBTITLE/TITLE try: eindex = line.index('=') except: msg = "cannot find an = sign in LABEL/SUBTITLE/TITLE line\n" msg += "line = %r" % line_upper.strip() raise RuntimeError(msg) key = line_upper[0:eindex].strip() value = line[eindex + 1:].strip() options = [] param_type = 'STRING-type' elif (line_upper.startswith('SET ') or line_upper.startswith('SETMC ') and equals_count == 1): # would have been caught by STRESS-type sline = line_upper.split('=') assert len(sline) == 2, sline key, value = sline try: (key, ID) = key.split() except: raise RuntimeError(key) key = key + ' ' + ID assert key.upper() == key, key options = int(ID) if self.debug: self.log.debug('SET-type key=%r ID=%r' % (key, ID)) fivalues = value.rstrip(' ,').split(',') # float/int values #: .. todo:: should be more efficient multiline reader... # read more lines.... if line[-1].strip() == ',': i += 1 #print("rawSETLine = %r" % (lines[i])) while 1: if lines[i].strip()[-1] == ',': fivalues += lines[i][:-1].split(',') else: # last case fivalues += lines[i].split(',') #print("fivalues last = i=%s %r" % (i, lines[i])) i += 1 break i += 1 #print("len(fivalues) = ",len(fivalues)) value = fivalues param_type = 'SET-type' elif equals_count == 1: # STRESS if '=' in line: (key, value) = line_upper.strip().split('=') else: msg = 'expected item of form "name = value" line=%r' % line.strip( ) raise RuntimeError(msg) key = key.strip().upper() value = value.strip() if self.debug: self.log.debug("key=%r value=%r" % (key, value)) param_type = 'STRESS-type' assert key.upper() == key, key if '(' in key: # comma may be in line - STRESS-type #paramType = 'STRESS-type' sline = key.strip(')').split('(') key = sline[0] options = sline[1].split(',') # handle TEMPERATURE(INITIAL) and TEMPERATURE(LOAD) cards if key in ['TEMPERATURE', 'TEMP']: option = options[0] if option == '': option = 'BOTH' key = 'TEMPERATURE' options = [option] #print("key=%r options=%s" %(key,options)) #elif ' ' in key and ',' in value: # SET-type #(key, ID) = key.split() #key = key + ' ' + ID #if self.debug: #self.log.debug('SET-type key=%r ID=%r' % (key, ID)) #fivalues = value.rstrip(' ,').split(',') # float/int values ##: .. todo:: should be more efficient multiline reader... ## read more lines.... #if line[-1].strip() == ',': #i += 1 ##print("rawSETLine = %r" % (lines[i])) #while 1: #if ',' == lines[i].strip()[-1]: #fivalues += lines[i][:-1].split(',') #else: # last case #fivalues += lines[i].split(',') ##print("fivalues last = i=%s %r" % (i, lines[i])) #i += 1 #break #i += 1 ##print("len(fivalues) = ",len(fivalues)) #value = fivalues #options = ID # needed a place to put it... #param_type = 'SET-type' elif ',' in value: # STRESS-type; special TITLE = stuffA,stuffB #print('A ??? line = ',line) #raise RuntimeError(line) pass else: # STRESS-type; TITLE = stuff #print('B ??? line = ',line) if key in ['TEMPERATURE', 'TEMP']: assert len(options) == 0, options key = 'TEMPERATURE' options = ['BOTH'] key = update_param_name(key.strip().upper()) verify_card(key, value, options, line) assert key.upper() == key, key elif equals_count > 2 and '(' in line and 'FLSPOUT' not in line: #GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES #print('****', lines) assert len(lines) == 1, lines line = lines[0] try: key, value_options = line.split('(', 1) except ValueError: msg = 'Expected a "(", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += '%r' % line raise ValueError(msg) try: options_paren, value = value_options.rsplit('=', 1) except ValueError: msg = 'Expected a "=", but did not find one.\n' msg += 'Looking for something of the form:\n' msg += ' GROUNDCHECK(PRINT,SET=(G,N,N+AUTOSPC,F,A),DATAREC=NO)=YES\n' msg += 'value_options=%r\n' % value_options msg += '%r' % line raise ValueError(msg) options_paren = options_paren.strip() value = value.strip() if value.isdigit(): value = int(value) if not options_paren.endswith(')'): raise RuntimeError(line) str_options = options_paren[:-1] if '(' in str_options: options_start = [] options_end = [] icomma = str_options.index(',') iparen = str_options.index('(') #print('icomma=%s iparen=%s' % (icomma, iparen)) while icomma < iparen: base, str_options = str_options.split(',', 1) str_options = str_options.strip() icomma = str_options.index(',') iparen = str_options.index('(') options_start.append(base.strip()) #print(' icomma=%s iparen=%s' % (icomma, iparen)) #print(' options_start=%s' % options_start) icomma = str_options.rindex(',') iparen = str_options.rindex(')') #print('icomma=%s iparen=%s' % (icomma, iparen)) while icomma > iparen: str_options, end = str_options.rsplit(')', 1) str_options = str_options.strip() + ')' #print(' str_options = %r' % str_options) icomma = str_options.rindex(',') iparen = str_options.rindex(')') options_end.append(end.strip(' ,')) #print(' icomma=%s iparen=%s' % (icomma, iparen)) #print(' options_end=%s' % options_end[::-1]) #print() #print('options_start=%s' % options_start) #print('options_end=%s' % options_end) #print('leftover = %r' % str_options) options = options_start + [str_options] + options_end[::-1] else: options = str_options.split(',') param_type = 'STRESS-type' key = key.upper() #print('options =', options) #asdf elif line_upper.startswith('BEGIN'): # begin bulk try: (key, value) = line_upper.split(' ') except: msg = 'excepted "BEGIN BULK" found=%r' % line raise RuntimeError(msg) key = key.upper() param_type = 'BEGIN_BULK-type' assert key.upper() == key, key elif 'PARAM' in line_upper: # param if ',' in line_upper: sline = line_upper.split(',') elif '\t' in line_upper: sline = line_upper.split('\t') else: raise SyntaxError("trying to parse %r..." % line) if len(sline) != 3: raise SyntaxError("trying to parse %r..." % line) (key, value, options) = sline param_type = 'CSV-type' assert key.upper() == key, key elif ' ' not in line: key = line.strip().upper() value = line.strip() options = None param_type = 'KEY-type' assert key.upper() == key, key else: msg = 'generic catch all...line=%r' % line key = '' value = line options = None param_type = 'KEY-type' assert key.upper() == key, key i += 1 assert key.upper() == key, 'key=%s param_type=%s' % (key, param_type) return (i, key, value, options, param_type)