Skip to content

Commit

Permalink
Migrate from Flake8 to Ruff
Browse files Browse the repository at this point in the history
- Also switched from default single quoted string to double.
- Source code moved from ./src/iproute4mac to ./iproute4mac.
- Ruff code style applied.
  • Loading branch information
signal-09 committed Jul 22, 2024
1 parent 5d65662 commit 4abe060
Show file tree
Hide file tree
Showing 21 changed files with 1,012 additions and 1,037 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install pytest
python -m pip install -r requirements.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 src --select=E9,F63,F7,F82 --show-source --statistics
flake8 src --exit-zero --max-line-length=128 --statistics --ignore=F403,F405,W503
- name: Test with pytest
run: |
pytest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ __pycache__/
build/
dist/
venv/
.tox/
*.egg-info/
_sharedmem.cpython*
.ipynb_checkpoints/
Expand Down
22 changes: 4 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
default_language_version:
python: python3
repos:
- repo: https://github.com/pycqa/flake8
rev: 7.1.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.4
hooks:
- id: flake8
pass_filenames: false
args: [
src,
--select, 'E9,F63,F7,F82',
--show-source,
--statistics
]
- id: flake8
pass_filenames: false
args: [
src,
--max-line-length=128,
--statistics,
--ignore, 'F403,F405,W503'
]
- id: ruff
- id: ruff-format
4 changes: 4 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
line-length = 128

[lint]
ignore = ["F403", "F405"]
1 change: 1 addition & 0 deletions iproute4mac/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION = "0.2.0"
138 changes: 138 additions & 0 deletions iproute4mac/bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env python3

import sys

import iproute4mac
from iproute4mac.utils import *
from iproute4mac.iplink import *
from iproute4mac.ipaddress import *


def do_help(argv=[], option={}):
usage()


def usage():
stderr("""\
Usage: bridge [ OPTIONS ] OBJECT { COMMAND | help }
bridge [ -force ] -batch filename
where OBJECT := { link | fdb | mdb | vlan | vni | monitor }
OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |
-o[neline] | -t[imestamp] | -n[etns] name |
-com[pressvlans] -c[olor] -p[retty] -j[son] }""")
exit(-1)


""" Implemented objects """
objs = [
("link", do_notimplemented),
("fdb", do_notimplemented),
("mdb", do_notimplemented),
("vlan", do_notimplemented),
("vni", do_notimplemented),
("monitor", do_notimplemented),
("help", do_help),
]


def do_obj(argv, option):
obj = argv.pop(0)
for o, f in objs:
if o.startswith(obj):
return f(argv, option)

stderr(f'Object "{obj}" is unknown, try "bridge help".')
return EXIT_FAILURE


def main():
batch_file = None
option = {
"preferred_family": AF_UNSPEC,
"show_stats": False,
"show_details": False,
"oneline": False,
"timestamp": False,
"compress_vlans": False,
"force": False,
"json": False,
"pretty": False,
"do_all": False,
}

if sys.platform != "darwin":
stderr("Unupported OS.")
exit(-1)

argv = sys.argv[1:]
while argv:
if argv[0] == "--":
argv.pop(0)
break
elif argv[0][0] != "-":
break

opt = argv.pop(0)
if opt[1] == "-":
opt = opt[1:]

if "-help".startswith(opt):
usage()
elif "-Version".startswith(opt):
print(f"bridge wrapper, iproute4mac-{iproute4mac.VERSION}")
exit(0)
elif "-stats".startswith(opt) or "-statistics".startswith(opt):
option["show_stats"] = True
elif "-details".startswith(opt):
option["show_details"] = True
elif "-oneline".startswith(opt):
option["oneline"] = True
elif "-timestamp".startswith(opt):
option["timestamp"] = True
elif "-family".startswith(opt):
try:
opt = argv.pop(0)
except IndexError:
missarg("family type")
if opt == "help":
usage()
option["preferred_family"] = read_family(opt)
if option["preferred_family"] == AF_UNSPEC:
invarg("invalid protocol family", opt)
elif opt == "-4":
option["preferred_family"] = AF_INET
elif opt == "-6":
option["preferred_family"] = AF_INET6
elif "-netns".startswith(opt):
do_notimplemented()
elif matches_color(opt):
# Color option is not implemented
pass
elif "-compressvlans".startswith(opt):
option["compress_vlans"] = True
elif "-force".startswith(opt):
option["force"] = True
elif "-json".startswith(opt):
option["json"] = True
elif "-pretty".startswith(opt):
option["pretty"] = True
elif "-batch".startswith(opt):
try:
batch_file = argv.pop(0)
except IndexError:
missarg("batch file")
else:
stderr(f'Option "{opt}" is unknown, try "bridge help".')
exit(-1)

if batch_file:
do_notimplemented()

if argv:
return do_obj(argv, option)

usage()


if __name__ == "__main__":
main()
Loading

0 comments on commit 4abe060

Please sign in to comment.