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.

90 lines
2.4 KiB

  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_pars": False,
  28. "debug": False,
  29. "jobs": str(no_jobs),
  30. }
  31. def load_from_file(self, path):
  32. """
  33. Load config from file.
  34. :param path Path to config file.
  35. """
  36. if os.path.isfile(path):
  37. self.config.read(path)
  38. if not self.config.has_section("build_ext"):
  39. self.config["build_ext"] = self._default_values()
  40. def write_config(self, path):
  41. """
  42. Save config with build settings.
  43. :param path Path to config file.
  44. """
  45. with open(path, 'w') as configfile:
  46. self.config.write(configfile)
  47. def get_as_bool(self, name):
  48. """
  49. Get the boolean value for option name.
  50. :param name: Name of option.
  51. :return Value as bool.
  52. """
  53. return self.config.getboolean("build_ext", name)
  54. def get_as_int(self, name):
  55. """
  56. Get the int value for option name.
  57. :param name: Name of option.
  58. :return Value as integer.
  59. """
  60. return self.config.getint("build_ext", name)
  61. def get_as_string(self, name):
  62. """
  63. Get the string value for option name.
  64. :param name: Name of option.
  65. :return Value as string.
  66. """
  67. return self.config.get("build_ext", name)
  68. def update(self, name, value):
  69. """
  70. Update name with given value if value is not None.
  71. :param name: Name of option.
  72. :param value: New value or None
  73. """
  74. if value is not None:
  75. assert self.config.has_option("build_ext", name)
  76. self.config.set("build_ext", name, str(value))