|
4 | 4 | This module defines class 'FireflyClient' and methods to remotely communicate to Firefly viewer |
5 | 5 | by dispatching remote actions. |
6 | 6 | """ |
| 7 | +import io |
| 8 | +import re |
7 | 9 | import requests |
8 | 10 | import webbrowser |
9 | 11 | import json |
|
12 | 14 | from urllib.parse import urljoin |
13 | 15 | import math |
14 | 16 | import weakref |
| 17 | +import os |
15 | 18 | from copy import copy |
16 | 19 |
|
17 | 20 |
|
@@ -729,6 +732,55 @@ def reinit_viewer(self): |
729 | 732 | Status of the request, like {'success': True}. |
730 | 733 | """ |
731 | 734 | return self.dispatch(ACTION_DICT['ReinitViewer'], {}) |
| 735 | + |
| 736 | + def get_file_payload(self, file_input): |
| 737 | + # TODO: debug displayName not appearing in some cases |
| 738 | + if isinstance(file_input, str): |
| 739 | + if file_input.startswith('$'): |
| 740 | + return {'fileOnServer': file_input} |
| 741 | + elif os.path.isfile(file_input): |
| 742 | + return {'fileOnServer': self.upload_file(file_input), |
| 743 | + 'displayName': os.path.basename(file_input)} |
| 744 | + elif re.match(r'^https?://', file_input): |
| 745 | + return {'url': file_input, |
| 746 | + 'displayName': os.path.basename(file_input)} |
| 747 | + elif isinstance(file_input, io.IOBase) and hasattr(file_input, 'seek'): # file-like object |
| 748 | + return {'fileOnServer': self.upload_data(file_input, 'UNKNOWN')} |
| 749 | + raise ValueError('file_input must be a valid file path string or a file-like object') |
| 750 | + |
| 751 | + def show_data(self, file_input, preview_metadata=False, |
| 752 | + **data_view_options): |
| 753 | + """ |
| 754 | + Show any data file of the type that Firefly supports: |
| 755 | + - Custom catalog or table in IPAC, CSV, TSV, VOTABLE, Parquet, or FITS table format |
| 756 | + - A ds9 region file |
| 757 | + - Images in FITS format, including multi-extension FITS files with images, tables, or a mixture of both |
| 758 | + - A Multi-Order Coverage Map (MOC) in FITS format |
| 759 | + - A Data Link Table file |
| 760 | + - A UWS job file |
| 761 | + |
| 762 | + Parameters |
| 763 | + ---------- |
| 764 | + file_input : `str` or `file-like object` |
| 765 | + If `str`, it can be a local file path, a URL to a remote file, or |
| 766 | + name of a file on the server (return value of `upload_file()`). |
| 767 | + Else it can be a file stream object (file-like object) opened in Python. |
| 768 | + preview_metadata : `bool`, optional |
| 769 | + If True, preview the metadata of the file before loading the data. |
| 770 | + This allows you to select FITS extensions, table columns, etc. and |
| 771 | + then you can click "Load" button in the UI to load your selections. |
| 772 | + Default is False. |
| 773 | + **data_view_options : optional keyword arguments |
| 774 | + Additional options for the view of this data file in the UI, such as: |
| 775 | + **displayName** : `str`, optional |
| 776 | + A name to display in the UI for the data file loaded. |
| 777 | + """ |
| 778 | + payload = { |
| 779 | + **self.get_file_payload(file_input), |
| 780 | + 'immediate': not preview_metadata, |
| 781 | + **data_view_options # TODO: pass data_view_id -> didn't work |
| 782 | + } |
| 783 | + return self.dispatch(ACTION_DICT['ShowAnyData'], payload) |
732 | 784 |
|
733 | 785 | def show_fits_image(self, file_on_server=None, plot_id=None, viewer_id=None, **additional_params): |
734 | 786 | """ |
|
0 commit comments