Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a message for codegen if generated files aren't available. #423

Merged
merged 6 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ env:
# You should go up in number, if you go down (or repeat a previous value)
# you might end up reusing a previous cache if it haven't been deleted already.
# It applies 7 days retention policy by default.
RESET_EXAMPLES_CACHE: 1
RESET_EXAMPLES_CACHE: 2

jobs:
stylecheck:
Expand Down
8 changes: 4 additions & 4 deletions codegen/tuigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class _TUIMenu:

def __init__(self, path: str):
self.path = path
self.tui_name = path[-1][0] if path else ""
self.tui_name = path[-1] if path else ""
self.name = convert_tui_menu_to_func_name(self.tui_name)
tui_path = convert_path_to_grpc_path(path)
self.doc = _XML_HELPSTRINGS.get(tui_path, None)
Expand All @@ -103,7 +103,7 @@ def __init__(self, path: str):
self.is_command = False

def get_command_path(self, command: str) -> str:
return convert_path_to_grpc_path(self.path + [(command, None)])
return convert_path_to_grpc_path(self.path + [command])


class TUIGenerator:
Expand Down Expand Up @@ -131,7 +131,7 @@ def _populate_menu(self, menu: _TUIMenu):
if child_names:
for child_name in child_names:
if child_name:
child_menu = _TUIMenu(menu.path + [(child_name, None)])
child_menu = _TUIMenu(menu.path + [child_name])
menu.children[child_menu.name] = child_menu
self._populate_menu(child_menu)
else:
Expand All @@ -157,7 +157,7 @@ def _write_menu_to_tui_file(self, menu: _TUIMenu, indent: int = 0):
if not v.is_command:
self._write_code_to_tui_file(
f"self.{k} = self.__class__.{k}"
f'(path + [("{v.tui_name}", None)], service)\n',
f'(path + ["{v.tui_name}"], service)\n',
indent,
)
self._write_code_to_tui_file("super().__init__(path, service)\n", indent)
Expand Down
87 changes: 76 additions & 11 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Wrappers over StateEngine based datamodel grpc service of Fluent."""

from enum import Enum
import itertools
from typing import Any, Dict, Iterator, List, Tuple

import grpc
Expand Down Expand Up @@ -363,7 +364,7 @@ def __init__(self, service: DatamodelService, rules: str, path: Path = None):
else:
self.path = path

def __get_child_object_names(self):
def _get_child_object_names(self):
request = DataModelProtoModule.GetSpecsRequest()
request.rules = self.rules
parent_path = self.path[0:-1]
Expand All @@ -379,9 +380,9 @@ def __get_child_object_names(self):
child_object_names.append(member[len(child_type_suffix) :])
return child_object_names

def __get_child_object_display_names(self):
def _get_child_object_display_names(self):
child_object_display_names = []
for name in self.__get_child_object_names():
for name in self._get_child_object_names():
name_path = self.path[0:-1]
name_path.append((self.path[-1][0], name))
name_path.append(("_name_", ""))
Expand All @@ -398,7 +399,7 @@ def __len__(self) -> int:
int
count
"""
return len(self.__get_child_object_display_names())
return len(self._get_child_object_display_names())

def __iter__(self) -> Iterator[PyMenu]:
"""Returns the next child object.
Expand All @@ -408,15 +409,15 @@ def __iter__(self) -> Iterator[PyMenu]:
Iterator[PyMenu]
iterator of child objects
"""
for name in self.__get_child_object_display_names():
for name in self._get_child_object_display_names():
child_path = self.path[:-1]
child_path.append((self.path[-1][0], name))
yield getattr(self.__class__, f"_{self.__class__.__name__}")(
self.service, self.rules, child_path
)

def __get_item(self, key: str):
if key in self.__get_child_object_display_names():
def _get_item(self, key: str):
if key in self._get_child_object_display_names():
child_path = self.path[:-1]
child_path.append((self.path[-1][0], key))
return getattr(self.__class__, f"_{self.__class__.__name__}")(
Expand All @@ -427,8 +428,8 @@ def __get_item(self, key: str):
f"{key} is not found at path " f"{_convert_path_to_se_path(self.path)}"
)

def __del_item(self, key: str):
if key in self.__get_child_object_display_names():
def _del_item(self, key: str):
if key in self._get_child_object_display_names():
child_path = self.path[:-1]
child_path.append((self.path[-1][0], key))
request = DataModelProtoModule.DeleteObjectRequest()
Expand All @@ -453,7 +454,7 @@ def __getitem__(self, key: str) -> PyMenu:
PyMenu
child object
"""
return self.__get_item(key)
return self._get_item(key)

def __setitem__(self, key: str, value: Any):
"""Set state of the child object by name.
Expand All @@ -478,7 +479,7 @@ def __delitem__(self, key: str):
key : str
child name
"""
self.__del_item(key)
self._del_item(key)


class PyCommand:
Expand Down Expand Up @@ -537,3 +538,67 @@ def help(self) -> None:
response.member, response.member.WhichOneof("as")
).common.helpstring
print(help_string)


class PyMenuGeneric(PyMenu):
attrs = ("service", "rules", "path")

def _get_child_names(self):
request = DataModelProtoModule.GetSpecsRequest()
request.rules = self.rules
request.path = _convert_path_to_se_path(self.path)
response = self.service.get_specs(request)
singleton_names = []
creatable_type_names = []
command_names = []
for struct_type in ("singleton", "namedobject"):
if response.member.HasField(struct_type):
struct_field = getattr(response.member, struct_type)
for member in struct_field.members:
if ":" not in member:
singleton_names.append(member)
creatable_type_names = struct_field.creatabletypes
command_names = [x.name for x in struct_field.commands]
return singleton_names, creatable_type_names, command_names

def _get_child(self, name: str):
singletons, creatable_types, commands = self._get_child_names()
if name in singletons:
child_path = self.path + [(name, "")]
return PyMenuGeneric(self.service, self.rules, child_path)
elif name in creatable_types:
child_path = self.path + [(name, "")]
return PyNamedObjectContainerGeneric(self.service, self.rules, child_path)
elif name in commands:
return PyCommand(self.service, self.rules, name, self.path)
else:
raise LookupError(
f"{name} is not found at path " f"{_convert_path_to_se_path(self.path)}"
)

def __dir__(self):
return list(itertools.chain(*self._get_child_names()))

def __getattr__(self, name: str):
if name in PyMenuGeneric.attrs:
return super().__getattr__(name)
else:
return self._get_child(name)


class PyNamedObjectContainerGeneric(PyNamedObjectContainer):
def __iter__(self):
for name in self._get_child_object_display_names():
child_path = self.path[:-1]
child_path.append((self.path[-1][0], name))
yield PyMenuGeneric(self.service, self.rules, child_path)

def _get_item(self, key: str):
if key in self._get_child_object_display_names():
child_path = self.path[:-1]
child_path.append((self.path[-1][0], key))
return PyMenuGeneric(self.service, self.rules, child_path)
else:
raise LookupError(
f"{key} is not found at path " f"{_convert_path_to_se_path(self.path)}"
)
37 changes: 29 additions & 8 deletions src/ansys/fluent/core/services/datamodel_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ansys.fluent.core.services.error_handler import catch_grpc_error
from ansys.fluent.core.services.interceptors import TracingInterceptor

Path = List[Tuple[str, str]]
Path = List[str]


class DatamodelService:
Expand Down Expand Up @@ -205,7 +205,33 @@ def __init__(self, path, service):
self.service = service

def __dir__(self) -> Iterable[str]:
return PyMenu(self.service, self.path).get_child_names()
return [
convert_tui_menu_to_func_name(x)
for x in PyMenu(self.service, self.path).get_child_names()
]


class TUIMenuGeneric(TUIMenu):
"""Generic menu class for when the explicit menu classes aren't
available."""

def __getattribute__(self, name):
if name in ["path", "service"]:
return super().__getattribute__(name)
name = convert_func_name_to_tui_menu(name)
path = self.path + [name]
if PyMenu(self.service, path).get_child_names():
return TUIMenuGeneric(path, self.service)
else:
return TUICommandGeneric(path, self.service)


class TUICommandGeneric(TUIMenu):
"""Generic command class for when the explicit menu classes aren't
available."""

def __call__(self, *args, **kwargs):
return PyMenu(self.service, self.path).execute(*args, **kwargs)


def convert_func_name_to_tui_menu(func_name: str) -> str:
Expand Down Expand Up @@ -257,9 +283,4 @@ def convert_path_to_grpc_path(path: Path) -> str:
str
grpc path
"""
grpc_path = ""
for comp in path:
grpc_path += "/" + convert_func_name_to_tui_menu(comp[0])
if comp[1]:
grpc_path += ":" + comp[1]
return grpc_path
return "/" + "/".join(convert_func_name_to_tui_menu(x) for x in path)
Loading