Skip to content

Commit 451de23

Browse files
authored
Restructure into subpackages (#352)
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.
1 parent 6a72e49 commit 451de23

File tree

509 files changed

+6928
-7170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

509 files changed

+6928
-7170
lines changed

compiler/src/main/scala/edg/compiler/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class AssignNamer() {
116116
}
117117

118118
object Compiler {
119-
final val kExpectedProtoVersion = 4
119+
final val kExpectedProtoVersion = 5
120120
}
121121

122122
/** Compiler for a particular design, with an associated library to elaborate references from.

compiler/src/main/scala/edg/compiler/CompilerServerMain.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import java.io.{File, PrintWriter, StringWriter}
1313

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

9696
def main(args: Array[String]): Unit = {
97-
val hdlServerOption = PythonInterface.serverFileOption(None) // local relative path
98-
hdlServerOption.foreach { serverFile => println(s"Using local $serverFile") }
99-
val pyIf = new ForwardingPythonInterface(hdlServerOption, Seq(new File(".").getAbsolutePath))
97+
val pyIf = new ForwardingPythonInterface()
10098
(pyIf.getProtoVersion() match {
10199
case Errorable.Success(pyVersion) if pyVersion == Compiler.kExpectedProtoVersion => None
102100
case Errorable.Success(pyMismatchVersion) => Some(pyMismatchVersion.toString)

compiler/src/main/scala/edg/compiler/PythonInterface.scala

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ object ProtobufStdioSubprocess {
2020

2121
class ProtobufStdioSubprocess[RequestType <: scalapb.GeneratedMessage, ResponseType <: scalapb.GeneratedMessage](
2222
responseType: scalapb.GeneratedMessageCompanion[ResponseType],
23-
pythonPaths: Seq[String],
24-
args: Seq[String]
23+
pythonPaths: Seq[String] = Seq(),
24+
args: Seq[String] = Seq()
2525
) {
2626
protected val process: Either[Process, Throwable] =
2727
try {
@@ -128,32 +128,22 @@ class ProtobufStdioSubprocess[RequestType <: scalapb.GeneratedMessage, ResponseT
128128
}
129129
}
130130

131-
object PythonInterface {
132-
private val kHdlServerFilePath = "edg_hdl_server/__main__.py"
133-
// returns the HDL server Python script if it exists locally, otherwise returns None.
134-
def serverFileOption(root: Option[File] = None): Option[File] = {
135-
val hdlServerFile = root match {
136-
case Some(root) => new File(root, kHdlServerFilePath)
137-
case None => new File(kHdlServerFilePath)
138-
}
139-
if (hdlServerFile.exists()) {
140-
Some(hdlServerFile)
141-
} else {
142-
None
143-
}
144-
}
145-
}
146-
147131
/** An interface to the Python HDL elaborator, which reads in Python HDL code and (partially) compiles them down to IR.
148132
* The underlying Python HDL should not change while this is open. This will not reload updated Python HDL files.
149133
*
150-
* If the serverFile is specified, run that; otherwise use "python -m edg_hdl_server" for the global package.
134+
* This invokes "python -m edg.hdl_server", using either the local or global (pip) module as available.
151135
*/
152-
class PythonInterface(serverFile: Option[File], pythonPaths: Seq[String], pythonInterpreter: String = "python") {
153-
val command = serverFile match { // -u for unbuffered mode
154-
case Some(serverFile) => Seq(pythonInterpreter, "-u", serverFile.getAbsolutePath)
155-
case None => Seq(pythonInterpreter, "-u", "-m", "edg_hdl_server")
136+
class PythonInterface(interpreter: String = "python", pythonPaths: Seq[String] = Seq()) {
137+
val submoduleSearchPaths = if (pythonPaths.nonEmpty) pythonPaths else Seq(".")
138+
val isSubmoduled = submoduleSearchPaths.map { searchPath => // check if submoduled, if so prepend the submodule name
139+
new File(new File(searchPath), "PolymorphicBlocks/edg/hdl_server/__init__.py").exists()
140+
}.exists(identity)
141+
private val packageName = if (isSubmoduled) {
142+
"PolymorphicBlocks.edg.hdl_server"
143+
} else {
144+
"edg.hdl_server"
156145
}
146+
private val command = Seq(interpreter, "-u", "-m", packageName)
157147
protected val process = new ProtobufStdioSubprocess[edgrpc.HdlRequest, edgrpc.HdlResponse](
158148
edgrpc.HdlResponse,
159149
pythonPaths,

compiler/src/main/scala/edg/wir/LibraryConnectivityAnalysis.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import edgir.elem.elem
77
object LibraryConnectivityAnalysis {
88
// Shared library path to the base PortBridge class
99
val portBridge =
10-
ref.LibraryPath(target = Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg_core.PortBlocks.PortBridge"))))
10+
ref.LibraryPath(target = Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg.core.PortBlocks.PortBridge"))))
1111
val portBridges = Set( // TODO this currently is a hack to avoid proper (multiple-level) subclass resolution
1212
portBridge,
1313
ref.LibraryPath(target =
14-
Some(ref.LocalStep(step = ref.LocalStep.Step.Name("electronics_model.CircuitBlock.CircuitPortBridge")))
14+
Some(ref.LocalStep(step = ref.LocalStep.Step.Name("edg.electronics_model.CircuitBlock.CircuitPortBridge")))
1515
)
1616
)
1717
val portBridgeOuterPort = "outer_port"

compiler/src/test/scala/edg/compiler/PythonInterfaceTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class PythonInterfaceTest extends AnyFlatSpec {
1212
val compiledDir = new File(getClass.getResource("").getPath)
1313
// above returns compiler/target/scala-2.xx/test-classes/edg/compiler, get the root repo dir
1414
val repoDir = compiledDir.getParentFile.getParentFile.getParentFile.getParentFile.getParentFile.getParentFile
15-
val pyIf = new PythonInterface(Some(new File(repoDir, "edg_hdl_server/__main__.py")), Seq(repoDir.getAbsolutePath))
16-
pyIf.indexModule("edg_core").getClass should equal(classOf[Errorable.Success[Seq[LibraryPath]]])
15+
val pyIf = new PythonInterface(pythonPaths = Seq(repoDir.getAbsolutePath))
16+
pyIf.indexModule("edg.core").getClass should equal(classOf[Errorable.Success[Seq[LibraryPath]]])
1717
pyIf.shutdown() should equal(0)
1818
}
1919
}

edg/BoardCompiler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from contextlib import suppress
44
from typing import Type, Optional, Tuple
55

6-
from edg_core import Block, ScalaCompiler, CompiledDesign
7-
from electronics_model import NetlistBackend, SvgPcbBackend
8-
from electronics_model.RefdesRefinementPass import RefdesRefinementPass
9-
from electronics_model.BomBackend import GenerateBom
6+
from .core import Block, ScalaCompiler, CompiledDesign
7+
from .electronics_model.NetlistBackend import NetlistBackend # imported separately b/c mypy confuses with the modules
8+
from .electronics_model.SvgPcbBackend import SvgPcbBackend
9+
from .electronics_model.RefdesRefinementPass import RefdesRefinementPass
10+
from .electronics_model.BomBackend import GenerateBom
1011

1112

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

4344
if compiled.errors:
44-
import edg_core
45-
raise edg_core.ScalaCompilerInterface.CompilerCheckError(f"error during compilation:\n{compiled.errors_str()}")
45+
from . import core
46+
raise core.ScalaCompilerInterface.CompilerCheckError(f"error during compilation:\n{compiled.errors_str()}")
4647

4748
netlist_all = NetlistBackend().run(compiled)
4849
netlist_refdes = NetlistBackend().run(compiled, {'RefdesMode': 'refdes'})

edg/BoardTop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from electronics_lib import *
1+
from .parts import *
22

33

44
class BaseBoardTop(DesignTop):

edg/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# A metapackage for all the packages needed for electronics design with EDG
22

3-
from edg_core import *
4-
from electronics_model import *
5-
from electronics_abstract_parts import *
6-
from electronics_lib import *
3+
from .core import *
4+
from .electronics_model import *
5+
from .abstract_parts import *
6+
from .parts import *
77

88
from .BoardTop import BoardTop, SimpleBoardTop, JlcBoardTop
99

electronics_abstract_parts/AbstractAnalogSwitch.py renamed to edg/abstract_parts/AbstractAnalogSwitch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, cast, Optional, Dict
22

3-
from electronics_model import *
3+
from ..electronics_model import *
44
from .Categories import Interface
55

66

electronics_abstract_parts/AbstractAntenna.py renamed to edg/abstract_parts/AbstractAntenna.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from electronics_model import *
1+
from ..electronics_model import *
22
from .Categories import *
33
from .PartsTable import PartsTableColumn, PartsTableRow
44
from .PartsTablePart import PartsTableSelector

0 commit comments

Comments
 (0)