Create Workspace
This example creates and opens a workspace. It uses the context manager to ensure its closed when finished
Usage:
with create_and_open_an_empty_workspace("workspace_path") as workspace:
# do something with the workspace
@contextmanager
def create_and_open_an_empty_workspace(workspace_path: str) -> Iterator[de.Workspace]:
# Ensure there isn't already a workspace open
if de.workspace_is_open():
de.close_workspace()
# Cannot create a workspace if the directory already exists
workspace_directory = Path(workspace_path)
if workspace_directory.exists():
raise RuntimeError(f"Workspace directory already exists: {workspace_directory}")
# Create the workspace
workspace = de.create_workspace(workspace_directory)
# Opening a workspace will change the current working directory
original_working_directory = Path(os.getcwd())
# Open the workspace
workspace.open()
# Return the open workspace and close when it finished
try:
yield workspace
finally:
# Assert if this workspace is no longer the active workspace
assert de.active_workspace().path == workspace_directory
workspace.close()
# Change the working directory back
os.chdir(original_working_directory)
This example creates a library and adds it to an open workspace
Usage:
with create_and_open_an_empty_workspace("workspace_path") as workspace:
create_a_library_and_add_it_to_the_workspace(workspace)
def create_a_library_and_add_it_to_the_workspace(workspace: de.Workspace) -> None:
assert workspace.path is not None
# Libraries can only be added to an open workspace
assert workspace.is_open
# We'll create a library in the directory of the workspace
library_name = "example_library"
library_path = workspace.path / library_name
# Create the library
de.create_new_library(library_name, library_path)
# And add it to the workspace (update lib.defs)
workspace.add_library(library_name, library_path, de.LibraryMode.SHARED)