Skip to content

Commit

Permalink
stm_layout: Convert to an installable package; fix pylint/flake8 issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgree committed Sep 20, 2020
1 parent f41b322 commit 5429bd5
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 113 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
ignore = E123, E126, E201, E203, E211, E221, E222, E225, E226, E227, E241, \
E741, W504 C901
exclude = .git,__pycache__,build,dist
max-complexity = 20
max-line-length = 80
good-names = l
145 changes: 130 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,131 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

*.swp
*.elf
*.o
*.d
*.a
*.map
*.out
*.pyc
/build/
/bin/
/tests/
tags
/.xml
/modm_devices
/modm-devices
/tgcurses*
17 changes: 17 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[MESSAGES CONTROL]
disable=bad-whitespace,
missing-module-docstring,
invalid-name,
missing-function-docstring,
too-many-arguments,
missing-class-docstring,
too-few-public-methods,
too-many-instance-attributes,
too-many-locals,
too-many-statements,
too-many-branches,
protected-access,
fixme,
global-statement,
broad-except,
useless-super-delegation
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Terry Greeniaus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 35 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
.PHONY: all
all: tgcurses modm_devices
MODULE := stm_layout
MODULE_VERS := 0.1.0
MODULE_DEPS := \
setup.cfg \
setup.py \
stm_layout/*.py

FLAKE_MODULES := stm_layout
LINT_MODULES := stm_layout

tgcurses:
@git clone https://github.com/tgree/tgcurses tgcurses.git
@ln -s tgcurses.git/tgcurses
.PHONY: all
all: $(MODULE)

.PHONY: clean
clean:
rm -rf dist *.egg-info build
find . -name "*.pyc" | xargs rm
find . -name __pycache__ | xargs rm -r

modm-devices:
@git clone -b feature/pinout https://github.com/salkinium/modm-devices
.PHONY: test
test: flake8 lint

.PHONY: flake8
flake8:
python3 -m flake8 $(FLAKE_MODULES)

.xml: modm-devices
@cd modm-devices/tools/generator && make init
@cd modm-devices/tools/generator && make extract-stm32
@cd modm-devices/tools/generator && make generate-stm32
@touch .xml
.PHONY: lint
lint:
pylint $(LINT_MODULES)

.PHONY: $(MODULE)
$(MODULE): dist/$(MODULE)-$(MODULE_VERS)-py3-none-any.whl

modm_devices: modm-devices
@ln -s modm-devices/modm_devices
.PHONY: install
install: $(MODULE)
sudo pip3 uninstall -y $(MODULE)
sudo pip3 install dist/$(MODULE)-$(MODULE_VERS)-py3-none-any.whl

.PHONY: uninstall
uninstall:
sudo pip3 uninstall $(MODULE)

clean:
@rm -rf tgcurses modm-devices .xml 2>/dev/null or true
dist/$(MODULE)-$(MODULE_VERS)-py3-none-any.whl: $(MODULE_DEPS) Makefile
python3 setup.py --quiet sdist bdist_wheel
python3 -m twine check $@
17 changes: 11 additions & 6 deletions README.txt → README.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
Python curses-based tool for configuring STM32 pins.
Curses-based tool for configuring STM32 pins.
=============================================

This tool uses a fork of the amazing curated .xml from the modm-devices
project. The modm-devices project provides metadata about all STM32 devices
in machine-parseable .xml format and is really what makes any tool like this
one possible.

Building: Just type 'make'.
Installing::

Usage: ./stm_layout -c <chip_name>
pip3 install stm_layout

Usage::

stm_layout -c <chip_name>

If chip_name is not fully-specified (i.e. 'stm32g474' is only a partial chip
name), then a list of available chips matching that part will be printed to
stdout. If the chip name is fully specified, (i.e. 'stm32g474cet'), then a
text UI will be brought up for browsing/searching pins and configuring them.
stdout. If the chip name is fully-specified, (i.e. 'stm32g474cet6'), then a
curses UI will be brought up for browsing/searching pins and configuring them.

Navigate using the arrow keys and the tab key. Search using standard regex
queries in the search bar. In any pane but the search pane:
queries in the search bar. In any pane but the search pane::

q - quits
w - writes /tmp/stm32_pinout.txt
Expand Down
26 changes: 26 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[metadata]
name = stm_layout
version = 0.1.0
author = Terry Greeniaus
author_email = [email protected]
description = Curses-based tool for configuring STM32 pins.
long_description = file: README.rst
long_description_content_type = text/x-rst
keywords = stm32 modm-devices pin pinout
url = https://github.com/tgree/stm_layout
license = License :: OSI Approved :: MIT License
classifiers =
Operating System :: POSIX
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License

[options]
python_requires = >=3.6
packages = stm_layout
install_requires =
modm-devices
tgcurses

[options.entry_points]
console_scripts =
stm_layout = stm_layout.stm_layout:_main
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import setuptools

setuptools.setup()
7 changes: 7 additions & 0 deletions stm_layout/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import chip_db
from . import chip_stm


__all__ = ['chip_db',
'chip_stm',
]
25 changes: 16 additions & 9 deletions chip_db.py → stm_layout/chip_db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import modm_devices.parser
import glob
import math
import re

import chip_package
import modm_devices.parser
import modm_devices.pkg

from . import chip_package


DEVICES = None
Expand All @@ -12,7 +14,8 @@
def _populate_devices():
global DEVICES
DEVICES = {}
for filename in glob.glob('modm-devices/devices/**/*.xml'):
basedir = modm_devices.pkg.get_filename('modm_devices', 'resources/devices')
for filename in glob.glob('%s/**/*.xml' % basedir):
parser = modm_devices.parser.DeviceParser()
devfile = parser.parse(filename)
for device in devfile.get_devices():
Expand All @@ -35,15 +38,16 @@ def pin_count(dev):
# Unfortunately, sometimes R means 64 and sometimes it means 68. So, we
# just count the pins. For some reason, counting the pins goes slower.
# Maybe modm-devices does some deferred loading? Yes it does. -Niklas
return len(set(p['position'] for p in dev.get_driver("gpio")["package"][0]["pin"]))
return len(set(p['position']
for p in dev.get_driver('gpio')['package'][0]['pin']))


def package(dev):
try:
return dev.get_driver("gpio")["package"][0]["name"]
except:
return dev.get_driver('gpio')['package'][0]['name']
except Exception:
pass
raise Exception("Device %s has unknown package '%s'."
raise Exception('Device %s has unknown package "%s".'
% (dev, dev.identifier.package))


Expand All @@ -58,12 +62,15 @@ def make_package(dev):
else:
dim = int(math.ceil(math.sqrt(float(n))))
return chip_package.BGA(dim, dim)
elif any(name in p for name in ('LQFP', 'UFQFPN', 'VFQFPN')):

if any(name in p for name in ('LQFP', 'UFQFPN', 'VFQFPN')):
dim = int(math.ceil(float(n)/4.))
return chip_package.LQFP(dim, dim)
elif any(name in p for name in ('TSSOP', 'SO8N')):

if any(name in p for name in ('TSSOP', 'SO8N')):
dim = int(math.ceil(float(n)/2.))
return chip_package.TSSOP(dim)

raise KeyError


Expand Down
Loading

0 comments on commit 5429bd5

Please sign in to comment.