Skip to content

Commit

Permalink
Added flag to enable/disable regular expression matching on Part pin …
Browse files Browse the repository at this point in the history
…names.
  • Loading branch information
xesscorp committed May 19, 2020
1 parent 815925d commit 9c60ea4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import setuptools

__version__ = "0.0.30"
__version__ = "0.0.31dev"
__author__ = "XESS Corp."
__email__ = "info@xess.com"

Expand Down
15 changes: 13 additions & 2 deletions skidl/Part.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def __init__(
self.tool = tool # Initial type of part (SKIDL, KICAD, etc.)
self.circuit = None # Part starts off unassociated with any circuit.
self.match_pin_substring = False # Only select pins with exact name matches.
self.match_pin_regex = False # Don't allow regex matches of pin names.

# Create a Part from a library entry.
if lib:
Expand Down Expand Up @@ -590,12 +591,16 @@ def get_pins(self, *pin_ids, **criteria):
match_substring = (
criteria.pop("match_substring", False) or self.match_pin_substring
)
match_regex = (
criteria.pop("match_regex", False) or self.match_pin_regex
)

# If no pin identifiers were given, then use a wildcard that will
# select all pins.
if not pin_ids:
pin_ids = [".*"]
match_substring = ".*" # Also turn on pin substring matching so .* works.
match_substring = True # Also turn on pin substring matching so .* works.
match_regex = True

# Determine the minimum and maximum pin ids if they don't already exist.
if "min_pin" not in dir(self) or "max_pin" not in dir(self):
Expand Down Expand Up @@ -638,6 +643,10 @@ def get_pins(self, *pin_ids, **criteria):
pins.extend(tmp_pins)
continue

# Skip matching for substrings or using regex if they're not enabled.
if not match_substring and not match_regex:
continue

# If matching a substring within a pin name is enabled, then
# create wildcards to match the beginning/ending surrounding a
# substring. Remove these wildcards if substring matching is disabled.
Expand All @@ -647,7 +656,8 @@ def get_pins(self, *pin_ids, **criteria):
# name or alias. Does it match a substring within a pin name?
# Or does it match as a regex?
try:
p_id_re = "".join([wildcard, p_id, wildcard])
p_id_re = p_id if match_regex else re.escape(p_id)
p_id_re = "".join([wildcard, p_id_re, wildcard])
except TypeError:
# This will happen if the p_id is a number and not a string.
# Skip this and the next block because p_id_re can't be made.
Expand All @@ -668,6 +678,7 @@ def get_pins(self, *pin_ids, **criteria):

return list_or_scalar(pins)


# Get pins from a part using brackets, e.g. [1,5:9,'A[0-9]+'].
__getitem__ = get_pins

Expand Down
2 changes: 2 additions & 0 deletions tests/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

def test_alias_1():
vreg = Part("xess.lib", "1117")
vreg.match_pin_regex = True
vreg.set_pin_alias("my_alias", 1)
vreg.set_pin_alias("my_alias", 2)
assert len(vreg["my_alias"]) == 2
Expand All @@ -23,6 +24,7 @@ def test_alias_2():

def test_alias_3():
vreg = Part("xess.lib", "1117")
vreg.match_pin_regex = True
vreg[1].aliases = "my_alias"
vreg[2].aliases = "my_alias"
vreg[2].aliases += "my_other_alias"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_index_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
def test_index_slicing_1():
mcu = Part("GameteSnapEDA", "STM32F767ZGT6")
mcu.match_pin_substring = True
mcu.match_pin_regex = True
assert len(mcu[r"\(FMC_D[0:15]\)"]) == 16
assert len(mcu[r"\(FMC_D[15:0]\)"]) == 16
assert len(mcu[r"FMC_D[0:15]\)"]) == 16
Expand All @@ -19,6 +20,7 @@ def test_index_slicing_1():
def test_index_slicing_2():
mem = Part("GameteSnapEDA", "MT48LC16M16A2TG-6A_IT:GTR")
mem.match_pin_substring = True
mem.match_pin_regex = True
assert len(mem["DQ[0:15]"]) == 16
assert len(mem["DQ[15:0]"]) == 16
assert len(mem["A[0:15]"]) == 13
Expand All @@ -29,6 +31,7 @@ def test_index_slicing_2():
def test_index_slicing_3():
mem = Part("xess", "SDRAM_16Mx16_TSOPII-54")
mem.match_pin_substring = True
mem.match_pin_regex = True
assert len(mem["DQ[0:15]"]) == 16
assert len(mem["DQ[15:0]"]) == 16
assert len(mem["A[0:15]"]) == 13
Expand Down
2 changes: 2 additions & 0 deletions tests/test_part_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

def test_part_unit_1():
vreg = Part("xess.lib", "1117")
vreg.match_pin_regex = True
vreg.make_unit("A", 1, 2)
vreg.make_unit("B", 3)
vreg.A.match_pin_substring = True
Expand All @@ -19,6 +20,7 @@ def test_part_unit_1():

def test_part_unit_2():
vreg = Part("xess.lib", "1117")
vreg.match_pin_regex = True
vreg.make_unit("A", 1, 2)
vreg.make_unit("A", 3)
assert len((vreg.unit["A"][".*"],)) == 1
Expand Down

0 comments on commit 9c60ea4

Please sign in to comment.