Initial attempt to list measurements through the API. 0.11.0

Fri, 21 May 2021 16:26:54 +0300

author
ioannis@ioannis-VirtualBox
date
Fri, 21 May 2021 16:26:54 +0300
changeset 65
74ff07f81f4a
parent 64
f192aa069261
child 66
58006f895f8a

Initial attempt to list measurements through the API.

scc_access/__init__.py file | annotate | diff | comparison | revisions
scc_access/scc_access.py file | annotate | diff | comparison | revisions
--- a/scc_access/__init__.py	Fri May 07 08:54:44 2021 +0300
+++ b/scc_access/__init__.py	Fri May 21 16:26:54 2021 +0300
@@ -1,1 +1,1 @@
-__version__ = "0.10.4"
\ No newline at end of file
+__version__ = "0.11.0"
\ No newline at end of file
--- a/scc_access/scc_access.py	Fri May 07 08:54:44 2021 +0300
+++ b/scc_access/scc_access.py	Fri May 21 16:26:54 2021 +0300
@@ -520,42 +520,21 @@
 
         return measurements
 
-    def list_measurements(self, station=None, system=None, start=None, stop=None, upload_status=None,
-                          processing_status=None, optical_processing=None):
+    def list_measurements(self, id_exact=None, id_startswith=None):
 
         # TODO: Change this to work through the API
 
         # Need to set to empty string if not specified, we won't get any results
-        params = {
-            "station": station if station is not None else "",
-            "system": system if system is not None else "",
-            "stop": stop if stop is not None else "",
-            "start": start if start is not None else "",
-            "upload_status": upload_status if upload_status is not None else "",
-            "preprocessing_status": processing_status if processing_status is not None else "",
-            "optical_processing_status": optical_processing if optical_processing is not None else ""
-        }
+        params = {}
 
-        response_txt = self.session.get(self.list_measurements_url, params=params).text
-        tbl_rgx = re.compile(r'<table id="measurements">(.*?)</table>', re.DOTALL)
-        entry_rgx = re.compile(r'<tr>(.*?)</tr>', re.DOTALL)
-        measurement_rgx = re.compile(
-            r'.*?<td><a[^>]*>(\w+)</a>.*?<td>.*?<td>([\w-]+ [\w:]+)</td>.*<td data-order="([-]?\d+),([-]?\d+),([-]?\d+)".*',
-            re.DOTALL)
-        matches = tbl_rgx.findall(response_txt)
-        if len(matches) != 1:
-            return []
+        if id_exact is not None:
+            params['id__exact'] = id_exact
+        else:
+            params['id__startswith'] = id_startswith
 
-        ret = []
-        for entry in entry_rgx.finditer(matches[0]):
-            m = measurement_rgx.match(entry.string[entry.start(0):entry.end(0)])
-            if m:
-                name, date, upload, preproc, optical = m.groups()
-                ret.append(
-                    Measurement(self.base_url, {"id": name, "upload": int(upload), "pre_processing": int(preproc),
-                                                "processing": int(optical)}))
+        response_json = self.session.get(self.api_measurements_url, params=params).text
 
-        return ret
+        return response_json
 
     def measurement_id_for_date(self, t1, call_sign, base_number=0):
         """ Give the first available measurement id on the SCC for the specific
@@ -827,16 +806,14 @@
         scc.logout()
 
 
-def list_measurements(settings, station=None, system=None, start=None, stop=None, upload_status=None,
-                      preprocessing_status=None,
-                      optical_processing=None):
+def list_measurements(settings, id_exact=None, id_startswith=None):
     """List all available measurements"""
     with SCC(settings['basic_credentials'], settings['output_dir'], settings['base_url']) as scc:
         scc.login(settings['website_credentials'])
-        ret = scc.list_measurements(station=station, system=system, start=start, stop=stop, upload_status=upload_status,
-                                    processing_status=preprocessing_status, optical_processing=optical_processing)
-        for entry in ret:
-            print("%s" % entry.id)
+
+        results_json = scc.list_measurements(id_exact=id_exact, id_startswith=id_startswith)
+        print(results_json)
+
         scc.logout()
 
 
@@ -942,33 +919,12 @@
 
 def setup_list_measurements(parser):
     def list_measurements_from_args(parsed):
-        # TODO: Fix this
-        logger.warning("This method needs to be updated. Cross-chceck any results.")
-
-        list_measurements(parsed.config, station=parsed.station, system=parsed.system, start=parsed.start,
-                          stop=parsed.stop,
-                          upload_status=parsed.upload_status, preprocessing_status=parsed.preprocessing_status,
-                          optical_processing=parsed.optical_processing_status)
-
-    def status(arg):
-        if -127 <= int(arg) <= 127:
-            return arg
-        else:
-            raise argparse.ArgumentTypeError("Status must be between -127 and 127")
+        list_measurements(parsed.config, id_exact=parsed.id_exact, id_startswith=parsed.id_startswith)
 
-    def date(arg):
-        if re.match(r'\d{4}-\d{2}-\d{2}', arg):
-            return arg
-        else:
-            raise argparse.ArgumentTypeError("Date must be in format 'YYYY-MM-DD'")
+    group = parser.add_mutually_exclusive_group()
+    group.add_argument("--id_exact", help="Exact measurement id.")
+    group.add_argument("--id_startswith", help="Initial part of measurement id.")
 
-    parser.add_argument("--station", help="Filter for only the selected station")
-    parser.add_argument("--system", help="Filter for only the selected station")
-    parser.add_argument("--start", help="Filter for only the selected station", type=date)
-    parser.add_argument("--stop", help="Filter for only the selected station", type=date)
-    parser.add_argument("--upload-status", help="Filter for only the selected station", type=status)
-    parser.add_argument("--preprocessing-status", help="Filter for only the selected station", type=status)
-    parser.add_argument("--optical-processing-status", help="Filter for only the selected station", type=status)
     parser.set_defaults(execute=list_measurements_from_args)
 
 

mercurial