X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plc_config.py;h=2d6afefa816eec05d7134958e077379c95c41635;hb=refs%2Fheads%2F4.3;hp=98f95d19e016dc45687002bdcadd5e3a19023460;hpb=10bef10c43616fd505e9463289f88411e83cbc42;p=myplc.git diff --git a/plc_config.py b/plc_config.py index 98f95d1..2d6afef 100644 --- a/plc_config.py +++ b/plc_config.py @@ -719,6 +719,31 @@ DO NOT EDIT. This file was automatically generated at return buf.getvalue() + def output_ruby(self, show_comments = True, encoding = "utf-8"): + """ + Return variables as a shell script. + """ + + buf = codecs.lookup(encoding)[3](StringIO()) + buf.writelines(["# " + line + os.linesep for line in self._header()]) + + for (category_id, (category, variables)) in self._variables.iteritems(): + for variable in variables.values(): + (id, name, value, comments) = self._sanitize_variable(category_id, variable) + if show_comments: + buf.write(os.linesep) + if name is not None: + buf.write("# " + name + os.linesep) + if comments is not None: + buf.writelines(["# " + line + os.linesep for line in comments]) + # bash does not have the concept of NULL + if value is not None: + buf.write("$" + id.lower() + "=" + value + os.linesep) + + return buf.getvalue() + + + def output_php(self, encoding = "utf-8"): """ @@ -828,6 +853,28 @@ DO NOT EDIT. This file was automatically generated at return buf.getvalue() + def validate_type(self, variable_type, value): + + # ideally we should use the "validate_*" methods in PLCAPI or + # even declare some checks along with the default + # configuration (using RELAX NG?) but this shall work for now. + def ip_validator(val): + import socket + try: + socket.inet_aton(val) + return True + except: return False + + validators = { + 'email' : lambda val: re.match('\A[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9._\-]+\.[a-zA-Z]+\Z', val), + 'ip': ip_validator + } + + # validate it if not a know type. + validator = validators.get(variable_type, lambda x: True) + return validator(value) + + # xml.dom.minidom.Text.writexml adds surrounding whitespace to textual # data when pretty-printing. Override this behavior. @@ -847,8 +894,8 @@ class TrimTextElement(xml.dom.minidom.Element): # GLOBAL VARIABLES # release_id = "$Id$" -release_rev = "$Revision:$" -release_url = "$URL:$" +release_rev = "$Revision$" +release_url = "$URL$" g_configuration=None usual_variables=None @@ -877,8 +924,9 @@ def init_configuration (): mainloop_usage= """Available commands: Uppercase versions give variables comments, when available u/U\t\t\tEdit usual variables - w/W\t\t\tWrite / Write & reload - r\t\t\tRestart %s service + w\t\t\tWrite + r\t\t\tRestart %(service)s service + R\t\t\tReload %(service)s service (rebuild config files for sh, python....) q\t\t\tQuit (without saving) h/?\t\t\tThis help --- @@ -887,10 +935,10 @@ def init_configuration (): e/E [|]\tEdit variables (all, in category, single) --- c\t\t\tList categories - v/V [|]List Variables (all, in category, single) + v/V [|]\tList Variables (all, in category, single) --- Typical usage involves: u, [l,] w, r, q -""" % service +""" % globals() def usage (): command_usage="%prog [options] [default-xml [site-xml [consolidated-xml]]]" @@ -916,6 +964,10 @@ def get_value (config, category_id, variable_id): (category, variable) = config.get (category_id, variable_id) return variable['value'] +def get_type (config, category_id, variable_id): + (category, variable) = config.get (category_id, variable_id) + return variable['type'] + def get_current_value (cread, cwrite, category_id, variable_id): # the value stored in cwrite, if present, is the one we want try: @@ -1017,6 +1069,7 @@ def prompt_variable (cdef, cread, cwrite, category, variable, while True: default_value = get_value(cdef,category_id,variable_id) + variable_type = get_type(cdef,category_id,variable_id) current_value = get_current_value(cread,cwrite,category_id, variable_id) varname = get_varname (cread,category_id, variable_id) @@ -1053,9 +1106,12 @@ def prompt_variable (cdef, cread, cwrite, category, variable, else: print "No support for next category" else: - variable['value'] = answer - cwrite.set(category,variable) - return + if cdef.validate_type(variable_type, answer): + variable['value'] = answer + cwrite.set(category,variable) + return + else: + print "Not a valid value" def prompt_variables_all (cdef, cread, cwrite, show_comments): try: @@ -1156,7 +1212,6 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con continue show_comments=command.isupper() - command=command.lower() mode='ALL' if arg: @@ -1177,10 +1232,10 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con print "%s: no such category or variable" % arg continue - if (command in "qQ"): + if command in "qQ": # todo check confirmation return - elif (command == "w"): + elif command == "w": try: # Confirm that various constraints are met before saving file. validate_variables = g_configuration.get('validate_variables',{}) @@ -1199,7 +1254,7 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con consolidate(default_config, site_config, consolidated_config) print ("You might want to type 'r' (restart %s), 'R' (reload %s) or 'q' (quit)" % \ (service,service)) - elif (command == "u"): + elif command in "uU": global usual_variables try: for varname in usual_variables: @@ -1209,13 +1264,13 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con except Exception, inst: if (str(inst) != 'BailOut'): raise - elif (command == "r"): + elif command == "r": restart_service() - elif (command == "R"): + elif command == "R": reload_service() - elif (command == "c"): + elif command == "c": print_categories(cread) - elif (command in "eE"): + elif command in "eE": if mode == 'ALL': prompt_variables_all(cdef, cread, cwrite,show_comments) elif mode == 'CATEGORY': @@ -1225,12 +1280,12 @@ def mainloop (cdef, cread, cwrite, default_config, site_config, consolidated_con prompt_variable (cdef,cread,cwrite,category,variable, show_comments,False) except Exception, inst: - if (str(inst) != 'BailOut'): + if str(inst) != 'BailOut': raise - elif (command in "vVsSlL"): + elif command in "vVsSlL": show_value=(command in "sSlL") (c1,c2,c3) = (cdef, cread, cwrite) - if (command in "lL"): + if command in "lL": (c1,c2,c3) = (cwrite,cwrite,cwrite) if mode == 'ALL': show_variables_all(c1,c2,c3,show_value,show_comments) @@ -1260,7 +1315,7 @@ def check_dir (config_file): #################### def optParserSetup(configuration): - parser = OptionParser(usage=usage(), version="%prog 1.0" + release_rev + release_url ) + parser = OptionParser(usage=usage(), version="%prog " + release_rev + release_url ) parser.set_defaults(config_dir=configuration['config_dir'], service=configuration['service'], usual_variables=configuration['usual_variables'])