slaclab / pydm

Python Display Manager
http://slaclab.github.io/pydm/
Other
111 stars 76 forks source link

can't reuse calc plugin after it's defined #1050

Open shilorigins opened 7 months ago

shilorigins commented 7 months ago

Describe the bug

If I define a calc using calc://shutters_vis_2?var1=... and then try to use "calc://shutters_vis_2" in a different widget, I get an error:

[2023-11-20 11:06:39,666] [ERROR   ] - Unable to make proper connection for <PyDMChannel (calc://my_calc)>
Traceback (most recent call last):
  File "/afs/slac/g/lcls/package/anaconda/envs/python3_env/lib/python3.8/site-packages/pydm/widgets/channel.py", line 152, in connect
    pydm.data_plugins.establish_connection(self)
  File "/afs/slac/g/lcls/package/anaconda/envs/python3_env/lib/python3.8/site-packages/pydm/data_plugins/__init__.py", line 65, in establish_connection
    establish_connection_immediately(channel)
  File "/afs/slac/g/lcls/package/anaconda/envs/python3_env/lib/python3.8/site-packages/pydm/data_plugins/__init__.py", line 70, in establish_connection_immediately
    plugin.add_connection(channel)
  File "/afs/slac/g/lcls/package/anaconda/envs/python3_env/lib/python3.8/site-packages/pydm/data_plugins/plugin.py", line 311, in add_connection
    self.connections[connection_id] = self.connection_class(channel, address, self.protocol)
  File "/afs/slac.stanford.edu/g/lcls/vol9/package/anaconda/envs/python3.8envs/v2.5/lib/python3.8/site-packages/pydm/data_plugins/calc_plugin.py", line 185, in __init__
    self.add_listener(channel)
  File "/afs/slac.stanford.edu/g/lcls/vol9/package/anaconda/envs/python3.8envs/v2.5/lib/python3.8/site-packages/pydm/data_plugins/calc_plugin.py", line 192, in add_listener
    self._setup_calc(channel)
  File "/afs/slac.stanford.edu/g/lcls/vol9/package/anaconda/envs/python3.8envs/v2.5/lib/python3.8/site-packages/pydm/data_plugins/calc_plugin.py", line 216, in _setup_calc
    self._calc_thread = CalcThread(self._configuration)
  File "/afs/slac.stanford.edu/g/lcls/vol9/package/anaconda/envs/python3.8envs/v2.5/lib/python3.8/site-packages/pydm/data_plugins/calc_plugin.py", line 71, in __init__
    self._expression = self.config.get("expr", "")[0]
IndexError: string index out of range

Expected behavior

I should be able to reuse an already defined calc plugin by setting a widget's channel as calc://{calc_name}

Steps to Reproduce

In designer

  1. make a new "form"
  2. add two new PyDMLabels
  3. set one label's channel to calc://my_calc?var1=1&var2=2&expr=var1 + var2
  4. set the other label's channel to calc://my_calc
  5. set the form's layout
  6. run the form using pydm
  7. observe the above error on the terminal

Possible Solution

The issue seems to be that in `self._expression = self.config.get("expr", "")[0]`, `get`s default arg `""` doesn't have a first element, so if the calc doesn't define `expr` then `""[0]` throws an index error. Maybe the default arg should be `[""]`, or some of the logic should be wrapped in a `try` block. **My Platform**

mcclogin python: 3.8.13 pydm: 1.21.0

Additional context