Skip to content

Commit d137b5f

Browse files
committed
Code cleanup and tweaks
1 parent f86415a commit d137b5f

File tree

14 files changed

+60
-51
lines changed

14 files changed

+60
-51
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"exportstl",
2525
"featureid",
2626
"FEATURESTUDIO",
27+
"Kempen",
2728
"levelname",
2829
"Onshape",
2930
"UNCONFIGURABLE",

backend/common/evaluate.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
""""Utilities for evaluating FeatureScripts against part studios."""
22

3-
43
from concurrent import futures
54
import dataclasses
65
import pathlib
76
from typing import Iterable, TypedDict
87
import onshape_api
9-
from onshape_api import endpoints
8+
from onshape_api.endpoints.part_studios import evaluate_feature_script
109

1110
SCRIPT_PATH = pathlib.Path("../scripts")
1211

@@ -28,23 +27,23 @@ class AutoAssemblyTarget(TypedDict):
2827
def evalute_auto_assembly_part(
2928
api: onshape_api.Api, part_studio_path: onshape_api.ElementPath
3029
) -> dict:
31-
return endpoints.evaluate_feature_script(
30+
return evaluate_feature_script(
3231
api, part_studio_path, open_script("parseAutoAssembly")
3332
)
3433

3534

3635
def evalute_auto_assembly_target_part(
3736
api: onshape_api.Api, part_studio_path: onshape_api.ElementPath
3837
) -> dict:
39-
return endpoints.evaluate_feature_script(
38+
return evaluate_feature_script(
4039
api, part_studio_path, open_script("parseAutoAssemblyTarget")
4140
)
4241

4342

4443
def evaluate_assembly_mirror_part(
4544
api: onshape_api.Api, part_studio_path: onshape_api.ElementPath
4645
) -> dict:
47-
return endpoints.evaluate_feature_script(
46+
return evaluate_feature_script(
4847
api, part_studio_path, open_script("parseAssemblyMirror")
4948
)
5049

backend_tools/pull_scripts.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import pathlib
44
from onshape_api.api import key_api
55
from onshape_api.endpoints import feature_studios
6-
from featurescript import endpoints, feature_studio
6+
from featurescript import feature_studio
77
from featurescript.transform import transform
88
from featurescript import conf
9+
from robot_code.documents import BACKEND
910

1011

1112
def insert_code(function: str, code: list[str]) -> str:
@@ -17,11 +18,7 @@ def insert_code(function: str, code: list[str]) -> str:
1718

1819
def main():
1920
onshape = key_api.make_key_api()
20-
config = conf.Config()
21-
backend_path = config.get_document("backend")
22-
if not backend_path:
23-
raise ValueError("Unexpectedly failed to find backend document")
24-
studio_path_map = feature_studio.get_feature_studios(onshape, backend_path)
21+
studio_path_map = feature_studio.get_feature_studios(onshape, BACKEND)
2522

2623
json_code = feature_studios.pull_code(onshape, studio_path_map["toJson.fs"].path)
2724
assembly_script_code = feature_studios.pull_code(
@@ -40,7 +37,7 @@ def main():
4037
function = transform.extract_function(assembly_script_code, name)
4138
function = insert_code(
4239
"function" + (function.strip().removeprefix("function " + name)), functions
43-
) # .replace("\\", "\\\\")
40+
)
4441
pathlib.Path("backend/scripts/" + name + ".fs").write_text(function)
4542

4643

featurescript/base/ctxt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Context:
3535
which circumvents nested predicate restrictions.
3636
"""
3737

38-
std_version: int
38+
std_version: str
3939

4040
imports: list[imp.Import] = dataclasses.field(default_factory=list)
4141

featurescript/base/imp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import override
33
from featurescript.base import node, ctxt, user_error
44
from featurescript.core import utils
5-
from featurescript.feature_studio import FeatureStudio
5+
from featurescript.feature_studio import LocalFeatureStudio
66
from onshape_api.assertions import assert_instance_type
77
from onshape_api.paths.instance_type import InstanceType
88
from onshape_api.paths.paths import ElementPath
@@ -48,7 +48,7 @@ def build(self, context: ctxt.Context):
4848
).build(context)
4949

5050

51-
def external_import(studio: FeatureStudio, export: bool = False) -> Import:
51+
def external_import(studio: LocalFeatureStudio, export: bool = False) -> Import:
5252
"""An import of a feature studio from an external document."""
5353
path = studio.path
5454
assert_instance_type(path, InstanceType.VERSION)

featurescript/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
STORAGE_FILE: str = "studio_data.pickle"
99

10-
FileData = dict[str, feature_studio.FeatureStudio]
10+
FileData = dict[str, feature_studio.LocalFeatureStudio]
1111

1212

1313
class ConfigData(Protocol):
@@ -30,7 +30,7 @@ def __init__(self) -> None:
3030
if not path.is_file:
3131
raise IOError("Failed to find conf.json in the root directory")
3232
config = json5.load(path.open())
33-
self._parse_config(config) # type: ignore
33+
self._parse_config(config)
3434

3535
def _get_config_key(self, config: dict, key: str) -> Any:
3636
"""Fetches the value of key from config. Throws if key does not exist."""

featurescript/feature_studio.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111

1212

1313
@dataclasses.dataclass
14-
class FeatureStudio:
14+
class LocalFeatureStudio:
1515
"""Represents a FeatureStudio in Onshape.
1616
1717
Attributes:
1818
name: The name of the feature studio.
1919
path: The path to the feature studio.
20-
microversion_id: The most recent microverison id of the feature studio.
20+
microversion_id: The most recent microverison id of the feature studio in the cloud.
2121
Used for external FeatureScript imports.
22-
created: Whether the FeatureStudio is newly created.
22+
created: True if the FeatureStudio actually exists in the cloud.
23+
generated: True if the FeatureStudio is locally generated.
24+
modified: True if the FeatureStudio has been modified since being pulled.
2325
"""
2426

2527
name: str
2628
path: ElementPath
2729
microversion_id: str
2830
created: bool
31+
generated: bool = False
32+
modified: bool = False
2933

3034
def push(self, api: Api, code: str) -> dict:
3135
"""Pushes this Studio to Onshape. The studio is created if it does not already exist."""
@@ -39,14 +43,14 @@ def push(self, api: Api, code: str) -> dict:
3943

4044
def pull_feature_studio(
4145
api: Api, instance_path: InstancePath, studio_name: str
42-
) -> FeatureStudio:
46+
) -> LocalFeatureStudio:
4347
"""Fetches a single feature studio by name, creating it if necessary."""
4448
feature_studio = get_feature_studio(api, instance_path, studio_name)
4549
if feature_studio == None:
4650
response = feature_studios.create_feature_studio(
4751
api, instance_path, studio_name
4852
)
49-
return FeatureStudio(
53+
return LocalFeatureStudio(
5054
studio_name,
5155
ElementPath.from_path(instance_path, response["id"]),
5256
response["microversionId"],
@@ -57,7 +61,7 @@ def pull_feature_studio(
5761

5862
def get_feature_studios(
5963
api: Api, instance_path: InstancePath
60-
) -> dict[str, FeatureStudio]:
64+
) -> dict[str, LocalFeatureStudio]:
6165
"""Returns a dict mapping feature studio names to feature studios."""
6266
elements = api.get(
6367
api_path("documents", instance_path, InstancePath, "elements"),
@@ -68,20 +72,20 @@ def get_feature_studios(
6872

6973
def get_feature_studio(
7074
api: Api, document_path: InstancePath, studio_name: str
71-
) -> FeatureStudio | None:
75+
) -> LocalFeatureStudio | None:
7276
"""Fetches a single feature studio by name, or None if no such studio exists."""
7377
return get_feature_studios(api, document_path).get(studio_name, None)
7478

7579

7680
def _extract_studios(
7781
elements: list[dict],
7882
instance_path: InstancePath,
79-
) -> dict[str, FeatureStudio]:
83+
) -> dict[str, LocalFeatureStudio]:
8084
"""Constructs a list of FeatureStudios from a list of elements returned by a get documents request."""
8185
return dict(
8286
(
8387
element["name"],
84-
FeatureStudio(
88+
LocalFeatureStudio(
8589
element["name"],
8690
ElementPath.from_path(instance_path, element["id"]),
8791
element["microversionId"],

featurescript/onshape.py featurescript/fs_cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Defines a command line parser for the `onshape` command.
1+
"""Defines a command line parser for the `fs` command.
22
"""
3+
34
import argparse
45

56
from onshape_api import key_api

featurescript/manager.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
from featurescript.base import ctxt, studio
1010
from featurescript import conf
11-
from featurescript.feature_studio import FeatureStudio, get_feature_studios
11+
from featurescript.feature_studio import LocalFeatureStudio, get_feature_studios
1212
from onshape_api import api_base
1313
from onshape_api.endpoints import feature_studios
1414
from onshape_api.endpoints.feature_studios import create_feature_studio
1515
from onshape_api.endpoints.std_versions import get_latest_std_version
1616
from onshape_api.paths.paths import ElementPath
17+
from robot_code.documents import BACKEND
1718

1819
OUTDATED_VERSION_MATCH: re.Pattern[str] = re.compile(
1920
r'version : "(\d{2,7})\.0"|FeatureScript (\d{2,7});'
@@ -43,15 +44,15 @@ def _finish(self) -> None:
4344
if self.conflict:
4445
print(CONFLICT_MESSAGE)
4546

46-
def _report_conflict(self, studio: FeatureStudio) -> None:
47+
def _report_conflict(self, studio: LocalFeatureStudio) -> None:
4748
self.conflict = True
4849
print(
4950
"{} has been modified both locally and in Onshape. Skipping.".format(
5051
studio.name
5152
)
5253
)
5354

54-
def _get_studio_map(self) -> dict[str, FeatureStudio]:
55+
def _get_studio_map(self) -> dict[str, LocalFeatureStudio]:
5556
"""Returns a dict mapping Feature Studio names to FeatureStudios."""
5657
with futures.ThreadPoolExecutor() as executor:
5758
threads = [
@@ -97,8 +98,8 @@ def pull(self, force: bool = False) -> None:
9798
self._finish()
9899

99100
def pull_studio(
100-
self, force: bool, studio_to_pull: FeatureStudio
101-
) -> FeatureStudio | None:
101+
self, force: bool, studio_to_pull: LocalFeatureStudio
102+
) -> LocalFeatureStudio | None:
102103
curr_studio = self.curr_data.get(studio_to_pull.path.element_id, None)
103104
if curr_studio is not None:
104105
if (
@@ -163,10 +164,10 @@ def push(self, force: bool = False) -> None:
163164

164165
def push_studio(
165166
self,
166-
onshape_studio_map: dict[str, FeatureStudio],
167+
onshape_studio_map: dict[str, LocalFeatureStudio],
167168
force: bool,
168-
studio_to_push: FeatureStudio,
169-
) -> FeatureStudio | None:
169+
studio_to_push: LocalFeatureStudio,
170+
) -> LocalFeatureStudio | None:
170171
onshape_studio = onshape_studio_map.get(studio_to_push.path.element_id, None)
171172
# next(
172173
# filter(
@@ -264,7 +265,7 @@ def _send_code(self, studio: studio.Studio, std_version: str) -> bool:
264265
return True
265266

266267
print("{}: Successfully built.".format(studio.studio_name))
267-
document = self.config.get_document("backend")
268+
document = BACKEND
268269
if document is None:
269270
print(
270271
"{}: Failed to find document in config.json named {}. Valid names are: {}".format(
@@ -278,10 +279,11 @@ def _send_code(self, studio: studio.Studio, std_version: str) -> bool:
278279

279280
if feature_studio is None:
280281
result = create_feature_studio(self.api, document, studio.studio_name)
281-
feature_studio = FeatureStudio(
282+
feature_studio = LocalFeatureStudio(
282283
result["name"],
283284
ElementPath.from_path(document, result["id"]),
284285
result["microversionId"],
286+
True,
285287
)
286288
feature_studio.generated = True
287289
feature_studio.modified = True

onshape_api/endpoints/std_versions.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from onshape_api.model.constants import STD_PATH
55

66

7-
def get_latest_std_version(api: Api) -> int:
7+
def get_latest_std_version(api: Api) -> str:
88
"""Returns the name of the latest version of the Onshape std."""
99
response = get_latest_version(api, STD_PATH)
1010
version_number = _extract_version_number(response["name"])
@@ -13,7 +13,7 @@ def get_latest_std_version(api: Api) -> int:
1313
return version_number
1414

1515

16-
def get_std_versions(api: Api) -> list[int]:
16+
def get_std_versions(api: Api) -> list[str]:
1717
"""Returns a list of the names of all versions of the Onshape std.
1818
1919
The versions are in reverse chronological order, with the oldest version first.
@@ -26,8 +26,8 @@ def get_std_versions(api: Api) -> list[int]:
2626
return [number for number in version_numbers if number != None]
2727

2828

29-
def _extract_version_number(version_name: str) -> int | None:
29+
def _extract_version_number(version_name: str) -> str | None:
3030
match = re.fullmatch(r"(\d+)\.0", version_name)
3131
if match == None:
3232
return None
33-
return int(match.group(1))
33+
return match.group(1)

robot_code/release.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from featurescript import *
1414
from featurescript.feature_studio import (
15-
FeatureStudio,
15+
LocalFeatureStudio,
1616
get_feature_studio,
1717
pull_feature_studio,
1818
)
@@ -228,8 +228,8 @@ def release_preamble(version_name: str, studio_path: ElementPath) -> str:
228228

229229
def get_release_studio_code(
230230
version_name: str,
231-
std_version: int,
232-
backend_studio: FeatureStudio,
231+
std_version: str,
232+
backend_studio: LocalFeatureStudio,
233233
) -> str:
234234
"""Generates a studio which exposes a backend Feature Script.
235235

robot_code/robot_cli.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,31 @@ def parse_args() -> argparse.Namespace:
8686
"-l", "--log", help="whether to run with logging enabled", action="store_true"
8787
)
8888

89-
subparsers = parser.add_subparsers(required=True, dest="action")
89+
action_parsers = parser.add_subparsers(required=True, dest="action")
9090

9191
release_parser = get_release_parser()
92-
subparsers.add_parser(
92+
action_parsers.add_parser(
9393
"release",
9494
help="release a new version of a FeatureScript",
9595
parents=[release_parser],
9696
description="Release a FeatureScript to the frontend document.",
9797
)
98-
subparsers.add_parser(
98+
action_parsers.add_parser(
9999
"test-release",
100100
parents=[release_parser],
101101
help="release a test FeatureScript",
102102
description="Release a test FeatureScript in the test-frontend document.",
103103
)
104-
subparsers.add_parser(
104+
action_parsers.add_parser(
105105
"sync-versions",
106106
help="sync versions to the frontend document",
107107
description="Create all missing versions in the frontend document.",
108108
)
109+
action_parsers.add_parser(
110+
"update-fs-versions",
111+
help="Update all Feature Studios to the latest FeatureScript version",
112+
description="Update all Feature Studios to the latest FeatureScript version.",
113+
)
109114

110115
return parser.parse_args()
111116

scripts/fs.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /bin/sh
2+
# A simple wrapper for fs_cli.py
3+
python featurescript/fs_cli.py "$@"

scripts/onshape.sh

-3
This file was deleted.

0 commit comments

Comments
 (0)