diff options
| -rw-r--r-- | fg21sim/configs/manager.py | 11 | ||||
| -rw-r--r-- | fg21sim/webui/handlers/configs.py | 14 | ||||
| -rw-r--r-- | fg21sim/webui/static/js/configs.js | 17 | 
3 files changed, 28 insertions, 14 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index 5cf9b00..7818d1c 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -344,7 +344,7 @@ class ConfigManager:      def setn(self, key, value):          """Set the value of config option specified by a list of keys or a -        "sep"-separated keys string. +        "/"-separated keys string.          The supplied key-value config pair is first used to create a          temporary ``ConfigObj`` instance, which is then validated against @@ -379,10 +379,9 @@ class ConfigManager:          ConfigError :              The value fails to pass the validation against specifications.          """ -        try: -            val_old = self.getn(key) -        except KeyError as e: -            raise KeyError(e) +        # NOTE: +        # May raise ``KeyError`` if the key does not exists +        val_old = self.getn(key)          if val_old == value:              # No need to set this option value              return @@ -396,6 +395,8 @@ class ConfigManager:          # Create the temporary ``ConfigObj`` instance and validate it          config_new = ConfigObj(d, interpolation=False,                                 configspec=self._configspec) +        # NOTE: +        # May raise ``ConfigError`` if fails to pass the validation          config_new = self._validate(config_new)          # NOTE:          # The validated ``config_new`` is populated with all other options diff --git a/fg21sim/webui/handlers/configs.py b/fg21sim/webui/handlers/configs.py index 296bd11..857138b 100644 --- a/fg21sim/webui/handlers/configs.py +++ b/fg21sim/webui/handlers/configs.py @@ -99,7 +99,7 @@ class ConfigsAJAXHandler(BaseRequestHandler):          if action == "set":              # Set the values of the specified options              try: -                errors = self._set_configs(data=request["data"]) +                data, errors = self._set_configs(request["data"])                  success = True              except KeyError:                  success = False @@ -198,10 +198,15 @@ class ConfigsAJAXHandler(BaseRequestHandler):          Returns          ------- +        data_orig : dict +            When the supplied value failed to pass the specification +            validation, then its original value was returned to the client +            to reset its value.          errors : dict              When error occurs (e.g., invalid key, invalid values), then the              specific errors with details are stored in this dictionary.          """ +        data_orig = {}          errors = {}          for key, value in data.items():              if key in ["workdir", "configfile"]: @@ -215,10 +220,13 @@ class ConfigsAJAXHandler(BaseRequestHandler):              else:                  try:                      self.configs.setn(key, value) -                except (KeyError, ConfigError) as e: +                except KeyError as e: +                    errors[key] = str(e) +                except ConfigError as e: +                    data_orig[key] = self.configs.getn(key)                      errors[key] = str(e)          # -        return errors +        return (data_orig, errors)      def _reset_configs(self):          """Reset the configurations to the defaults.""" diff --git a/fg21sim/webui/static/js/configs.js b/fg21sim/webui/static/js/configs.js index f7e3731..f321881 100644 --- a/fg21sim/webui/static/js/configs.js +++ b/fg21sim/webui/static/js/configs.js @@ -118,8 +118,9 @@ var resetFormConfigs = function () {   *   * @param {String} name - The name of filed name   * - * @returns value - value of the option field - *                  + `null` if the field not exists or empty (no value) + * @returns value - value of the configuration option field + *                  + `null` if the field is empty (no value) + *                  + `undefined` if the field does not exists   */  var getFormConfigSingle = function (name) {    var value = null; @@ -143,8 +144,11 @@ var getFormConfigSingle = function (name) {          value = target.filter(":checked").map(            function () { return $(this).val(); }).get();        } else if (target.is(":text") && target.data("type") === "array") { -        // Convert back to Array +        // Convert comma-separated string back to Array          value = target.val().split(/\s*,\s*/); +        if (value.length === 1 && value[0] === "") { +          value = [];  // Empty Array +        }        } else {          value = target.val();        } @@ -153,6 +157,7 @@ var getFormConfigSingle = function (name) {          value = null;        }      } else { +      value = undefined;        console.error("No such element:", selector);      }    } @@ -241,11 +246,11 @@ var setFormConfigSingle = function (name, value) {  var setFormConfigs = function (data, errors) {    // Set the values of form field to the input configurations data    $.each(data, function (name, value) { -    if (value == null) { +    if (value === null) {        value = "";  // Default to empty string      }      var val_old = getFormConfigSingle(name); -    if (val_old !== value) { +    if (typeof val_old !== "undefined" && val_old !== value) {        setFormConfigSingle(name, value);        console.log("Set input '" + name + "' to:", value, " <-", val_old);      } @@ -388,7 +393,7 @@ var setServerConfigs = function (url, data) {    data = typeof data !== "undefined" ? data : {};    return $.postJSON(url, {action: "set", data: data},                      function (response) { -                      setFormConfigs({}, response.errors); +                      setFormConfigs(response.data, response.errors);                      })      .fail(function (jqxhr) {        var modalData = {};  | 
