-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
96 lines (78 loc) · 3.05 KB
/
Copy pathconfig.py
File metadata and controls
96 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Loading in the .env file creating a usable ConfluenceConfig class
from dataclasses import dataclass, field
from typing import Optional
from pathlib import Path
from dotenv import load_dotenv
import os
import logging
# No need to initialize the constructor with @dataclass
@dataclass
class ConfluenceConfig:
# Confluence connection config.
url: str = ""
username: str = ""
api_token: str = ""
space_key: str = ""
parent_page_id: Optional[str] = None
parent_page_title: Optional[str] = None
update_existing: bool = True
def is_valid(self) -> bool:
return all([self.url, self.username, self.api_token, self.space_key])
# uses cls thats why @classmethod
@classmethod
def from_env(cls) -> "ConfluenceConfig":
return cls(
url=os.environ.get("CONFLUENCE_URL", ""),
username=os.environ.get("CONFLUENCE_USERNAME", ""),
api_token=os.environ.get("CONFLUENCE_API_TOKEN", ""),
space_key=os.environ.get("CONFLUENCE_SPACE_KEY", ""),
parent_page_id=os.environ.get("CONFLUENCE_PARENT_PAGE_ID"),
parent_page_title=os.environ.get("CONFLUENCE_PARENT_PAGE_TITLE"),
update_existing=os.environ.get("CONFLUENCE_UPDATE_EXISTING", "true").lower() == "true",
)
@dataclass
class Config:
# App Configuration
# default_factory pattern ensures each instance of the config gets its own indepent objects
confluence: ConfluenceConfig = field(default_factory=ConfluenceConfig)
output_dir: Path = field(default_factory=lambda: Path('./output'))
log_level: str = "INFO"
@classmethod
def load(cls, env_file: Optional[Path] = None) -> "Config":
if env_file is None:
env_file = Path(__file__).parent / ".env"
if env_file.exists():
load_dotenv(env_file)
return cls(
confluence=ConfluenceConfig.from_env(),
output_dir=Path(os.environ.get("OUTPUT_DIR", "./output")),
log_level=os.environ.get("LOG_LEVEL", "INFO"),
)
def setup_logging(level: str = "INFO") -> None:
logging.basicConfig(
level=getattr(logging, level.upper()),
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# Suppress noisy Atlassian library errors (error when creating page that didn't exist yet)
logging.getLogger("atlassian").setLevel(logging.CRITICAL)
ENV_TEMPLATE = """# Meetily Automation Configuration
# Confluence (required)
CONFLUENCE_URL=https://your-domain.atlassian.net
CONFLUENCE_USERNAME=your-email@example.com
CONFLUENCE_API_TOKEN=your-api-token
CONFLUENCE_SPACE_KEY=MEET
# Optional
# CONFLUENCE_PARENT_PAGE_ID=123456
# CONFLUENCE_PARENT_PAGE_TITLE=Meeting Notes
CONFLUENCE_UPDATE_EXISTING=true
# General
LOG_LEVEL=INFO
OUTPUT_DIR=./output
"""
# Creates a .env.example file
def create_env_template(output_path: Optional[Path] = None) -> Path:
if output_path is None:
output_path = Path(__file__).parent / ".env.example"
output_path.write_text(ENV_TEMPLATE)
return output_path