From 2b8fe4d6bbe15b4e95ced041a53ab814407998c3 Mon Sep 17 00:00:00 2001
From: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Date: Tue, 17 Jan 2023 17:32:48 +0100
Subject: [PATCH] Add pre-commit hooks and run them

---
 .clang-format                          |  2 +-
 .pre-commit-config.yaml                | 11 ++++++
 CMakeLists.txt                         |  2 +-
 b_asic/GUI/gui_interface.ui            |  8 ++--
 docs_sphinx/api/core_operations.rst    |  1 -
 docs_sphinx/api/graph_component.rst    |  1 -
 docs_sphinx/api/index.rst              |  2 +-
 docs_sphinx/api/operation.rst          |  2 +-
 docs_sphinx/api/port.rst               |  1 -
 docs_sphinx/api/signal.rst             |  1 -
 docs_sphinx/api/signal_flow_graph.rst  |  1 -
 docs_sphinx/api/simulation.rst         |  1 -
 docs_sphinx/api/special_operations.rst |  1 -
 examples/secondorderdirectformiir.py   |  4 +-
 examples/thirdorderblwdf.py            |  7 ++--
 examples/threepointwinograddft.py      | 15 +++++--
 setup.py                               | 37 +++++++++++------
 test/test_operation.py                 | 12 ++++--
 test/test_process.py                   |  1 +
 test/test_sfg.py                       | 55 +++++++++++++++-----------
 20 files changed, 101 insertions(+), 64 deletions(-)
 create mode 100644 .pre-commit-config.yaml

diff --git a/.clang-format b/.clang-format
index 22e04bab..e8824535 100644
--- a/.clang-format
+++ b/.clang-format
@@ -148,4 +148,4 @@ TypenameMacros:
   - LIST
   - LIST_ENTRY
 
-UseTab: ForContinuationAndIndentation
\ No newline at end of file
+UseTab: ForContinuationAndIndentation
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 00000000..48ee0308
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,11 @@
+repos:
+-   repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v2.3.0
+    hooks:
+    -   id: check-yaml
+    -   id: end-of-file-fixer
+    -   id: trailing-whitespace
+-   repo: https://github.com/psf/black
+    rev: 22.10.0
+    hooks:
+    -   id: black
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d087f86..f10f60c9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,7 +41,7 @@ pybind11_add_module("${TARGET_NAME}"
 	# Main files.
 	"${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp"
 	"${CMAKE_CURRENT_SOURCE_DIR}/src/simulation.cpp"
-	
+
 	# For DOD simulation.
 	"${CMAKE_CURRENT_SOURCE_DIR}/src/simulation/compile.cpp"
 	"${CMAKE_CURRENT_SOURCE_DIR}/src/simulation/run.cpp"
diff --git a/b_asic/GUI/gui_interface.ui b/b_asic/GUI/gui_interface.ui
index 38274781..fb0f07cf 100644
--- a/b_asic/GUI/gui_interface.ui
+++ b/b_asic/GUI/gui_interface.ui
@@ -33,11 +33,11 @@
      <bool>false</bool>
     </property>
     <property name="styleSheet">
-     <string notr="true">QGroupBox { 
-     border: 2px solid gray; 
+     <string notr="true">QGroupBox {
+     border: 2px solid gray;
      border-radius: 3px;
-	 margin-top: 0.5em; 
- } 
+	 margin-top: 0.5em;
+ }
 
 QGroupBox::title {
     subcontrol-origin: margin;
diff --git a/docs_sphinx/api/core_operations.rst b/docs_sphinx/api/core_operations.rst
index c362191c..de51d4a6 100644
--- a/docs_sphinx/api/core_operations.rst
+++ b/docs_sphinx/api/core_operations.rst
@@ -10,4 +10,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/graph_component.rst b/docs_sphinx/api/graph_component.rst
index 5c599035..9d0bbe09 100644
--- a/docs_sphinx/api/graph_component.rst
+++ b/docs_sphinx/api/graph_component.rst
@@ -10,4 +10,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/index.rst b/docs_sphinx/api/index.rst
index 9fa92475..a4cc2476 100644
--- a/docs_sphinx/api/index.rst
+++ b/docs_sphinx/api/index.rst
@@ -4,7 +4,7 @@ API
 ===
 .. toctree::
     :maxdepth: 1
-	       
+
     core_operations.rst
     graph_component.rst
     operation.rst
diff --git a/docs_sphinx/api/operation.rst b/docs_sphinx/api/operation.rst
index 2d96ba5a..22d3c0b4 100644
--- a/docs_sphinx/api/operation.rst
+++ b/docs_sphinx/api/operation.rst
@@ -5,7 +5,7 @@
 .. inheritance-diagram:: b_asic.operation
    :parts: 1
    :top-classes: b_asic.graph_component.GraphComponent, b_asic.port.SignalSourceProvider
-		 
+
 .. automodule:: b_asic.operation
    :members:
    :undoc-members:
diff --git a/docs_sphinx/api/port.rst b/docs_sphinx/api/port.rst
index f34f4301..40872c76 100644
--- a/docs_sphinx/api/port.rst
+++ b/docs_sphinx/api/port.rst
@@ -10,4 +10,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/signal.rst b/docs_sphinx/api/signal.rst
index 29979486..3e928163 100644
--- a/docs_sphinx/api/signal.rst
+++ b/docs_sphinx/api/signal.rst
@@ -6,4 +6,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/signal_flow_graph.rst b/docs_sphinx/api/signal_flow_graph.rst
index ee2f7fb0..757936e4 100644
--- a/docs_sphinx/api/signal_flow_graph.rst
+++ b/docs_sphinx/api/signal_flow_graph.rst
@@ -6,4 +6,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/simulation.rst b/docs_sphinx/api/simulation.rst
index 580596f5..6c74873c 100644
--- a/docs_sphinx/api/simulation.rst
+++ b/docs_sphinx/api/simulation.rst
@@ -6,4 +6,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/docs_sphinx/api/special_operations.rst b/docs_sphinx/api/special_operations.rst
index d80e4460..e3585c98 100644
--- a/docs_sphinx/api/special_operations.rst
+++ b/docs_sphinx/api/special_operations.rst
@@ -10,4 +10,3 @@
    :members:
    :undoc-members:
    :show-inheritance:
-
diff --git a/examples/secondorderdirectformiir.py b/examples/secondorderdirectformiir.py
index f812d10f..49cb20d3 100644
--- a/examples/secondorderdirectformiir.py
+++ b/examples/secondorderdirectformiir.py
@@ -34,7 +34,9 @@ a0 = ConstantMultiplication(0.7, add1, "A0")
 add4 = Addition(a0, add3, "ADD4")
 out1 = Output(add4, "OUT1")
 
-sfg = SFG(inputs=[in1], outputs=[out1], name="Second-order direct form IIR filter")
+sfg = SFG(
+    inputs=[in1], outputs=[out1], name="Second-order direct form IIR filter"
+)
 
 # Set latencies and exection times
 sfg.set_latency_of_type(ConstantMultiplication.type_name(), 2)
diff --git a/examples/thirdorderblwdf.py b/examples/thirdorderblwdf.py
index 65dd4b4d..bc2676d4 100644
--- a/examples/thirdorderblwdf.py
+++ b/examples/thirdorderblwdf.py
@@ -16,8 +16,7 @@ D1 << s.output(1)
 a = s.output(0) + D0
 out0 = Output(a, "y")
 
-sfg = SFG(inputs=[in0], outputs=[out0],
-          name="Third-order BLWDF")
+sfg = SFG(inputs=[in0], outputs=[out0], name="Third-order BLWDF")
 
 # Set latencies and exection times
 sfg.set_latency_of_type(SymmetricTwoportAdaptor.type_name(), 4)
@@ -29,10 +28,12 @@ sim = Simulation(sfg, [lambda n: 0 if n else 1])
 sim.run_for(1000)
 
 import scipy.signal
+
 w, h = scipy.signal.freqz(sim.results['0'])
 
 import numpy as np
 import matplotlib.pyplot as plt
-plt.plot(w, 20*np.log10(np.abs(h)/2))
+
+plt.plot(w, 20 * np.log10(np.abs(h) / 2))
 
 schedule = Schedule(sfg, cyclic=True)
diff --git a/examples/threepointwinograddft.py b/examples/threepointwinograddft.py
index ea34231b..6d81fea8 100644
--- a/examples/threepointwinograddft.py
+++ b/examples/threepointwinograddft.py
@@ -1,13 +1,17 @@
 """Three-point Winograd DFT.
 """
 
-from b_asic.core_operations import Addition, ConstantMultiplication, Subtraction
+from b_asic.core_operations import (
+    Addition,
+    ConstantMultiplication,
+    Subtraction,
+)
 from b_asic.special_operations import Input, Output
 from b_asic.signal_flow_graph import SFG
 from b_asic.schedule import Schedule
 from math import cos, pi, sin
 
-u = -2*pi/3
+u = -2 * pi / 3
 c30 = cos(u) - 1
 c31 = sin(u)
 
@@ -27,8 +31,11 @@ out0 = Output(a2, "X0")
 out1 = Output(a4, "X1")
 out2 = Output(a5, "X2")
 
-sfg = SFG(inputs=[in0, in1, in2], outputs=[out0, out1, out2],
-          name="3-point Winograd DFT")
+sfg = SFG(
+    inputs=[in0, in1, in2],
+    outputs=[out0, out1, out2],
+    name="3-point Winograd DFT",
+)
 
 # Set latencies and exection times
 sfg.set_latency_of_type(ConstantMultiplication.type_name(), 2)
diff --git a/setup.py b/setup.py
index 9bd76330..0c8ec302 100644
--- a/setup.py
+++ b/setup.py
@@ -15,9 +15,9 @@ class CMakeExtension(Extension):
 
 class CMakeBuild(build_ext):
     def build_extension(self, ext):
-
         CMAKE_EXE = os.environ.get(
-            "CMAKE_EXE", shutil.which("cmake3") or shutil.which("cmake"))
+            "CMAKE_EXE", shutil.which("cmake3") or shutil.which("cmake")
+        )
         if not isinstance(ext, CMakeExtension):
             return super().build_extension(ext)
 
@@ -25,21 +25,27 @@ class CMakeBuild(build_ext):
             raise RuntimeError(
                 f"Cannot build extension {ext.name}: CMake executable not "
                 "found! Set the CMAKE_EXE environment variable or update your "
-                "path.")
+                "path."
+            )
 
         cmake_build_type = "Debug" if self.debug else "Release"
         cmake_output_dir = os.path.abspath(
-            os.path.dirname(self.get_ext_fullpath(ext.name)))
+            os.path.dirname(self.get_ext_fullpath(ext.name))
+        )
         cmake_configure_argv = [
-            CMAKE_EXE, ext.sourcedir,
+            CMAKE_EXE,
+            ext.sourcedir,
             "-DASIC_BUILDING_PYTHON_DISTRIBUTION=ON",
             "-DCMAKE_BUILD_TYPE=" + cmake_build_type,
             "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + cmake_output_dir,
             "-DPYTHON_EXECUTABLE=" + sys.executable,
         ]
         cmake_build_argv = [
-            CMAKE_EXE, "--build", ".",
-            "--config", cmake_build_type
+            CMAKE_EXE,
+            "--build",
+            ".",
+            "--config",
+            cmake_build_type,
         ]
 
         if not os.path.exists(self.build_temp):
@@ -50,8 +56,9 @@ class CMakeBuild(build_ext):
         print(f"=== Configuring {ext.name} ===")
         print(f"Temp dir: {self.build_temp}")
         print(f"Output dir: {cmake_output_dir}")
-        subprocess.check_call(cmake_configure_argv,
-                              cwd=self.build_temp, env=env)
+        subprocess.check_call(
+            cmake_configure_argv, cwd=self.build_temp, env=env
+        )
 
         print(f"=== Building {ext.name} ===")
         print(f"Temp dir: {self.build_temp}")
@@ -63,14 +70,18 @@ class CMakeBuild(build_ext):
 
 
 setuptools.setup(
-    author="Adam Jakobsson, Angus Lothian, Arvid Westerlund, Felix Goding, "
+    author=(
+        "Adam Jakobsson, Angus Lothian, Arvid Westerlund, Felix Goding, "
         "Ivar Härnqvist, Jacob Wahlman, Kevin Scott, Rasmus Karlsson, "
-        "Oscar Gustafsson, Andreas Bolin",
-    author_email="adaja901@student.liu.se, anglo547@student.liu.se, "
+        "Oscar Gustafsson, Andreas Bolin"
+    ),
+    author_email=(
+        "adaja901@student.liu.se, anglo547@student.liu.se, "
         "arvwe160@student.liu.se, felgo673@student.liu.se, "
         "ivaha717@student.liu.se, jacwa448@student.liu.se, "
         "kevsc634@student.liu.se, raska119@student.liu.se, "
-        "oscar.gustafsson@liu.se, andbo467@student.liu.se",
+        "oscar.gustafsson@liu.se, andbo467@student.liu.se"
+    ),
     ext_modules=[CMakeExtension("_b_asic")],
     cmdclass={"build_ext": CMakeBuild},
 )
diff --git a/test/test_operation.py b/test/test_operation.py
index e09aa5fa..a92f07ce 100644
--- a/test/test_operation.py
+++ b/test/test_operation.py
@@ -17,7 +17,8 @@ from b_asic import (
 
 class TestOperationOverloading:
     def test_addition_overload(self):
-        """Tests addition overloading for both operation and number argument."""
+        """Tests addition overloading for both operation and number argument.
+        """
         add1 = Addition(None, None, "add1")
         add2 = Addition(None, None, "add2")
 
@@ -37,7 +38,8 @@ class TestOperationOverloading:
         assert add5.input(1).signals == add4.output(0).signals
 
     def test_subtraction_overload(self):
-        """Tests subtraction overloading for both operation and number argument."""
+        """Tests subtraction overloading for both operation and number argument.
+        """
         add1 = Addition(None, None, "add1")
         add2 = Addition(None, None, "add2")
 
@@ -57,7 +59,8 @@ class TestOperationOverloading:
         assert sub3.input(1).signals == sub2.output(0).signals
 
     def test_multiplication_overload(self):
-        """Tests multiplication overloading for both operation and number argument."""
+        """Tests multiplication overloading for both operation and number argument.
+        """
         add1 = Addition(None, None, "add1")
         add2 = Addition(None, None, "add2")
 
@@ -77,7 +80,8 @@ class TestOperationOverloading:
         assert mul3.value == 5
 
     def test_division_overload(self):
-        """Tests division overloading for both operation and number argument."""
+        """Tests division overloading for both operation and number argument.
+        """
         add1 = Addition(None, None, "add1")
         add2 = Addition(None, None, "add2")
 
diff --git a/test/test_process.py b/test/test_process.py
index 80dd2ab6..57837ee2 100644
--- a/test/test_process.py
+++ b/test/test_process.py
@@ -2,6 +2,7 @@ import pytest
 
 from b_asic.process import PlainMemoryVariable
 
+
 def test_PlainMemoryVariable():
     mem = PlainMemoryVariable(3, 0, {4: 1, 5: 2})
     assert mem.write_port == 0
diff --git a/test/test_sfg.py b/test/test_sfg.py
index b965e80a..ac73cf3f 100644
--- a/test/test_sfg.py
+++ b/test/test_sfg.py
@@ -78,7 +78,8 @@ class TestPrintSfg:
 
         assert (
             sfg.__str__()
-            == "id: no_id, \tname: SFG1, \tinputs: {0: '-'}, \toutputs: {0: '-'}\n"
+            == "id: no_id, \tname: SFG1, \tinputs: {0: '-'}, \toutputs: {0:"
+            " '-'}\n"
             + "Internal Operations:\n"
             + "----------------------------------------------------------------------------------------------------\n"
             + str(sfg.find_by_name("INP1")[0])
@@ -103,7 +104,8 @@ class TestPrintSfg:
 
         assert (
             sfg.__str__()
-            == "id: no_id, \tname: mac_sfg, \tinputs: {0: '-'}, \toutputs: {0: '-'}\n"
+            == "id: no_id, \tname: mac_sfg, \tinputs: {0: '-'}, \toutputs: {0:"
+            " '-'}\n"
             + "Internal Operations:\n"
             + "----------------------------------------------------------------------------------------------------\n"
             + str(sfg.find_by_name("INP1")[0])
@@ -131,7 +133,8 @@ class TestPrintSfg:
 
         assert (
             sfg.__str__()
-            == "id: no_id, \tname: sfg, \tinputs: {0: '-'}, \toutputs: {0: '-'}\n"
+            == "id: no_id, \tname: sfg, \tinputs: {0: '-'}, \toutputs: {0:"
+            " '-'}\n"
             + "Internal Operations:\n"
             + "----------------------------------------------------------------------------------------------------\n"
             + str(sfg.find_by_name("CONST")[0])
@@ -148,7 +151,8 @@ class TestPrintSfg:
     def test_simple_filter(self, sfg_simple_filter):
         assert (
             sfg_simple_filter.__str__()
-            == "id: no_id, \tname: simple_filter, \tinputs: {0: '-'}, \toutputs: {0: '-'}\n"
+            == "id: no_id, \tname: simple_filter, \tinputs: {0: '-'},"
+            " \toutputs: {0: '-'}\n"
             + "Internal Operations:\n"
             + "----------------------------------------------------------------------------------------------------\n"
             + str(sfg_simple_filter.find_by_name("IN1")[0])
@@ -620,7 +624,6 @@ class TestFindComponentsWithTypeName:
 
 class TestGetPrecedenceList:
     def test_inputs_delays(self, precedence_sfg_delays):
-
         precedence_list = precedence_sfg_delays.get_precedence_list()
 
         assert len(precedence_list) == 7
@@ -677,7 +680,6 @@ class TestGetPrecedenceList:
     def test_inputs_constants_delays_multiple_outputs(
         self, precedence_sfg_delays_and_constants
     ):
-
         precedence_list = (
             precedence_sfg_delays_and_constants.get_precedence_list()
         )
@@ -955,7 +957,8 @@ class TestConnectExternalSignalsToComponentsSoloComp:
     def test_connect_external_signals_to_components_operation_tree(
         self, operation_tree
     ):
-        """Replaces an SFG with only a operation_tree component with its inner components"""
+        """Replaces an SFG with only a operation_tree component with its inner components
+        """
         sfg1 = SFG(outputs=[Output(operation_tree)])
         out1 = Output(None, "OUT1")
         out1.input(0).connect(sfg1.outputs[0], "S1")
@@ -968,7 +971,8 @@ class TestConnectExternalSignalsToComponentsSoloComp:
     def test_connect_external_signals_to_components_large_operation_tree(
         self, large_operation_tree
     ):
-        """Replaces an SFG with only a large_operation_tree component with its inner components"""
+        """Replaces an SFG with only a large_operation_tree component with its inner components
+        """
         sfg1 = SFG(outputs=[Output(large_operation_tree)])
         out1 = Output(None, "OUT1")
         out1.input(0).connect(sfg1.outputs[0], "S1")
@@ -1030,7 +1034,8 @@ class TestConnectExternalSignalsToComponentsMultipleComp:
         assert not test_sfg.connect_external_signals_to_components()
 
     def create_sfg(self, op_tree):
-        """Create a simple SFG with either operation_tree or large_operation_tree"""
+        """Create a simple SFG with either operation_tree or large_operation_tree
+        """
         sfg1 = SFG(outputs=[Output(op_tree)])
 
         inp1 = Input("INP1")
@@ -1050,7 +1055,8 @@ class TestConnectExternalSignalsToComponentsMultipleComp:
     def test_connect_external_signals_to_components_many_op(
         self, large_operation_tree
     ):
-        """Replaces an sfg component in a larger SFG with several component operations"""
+        """Replaces an sfg component in a larger SFG with several component operations
+        """
         inp1 = Input("INP1")
         inp2 = Input("INP2")
         inp3 = Input("INP3")
@@ -1357,20 +1363,21 @@ class TestGetComponentsOfType:
 class TestPrecedenceGraph:
     def test_precedence_graph(self, sfg_simple_filter):
         res = (
-            "digraph {\n\trankdir=LR\n\tsubgraph cluster_0 "
-            '{\n\t\tlabel=N1\n\t\t"in1.0" [label=in1]\n\t\t"t1.0" [label=t1]'
-            '\n\t}\n\tsubgraph cluster_1 {\n\t\tlabel=N2\n\t\t"cmul1.0" '
-            "[label=cmul1]\n\t}\n\tsubgraph cluster_2 "
-            '{\n\t\tlabel=N3\n\t\t"add1.0" [label=add1]\n\t}\n\t"in1.0" '
-            '-> add1\n\tadd1 [label=add1 shape=square]\n\tin1 -> "in1.0"'
-            '\n\tin1 [label=in1 shape=square]\n\t"t1.0" -> cmul1\n\tcmul1 '
-            '[label=cmul1 shape=square]\n\t"t1.0" -> out1\n\tout1 '
-            '[label=out1 shape=square]\n\tt1Out -> "t1.0"\n\tt1Out '
-            '[label=t1 shape=square]\n\t"cmul1.0" -> add1\n\tadd1 '
-            '[label=add1 shape=square]\n\tcmul1 -> "cmul1.0"\n\tcmul1 '
-            '[label=cmul1 shape=square]\n\t"add1.0" -> t1In\n\tt1In '
-            '[label=t1 shape=square]\n\tadd1 -> "add1.0"\n\tadd1 '
-            "[label=add1 shape=square]\n}\n"
+            "digraph {\n\trankdir=LR\n\tsubgraph cluster_0"
+            " {\n\t\tlabel=N1\n\t\t\"in1.0\" [label=in1]\n\t\t\"t1.0\""
+            " [label=t1]\n\t}\n\tsubgraph cluster_1"
+            " {\n\t\tlabel=N2\n\t\t\"cmul1.0\" [label=cmul1]\n\t}\n\tsubgraph"
+            " cluster_2 {\n\t\tlabel=N3\n\t\t\"add1.0\""
+            " [label=add1]\n\t}\n\t\"in1.0\" -> add1\n\tadd1 [label=add1"
+            " shape=square]\n\tin1 -> \"in1.0\"\n\tin1 [label=in1"
+            " shape=square]\n\t\"t1.0\" -> cmul1\n\tcmul1 [label=cmul1"
+            " shape=square]\n\t\"t1.0\" -> out1\n\tout1 [label=out1"
+            " shape=square]\n\tt1Out -> \"t1.0\"\n\tt1Out [label=t1"
+            " shape=square]\n\t\"cmul1.0\" -> add1\n\tadd1 [label=add1"
+            " shape=square]\n\tcmul1 -> \"cmul1.0\"\n\tcmul1 [label=cmul1"
+            " shape=square]\n\t\"add1.0\" -> t1In\n\tt1In [label=t1"
+            " shape=square]\n\tadd1 -> \"add1.0\"\n\tadd1 [label=add1"
+            " shape=square]\n}\n"
         )
 
         assert sfg_simple_filter.precedence_graph().source == res
-- 
GitLab