handlers.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import os
  18. from jupyter_server.base.handlers import APIHandler
  19. from jupyter_server.utils import url_unescape
  20. from tornado import web
  21. from elyra.contents.parser import ContentParser
  22. from elyra.util.http import HttpErrorMixin
  23. from elyra.util.path import get_expanded_path
  24. class ContentHandler(HttpErrorMixin, APIHandler):
  25. """Handler to expose REST API to parse envs from a File"""
  26. content_parser: ContentParser = ContentParser()
  27. @web.authenticated
  28. async def post(self, path):
  29. msg_json = dict(title="Operation not supported.")
  30. self.write(msg_json)
  31. self.flush()
  32. @web.authenticated
  33. async def get(self, path=""):
  34. path = path or ""
  35. path = url_unescape(path)
  36. self.log.debug(f"Parsing file: {path}")
  37. try:
  38. absolute_path = self._get_absolute_path(path)
  39. properties = self.content_parser.parse(absolute_path)
  40. # TODO: Validation of model
  41. self.finish(json.dumps(properties))
  42. except FileNotFoundError as fnfe:
  43. raise web.HTTPError(404, str(fnfe)) from fnfe
  44. except IsADirectoryError as iade:
  45. raise web.HTTPError(400, str(iade)) from iade
  46. except Exception as e:
  47. # Parser could not parse the given file, but this does not necessarily indicate an error with the file.
  48. # Log the issue and return an empty model so that other user processes are not disrupted.
  49. self.log.debug(f"Could not parse '{path}': {str(e)}")
  50. empty_properties = {"env_vars": {}, "inputs": [], "outputs": []}
  51. self.finish(json.dumps(empty_properties))
  52. def _get_absolute_path(self, path: str) -> str:
  53. """Returns the absolute path of 'path' in relation to the configured root_dir.
  54. Note: This version of `get_absolute_path` is used over the version in util/path.py because
  55. 'path' will have a leading `/` and therefore be considered absolute already when, within
  56. this handler, it should always be a path relative to root_dir.
  57. """
  58. root_dir = get_expanded_path(self.settings["server_root_dir"])
  59. if path[0] == "/": # if path starts with a slash, use the follow characters so join can be performed
  60. path = path[1:]
  61. absolute_path = os.path.normpath(os.path.join(root_dir, path))
  62. return absolute_path