aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/webui/handlers
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-11-17 09:26:46 +0800
committerAaron LI <aaronly.me@outlook.com>2016-11-17 09:26:46 +0800
commit845c125f1bb94951aca4f0541ad6e1400edb8666 (patch)
treedfe22ff572b7dc4fb87cb08f991c04ff1e9a8950 /fg21sim/webui/handlers
parent695c1b3b12db4db77a2ad7aba478ba4fd42dd76f (diff)
downloadfg21sim-845c125f1bb94951aca4f0541ad6e1400edb8666.tar.bz2
webui: configs.py: Add "exists" action support through GET
The "exists" action through the GET request will check the existence of the specified filepath (which should be an absolute path).
Diffstat (limited to 'fg21sim/webui/handlers')
-rw-r--r--fg21sim/webui/handlers/configs.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/fg21sim/webui/handlers/configs.py b/fg21sim/webui/handlers/configs.py
index 18adb65..9736650 100644
--- a/fg21sim/webui/handlers/configs.py
+++ b/fg21sim/webui/handlers/configs.py
@@ -34,6 +34,7 @@ class ConfigsAJAXHandler(BaseRequestHandler):
Supported actions:
- get: Get the specified/all configuration values
- validate: Validate the configurations and response the errors
+ - exists: Whether the file already exists
NOTE
----
@@ -50,6 +51,15 @@ class ConfigsAJAXHandler(BaseRequestHandler):
elif action == "validate":
__, errors = self.configs.check_all(raise_exception=False)
success = True
+ elif action == "exists":
+ filepath = json_decode(self.get_argument("filepath", "null"))
+ exists, error = self._exists_file(filepath)
+ if exists is None:
+ success = False
+ reason = error
+ else:
+ success = True
+ data["exists"] = exists
else:
# ERROR: bad action
success = False
@@ -270,3 +280,33 @@ class ConfigsAJAXHandler(BaseRequestHandler):
except (ValueError, OSError) as e:
error = str(e)
return (success, error)
+
+ @staticmethod
+ def _exists_file(filepath):
+ """Check whether the given filepath already exists?
+
+ Parameters
+ ----------
+ filepath: str
+ The input filepath to check its existence, must be
+ an *absolute path*.
+
+ Returns
+ -------
+ exists : bool
+ ``True`` if the filepath already exists, ``False`` if not exists,
+ and ``None`` if error occurs.
+ error : str
+ The error information, and ``None`` if no errors.
+ """
+ exists = None
+ error = None
+ try:
+ filepath = os.path.expanduser(filepath)
+ if os.path.isabs(filepath):
+ exists = os.path.exists(filepath)
+ else:
+ error = "Not an absolute path: {0}".format(filepath)
+ except AttributeError:
+ error = "Invalid input filepath: {0}".format(filepath)
+ return (exists, error)