<commit_before>from lxml import etree

import schematicTransformations
import eagleLibrary

class SchematicParsingException(Exception):
  pass

class EaglePart:
  def __init__(self, partNode, schematic):
    self.attributes = {}

    self.xml = partNode
    self.name = partNode.get('name')
    self.value = partNode.get('value')

    self.libraryName = partNode.get('library')
    self.devicesetName = partNode.get('deviceset')
    self.deviceName = partNode.get('device')
    self.technologyName = partNode.get('technology')

    self.attributes = self._extractAttributes(schematic)
    self.netMap = self._extractNetMap(schematic)
    self.gates = self._extractGates(schematic)

    pass

  def _extractGates(self, schematic):
    result = {}
    queryString = ".//instance[@part='{name}']".format(name=self.name)
    for element in schematic.findall(queryString):
      result[element.get('gate')] = element
    return result


  def _extractAttributes(self, schematic):
    technology = self.getTechnologyValue(schematic, self.technologyName)

    if technology == None:
      technology = self.getTechnologyValue(schematic, "DEFAULT")

    if technology == None:
      technology = self.getTechnologyValue(schematic, "")

    if technology == None:
      raise SchematicParsingException("Can't find correct attributes for part {}".format(self.name))

    attributes = {}
    for element in technology.findall('attribute'):
      attributes[element.get('name')] = element.get('value')

    return attributes

  def getTechnologyValue(self, schematic, technologyName):
    technologyPathString = "./libraries/library[@name='{library}']/devicesets/deviceset[@name='{deviceset}']/devices/device[@name='{device}']/technologies/technology[@name='{technology}']"
    return schematic.find(
        technologyPathString.format(library=self.libraryName, deviceset=self.devicesetName, device=self.deviceName,
                                    technology=technologyName))

  def _extractNetMap(self, schematic):
    nets = schematic.findall('.//net')
    netMap = {}

    for net in nets:
      pinrefs = net.findall('.//pinref')
      for pinref in pinrefs:
        if pinref.get('part') == self.name:
          netMap[pinref.get('pin')] = net.get('name')

    return netMap

  def getSpiceNetlist(self):
    spicePrefix = self.getAttribute("SV_SPICE_PREFIX")

    if spicePrefix == None:
      return None

    spiceOrder = self.getAttribute("SV_SPICE_ORDER").split(';')

    netList = ''
    for pin in spiceOrder:
      net = self.netMap[pin]
      net = net.replace('-', '_')
      net = net.replace('+', 'P')
      netList += net + ' '

    name = self.name.replace('-', '_')

    return spicePrefix + name + ' ' + netList + self.value

  def getSpiceModel(self):
    spiceModel = self.getAttribute("SV_SPICE_MODEL")

    if spiceModel == None:
      return None

    return '.model ' + self.value + ' ' + spiceModel

  def getSpiceSupply(self):

    if self.libraryName not in ['supply1', 'supply2']:
      return None

    if 'GND' in self.devicesetName:
      return None

    voltage = self.devicesetName
    name = voltage.replace('+', 'P').replace('-', 'M')
    connection = ' '.join(self.netMap.values())
    connection = connection.replace('-', '_')
    connection = connection.replace('+', 'P')

    return 'V' + name + ' ' + connection + ' GND dc ' + voltage + ' ac 0V'<commit_msg>Turns out plusses in net names are ok<commit_after>from lxml import etree

import schematicTransformations
import eagleLibrary

class SchematicParsingException(Exception):
  pass

class EaglePart:
  def __init__(self, partNode, schematic):
    self.attributes = {}

    self.xml = partNode
    self.name = partNode.get('name')
    self.value = partNode.get('value')

    self.libraryName = partNode.get('library')
    self.devicesetName = partNode.get('deviceset')
    self.deviceName = partNode.get('device')
    self.technologyName = partNode.get('technology')

    self.attributes = self._extractAttributes(schematic)
    self.netMap = self._extractNetMap(schematic)
    self.gates = self._extractGates(schematic)

    pass

  def _extractGates(self, schematic):
    result = {}
    queryString = ".//instance[@part='{name}']".format(name=self.name)
    for element in schematic.findall(queryString):
      result[element.get('gate')] = element
    return result


  def _extractAttributes(self, schematic):
    technology = self.getTechnologyValue(schematic, self.technologyName)

    if technology == None:
      technology = self.getTechnologyValue(schematic, "DEFAULT")

    if technology == None:
      technology = self.getTechnologyValue(schematic, "")

    if technology == None:
      raise SchematicParsingException("Can't find correct attributes for part {}".format(self.name))

    attributes = {}
    for element in technology.findall('attribute'):
      attributes[element.get('name')] = element.get('value')

    return attributes

  def getTechnologyValue(self, schematic, technologyName):
    technologyPathString = "./libraries/library[@name='{library}']/devicesets/deviceset[@name='{deviceset}']/devices/device[@name='{device}']/technologies/technology[@name='{technology}']"
    return schematic.find(
        technologyPathString.format(library=self.libraryName, deviceset=self.devicesetName, device=self.deviceName,
                                    technology=technologyName))

  def _extractNetMap(self, schematic):
    nets = schematic.findall('.//net')
    netMap = {}

    for net in nets:
      pinrefs = net.findall('.//pinref')
      for pinref in pinrefs:
        if pinref.get('part') == self.name:
          netMap[pinref.get('pin')] = net.get('name')

    return netMap

  def getSpiceNetlist(self):
    spicePrefix = self.getAttribute("SV_SPICE_PREFIX")

    if spicePrefix == None:
      return None

    spiceOrder = self.getAttribute("SV_SPICE_ORDER").split(';')

    netList = ''
    for pin in spiceOrder:
      net = self.netMap[pin]
      net = net.replace('-', '_')
      netList += net + ' '

    name = self.name.replace('-', '_')

    return spicePrefix + name + ' ' + netList + self.value

  def getSpiceModel(self):
    spiceModel = self.getAttribute("SV_SPICE_MODEL")

    if spiceModel == None:
      return None

    return '.model ' + self.value + ' ' + spiceModel

  def getSpiceSupply(self):

    if self.libraryName not in ['supply1', 'supply2']:
      return None

    if 'GND' in self.devicesetName:
      return None

    voltage = self.devicesetName
    name = voltage.replace('+', 'P').replace('-', 'M')
    connection = ' '.join(self.netMap.values())
    connection = connection.replace('-', '_')

    return 'V' + name + ' ' + connection + ' GND dc ' + voltage + ' ac 0V'
{'fraction_non_alphanumeric': 0.13666072489601902, 'fraction_numerical': 0.00089126559714795, 'mean_word_length': 3.4296052631578946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4481724', 'n_tokens_mistral': 2101, 'n_tokens_neox': 1976, 'n_words': 416}