def _parse(self): line_number = 0 # store header self._header = [] while not re.match('^F 0 "', self._original_content[line_number]): self._header.append(self._original_content[line_number]) line_number += 1 # create fields while re.match('^F [0-9]', self._original_content[line_number]): line = self._original_content[line_number] # obtain position of the value if re.match('^F 1 "', line): tokens = line.split('"') tokens2 = tokens[2].split(' ') self._x_pos = int(tokens2[2]) self._y_pos = int(tokens2[3]) Out.fine('Detected component position [' + str(self._x_pos) + '|' + str(self._y_pos) + ']') # parse fields new_field = FieldParser.parse(line) self._fields.append(new_field) line_number += 1 # store trailer last_line = '$EndComp' line = '' while line != last_line: line = self._original_content[line_number] self._trailer.append(line) line_number += 1
def parse(line): assert(type(line) is str) if re.match('^F [0-3] "', line): # native field Out.fine('Parsing native field: ' + line) return NativeField.parse(line) else: # custom field Out.fine('Parsing custom field: ' + line) return CustomField.parse(line)
def get_field(self, name): """ Return field instance with a given name """ matching_fields = list(filter(lambda field: field.name == name, self._fields)) if len(matching_fields) == 1: return matching_fields[0] elif len(matching_fields) > 1: Out.warn('More than one field with name ' + name + ' defined!') return None else: Out.fine('No field with name ' + name) return None
def matches(self, name, value): """ Return true if both name and value are equal """ if self.name == name: if self.value == value: return True else: if self.value == value: # exact match return True else: # check regex match # value may contain a regex, i.e. R[1-3] for matching R1, R2 and R3 pattern = re.compile(value) if pattern.match(self.value) != None: Out.fine('Detected field match using regular expression: ' + value) return True else: return False
def add_or_update_field(self, name, value): """ Add a new (custom) field or replace an existing field """ # warn and return if name is empty if name == '': Out.warn('Will not add or update field in ' + self.get_field('Designator').value + ' due to empty name') return # log when clearing a value elif value == '': Out.log('Clearing value of field "' + name + '" in ' + self.get_field('Designator').value + '.') # change value of name-matching fields Out.fine('Changing value of fields matching the name "' + name + '"') for index, field in enumerate(self._fields): if field.name == name: Out.fine('Overwriting value of field #' + str(index) + ' with ' + name + ':' + value) self._fields[index].value = value # only designator and value should be visible if name == 'Designator' or name == 'Value': self._fields[index].set_visible(True) else: self._fields[index].set_visible(False) return # Construct and add new custom field new_field = CustomField.copy(self.get_field('Designator')) new_field.name = name new_field.value = value new_field.number = len(self._fields) new_field.set_visible(False) Out.fine('Adding new custom field:' + new_field.serialize()) self._fields.append(new_field)
def edit_component_template(self): """ Edit first matching component """ self.component_template = self.matching_components[0] self.component_template.edit() Out.fine('Component template:\n' + self.component_template.serialize())