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.

91 lines
2.4 KiB

2 months ago
  1. import configparser
  2. import os
  3. import multiprocessing
  4. class SetupConfig:
  5. """
  6. Configuration for setup.
  7. """
  8. def __init__(self):
  9. """
  10. Create config with default values
  11. """
  12. self.config = configparser.ConfigParser()
  13. self.config["build_ext"] = self._default_values()
  14. @staticmethod
  15. def _default_values():
  16. """
  17. Return default values for config.
  18. :return: Dict with default values for build settings.
  19. """
  20. try:
  21. no_jobs = multiprocessing.cpu_count() if multiprocessing.cpu_count() is not None else 1
  22. except NotImplementedError:
  23. no_jobs = 1
  24. return {
  25. "storm_dir": "",
  26. "disable_dft": False,
  27. "disable_gspn": False,
  28. "disable_pars": False,
  29. "debug": False,
  30. "jobs": str(no_jobs),
  31. }
  32. def load_from_file(self, path):
  33. """
  34. Load config from file.
  35. :param path Path to config file.
  36. """
  37. if os.path.isfile(path):
  38. self.config.read(path)
  39. if not self.config.has_section("build_ext"):
  40. self.config["build_ext"] = self._default_values()
  41. def write_config(self, path):
  42. """
  43. Save config with build settings.
  44. :param path Path to config file.
  45. """
  46. with open(path, 'w') as configfile:
  47. self.config.write(configfile)
  48. def get_as_bool(self, name):
  49. """
  50. Get the boolean value for option name.
  51. :param name: Name of option.
  52. :return Value as bool.
  53. """
  54. return self.config.getboolean("build_ext", name)
  55. def get_as_int(self, name):
  56. """
  57. Get the int value for option name.
  58. :param name: Name of option.
  59. :return Value as integer.
  60. """
  61. return self.config.getint("build_ext", name)
  62. def get_as_string(self, name):
  63. """
  64. Get the string value for option name.
  65. :param name: Name of option.
  66. :return Value as string.
  67. """
  68. return self.config.get("build_ext", name)
  69. def update(self, name, value):
  70. """
  71. Update name with given value if value is not None.
  72. :param name: Name of option.
  73. :param value: New value or None
  74. """
  75. if value is not None:
  76. assert self.config.has_option("build_ext", name)
  77. self.config.set("build_ext", name, str(value))