Skip to content

Commit

Permalink
Parts now record the file and line at which they were instantiated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Vandenbout committed Apr 26, 2017
1 parent 99b3175 commit 79beb8b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ output/*/index.html

*.log
.cache
uno_r3
uno_r3

*.erc
34 changes: 34 additions & 0 deletions skidl/skidl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,40 @@ def add_parts(self, *parts):
part.circuit = self # Record the Circuit object the part belongs to.
part.ref = part.ref # This adjusts the part reference if necessary.
part.hierarchy = self.hierarchy # Tag the part with its hierarchy position.

# To determine where this part was created, trace the function
# calls that led to this part and place into a field
# but strip off all the calls to internal SKiDL functions.
call_stack = inspect.stack() # Get function call stack.
# Use the function at the top of the stack to
# determine the location of the SKiDL library functions.
try:
skidl_dir, _ = os.path.split(call_stack[0].filename)
except AttributeError:
skidl_dir, _ = os.path.split(call_stack[0][1])
# Record file_name#line_num starting from the bottom of the stack
# and terminate as soon as a function is found that's in the
# SKiDL library (no use recording internal calls).
skidl_trace = []
for frame in reversed(call_stack):
try:
filename = frame.filename
lineno = frame.lineno
except AttributeError:
filename = frame[1]
lineno = frame[2]
if os.path.split(filename)[0] == skidl_dir:
# Found function in SKiDL library, so trace is complete.
break
# Get the absolute path to the file containing the function
# and the line number of the call in the file. Append these
# to the trace.
filepath = os.path.abspath(filename)
skidl_trace.append('#'.join((filepath, str(lineno))))
# Store the function call trace into a part field.
if skidl_trace:
part.skidl_trace = ';'.join(skidl_trace)

self.parts.append(part)

else:
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.net
*.erc
*.log
__pycache__
6 changes: 6 additions & 0 deletions tests/test_skidl_loc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from skidl import *

rt = Part('device', 'R', dest=TEMPLATE, footprint='null')
r1 = rt()
resistors = rt(5)
generate_netlist()

0 comments on commit 79beb8b

Please sign in to comment.