Skip to content

Commit 69e63a9

Browse files
authored
Base path mixin and other shared functionality (#138)
* Move version to shared file that docs and setup read from * Add missing return_path option to get_value * Move image's call_action, get_value and macro to mixin class which can be shared by all child objects with their own base paths in the frontend store tree
1 parent c527570 commit 69e63a9

6 files changed

Lines changed: 92 additions & 66 deletions

File tree

VERSION.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.10

carta/image.py

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Image objects should not be instantiated directly, and should only be created through methods on the :obj:`carta.session.Session` object.
44
"""
55
from .constants import Colormap, Scaling, SmoothingMode, ContourDashMode, Polarization, CoordinateSystem, SpatialAxis
6-
from .util import Macro, cached
6+
from .util import Macro, cached, BasePathMixin
77
from .units import PixelValue, AngularSize, WorldCoordinate
88
from .validation import validate, Number, Color, Constant, Boolean, NoneOr, IterableOf, Evaluate, Attr, Attrs, OneOf, Size, Coordinate, all_optional
99
from .metadata import parse_header
1010

1111

12-
class Image:
12+
class Image(BasePathMixin):
1313
"""This object corresponds to an image open in a CARTA frontend session.
1414
1515
This class should not be instantiated directly. Instead, use the session object's methods for opening new images or retrieving existing images.
@@ -100,64 +100,6 @@ def from_list(cls, session, image_list):
100100
def __repr__(self):
101101
return f"{self.session.session_id}:{self.image_id}:{self.file_name}"
102102

103-
def call_action(self, path, *args, **kwargs):
104-
"""Convenience wrapper for the session object's generic action method.
105-
106-
This method calls :obj:`carta.session.Session.call_action` after prepending this image's base path to the path parameter.
107-
108-
Parameters
109-
----------
110-
path : string
111-
The path to an action relative to this image's frame store.
112-
*args
113-
A variable-length list of parameters. These are passed unmodified to the session method.
114-
**kwargs
115-
Arbitrary keyword parameters. These are passed unmodified to the session method.
116-
117-
Returns
118-
-------
119-
object or None
120-
The unmodified return value of the session method.
121-
"""
122-
return self.session.call_action(f"{self._base_path}.{path}", *args, **kwargs)
123-
124-
def get_value(self, path):
125-
"""Convenience wrapper for the session object's generic method for retrieving attribute values.
126-
127-
This method calls :obj:`carta.session.Session.get_value` after prepending this image's base path to the *path* parameter.
128-
129-
Parameters
130-
----------
131-
path : string
132-
The path to an attribute relative to this image's frame store.
133-
134-
Returns
135-
-------
136-
object
137-
The unmodified return value of the session method.
138-
"""
139-
return self.session.get_value(f"{self._base_path}.{path}")
140-
141-
def macro(self, target, variable):
142-
"""Convenience wrapper for creating a :obj:`carta.util.Macro` for an image property.
143-
144-
This method prepends this image's base path to the *target* parameter. If *target* is the empty string, the base path will be substituted.
145-
146-
Parameters
147-
----------
148-
target : str
149-
The target frontend object.
150-
variable : str
151-
The variable on the target object.
152-
153-
Returns
154-
-------
155-
:obj:carta.util.Macro
156-
A placeholder for a variable which will be evaluated dynamically by the frontend.
157-
"""
158-
target = f"{self._base_path}.{target}" if target else self._base_path
159-
return Macro(target, variable)
160-
161103
# METADATA
162104

163105
@property

carta/session.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def call_action(self, path, *args, **kwargs):
254254
"""
255255
return self._protocol.request_scripting_action(self.session_id, path, *args, **kwargs)
256256

257-
def get_value(self, path):
257+
def get_value(self, path, return_path=None):
258258
"""Get the value of an attribute from a frontend store.
259259
260260
Like the :obj:`carta.session.Session.call_action` method, this is exposed in the public API but is not intended to be used directly under normal circumstances.
@@ -263,6 +263,8 @@ def get_value(self, path):
263263
----------
264264
path : string
265265
The full path to the attribute.
266+
return_path : string, optional
267+
Specifies a subobject of the attribute value which should be returned instead of the whole object.
266268
267269
Returns
268270
-------
@@ -271,7 +273,12 @@ def get_value(self, path):
271273
"""
272274
path, parameter = split_action_path(path)
273275
macro = Macro(path, parameter)
274-
return self.call_action("fetchParameter", macro, response_expected=True)
276+
277+
kwargs = {"response_expected": True}
278+
if return_path is not None:
279+
kwargs["return_path"] = return_path
280+
281+
return self.call_action("fetchParameter", macro, **kwargs)
275282

276283
# FILE BROWSING
277284

carta/util.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,72 @@ def split_action_path(path):
136136
"""
137137
parts = path.split('.')
138138
return '.'.join(parts[:-1]), parts[-1]
139+
140+
141+
class BasePathMixin:
142+
"""A mixin which provides ``call_action`` and ``get_value`` methods which prepend the object's base path to the path before calling the corresponding :obj:`carta.session.Session` methods.
143+
144+
It also provides a ``macro`` method which prepends the path when creating a :obj:`carta.util.Macro`.
145+
146+
A class inheriting from this mixin must define a `_base_path` attribute (the string prefix) and a `session` attribute (a :obj:`carta.session.Session` object).
147+
"""
148+
149+
def call_action(self, path, *args, **kwargs):
150+
"""Convenience wrapper for the session object's generic action method.
151+
152+
This method calls :obj:`carta.session.Session.call_action` after prepending this object's base path to the path parameter.
153+
154+
Parameters
155+
----------
156+
path : string
157+
The path to an action relative to this object's store.
158+
*args
159+
A variable-length list of parameters. These are passed unmodified to the session method.
160+
**kwargs
161+
Arbitrary keyword parameters. These are passed unmodified to the session method.
162+
163+
Returns
164+
-------
165+
object or None
166+
The unmodified return value of the session method.
167+
"""
168+
return self.session.call_action(f"{self._base_path}.{path}", *args, **kwargs)
169+
170+
def get_value(self, path, return_path=None):
171+
"""Convenience wrapper for the session object's generic method for retrieving attribute values.
172+
173+
This method calls :obj:`carta.session.Session.get_value` after prepending this object's base path to the *path* parameter.
174+
175+
Parameters
176+
----------
177+
path : string
178+
The path to an attribute relative to this object's store.
179+
return_path : string, optional
180+
Specifies a subobject of the attribute value which should be returned instead of the whole object.
181+
182+
Returns
183+
-------
184+
object
185+
The unmodified return value of the session method.
186+
"""
187+
return self.session.get_value(f"{self._base_path}.{path}", return_path=return_path)
188+
189+
def macro(self, target, variable):
190+
"""Convenience wrapper for creating a :obj:`carta.util.Macro` for an object property.
191+
192+
This method prepends this object's base path to the *target* parameter. If *target* is the empty string, the base path will be substituted.
193+
194+
Parameters
195+
----------
196+
target : str
197+
The target frontend object.
198+
variable : str
199+
The variable on the target object.
200+
201+
Returns
202+
-------
203+
:obj:carta.util.Macro
204+
A placeholder for a variable which will be evaluated dynamically by the frontend.
205+
"""
206+
target = f"{self._base_path}.{target}" if target else self._base_path
207+
return Macro(target, variable)

docs/source/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
author = 'Adrianna Pińska'
2424

2525
# The full version, including alpha/beta/rc tags
26-
release = '1.0.0-beta'
26+
27+
with open("../../VERSION.txt") as f:
28+
version = f.read()
29+
30+
release = version
2731

2832

2933
# -- General configuration ---------------------------------------------------

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import setuptools
22

3-
with open("README.md", "r") as fh:
4-
long_description = fh.read()
3+
with open("README.md") as f:
4+
long_description = f.read()
5+
6+
with open("VERSION.txt") as f:
7+
version = f.read()
58

69
setuptools.setup(
710
name="carta",
8-
version="1.1.10",
11+
version=version,
912
author="Adrianna Pińska",
1013
author_email="adrianna.pinska@gmail.com",
1114
description="CARTA scripting wrapper written in Python",

0 commit comments

Comments
 (0)