changeset 3640:f4c02bdb2d91

CLI: use global constants for output names
author Goffi <goffi@goffi.org>
date Wed, 08 Sep 2021 11:13:35 +0200
parents 43542cf32e5a
children 0ffaa231138c
files sat_frontends/jp/constants.py sat_frontends/jp/output_std.py sat_frontends/jp/output_xml.py
diffstat 3 files changed, 35 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/jp/constants.py	Wed Sep 01 13:46:43 2021 +0200
+++ b/sat_frontends/jp/constants.py	Wed Sep 08 11:13:35 2021 +0200
@@ -54,6 +54,11 @@
         OUTPUT_XMLUI,
         OUTPUT_LIST_XMLUI,
     )
+    OUTPUT_NAME_SIMPLE = "simple"
+    OUTPUT_NAME_XML = "xml"
+    OUTPUT_NAME_XML_RAW = "xml-raw"
+    OUTPUT_NAME_JSON = "json"
+    OUTPUT_NAME_JSON_RAW = "json-raw"
 
     # Pubsub options flags
     SERVICE = "service"  # service required
--- a/sat_frontends/jp/output_std.py	Wed Sep 01 13:46:43 2021 +0200
+++ b/sat_frontends/jp/output_std.py	Wed Sep 08 11:13:35 2021 +0200
@@ -26,9 +26,6 @@
 import json
 
 __outputs__ = ["Simple", "Json"]
-SIMPLE = "simple"
-JSON = "json"
-JSON_RAW = "json_raw"
 
 
 class Simple(object):
@@ -36,13 +33,13 @@
 
     def __init__(self, host):
         self.host = host
-        host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
-        host.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
-        host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
-        host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict)
-        host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict)
-        host.register_output(C.OUTPUT_MESS, SIMPLE, self.messages)
-        host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
+        host.register_output(C.OUTPUT_TEXT, C.OUTPUT_NAME_SIMPLE, self.simple_print)
+        host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_SIMPLE, self.list)
+        host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_SIMPLE, self.dict)
+        host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_SIMPLE, self.list_dict)
+        host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_SIMPLE, self.dict_dict)
+        host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_SIMPLE, self.messages)
+        host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_SIMPLE, self.simple_print)
 
     def simple_print(self, data):
         self.host.disp(str(data))
@@ -109,19 +106,19 @@
 
     def __init__(self, host):
         self.host = host
-        host.register_output(C.OUTPUT_TEXT, JSON, self.dump)
-        host.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
-        host.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
-        host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump)
-        host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump)
-        host.register_output(C.OUTPUT_MESS, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_MESS, JSON_RAW, self.dump)
-        host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
-        host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_TEXT, C.OUTPUT_NAME_JSON, self.dump)
+        host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_JSON_RAW, self.dump)
+        host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON, self.dump_pretty)
+        host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON_RAW, self.dump)
 
     def dump(self, data):
         self.host.disp(json.dumps(data, default=str))
--- a/sat_frontends/jp/output_xml.py	Wed Sep 01 13:46:43 2021 +0200
+++ b/sat_frontends/jp/output_xml.py	Wed Sep 08 11:13:35 2021 +0200
@@ -1,7 +1,6 @@
 #! /usr/bin/env python3
 
-
-# jp: a SàT command line tool
+# Libervia CLI frontend
 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
 
 # This program is free software: you can redistribute it and/or modify
@@ -36,8 +35,6 @@
 
 
 __outputs__ = ["XML"]
-RAW = "xml_raw"
-PRETTY = "xml_pretty"
 
 
 class XML(object):
@@ -45,16 +42,20 @@
 
     def __init__(self, host):
         self.host = host
-        host.register_output(C.OUTPUT_XML, PRETTY, self.pretty, default=True)
-        host.register_output(C.OUTPUT_LIST_XML, PRETTY, self.pretty_list, default=True)
-        host.register_output(C.OUTPUT_XML, RAW, self.raw)
-        host.register_output(C.OUTPUT_LIST_XML, RAW, self.list_raw)
+        host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML, self.pretty, default=True)
+        host.register_output(
+            C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML, self.pretty_list, default=True
+        )
+        host.register_output(C.OUTPUT_XML, C.OUTPUT_NAME_XML_RAW, self.raw)
+        host.register_output(C.OUTPUT_LIST_XML, C.OUTPUT_NAME_XML_RAW, self.list_raw)
 
     def colorize(self, xml):
         if pygments is None:
             self.host.disp(
                 _(
-                    "Pygments is not available, syntax highlighting is not possible. Please install if from http://pygments.org or with pip install pygments"
+                    "Pygments is not available, syntax highlighting is not possible. "
+                    "Please install if from http://pygments.org or with pip install "
+                    "pygments"
                 ),
                 error=True,
             )