Skip to content

Commit

Permalink
Eliminate all dead code (#521)
Browse files Browse the repository at this point in the history
* Eliminate all dead code

* Format using ruff

* add deadcode to pre-commit-config and CI linter

* Remove unused methods
  • Loading branch information
Bouni authored Aug 27, 2024
1 parent 2713daa commit 1da1ad9
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 248 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ jobs:
version: 0.5.6
args: check
src: "."
deadcode:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install deadcode
run: pip install deadcode
- name: Run deadcode
run: deadcode .
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ repos:
- id: markdownlint
args:
- --fix
# - repo: https://github.com/albertas/deadcode
# rev: 2.4.1
# hooks:
# - id: deadcode
34 changes: 6 additions & 28 deletions datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ def natural_sort_key(s):
for text in re.split("([0-9]+)", s)
]

def GetColumnCount(self):
def GetColumnCount(self): # noqa: DC04
"""Get number of columns."""
return len(self.columns)

def GetColumnType(self, col):
def GetColumnType(self, col): # noqa: DC04
"""Get type of each column."""
columntypes = (
"string",
Expand All @@ -77,19 +77,19 @@ def GetColumnType(self, col):
)
return columntypes[col]

def GetChildren(self, parent, children):
def GetChildren(self, parent, children): # noqa: DC04
"""Get child items of a parent."""
if not parent:
for row in self.data:
children.append(self.ObjectToItem(row))
return len(self.data)
return 0

def IsContainer(self, item):
def IsContainer(self, item): # noqa: DC04
"""Check if tem is a container."""
return not item

def GetParent(self, item):
def GetParent(self, item): # noqa: DC04
"""Get parent item."""
return dv.NullDataViewItem

Expand Down Expand Up @@ -117,7 +117,7 @@ def SetValue(self, value, item, col):
row[col] = value
return True

def Compare(self, item1, item2, column, ascending):
def Compare(self, item1, item2, column, ascending): # noqa: DC04
"""Override to implement natural sorting."""
val1 = self.GetValue(item1, column)
val2 = self.GetValue(item2, column)
Expand Down Expand Up @@ -159,28 +159,6 @@ def AddEntry(self, data: list):
self.data.append(data)
self.ItemAdded(dv.NullDataViewItem, self.ObjectToItem(data))

def UpdateEntry(self, data: list):
"""Update an entry in the data model."""
if (index := self.find_index(data[0])) is None:
return
item = self.data[index]
for i in range(0, len(data)):
if i in [self.columns["BOM_COL"], self.columns["POS_COL"]]:
item[i] = self.get_bom_pos_icon(data[i])
elif i == self.columns["SIDE_COL"]:
item[i] = self.get_side_icon(data[i])
else:
item[i] = data[i]
self.ItemChanged(self.ObjectToItem(item))

def RemoveEntry(self, ref: str):
"""Remove an entry from the data model."""
if (index := self.find_index(ref)) is None:
return
item = self.ObjectToItem(self.data[index])
self.data.remove(self.data[index])
self.ItemDeleted(dv.NullDataViewItem, item)

def RemoveAll(self):
"""Remove all entries from the data model."""
self.data.clear()
Expand Down
158 changes: 0 additions & 158 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

PLUGIN_PATH = os.path.split(os.path.abspath(__file__))[0]

THT = 0
SMD = 1
EXCLUDE_FROM_POS = 2
EXCLUDE_FROM_BOM = 3
NOT_IN_SCHEMATIC = 4


def is_version8(version: str) -> bool:
Expand Down Expand Up @@ -94,25 +91,6 @@ def loadIconScaled(filename, scale=1.0):
return wx.Icon(bmp)


def GetListIcon(value, scale_factor):
"""Get check or cross icon depending on passed value."""
if value == 0:
return wx.dataview.DataViewIconText(
"",
loadIconScaled(
"mdi-check-color.png",
scale_factor,
),
)
return wx.dataview.DataViewIconText(
"",
loadIconScaled(
"mdi-close-color.png",
scale_factor,
),
)


def natural_sort_collation(a, b):
"""Natural sort collation for use in sqlite."""
if a == b:
Expand Down Expand Up @@ -173,55 +151,16 @@ def get_valid_footprints(board):
return footprints


def get_footprint_keys(fp):
"""Get keys from footprint for sorting."""
try:
package = str(fp.GetFPID().GetLibItemName())
except ValueError:
package = ""
try:
reference = int(re.search(r"\d+", fp.GetReference())[0])
except ValueError:
reference = 0
return (package, reference)


def get_bit(value, bit):
"""Get the nth bit of a byte."""
return value & (1 << bit)


def set_bit(value, bit):
"""Set the nth bit of a byte."""
return value | (1 << bit)


def clear_bit(value, bit):
"""Clear the nth bit of a byte."""
return value & ~(1 << bit)


def toggle_bit(value, bit):
"""Toggle the nth bit of a byte."""
return value ^ (1 << bit)


def get_tht(footprint):
"""Get the THT property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
return bool(get_bit(val, THT))


def get_smd(footprint):
"""Get the SMD property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
return bool(get_bit(val, SMD))


def get_exclude_from_pos(footprint):
"""Get the 'exclude from POS' property of a footprint."""
if not footprint:
Expand All @@ -238,93 +177,6 @@ def get_exclude_from_bom(footprint):
return bool(get_bit(val, EXCLUDE_FROM_BOM))


def get_not_in_schematic(footprint):
"""Get the 'not in schematic' property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
return bool(get_bit(val, NOT_IN_SCHEMATIC))


def set_tht(footprint):
"""Set the THT property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
val = set_bit(val, THT)
footprint.SetAttributes(val)
return bool(get_bit(val, THT))


def set_smd(footprint):
"""Set the SMD property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
val = set_bit(val, SMD)
footprint.SetAttributes(val)
return bool(get_bit(val, SMD))


def set_exclude_from_pos(footprint, v):
"""Set the 'exclude from POS' property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
if v:
val = set_bit(val, EXCLUDE_FROM_POS)
else:
val = clear_bit(val, EXCLUDE_FROM_POS)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_POS))


def set_exclude_from_bom(footprint, v):
"""Set the 'exclude from BOM' property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
if v:
val = set_bit(val, EXCLUDE_FROM_BOM)
else:
val = clear_bit(val, EXCLUDE_FROM_BOM)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_BOM))


def set_not_in_schematic(footprint, v):
"""Set the 'not in schematic' property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
if v:
val = set_bit(val, NOT_IN_SCHEMATIC)
else:
val = clear_bit(val, NOT_IN_SCHEMATIC)
footprint.SetAttributes(val)
return bool(get_bit(val, NOT_IN_SCHEMATIC))


def toggle_tht(footprint):
"""Toggle the THT property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
val = toggle_bit(val, THT)
footprint.SetAttributes(val)
return bool(get_bit(val, THT))


def toggle_smd(footprint):
"""Toggle the SMD property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
val = toggle_bit(val, SMD)
footprint.SetAttributes(val)
return bool(get_bit(val, SMD))


def toggle_exclude_from_pos(footprint):
"""Toggle the 'exclude from POS' property of a footprint."""
if not footprint:
Expand All @@ -343,13 +195,3 @@ def toggle_exclude_from_bom(footprint):
val = toggle_bit(val, EXCLUDE_FROM_BOM)
footprint.SetAttributes(val)
return bool(get_bit(val, EXCLUDE_FROM_BOM))


def toggle_not_in_schematic(footprint):
"""Toggle the 'not in schematic' property of a footprint."""
if not footprint:
return None
val = footprint.GetAttributes()
val = toggle_bit(val, NOT_IN_SCHEMATIC)
footprint.SetAttributes(val)
return bool(get_bit(val, NOT_IN_SCHEMATIC))
24 changes: 1 addition & 23 deletions library.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class LibraryState(Enum):
class Library:
"""A storage class to get data from a sqlite database and write it back."""

# no longer works
CSV_URL = "https://jlcpcb.com/componentSearch/uploadComponentInfo"

def __init__(self, parent):
self.logger = logging.getLogger(__name__)
self.parent = parent
Expand Down Expand Up @@ -355,36 +352,17 @@ def get_all_mapping_data(self):
).fetchall()
]

def update_meta_data(self, filename, size, partcount, date, last_update):
"""Update the meta data table."""
with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con, con as cur:
cur.execute("DELETE from meta")
cur.commit()
cur.execute(
"INSERT INTO meta VALUES (?, ?, ?, ?, ?)",
(filename, size, partcount, date, last_update),
)
cur.commit()

def create_parts_table(self, columns):
"""Create the parts table."""
with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con, con as cur:
cols = ",".join([f" '{c}'" for c in columns])
cur.execute(f"CREATE TABLE IF NOT EXISTS parts ({cols})")
cur.commit()

def insert_parts(self, data, cols):
"""Insert many parts at once."""
with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con:
cols = ",".join(["?"] * cols)
query = f"INSERT INTO parts VALUES ({cols})"
con.executemany(query, data)
con.commit()

def get_part_details(self, number: str) -> dict:
"""Get the part details for a LCSC number using optimized FTS5 querying."""
with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con:
con.row_factory = dict_factory
con.row_factory = dict_factory # noqa: DC05
cur = con.cursor()
query = """SELECT "LCSC Part" AS lcsc, "Stock" AS stock, "Library Type" AS type FROM parts WHERE parts MATCH :number"""
cur.execute(query, {"number": number})
Expand Down
Loading

0 comments on commit 1da1ad9

Please sign in to comment.