summaryrefslogtreecommitdiff
path: root/dots_manager/config.py
blob: 96d32939a12d9f13e2824733ac99595dcdad4807 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import argparse
import os
import json
import logging
from dataclasses import dataclass
from typing import Dict, Any, Optional
from pathlib import Path
from dots_manager.shell import run_shell_command
from dots_manager.utils import merge_dicts
from dots_manager.kawaii_logger import setup_logger


@dataclass(frozen=True)
class Environment:
    platform: str
    system_name: str
    context: Dict[str, Any]
    logger: logging.Logger

    @staticmethod
    def from_argv(args: argparse.Namespace):
        logger = setup_logger(verbose=args.verbose, logger_name="dots_manager")

        platform = args.platform or run_shell_command(
            [str(args.helper_scripts / "platform.sh")], logger
        )
        if not platform:
            raise ValueError("failed to determine platform... ")
        os.environ["PLATFORM"] = platform

        system_name: Optional[str] = args.system_name or run_shell_command(
            [str(args.helper_scripts / "system_name.sh")], logger
        )
        if not system_name:
            raise ValueError("failed to determine system name... ")

        context = load_context(platform, system_name, args.context, logger)
        return Environment(platform, system_name, context, logger)


@dataclass(frozen=True)
class Constants:
    default_target_dir: Path = Path.home()

    dots_repo_dir: Path = Path.home() / Path("dotfiles")
    default_source_dir: Path = dots_repo_dir / Path("dots")
    default_compiled_dir: Path = dots_repo_dir / Path(".compiled_dotfiles")

    default_script_dir: Path = default_source_dir / Path("home/scripts")
    default_context: Path = dots_repo_dir / Path("context.json")

    template_extension: str = ".j2"
    max_workers: int = (os.cpu_count() or 1) * 2

    global_context_key: str = "_global"
    platform_default_context_key: str = "_default"


def load_context(
    platform: str, system_name: str, context_file: Path, logger: logging.Logger
):
    logger.info(f"reading context file: {context_file} ✧*:。゚✧")

    context = json.loads(context_file.read_text())

    global_context = context.get(Constants.global_context_key, {})
    platform_defaults = context.get(platform, {}).get(
        Constants.platform_default_context_key, {}
    )
    defaults = merge_dicts(global_context, platform_defaults)

    system_config = context.get(platform, {}).get(system_name, {})
    if not system_config:
        logger.warning(
            f"could not find context for 'contexts.{platform}.{system_name}' in {context_file.absolute()}"
        )
    return {
        "platform": platform,
        "system_name": system_name,
        **merge_dicts(defaults, system_config),
    }


def parse_arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="૮ ․ ․ ྀིა emprespresso's dotfiles manager 🐧✧˖°"
    )
    parser.add_argument(
        "--verbose",
        "-v",
        action="store_true",
        default=os.environ.get("DEBUG", "false").lower() in ("y", "yes", "true", "t"),
        help="enables verbose logging.",
    )

    parser.add_argument("--compile", action="store_true", help="compile le dotfiles.")
    parser.add_argument(
        "--source",
        type=Path,
        default=Constants.default_source_dir,
        help=f"where to look for templated dotfile packages. default: '{Constants.default_source_dir}'.",
    )
    parser.add_argument(
        "--output",
        type=Path,
        default=Constants.default_compiled_dir,
        help=f"where to store compiled, stowable dotfile packages. default: '{Constants.default_compiled_dir}'.",
    )
    parser.add_argument(
        "--context",
        type=Path,
        default=Constants.default_context,
        help=f"path to contexts, stored as json. default: '{Constants.default_context}'.",
    )
    parser.add_argument(
        "--helper-scripts",
        type=Path,
        default=Constants.default_script_dir,
        help="where to find executable scripts to determine system info (device name, platform, etc.).",
    )
    parser.add_argument(
        "--system-name",
        type=str,
        default=None,
        help="the system's name. when unspecified, inferred via the hostname, or on osx, computername.",
    )
    parser.add_argument(
        "--platform",
        type=str,
        default=None,
        help="the system's os platform (i.e. osx, linux, bsd, windows, etc.). when unspecified, inferred via $OSTYPE.",
    )

    parser.add_argument(
        "--stow", action="store_true", help="action: stow compiled dotfiles"
    )
    parser.add_argument(
        "--clean", action="store_true", help="action: clean stowed dotfiles"
    )
    parser.add_argument(
        "--target",
        type=Path,
        default=Constants.default_target_dir,
        help=f"where the dotfile packages' symlinks will be stowed. default: '{Constants.default_target_dir}'",
    )

    return parser.parse_args()