Skip to content

Commit f20cdcd

Browse files
authored
Merge pull request #49 from Caltech-IPAC/cleanup-plot.py
Cleanup Plot.py
2 parents 551c924 + 0ef5320 commit f20cdcd

File tree

5 files changed

+81
-115
lines changed

5 files changed

+81
-115
lines changed

firefly_client/fc_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,35 @@ def warn(s): print('WARNING: %s' % s)
1515
def dict_to_str(in_dict): return json.dumps(in_dict, indent=2, default=str)
1616

1717

18+
# id for table, region layer, extension
19+
_item_id = {'Table': 0, 'RegionLayer': 0, 'Extension': 0, 'MaskLayer': 0, 'XYPlot': 0,
20+
'Cell': 0, 'Histogram': 0, 'Plotly': 0, 'Image': 0, 'FootprintLayer': 0}
21+
1822
ALL = 'ALL_EVENTS_ENABLED'
1923

2024

25+
def gen_item_id(item):
26+
"""
27+
Generate an ID for table, region layer, or extension entity.
28+
29+
Parameters
30+
----------
31+
item : {'Table', 'RegionLayer', 'Extension', 'XYPlot', 'Cell', 'FootprintLayer'}
32+
Entity type.
33+
34+
Returns
35+
-------
36+
out : `str`
37+
ID string.
38+
"""
39+
40+
if item in _item_id:
41+
_item_id[item] += 1
42+
return item + '-' + str(_item_id[item])
43+
else:
44+
return None
45+
46+
2147
def create_image_url(image_source):
2248
def is_url(url): return urllib.parse.urlparse(url).scheme != ''
2349

firefly_client/firefly_client.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import weakref
1515
from copy import copy
1616

17+
1718
try:
1819
from .ffws import FFWs
1920
except ImportError:
@@ -27,10 +28,10 @@
2728
except ImportError:
2829
from range_values import RangeValues
2930
try:
30-
from .fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, \
31+
from .fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, gen_item_id,\
3132
DebugMarker, ALL, ACTION_DICT, LO_VIEW_DICT
3233
except ImportError:
33-
from fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, \
34+
from fc_utils import debug, warn, dict_to_str, create_image_url, ensure3, gen_item_id,\
3435
DebugMarker, ALL, ACTION_DICT, LO_VIEW_DICT
3536

3637
__docformat__ = 'restructuredtext'
@@ -579,7 +580,7 @@ def add_cell(self, row, col, width, height, element_type, cell_id=None):
579580
cell_id = 'main'
580581
else:
581582
if not cell_id:
582-
cell_id = FireflyClient._gen_item_id('Cell')
583+
cell_id = gen_item_id('Cell')
583584

584585
payload = {'row': row,
585586
'col': col,
@@ -778,7 +779,7 @@ def show_table(self, file_on_server=None, tbl_id=None, title=None, page_size=100
778779
"""
779780

780781
if not tbl_id:
781-
tbl_id = FireflyClient._gen_item_id('Table')
782+
tbl_id = gen_item_id('Table')
782783
if not title:
783784
title = tbl_id if file_on_server else target_search_info.get('catalog', tbl_id)
784785

@@ -838,7 +839,7 @@ def fetch_table(self, file_on_server, tbl_id=None, title=None, page_size=1, tabl
838839
"""
839840

840841
if not tbl_id:
841-
tbl_id = FireflyClient._gen_item_id('Table')
842+
tbl_id = gen_item_id('Table')
842843
if not title:
843844
title = tbl_id
844845
tbl_req = {'startIdx': 0, 'pageSize': page_size, 'source': file_on_server,
@@ -922,7 +923,7 @@ def show_xyplot(self, tbl_id, standalone=False, group_id=None, **chart_params):
922923

923924
chart_data_elements = [{'type': 'xycols', 'options': options, 'tblId': tbl_id}]
924925

925-
cid = FireflyClient._gen_item_id('XYPlot')
926+
cid = gen_item_id('XYPlot')
926927

927928
if not group_id:
928929
group_id = 'default' if standalone else tbl_id
@@ -991,7 +992,7 @@ def show_histogram(self, tbl_id, group_id='default', **histogram_params):
991992
'binWidth': histogram_params.get('binWidth')})
992993
chart_data_elements.update({'options': options})
993994

994-
cid = FireflyClient._gen_item_id('Histogram')
995+
cid = gen_item_id('Histogram')
995996
payload = {'chartId': cid, 'chartType': 'histogram',
996997
'groupId': group_id,
997998
'viewerId': group_id,
@@ -1034,7 +1035,7 @@ def show_chart(self, group_id='default', **chart_params):
10341035
Status of the request, like {'success': True}.
10351036
10361037
"""
1037-
chart_id = chart_params.get('chartId') if 'chartId' in chart_params else FireflyClient._gen_item_id('Plotly')
1038+
chart_id = chart_params.get('chartId') if 'chartId' in chart_params else gen_item_id('Plotly')
10381039
payload = {'chartId': chart_id,
10391040
'groupId': group_id,
10401041
'viewerId': group_id,
@@ -1125,7 +1126,7 @@ def add_extension(self, ext_type, plot_id=None, title='', tool_tip='',
11251126
"""
11261127

11271128
if not extension_id:
1128-
extension_id = FireflyClient._gen_item_id('Extension')
1129+
extension_id = gen_item_id('Extension')
11291130
payload = {'extension': {
11301131
'id': extension_id, 'plotId': plot_id,
11311132
'imageUrl': create_image_url(image_src) if image_src else None,
@@ -1219,7 +1220,7 @@ def show_hips(self, plot_id=None, viewer_id=None, hips_root_url=None, hips_image
12191220
payload = {'wpRequest': wp_request}
12201221

12211222
if not plot_id:
1222-
plot_id = FireflyClient._gen_item_id('Image')
1223+
plot_id = gen_item_id('Image')
12231224

12241225
payload.update({'plotId': plot_id})
12251226
wp_request.update({'plotId': plot_id})
@@ -1270,7 +1271,7 @@ def show_image_or_hips(self, plot_id=None, viewer_id=None, image_request=None, h
12701271
return
12711272

12721273
if not plot_id:
1273-
plot_id = FireflyClient._gen_item_id('Image')
1274+
plot_id = gen_item_id('Image')
12741275
if not viewer_id:
12751276
viewer_id = 'DEFAULT_FITS_VIEWER_ID'
12761277
if self.render_tree_id:
@@ -1550,7 +1551,7 @@ def overlay_footprints(self, footprint_file, footprint_image=None, title=None,
15501551
"""
15511552

15521553
if not footprint_layer_id:
1553-
footprint_layer_id = FireflyClient._gen_item_id('FootprintLayer')
1554+
footprint_layer_id = gen_item_id('FootprintLayer')
15541555
payload = {'drawLayerId': footprint_layer_id}
15551556

15561557
title and payload.update({'title': title})
@@ -1598,7 +1599,7 @@ def overlay_region_layer(self, file_on_server=None, region_data=None, title=None
15981599
"""
15991600

16001601
if not region_layer_id:
1601-
region_layer_id = FireflyClient._gen_item_id('RegionLayer')
1602+
region_layer_id = gen_item_id('RegionLayer')
16021603
payload = {'drawLayerId': region_layer_id}
16031604

16041605
title and payload.update({'layerTitle': title})
@@ -1714,7 +1715,7 @@ def add_mask(self, bit_number, image_number, plot_id, mask_id=None, color=None,
17141715
"""
17151716

17161717
if not mask_id:
1717-
mask_id = FireflyClient._gen_item_id('MaskLayer')
1718+
mask_id = gen_item_id('MaskLayer')
17181719
if not title:
17191720
title = 'bit %23 ' + str(bit_number)
17201721

@@ -1743,29 +1744,3 @@ def remove_mask(self, plot_id, mask_id):
17431744

17441745
payload = {'plotId': plot_id, 'imageOverlayId': mask_id}
17451746
return self.dispatch(ACTION_DICT['DeleteOverlayMask'], payload)
1746-
1747-
# -----------------------------------------------------------------
1748-
# Range Values
1749-
# -----------------------------------------------------------------
1750-
1751-
@classmethod
1752-
def _gen_item_id(cls, item):
1753-
"""
1754-
Generate an ID for table, region layer, or extension entity.
1755-
1756-
Parameters
1757-
----------
1758-
item : {'Table', 'RegionLayer', 'Extension', 'XYPlot', 'Cell', 'FootprintLayer'}
1759-
Entity type.
1760-
1761-
Returns
1762-
-------
1763-
out : `str`
1764-
ID string.
1765-
"""
1766-
1767-
if item in cls._item_id:
1768-
cls._item_id[item] += 1
1769-
return item + '-' + str(cls._item_id[item])
1770-
else:
1771-
return None

0 commit comments

Comments
 (0)