From c60f1811d67352a04e715d43b7b64b3fbbcf6d5f Mon Sep 17 00:00:00 2001 From: Bobbe Date: Wed, 24 Jan 2024 00:08:50 +0100 Subject: [PATCH] Add pre-commit-config, Format code --- .pre-commit-config.yaml | 22 ++++++++++++++ README.md | 2 +- generate_release.py | 12 ++++---- pcb2blender_exporter/__init__.py | 5 ++-- pcb2blender_exporter/export.py | 14 ++++----- pcb2blender_exporter/ui.py | 3 +- pcb2blender_importer/blender_addon_utils | 2 +- pcb2blender_importer/importer.py | 37 +++++++++++++----------- pcb2blender_importer/materials.py | 13 ++++----- pcb2blender_importer/solder_joints.py | 6 ++-- pyproject.toml | 15 ++++++++++ tests/test_importer.py | 6 ++-- 12 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..25bf1a5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,22 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: "v4.5.0" + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: mixed-line-ending +- repo: https://github.com/hhatto/autopep8 + rev: "v2.0.4" + hooks: + - id: autopep8 +- repo: https://github.com/pycqa/isort + rev: "5.12.0" + hooks: + - id: isort +- repo: https://github.com/codespell-project/codespell + rev: "v2.2.6" + hooks: + - id: codespell + additional_dependencies: + - tomli diff --git a/README.md b/README.md index 21b3e1f..d903ed9 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ them, after last minute board revisions. `Edit -> Preferences -> Add-ons -> Install`
(you need to install the actual zip file, don't unpack it!) and enable it (enabling the addon for the first time will take a few minutes). - + ### Note on python version (Linux and macOS) On Windows, KiCad and Blender both ship with their own bundled python version diff --git a/generate_release.py b/generate_release.py index 0bb818e..a2145ca 100644 --- a/generate_release.py +++ b/generate_release.py @@ -1,14 +1,16 @@ #!/usr/bin/env python3 -from pathlib import Path -from git import Repo -from zipfile import ZipFile, ZIP_DEFLATED +import json, re, shutil, sys from hashlib import sha256 from itertools import chain +from pathlib import Path +from unittest.mock import patch +from zipfile import ZIP_DEFLATED, ZipFile + +import requests from autopep8 import main as autopep8 +from git import Repo from pytest import main as pytest -from unittest.mock import patch -import json, shutil, requests, re, sys RELEASE_DIRECTORY = Path("release") ARCHIVE_DIRECTORY = RELEASE_DIRECTORY / "archive" diff --git a/pcb2blender_exporter/__init__.py b/pcb2blender_exporter/__init__.py index e6ddcda..10076f8 100644 --- a/pcb2blender_exporter/__init__.py +++ b/pcb2blender_exporter/__init__.py @@ -1,8 +1,9 @@ -import pcbnew, wx from pathlib import Path -from .ui import SettingsDialog +import pcbnew, wx + from .export import export_pcb3d, get_boarddefs +from .ui import SettingsDialog __version__ = "2.8" diff --git a/pcb2blender_exporter/export.py b/pcb2blender_exporter/export.py index fe2bcfa..64a8234 100644 --- a/pcb2blender_exporter/export.py +++ b/pcb2blender_exporter/export.py @@ -1,12 +1,12 @@ -import pcbnew -from pcbnew import ToMM, PLOT_CONTROLLER, PLOT_FORMAT_SVG, DRILL_MARKS_NO_DRILL_SHAPE - -import tempfile, shutil, struct, re -from pathlib import Path -from zipfile import ZipFile, ZIP_DEFLATED +import re, shutil, struct, tempfile from dataclasses import dataclass, field from enum import IntEnum +from pathlib import Path from typing import List, Tuple +from zipfile import ZIP_DEFLATED, ZipFile + +import pcbnew +from pcbnew import DRILL_MARKS_NO_DRILL_SHAPE, PLOT_CONTROLLER, PLOT_FORMAT_SVG, ToMM PCB = "pcb.wrl" COMPONENTS = "components" @@ -290,7 +290,7 @@ def init_tempdir(): file.unlink() shutil.rmtree(tempdir) except OSError: - # if this stil doesn't work, fuck it + # if this still doesn't work, fuck it return tempdir.mkdir() diff --git a/pcb2blender_exporter/ui.py b/pcb2blender_exporter/ui.py index 2694354..fbfb3c6 100644 --- a/pcb2blender_exporter/ui.py +++ b/pcb2blender_exporter/ui.py @@ -1,6 +1,7 @@ -import wx from pathlib import Path +import wx + class SettingsDialog(wx.Dialog): def __init__(self, parent, boarddefs, ignored): wx.Dialog.__init__(self, parent, title="Export to Blender") diff --git a/pcb2blender_importer/blender_addon_utils b/pcb2blender_importer/blender_addon_utils index e17478c..e6ee9cd 160000 --- a/pcb2blender_importer/blender_addon_utils +++ b/pcb2blender_importer/blender_addon_utils @@ -1 +1 @@ -Subproject commit e17478c6db07a631b0ad39cea19007e25e771eee +Subproject commit e6ee9cdb8942a7c10478e58dba2ff0a354d63a08 diff --git a/pcb2blender_importer/importer.py b/pcb2blender_importer/importer.py index eec146b..b22360a 100644 --- a/pcb2blender_importer/importer.py +++ b/pcb2blender_importer/importer.py @@ -1,26 +1,26 @@ -import bpy, bmesh, addon_utils -from bpy_extras.io_utils import ImportHelper, orientation_helper, axis_conversion -from bpy.props import * -from mathutils import Vector, Matrix - -import tempfile, random, shutil, re, struct, io -from math import inf, radians -from pathlib import Path -from zipfile import ZipFile, BadZipFile, Path as ZipPath +import io, random, re, shutil, struct, tempfile from dataclasses import dataclass from enum import Enum -import numpy as np +from math import inf, radians +from pathlib import Path +from zipfile import BadZipFile, Path as ZipPath, ZipFile -from skia import SVGDOM, Stream, Surface, Color4f -SKIA_MAGIC = 0.282222222 +import numpy as np from PIL import Image, ImageOps +from skia import SVGDOM, Color4f, Stream, Surface + +import addon_utils, bmesh, bpy +from bpy.props import * +from bpy_extras.io_utils import ImportHelper, axis_conversion, orientation_helper +from io_scene_x3d import ( + ImportX3D, X3D_PT_import_transform, import_x3d, + menu_func_import as menu_func_import_x3d_original +) +from mathutils import Matrix, Vector from .blender_addon_utils import ErrorHelper from .materials import * -from io_scene_x3d import ImportX3D, X3D_PT_import_transform, import_x3d -from io_scene_x3d import menu_func_import as menu_func_import_x3d_original - PCB = "pcb.wrl" COMPONENTS = "components" LAYERS = "layers" @@ -396,7 +396,7 @@ def import_pcb3d(self, context, filepath): # cut boards - # TODO: maybe handle this differently by always providing atleast one board def? + # TODO: maybe handle this differently by always providing at least one board def? if not (has_multiple_boards := bool(pcb.boards and self.cut_boards)): name = f"PCB_{filepath.stem}" pcb_object.name = pcb_object.data.name = name @@ -864,8 +864,11 @@ def apply_transformation(obj, matrix): @staticmethod def svg2img(svg_path, dpi): svg = SVGDOM.MakeFromStream(Stream.MakeFromFile(str(svg_path))) - width, height = svg.containerSize() + + SKIA_MAGIC = 0.282222222 dpmm = dpi * INCH_TO_MM * SKIA_MAGIC + + width, height = svg.containerSize() pixels_width, pixels_height = round(width * dpmm), round(height * dpmm) surface = Surface(pixels_width, pixels_height) diff --git a/pcb2blender_importer/materials.py b/pcb2blender_importer/materials.py index dc3201d..2d32a7c 100644 --- a/pcb2blender_importer/materials.py +++ b/pcb2blender_importer/materials.py @@ -1,14 +1,13 @@ -from .mat4cad.core import Material, rgb2hex, hex2rgb, srgb2lin, lin2srgb -from .mat4cad.colors import PCB_YELLOW -from .mat4cad.blender import register as register_mat4cad, unregister as unregister_mat4cad -from .custom_node_utils import * - import bpy from bl_ui import node_add_menu from bpy.props import EnumProperty -from mathutils import Vector, Color - from bpy.types import Menu, NodeTree, ShaderNodeCustomGroup +from mathutils import Color, Vector + +from .custom_node_utils import * +from .mat4cad.blender import register as register_mat4cad, unregister as unregister_mat4cad +from .mat4cad.colors import PCB_YELLOW +from .mat4cad.core import Material, hex2rgb, lin2srgb, rgb2hex, srgb2lin LAYER_BOARD_EDGE = "pcb_board_edge" LAYER_THROUGH_HOLES = "pcb_through_holes" diff --git a/pcb2blender_importer/solder_joints.py b/pcb2blender_importer/solder_joints.py index e8616ee..ae8e8a2 100644 --- a/pcb2blender_importer/solder_joints.py +++ b/pcb2blender_importer/solder_joints.py @@ -1,10 +1,10 @@ +import numpy as np + import bpy from bpy.props import * -import numpy as np - -from .importer import MM_TO_M from .custom_node_utils import setup_node_tree +from .importer import MM_TO_M class PCB2BLENDER_OT_solder_joint_add(bpy.types.Operator): """Add a solder joint""" diff --git a/pyproject.toml b/pyproject.toml index 55229df..d58d09f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,5 +4,20 @@ max_line_length = 96 ignore = "E12,E13,E201,E202,E221,E241,E261,E302,E305,E401,E402" exclude = "site-packages" +[tool.isort] +line_length = 96 +lines_after_imports = 1 +combine_as_imports = true +combine_straight_imports = true +multi_line_output = 5 +known_blender = [ + "addon_utils", "bpy", "bpy_extras", "bl_ui", "bmesh", "io_scene_x3d", "mathutils" +] +known_pcbnew = ["pcbnew", "wx"] +sections = ["FUTURE", "STDLIB", "THIRDPARTY", "BLENDER", "PCBNEW", "FIRSTPARTY", "LOCALFOLDER"] + [tool.pytest.ini_options] addopts = "--ignore-glob=*site-packages*" + +[tool.codespell] +ignore-words-list = "tht" diff --git a/tests/test_importer.py b/tests/test_importer.py index 138de2e..748b80f 100644 --- a/tests/test_importer.py +++ b/tests/test_importer.py @@ -1,11 +1,11 @@ -import bpy - -from itertools import product, chain +from itertools import chain, product from pathlib import Path from tempfile import gettempdir import pytest +import bpy + test_filepaths = list((Path(__file__).parent / "test_pcbs").resolve().glob("**/*.pcb3d")) kwargs_test_permutations = {