-
Notifications
You must be signed in to change notification settings - Fork 184
/
cli.py
181 lines (133 loc) · 4.57 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Command line interface. Type `gf` into a terminal."""
from __future__ import annotations
import pathlib
from typing import Optional
import click
from click.core import Context, Option
import gdsfactory
from gdsfactory.config import cwd, print_config
from gdsfactory.install import install_gdsdiff, install_klayout_package
from gdsfactory.layers import lyp_to_dataclass
from gdsfactory.tech import LAYER
from gdsfactory.types import PathType
from gdsfactory.write_cells import write_cells as write_cells_to_separate_gds
VERSION = "6.16.1"
LAYER_LABEL = LAYER.LABEL
def print_version(ctx: Context, param: Option, value: bool) -> None:
"""Prints the version."""
if not value or ctx.resilient_parsing:
return
click.echo(VERSION)
ctx.exit()
# TOOL
@click.group()
def tool() -> None:
"""Commands working with gdsfactory tool."""
pass
@click.command(name="config")
@click.argument("key", required=False, default=None)
def config_get(key: str) -> None:
"""Shows key values from CONFIG."""
print_config(key)
# GDS
@click.group()
def gds() -> None:
"""Commands for dealing with GDS files."""
pass
@click.command(name="layermap_to_dataclass")
@click.argument("filepath", type=click.Path(exists=True))
@click.option("--force", "-f", default=False, help="Force deletion", is_flag=True)
def layermap_to_dataclass(filepath, force: bool) -> None:
"""Converts KLayout LYP to a dataclass."""
filepath_lyp = pathlib.Path(filepath)
filepath_py = filepath_lyp.with_suffix(".py")
if not filepath_lyp.exists():
raise FileNotFoundError(f"{filepath_lyp} not found")
if not force and filepath_py.exists():
raise FileExistsError(f"found {filepath_py}")
lyp_to_dataclass(lyp_filepath=filepath_lyp)
@click.command(name="write_cells")
@click.argument("gdspath")
@click.argument("dirpath", required=False, default=None)
def write_cells(gdspath, dirpath) -> None:
"""Write each all level cells into separate GDS files."""
write_cells_to_separate_gds(gdspath=gdspath, dirpath=dirpath)
@click.command(name="merge_gds")
@click.argument("dirpath", required=False, default=None)
@click.argument("gdspath", required=False, default=None)
def merge_gds(
dirpath: Optional[PathType] = None, gdspath: Optional[PathType] = None
) -> None:
"""Merges GDS cells from a directory into a single GDS."""
dirpath = dirpath or pathlib.Path.cwd()
gdspath = gdspath or pathlib.Path.cwd() / "merged.gds"
dirpath = pathlib.Path(dirpath)
c = gdsfactory.read.from_gdsdir(dirpath=dirpath)
c.write_gds(gdspath=gdspath)
c.show(show_ports=True)
# @click.group()
# def watch() -> None:
# """Watch YAML or python files."""
# pass
# @click.option("--debug", "-d", default=False, help="debug", is_flag=True)
# @click.command()
# def webapp(debug: bool = False) -> None:
# """Opens YAML based webapp."""
# from gdsfactory.icyaml import app
# if debug:
# app.run_debug()
# else:
# app.run()
@click.argument("path", type=click.Path(exists=True), required=False, default=cwd)
@click.command()
def watch(path=cwd) -> None:
"""Filewatch a folder for changes in python or pic.yaml files."""
from gdsfactory.watch import watch
path = pathlib.Path(path)
path = path.parent if path.is_dir() else path
watch(str(path))
# EXTRA
@click.command()
@click.argument("filename")
def show(filename: str) -> None:
"""Show a GDS file using klive."""
gdsfactory.show(filename)
@click.command()
@click.argument("gdspath1")
@click.argument("gdspath2")
@click.option("--xor", "-x", default=False, help="include xor", is_flag=True)
def diff(gdspath1: str, gdspath2: str, xor: bool = False) -> None:
"""Show boolean difference between two GDS files."""
from gdsfactory.gdsdiff.gdsdiff import gdsdiff
diff = gdsdiff(gdspath1, gdspath2, xor=xor)
diff.show()
@click.command()
def install() -> None:
"""Install Klive and generic tech layermap."""
install_klayout_package()
install_gdsdiff()
@click.group()
@click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Show the version number.",
)
def cli(name="gf") -> None:
"""`gf` is the gdsfactory command line tool."""
gds.add_command(layermap_to_dataclass)
gds.add_command(write_cells)
gds.add_command(merge_gds)
gds.add_command(show)
gds.add_command(diff)
tool.add_command(config_get)
tool.add_command(install)
# yaml.add_command(webapp)
# watch.add_command(watch_yaml)
cli.add_command(gds)
cli.add_command(tool)
cli.add_command(watch)
if __name__ == "__main__":
cli()