comparison sat_frontends/jp/cmd_param.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children fee60f17ebac
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
17 17
18 # You should have received a copy of the GNU Affero General Public License 18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 21
22 import base 22 from . import base
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24 __commands__ = ["Param"] 24 __commands__ = ["Param"]
25 25
26 26
27 class Get(base.CommandBase): 27 class Get(base.CommandBase):
28 def __init__(self, host): 28 def __init__(self, host):
29 super(Get, self).__init__(host, 'get', need_connect=False, help=_('Get a parameter value')) 29 super(Get, self).__init__(host, 'get', need_connect=False, help=_('Get a parameter value'))
30 30
31 def add_parser_options(self): 31 def add_parser_options(self):
32 self.parser.add_argument("category", nargs='?', type=base.unicode_decoder, help=_(u"Category of the parameter")) 32 self.parser.add_argument("category", nargs='?', help=_("Category of the parameter"))
33 self.parser.add_argument("name", nargs='?', type=base.unicode_decoder, help=_(u"Name of the parameter")) 33 self.parser.add_argument("name", nargs='?', help=_("Name of the parameter"))
34 self.parser.add_argument("-a", "--attribute", type=str, default="value", help=_(u"Name of the attribute to get")) 34 self.parser.add_argument("-a", "--attribute", type=str, default="value", help=_("Name of the attribute to get"))
35 self.parser.add_argument("--security-limit", type=int, default=-1, help=_(u"Security limit")) 35 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit"))
36 36
37 def start(self): 37 def start(self):
38 if self.args.category is None: 38 if self.args.category is None:
39 categories = self.host.bridge.getParamsCategories() 39 categories = self.host.bridge.getParamsCategories()
40 print u"\n".join(categories) 40 print("\n".join(categories))
41 elif self.args.name is None: 41 elif self.args.name is None:
42 try: 42 try:
43 values_dict = self.host.bridge.asyncGetParamsValuesFromCategory(self.args.category, self.args.security_limit, self.profile) 43 values_dict = self.host.bridge.asyncGetParamsValuesFromCategory(self.args.category, self.args.security_limit, self.profile)
44 except Exception as e: 44 except Exception as e:
45 print u"Can't find requested parameters: {}".format(e) 45 print("Can't find requested parameters: {}".format(e))
46 self.host.quit(1) 46 self.host.quit(1)
47 for name, value in values_dict.iteritems(): 47 for name, value in values_dict.items():
48 print u"{}\t{}".format(name, value) 48 print("{}\t{}".format(name, value))
49 else: 49 else:
50 try: 50 try:
51 value = self.host.bridge.asyncGetParamA(self.args.name, self.args.category, self.args.attribute, 51 value = self.host.bridge.asyncGetParamA(self.args.name, self.args.category, self.args.attribute,
52 self.args.security_limit, self.profile) 52 self.args.security_limit, self.profile)
53 except Exception as e: 53 except Exception as e:
54 print u"Can't find requested parameter: {}".format(e) 54 print("Can't find requested parameter: {}".format(e))
55 self.host.quit(1) 55 self.host.quit(1)
56 print value 56 print(value)
57 57
58 58
59 class Set(base.CommandBase): 59 class Set(base.CommandBase):
60 def __init__(self, host): 60 def __init__(self, host):
61 super(Set, self).__init__(host, 'set', need_connect=False, help=_('Set a parameter value')) 61 super(Set, self).__init__(host, 'set', need_connect=False, help=_('Set a parameter value'))
62 62
63 def add_parser_options(self): 63 def add_parser_options(self):
64 self.parser.add_argument("category", type=base.unicode_decoder, help=_(u"Category of the parameter")) 64 self.parser.add_argument("category", help=_("Category of the parameter"))
65 self.parser.add_argument("name", type=base.unicode_decoder, help=_(u"Name of the parameter")) 65 self.parser.add_argument("name", help=_("Name of the parameter"))
66 self.parser.add_argument("value", type=base.unicode_decoder, help=_(u"Name of the parameter")) 66 self.parser.add_argument("value", help=_("Name of the parameter"))
67 self.parser.add_argument("--security-limit", type=int, default=-1, help=_(u"Security limit")) 67 self.parser.add_argument("--security-limit", type=int, default=-1, help=_("Security limit"))
68 68
69 def start(self): 69 def start(self):
70 try: 70 try:
71 self.host.bridge.setParam(self.args.name, self.args.value, self.args.category, self.args.security_limit, self.profile) 71 self.host.bridge.setParam(self.args.name, self.args.value, self.args.category, self.args.security_limit, self.profile)
72 except Exception as e: 72 except Exception as e:
73 print u"Can set requested parameter: {}".format(e) 73 print("Can set requested parameter: {}".format(e))
74 74
75 75
76 class SaveTemplate(base.CommandBase): 76 class SaveTemplate(base.CommandBase):
77 def __init__(self, host): 77 def __init__(self, host):
78 super(SaveTemplate, self).__init__(host, 'save', use_profile=False, help=_('Save parameters template to xml file')) 78 super(SaveTemplate, self).__init__(host, 'save', use_profile=False, help=_('Save parameters template to xml file'))
81 self.parser.add_argument("filename", type=str, help=_("Output file")) 81 self.parser.add_argument("filename", type=str, help=_("Output file"))
82 82
83 def start(self): 83 def start(self):
84 """Save parameters template to xml file""" 84 """Save parameters template to xml file"""
85 if self.host.bridge.saveParamsTemplate(self.args.filename): 85 if self.host.bridge.saveParamsTemplate(self.args.filename):
86 print _("Parameters saved to file %s") % self.args.filename 86 print(_("Parameters saved to file %s") % self.args.filename)
87 else: 87 else:
88 print _("Can't save parameters to file %s") % self.args.filename 88 print(_("Can't save parameters to file %s") % self.args.filename)
89 89
90 90
91 class LoadTemplate(base.CommandBase): 91 class LoadTemplate(base.CommandBase):
92 92
93 def __init__(self, host): 93 def __init__(self, host):
97 self.parser.add_argument("filename", type=str, help=_("Input file")) 97 self.parser.add_argument("filename", type=str, help=_("Input file"))
98 98
99 def start(self): 99 def start(self):
100 """Load parameters template from xml file""" 100 """Load parameters template from xml file"""
101 if self.host.bridge.loadParamsTemplate(self.args.filename): 101 if self.host.bridge.loadParamsTemplate(self.args.filename):
102 print _("Parameters loaded from file %s") % self.args.filename 102 print(_("Parameters loaded from file %s") % self.args.filename)
103 else: 103 else:
104 print _("Can't load parameters from file %s") % self.args.filename 104 print(_("Can't load parameters from file %s") % self.args.filename)
105 105
106 106
107 class Param(base.CommandBase): 107 class Param(base.CommandBase):
108 subcommands = (Get, Set, SaveTemplate, LoadTemplate) 108 subcommands = (Get, Set, SaveTemplate, LoadTemplate)
109 109