Skip to content

Commit 433e938

Browse files
committed
removed prints, fixed python publish parameter, fixed return variable generation in implementation functions, added exception on configloader import with suggestion.
1 parent a780d4f commit 433e938

4 files changed

Lines changed: 44 additions & 22 deletions

File tree

cli/robocompdsl/robocompdsl/templates/templateICE/plugins/base/functions/TEMPLATE_ICE.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def __init__(self, module):
5959
def ice_imports(self):
6060
result = ""
6161
if 'imports' in self.module and self.module["imports"]:
62-
print("\n\n", self.module['interfaces'], "\n\n")
6362
for imp in self.module['imports']:
6463
if imp != '':
6564
result += "#include <" + os.path.basename(imp).split('.')[0] + ".ice>\n"

cli/robocompdsl/robocompdsl/templates/templatePython/files/generated/main.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,31 @@
6161
import os
6262
from pathlib import Path
6363

64+
from rich.console import Console
65+
from rich.text import Text
66+
console = Console()
67+
6468
try:
6569
ROBOCOMP = os.environ['ROBOCOMP']
6670
except KeyError:
67-
print('ROBOCOMP environment variable not set, using the default value /home/robocomp/robocomp')
71+
console.print(Text('ROBOCOMP environment variable not set, using the default value /home/robocomp/robocomp', "yellow"))
6872
ROBOCOMP = '/home/robocomp/robocomp'
6973

70-
sys.path.append(str(os.path.join(ROBOCOMP, "classes/ConfigLoader")))
71-
from ConfigLoader import ConfigLoader
74+
configloader_path = os.path.join(ROBOCOMP, "classes", "ConfigLoader")
75+
sys.path.append(str(configloader_path))
76+
try:
77+
from ConfigLoader import ConfigLoader
78+
except ModuleNotFoundError:
79+
console.print(Text(f"ERROR: Could not find ConfigLoader.py. Expected path: {configloader_path}/ConfigLoader.py", "red"))
80+
console.print(Text("Please update RoboComp classes or check the path.", "green"))
81+
exit(-1)
7282

7383
sys.path.append(str(Path(__file__).parent.parent))
7484
from src.specificworker import *
7585
import interfaces
7686

7787
${import_qtwidgets}
7888

79-
from rich.console import Console
80-
console = Console()
81-
8289
#SIGNALS handler
8390
def sigint_handler(*args):
8491
QtCore.QCoreApplication.quit()

cli/robocompdsl/robocompdsl/templates/templatePython/plugins/base/functions/SERVANT_PY.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
INTERFACE_METHOD_STR = """
10-
def ${method_name}(self, ${params_str_a}):
10+
def ${method_name}(self, ${params_str_a}ice):
1111
return getattr(self.worker, f"${interface_name}{self.id}_${method_name}")(${params_str_b})
1212
"""
1313

cli/robocompdsl/robocompdsl/templates/templatePython/plugins/base/functions/src/specificworker_py.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from string import Template
3+
import re
34

45
from robocompdsl.dsl_parsers.parsing_utils import communication_is_ice, get_name_number
56
from robocompdsl.templates.templatePython.plugins.base.functions import function_utils as utils
@@ -78,11 +79,30 @@ def replace_type_cpp_to_python(t):
7879
def compute_creation(self):
7980
result = COMPUTE_METHOD_STR
8081
return result
82+
83+
def is_ifaces(self, type_data, module, import_modules):
84+
if type_data in [struct['name'].split('/')[1] for struct in module['structs']+module['sequences']] or \
85+
type_data in [struct['strName'] for struct in module['simpleSequences']]:
86+
return True
87+
88+
for mod in import_modules:
89+
patron = fr'RoboComp{mod}::(\w+)'
90+
match = re.fullmatch(patron, type_data)
91+
if match:
92+
return True
93+
return False
94+
8195

8296
def methods(self, interfaces, subscribe=False):
8397
result = ""
8498
for interface, num in get_name_number(interfaces):
8599
module = self.component.idsl_pool.module_providing_interface(interface.name)
100+
if module.get("imports") is not None:
101+
import_modules = [mod.split('.')[0] for mod in module['imports']]
102+
else:
103+
import_modules = []
104+
105+
86106
for module_interface in module['interfaces']:
87107
if module_interface['name'] == interface.name:
88108
for mname in module_interface['methods']:
@@ -100,27 +120,23 @@ def methods(self, interfaces, subscribe=False):
100120
param_str_a += ', ' + p['name']
101121

102122
return_creation = ''
103-
for out in out_values:
104-
returned_type = ""
105-
simple_type = out[0]
106-
if simple_type in [struct['name'].split('/')[1] for struct in module['structs']+module['sequences']] or \
107-
simple_type in [struct['strName'] for struct in module['simpleSequences']]:
108-
returned_type= "ifaces."
109-
returned_type += utils.get_type_string(simple_type, module['name'])
110-
return_creation += f' {out[1]} = {returned_type}()\n'
111-
112-
113123
return_str = "pass\n\n"
114124
if len(out_values) == 1:
115125
if method['return'] != 'void':
116126
return_str = "return ret"
117127
else:
118-
return_str = out_values[0][1] + " = " + self.replace_type_cpp_to_python(out_values[0][0]) + "()\n"
119-
return_str += " return " + out_values[0][1]
128+
ifaces = ""
129+
if self.is_ifaces(type_data=out_values[0][0], module=module, import_modules=import_modules):
130+
ifaces = "ifaces."
131+
return_creation = " " + out_values[0][1] + " = " + ifaces + self.replace_type_cpp_to_python(out_values[0][0]) + "()\n"
132+
return_str = "return " + out_values[0][1]
120133
elif len(out_values) > 1:
121134
for v in out_values:
135+
ifaces = ""
136+
if self.is_ifaces(type_data=v[0], module=module, import_modules=import_modules):
137+
ifaces = "ifaces."
122138
if v[1] != 'ret':
123-
return_str += " " + v[1] + " = " + self.replace_type_cpp_to_python(v[0]) + "()\n"
139+
return_creation += " " + v[1] + " = " + ifaces + self.replace_type_cpp_to_python(v[0]) + "()\n"
124140
vector_str = ", ".join([v[1] for v in out_values])
125141
return_str = f"return [{vector_str}]"
126142
if subscribe:
@@ -198,7 +214,7 @@ def interface_specific_comment(self):
198214
action=action)
199215
structs_str = ""
200216
for struct in module['structs']:
201-
structs_str += f"# {struct['name'].replace('/', '.')}\n"
217+
structs_str += f"# ifaces.{struct['name'].replace('/', '.')}\n"
202218
if structs_str:
203219
result += Template(INTERFACE_TYPES_COMMENT_STR).substitute(module_name=module['name'],
204220
types=structs_str)

0 commit comments

Comments
 (0)