config.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from pathlib import Path
  17. from ruamel import yaml
  18. from pipeline.constant import JobStatus
  19. __all__ = ["JobStatus", "VERSION", "SERVER_VERSION", "TIME_QUERY_FREQS", "Role", "StatusCode",
  20. "LogPath", "LogFormat", "IODataType", "PipelineConfig"]
  21. VERSION = 2
  22. SERVER_VERSION = "v1"
  23. TIME_QUERY_FREQS = 1
  24. MAX_RETRY = 3
  25. def get_default_config() -> dict:
  26. with (Path(__file__).parent.parent / "config.yaml").open(encoding="utf-8") as f:
  27. return yaml.safe_load(f)
  28. class Role(object):
  29. LOCAL = "local"
  30. GUEST = "guest"
  31. HOST = "host"
  32. ARBITER = "arbiter"
  33. @classmethod
  34. def support_roles(cls):
  35. roles = set()
  36. for role_key, role in cls.__dict__.items():
  37. if role_key.startswith("__") and isinstance(role_key, str):
  38. continue
  39. roles.add(role)
  40. return roles
  41. class StatusCode(object):
  42. SUCCESS = 0
  43. FAIL = 1
  44. CANCELED = 2
  45. class IODataType:
  46. SINGLE = "data"
  47. TRAIN = "train_data"
  48. VALIDATE = "validate_data"
  49. TEST = "test_data"
  50. class PipelineConfigMeta(type):
  51. def __getattr__(cls, name):
  52. if name not in cls._keys:
  53. raise AttributeError(f"type object '{cls.__name__}' has no attribute '{name}'")
  54. if cls._conf is None:
  55. cls._conf = get_default_config()
  56. value = cls._conf.get(name.lower())
  57. if value is not None:
  58. return value
  59. if name in {"IP", "PORT"}:
  60. raise ValueError(
  61. f"{name} not configured. "
  62. "Please use command line tool `pipeline init` to set it."
  63. )
  64. return cls._defaults.get(name)
  65. class PipelineConfig(metaclass=PipelineConfigMeta):
  66. _conf = None
  67. _keys = {"IP", "PORT", "APP_KEY", "SECRET_KEY", "CONSOLE_DISPLAY_LOG", "SYSTEM_SETTING"}
  68. _defaults = {
  69. "CONSOLE_DISPLAY_LOG": True,
  70. "SYSTEM_SETTING": {"role": None},
  71. }
  72. class LogPath(object):
  73. @classmethod
  74. def log_directory(cls):
  75. conf = get_default_config()
  76. # log_directory = os.environ.get("FATE_PIPELINE_LOG", "")
  77. log_directory = conf.get("log_directory")
  78. if log_directory:
  79. log_directory = Path(log_directory).resolve()
  80. else:
  81. log_directory = Path(__file__).parent.parent.joinpath("logs")
  82. try:
  83. log_directory.mkdir(parents=True, exist_ok=True)
  84. except Exception as e:
  85. raise RuntimeError(f"can't create log directory for pipeline: {log_directory}") from e
  86. if not Path(log_directory).resolve().is_dir():
  87. raise NotADirectoryError(f"provided log directory {log_directory} is not a directory.")
  88. return log_directory
  89. DEBUG = 'DEBUG.log'
  90. INFO = 'INFO.log'
  91. ERROR = 'ERROR.log'
  92. class LogFormat(object):
  93. SIMPLE = '<green>[{time:HH:mm:ss}]</green><level>{message}</level>'
  94. NORMAL = '<green>{time:YYYY-MM-DD HH:mm:ss}</green> | ' \
  95. '<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>'