Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in kibom.component.Component, self.libpart is None #147

Open
hkennyv opened this issue Nov 16, 2020 · 8 comments
Open

in kibom.component.Component, self.libpart is None #147

hkennyv opened this issue Nov 16, 2020 · 8 comments

Comments

@hkennyv
Copy link
Contributor

hkennyv commented Nov 16, 2020

I ran into this error:

  File "/Users/khuynh/me/rejoule/kicad/dcdc_mgthv/venv/lib/python3.8/site-packages/kibom/component.py", line 191, in getDescription
    ret = self.libpart.getDescription()
AttributeError: 'NoneType' object has no attribute 'getDescription'

i tried to trace it down and i found that in the constructor, self.libpart is initialized to None:

KiBoM/kibom/component.py

Lines 46 to 48 in 0ce83c2

def __init__(self, xml_element, prefs=None):
self.element = xml_element
self.libpart = None

This is particularly problematic because there are multiple places in this file that call some method on self.libpart, which is guaranteed to throw an AttributeError.

Particularly, the one I ran into is this path that gets executed and there's not guard for checking if self.libpart is None:

KiBoM/kibom/component.py

Lines 186 to 200 in 0ce83c2

def getDescription(self):
try:
ret = self.element.get("libsource", "description")
except:
# Compatibility with old KiCad versions (4.x)
ret = self.element.get("field", "name", "description")
if ret == "":
try:
ret = self.libpart.getDescription()
except AttributeError:
# Raise a good error description here, so the user knows what the culprit component is.
# (sometimes libpart is None)
raise AttributeError('Could not get description for part {}{}.'.format(self.getPrefix()),
self.getSuffix())

A quick search for self.libpart shows that not all method calls have a guard to check if self.libpart is not None

It looks like Component.setLibPart gets called in netlist_reader.py, but for some reason, self.libpart was still None in my example. It seems like the solution is to ensure that there is a guard every time that a method on self.libpart is called

@hkennyv hkennyv changed the title in kibom.component.Component, self.libpart is always None in kibom.component.Component, self.libpart is None Nov 16, 2020
@set-soft
Copy link
Contributor

Note that the line 191 in the code you quote can't raise this exception.
I think you are using older code.
The ret = self.libpart.getDescription() is in line 195 and inside a try block. This should be catched by except AttributeError.

@hkennyv
Copy link
Contributor Author

hkennyv commented Nov 19, 2020

good catch @set-soft, i think i used the pypi installation. let me try installing from the repo.

@hkennyv
Copy link
Contributor Author

hkennyv commented Nov 19, 2020

so i was able to more-or-less fix it, thanks @set-soft . There was a misplaced parenthesis on line 199 that I had to fix as well before I could get it to run and raise the AttributeError properly (see #148).

After fixing that and getting it to run, I had a question. Is there a reason we cannot return "" as the description here? It's telling me that it cannot get a description for a part, and upon looking through my xml netlist, I have quite a few components that have something like:

      <libsource lib="" part="Rsense" description=""/>

Which is what I am assuming is causing this error to be raised. So I'm wondering what the rationale is for raising an AttributeError instead of using "" as the description and moving on.

KiBoM/kibom/component.py

Lines 193 to 200 in 0ce83c2

if ret == "":
try:
ret = self.libpart.getDescription()
except AttributeError:
# Raise a good error description here, so the user knows what the culprit component is.
# (sometimes libpart is None)
raise AttributeError('Could not get description for part {}{}.'.format(self.getPrefix()),
self.getSuffix())

@eeintech
Copy link
Contributor

@hkennyv Assuming the Description field is set for all your parts in your library, try to update your eeschema parts. If it still does not work then you may have to remove/re-add them to your schematic.

image

@gbalke
Copy link

gbalke commented Aug 18, 2021

I was getting the same error:

Traceback (most recent call last):                                                                  
  File "/home/greg/.local/lib/python3.8/site-packages/kibom/component.py", line 427, in getDatasheet
    ret = self.libpart.getDescription()                                                             
AttributeError: 'NoneType' object has no attribute 'getDescription'                                 

I was able to resolve this error by manually adjusting the .xml part file. Using the code snippet suggested by @hkennyv, I found that kicad's xml generator was creating entries such as:

<libsource lib="" part="Device:R_Small_11" description="Resistor, small symbol"/>

This should be:

<libsource lib="Device" part="R_Small_11" description="Resistor, small symbol"/>

After the change, I no longer get the error.

This snippet of regex might help those in need as there were a lot of mistakes in my file:

:%s/lib=""\ part="\(.*\):\(.*\)"/lib="\1"\ part="\2"/g

I'm running on nightly so this might just be one of the latest bugs but I thought I should mention it should anyone run into it as well!

@set-soft
Copy link
Contributor

Hi @gbalke !
Which KiBoM version are you using? Is the current git code or older? I think this problem is already solved in the current code (as I mention above).

@gbalke
Copy link

gbalke commented Aug 27, 2021

You're correct, it was the older version on PyPi. It was interesting to see KiCad generating these sorts of xmls though 🤔
Simply throwing an error doesn't necessarily solve the problem, just notifies the user of it? Assuming this needs to be fixed in KiCad's tooling (out of scope for KiBom).

Apologies for the slow response and thank you for the speedy reply!

@set-soft
Copy link
Contributor

Hi @gbalke !
I think the problem is just a change between KiCad 4.x and 5.x XML format.
With the current code you should get the descriptions without problems.
I don't think KiCad can generate XMLs that rises the current exception, not at least under normal operation. Do you have a valid XML that raises the error in the current code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants