params.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. from orx.orx import Orx
  3. import orx.parse
  4. import util
  5. class Parameters(Orx):
  6. """
  7. Class representing all of the data within the input parameters.
  8. """
  9. def __init__(self, homedir='.', filename=None, string=None):
  10. self.homedir = homedir
  11. self.defines = {}
  12. self.dests = []
  13. # get the actual data for this stuff
  14. if filename is not None:
  15. self.load_and_parse(filename)
  16. elif string is not None:
  17. self.single_line_parse(string)
  18. #print(self.dests)
  19. def exec_dest(self, args, kwargs):
  20. """
  21. Executes an orx parameters `dest` statement.
  22. """
  23. if 'structure' not in kwargs:
  24. raise ValueError("Missing structure value")
  25. if 'format' not in kwargs:
  26. raise ValueError("Missing format value(s)")
  27. if 'license' not in kwargs:
  28. raise ValueError("Missing license value(s)")
  29. # TODO: Just fall back to 'yes'
  30. for k, v in kwargs.items():
  31. if k == "format":
  32. for format in v.split(" "):
  33. res = {}
  34. res["structure"] = kwargs["structure"]
  35. res["format"] = format
  36. if kwargs["license"] == "yes":
  37. res["license"] = True
  38. else:
  39. res["license"] = False
  40. self.dests.append(res)
  41. def exec_expr(self, expr):
  42. """
  43. Executes an orx parameters expression.
  44. """
  45. # finds all of the constants from `define` expressions and fills
  46. # in their actual value
  47. final_expr = orx.parse.subst_consts(expr, self.defines)
  48. # now parse all of the expressions
  49. #
  50. # head: the expression name ('emoji', 'define', 'color', etc.)
  51. # args: the arguments without parameters
  52. # kwargs: the arguments with parameters
  53. try:
  54. head, args, kwargs = orx.parse.parse_expr(final_expr)
  55. except Exception:
  56. raise ValueError('Syntax error')
  57. # execute each expression based on the head.
  58. # (or do nothing if there's no head)
  59. if head is None:
  60. return
  61. elif head == 'define':
  62. self.exec_include(args, kwargs)
  63. elif head == 'include':
  64. self.exec_include(args, kwargs)
  65. elif head == 'dest':
  66. self.exec_dest(args, kwargs)
  67. else:
  68. raise ValueError('Unknown expression type: ' + head)
  69. def single_line_parse(self, string):
  70. try:
  71. self.exec_expr(string)
  72. except Exception as e:
  73. raise Exception(f'In the given orx string, there was an error. {e}')