Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
2.2 KiB

  1. # Copyright P G Jones 2018.
  2. #
  3. # Permission is hereby granted, free of charge, to any person
  4. # obtaining a copy of this software and associated documentation
  5. # files (the "Software"), to deal in the Software without
  6. # restriction, including without limitation the rights to use,
  7. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the
  9. # Software is furnished to do so, subject to the following
  10. # conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be
  13. # included in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. # OTHER DEALINGS IN THE SOFTWARE.
  23. #
  24. # Obtained from:
  25. # https://github.com/pgjones/hypercorn/blob/master/src/hypercorn/utils.py
  26. #
  27. # Slightly modified to adapt to my uses.
  28. #
  29. # Remove import path from sys.path as part of clean up.
  30. #
  31. # Use getattr instead of eval.
  32. import sys
  33. from pathlib import Path
  34. from importlib import import_module
  35. def load_application(path: str):
  36. try:
  37. module_name, app_name = path.split(":", 1)
  38. except ValueError:
  39. module_name, app_name = path, "func"
  40. except AttributeError:
  41. raise ValueError()
  42. module_path = Path(module_name).resolve()
  43. added_module = str(module_path.parent)
  44. sys.path.insert(0, added_module)
  45. try:
  46. if module_path.is_file():
  47. import_name = module_path.with_suffix("").name
  48. else:
  49. import_name = module_path.name
  50. try:
  51. module = import_module(import_name)
  52. except ModuleNotFoundError as error:
  53. if error.name == import_name:
  54. raise ValueError('module %s not found' %
  55. repr(import_name))
  56. else:
  57. raise
  58. try:
  59. for i in app_name.split('.'):
  60. module = getattr(module, i)
  61. return module
  62. except AttributeError:
  63. raise ValueError('attribute %s not found on %s' %
  64. (repr(i), repr(module)))
  65. finally:
  66. sys.path.remove(added_module)