orx.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import json
  2. import os
  3. import orx.parse
  4. import util
  5. class Orx:
  6. """
  7. Class that represents the basics of an orx file.
  8. """
  9. def __init__(self, homedir='.', filename=None):
  10. self.homedir = homedir
  11. self.defines = {}
  12. # get the actual data for this stuff
  13. if filename is not None:
  14. self.load_and_parse(filename)
  15. def exec_define(self, args, kwargs):
  16. """
  17. Executes an orx manifest `define` statement.
  18. """
  19. if kwargs:
  20. raise ValueError('kwargs not allowed in define expression')
  21. if len(args) < 2:
  22. raise ValueError('Missing argument')
  23. if args[0] in self.defines:
  24. raise ValueError('Already defined: ' + args[0])
  25. self.defines[args[0]] = ' '.join(args[1:])
  26. def exec_include(self, args, kwargs):
  27. """
  28. Executes an orx manifest `include` statement.
  29. """
  30. if not args:
  31. raise Exception('Missing filename')
  32. if len(args) > 1:
  33. raise ValueError('Multiple filenames')
  34. self.load_and_parse(args[0])
  35. def load_and_parse(self, filename):
  36. """
  37. Loads and parses an orx manifest file.
  38. """
  39. try:
  40. file = open(os.path.join(self.homedir, filename), 'r')
  41. except OSError:
  42. raise Exception('Could not open orx file: ' + filename)
  43. for expr, line_num in orx.parse.get_exprs(file):
  44. try:
  45. self.exec_expr(expr)
  46. except Exception as e:
  47. raise Exception(f'In orx file `{filename}` at line '
  48. f'{line_num}:\n'
  49. f'`{expr.strip()}`\n'
  50. f'Error: {e}')
  51. file.close()