Skip to content

Commit

Permalink
Restructure into subpackages (#352)
Browse files Browse the repository at this point in the history
Instead of a bunch of top-level packages, moves things like `edg_core`
into `edg.core`. Also renames some subpackages for simplicity:
`electronics_abstract_parts` -> `abstract_parts`, `electronics_lib` ->
`parts`. Should not break compatibility for those using `from edg import
*`, but will break for those importing internals.

This structure should allow this to be used as a submodule and is
overall cleaner. The compiler will detect if PolymorphicBlocks exists in
the current path, and if so, uses the qualified path.

`examples` is still independent and is not part of the public API or
package structure.
  • Loading branch information
ducky64 authored May 22, 2024
1 parent 6a72e49 commit 451de23
Show file tree
Hide file tree
Showing 509 changed files with 6,928 additions and 7,170 deletions.
2 changes: 1 addition & 1 deletion compiler/src/main/scala/edg/compiler/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class AssignNamer() {
}

object Compiler {
final val kExpectedProtoVersion = 4
final val kExpectedProtoVersion = 5
}

/** Compiler for a particular design, with an associated library to elaborate references from.
Expand Down
8 changes: 3 additions & 5 deletions compiler/src/main/scala/edg/compiler/CompilerServerMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import java.io.{File, PrintWriter, StringWriter}

// a PythonInterface that uses the on-event hooks to forward stderr and stdout
// without this, the compiler can freeze on large stdout/stderr data, possibly because of queue sizing
class ForwardingPythonInterface(serverFile: Option[File], pythonPaths: Seq[String])
extends PythonInterface(serverFile, pythonPaths) {
class ForwardingPythonInterface(pythonPaths: Seq[String] = Seq())
extends PythonInterface(pythonPaths = pythonPaths) {
def forwardProcessOutput(): Unit = {
StreamUtils.forAvailable(processOutputStream) { data =>
System.out.print(new String(data))
Expand Down Expand Up @@ -94,9 +94,7 @@ object CompilerServerMain {
}

def main(args: Array[String]): Unit = {
val hdlServerOption = PythonInterface.serverFileOption(None) // local relative path
hdlServerOption.foreach { serverFile => println(s"Using local $serverFile") }
val pyIf = new ForwardingPythonInterface(hdlServerOption, Seq(new File(".").getAbsolutePath))
val pyIf = new ForwardingPythonInterface()
(pyIf.getProtoVersion() match {
case Errorable.Success(pyVersion) if pyVersion == Compiler.kExpectedProtoVersion => None
case Errorable.Success(pyMismatchVersion) => Some(pyMismatchVersion.toString)
Expand Down
36 changes: 13 additions & 23 deletions compiler/src/main/scala/edg/compiler/PythonInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ object ProtobufStdioSubprocess {

class ProtobufStdioSubprocess[RequestType <: scalapb.GeneratedMessage, ResponseType <: scalapb.GeneratedMessage](
responseType: scalapb.GeneratedMessageCompanion[ResponseType],
pythonPaths: Seq[String],
args: Seq[String]
pythonPaths: Seq[String] = Seq(),
args: Seq[String] = Seq()
) {
protected val process: Either[Process, Throwable] =
try {
Expand Down Expand Up @@ -128,32 +128,22 @@ class ProtobufStdioSubprocess[RequestType <: scalapb.GeneratedMessage, ResponseT
}
}

object PythonInterface {
private val kHdlServerFilePath = "edg_hdl_server/__main__.py"
// returns the HDL server Python script if it exists locally, otherwise returns None.
def serverFileOption(root: Option[File] = None): Option[File] = {
val hdlServerFile = root match {
case Some(root) => new File(root, kHdlServerFilePath)
case None => new File(kHdlServerFilePath)
}
if (hdlServerFile.exists()) {
Some(hdlServerFile)
} else {
None
}
}
}

/** An interface to the Python HDL elaborator, which reads in Python HDL code and (partially) compiles them down to IR.
* The underlying Python HDL should not change while this is open. This will not reload updated Python HDL files.
*
* If the serverFile is specified, run that; otherwise use "python -m edg_hdl_server" for the global package.
* This invokes "python -m edg.hdl_server", using either the local or global (pip) module as available.
*/
class PythonInterface(serverFile: Option[File], pythonPaths: Seq[String], pythonInterpreter: String = "python") {
val command = serverFile match { // -u for unbuffered mode
case Some(serverFile) => Seq(pythonInterpreter, "-u", serverFile.getAbsolutePath)
case None => Seq(pythonInterpreter, "-u", "-m", "edg_hdl_server")
class PythonInterface(interpreter: String = "python", pythonPaths: Seq[String] = Seq()) {
val submoduleSearchPaths = if (pythonPaths.nonEmpty) pythonPaths else Seq(".")
val isSubmoduled = submoduleSearchPaths.map { searchPath => // check if submoduled, if so prepend the submodule name
new File(new File(searchPath), "PolymorphicBlocks/edg/hdl_server/__init__.py").exists()
}.exists(identity)
private val packageName = if (isSubmoduled) {
"PolymorphicBlocks.edg.hdl_server"
} else {
"edg.hdl_server"
}
private val command = Seq(interpreter, "-u", "-m", packageName)
protected val process = new ProtobufStdioSubprocess[edgrpc.HdlRequest, edgrpc.HdlResponse](
edgrpc.HdlResponse,
pythonPaths,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import edgir.elem.elem
object LibraryConnectivityAnalysis {
// Shared library path to the base PortBridge class
val portBridge =
ref.LibraryPath(target = Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg_core.PortBlocks.PortBridge"))))
ref.LibraryPath(target = Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg.core.PortBlocks.PortBridge"))))
val portBridges = Set( // TODO this currently is a hack to avoid proper (multiple-level) subclass resolution
portBridge,
ref.LibraryPath(target =
Some(ref.LocalStep(step = ref.LocalStep.Step.Name("electronics_model.CircuitBlock.CircuitPortBridge")))
Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg.electronics_model.CircuitBlock.CircuitPortBridge")))
)
)
val portBridgeOuterPort = "outer_port"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class PythonInterfaceTest extends AnyFlatSpec {
val compiledDir = new File(getClass.getResource("").getPath)
// above returns compiler/target/scala-2.xx/test-classes/edg/compiler, get the root repo dir
val repoDir = compiledDir.getParentFile.getParentFile.getParentFile.getParentFile.getParentFile.getParentFile
val pyIf = new PythonInterface(Some(new File(repoDir, "edg_hdl_server/__main__.py")), Seq(repoDir.getAbsolutePath))
pyIf.indexModule("edg_core").getClass should equal(classOf[Errorable.Success[Seq[LibraryPath]]])
val pyIf = new PythonInterface(pythonPaths = Seq(repoDir.getAbsolutePath))
pyIf.indexModule("edg.core").getClass should equal(classOf[Errorable.Success[Seq[LibraryPath]]])
pyIf.shutdown() should equal(0)
}
}
13 changes: 7 additions & 6 deletions edg/BoardCompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from contextlib import suppress
from typing import Type, Optional, Tuple

from edg_core import Block, ScalaCompiler, CompiledDesign
from electronics_model import NetlistBackend, SvgPcbBackend
from electronics_model.RefdesRefinementPass import RefdesRefinementPass
from electronics_model.BomBackend import GenerateBom
from .core import Block, ScalaCompiler, CompiledDesign
from .electronics_model.NetlistBackend import NetlistBackend # imported separately b/c mypy confuses with the modules
from .electronics_model.SvgPcbBackend import SvgPcbBackend
from .electronics_model.RefdesRefinementPass import RefdesRefinementPass
from .electronics_model.BomBackend import GenerateBom


def compile_board(design: Type[Block], target_dir_name: Optional[Tuple[str, str]]) -> CompiledDesign:
Expand Down Expand Up @@ -41,8 +42,8 @@ def compile_board(design: Type[Block], target_dir_name: Optional[Tuple[str, str]
raw_file.write(compiled.design.SerializeToString())

if compiled.errors:
import edg_core
raise edg_core.ScalaCompilerInterface.CompilerCheckError(f"error during compilation:\n{compiled.errors_str()}")
from . import core
raise core.ScalaCompilerInterface.CompilerCheckError(f"error during compilation:\n{compiled.errors_str()}")

netlist_all = NetlistBackend().run(compiled)
netlist_refdes = NetlistBackend().run(compiled, {'RefdesMode': 'refdes'})
Expand Down
2 changes: 1 addition & 1 deletion edg/BoardTop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_lib import *
from .parts import *


class BaseBoardTop(DesignTop):
Expand Down
8 changes: 4 additions & 4 deletions edg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# A metapackage for all the packages needed for electronics design with EDG

from edg_core import *
from electronics_model import *
from electronics_abstract_parts import *
from electronics_lib import *
from .core import *
from .electronics_model import *
from .abstract_parts import *
from .parts import *

from .BoardTop import BoardTop, SimpleBoardTop, JlcBoardTop

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, cast, Optional, Dict

from electronics_model import *
from ..electronics_model import *
from .Categories import Interface


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableSelector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprintSelector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional, cast, Dict, Any, List, Tuple, Mapping
import math

from electronics_model import *
from ..electronics_model import *
from .PartsTable import PartsTableColumn, PartsTableRow, PartsTable
from .PartsTablePart import PartsTableFootprintSelector
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import Connector


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from . import PartsTableFootprintSelector, PartsTableColumn, Capacitor, PartsTableRow
from .Categories import *
from .StandardFootprint import StandardFootprint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .Categories import ProgrammingConnector
from electronics_model import *
from ..electronics_model import *


@abstract_block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Dict
from deprecated import deprecated

from electronics_model import *
from ..electronics_model import *
from .DummyDevices import ForcedAnalogVoltage
from .Categories import *
from .PartsTable import PartsTableColumn, PartsTableRow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, cast, Dict

from electronics_model import *
from ..electronics_model import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprintSelector
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Any, Dict

from electronics_model import *
from ..electronics_model import *
from .PartsTable import PartsTableColumn, PartsTableRow, PartsTable
from .PartsTablePart import PartsTableFootprintSelector
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, cast

from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprint, PartsTableFootprintSelector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, Optional, cast

from electronics_model import *
from ..electronics_model import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprintSelector
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import DiscreteComponent, TypedJumper


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .AbstractResistor import Resistor
from .StandardFootprint import StandardFootprint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_abstract_parts import *
from ..abstract_parts import *


@abstract_block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Mapping, Tuple, List, NamedTuple

from electronics_model import *
from ..electronics_model import *
from .Categories import Analog


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod

from electronics_model import *
from ..electronics_model import *
from . import PartsTableFootprint, PartsTableColumn, PartsTableRow, PartsTableSelector
from .Categories import *

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import abstractmethod
from typing import Optional
from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .AbstractCapacitor import DecouplingCapacitor
from .AbstractInductor import Inductor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from typing import Optional, cast, Mapping, Dict

from electronics_model import *
from ..electronics_model import *
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprintSelector
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from electronics_model import *
from ..electronics_model import *
from .AbstractResistor import Resistor
from .PartsTable import PartsTableColumn, PartsTableRow
from .PartsTablePart import PartsTableFootprintSelector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from electronics_model import *
from ..electronics_model import *
from .MergedBlocks import MergedAnalogSource
from .AbstractResistor import Resistor
from .Categories import Interface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from electronics_model import *
from ..electronics_model import *
from .Categories import *


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import cast

from electronics_model import *
from electronics_model.CanPort import CanLogicLink
from electronics_model.I2cPort import I2cLink
from ..electronics_model import *
from ..electronics_model.CanPort import CanLogicLink
from ..electronics_model.I2cPort import I2cLink
from .AbstractConnector import RfConnector, RfConnectorTestPoint
from .AbstractResistor import Resistor
from .Categories import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *
from .AbstractDiodes import BaseDiode

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import *


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from edg_core import BoolLike, init_in_parent
from electronics_model import Block, abstract_block, InternalBlock
from ..core import BoolLike, init_in_parent
from ..electronics_model import Block, abstract_block, InternalBlock


@abstract_block
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from electronics_model import *
from ..electronics_model import *
from .AbstractFets import SwitchFet
from .AbstractResistor import Resistor
from .AbstractDiodes import ZenerDiode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from electronics_model import *
from ..electronics_model import *
from .Categories import Interface


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

from electronics_model import *
from ..electronics_model import *
from .Categories import *


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import deque
from typing import Sequence, Optional, TypeVar, Tuple, List, Generic, Type

from electronics_model import *
from ..electronics_model import *


class ESeriesUtil:
Expand Down
Loading

0 comments on commit 451de23

Please sign in to comment.