Skip to content

wake.config.wake_config module #

UnsupportedPlatformError class #

Bases: Exception

The current platform is not supported. Supported platforms are: Linux, macOS, Windows.

Source code in wake/config/wake_config.py
class UnsupportedPlatformError(Exception):
    """
    The current platform is not supported. Supported platforms are: Linux, macOS, Windows.
    """

WakeConfig class #

Wake configuration class. This class is responsible for loading, storing and merging all Wake config options.

Source code in wake/config/wake_config.py
 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
class WakeConfig:
    """
    Wake configuration class. This class is responsible for loading, storing and merging all Wake config options.
    """

    __local_config_path: Path
    __project_root_path: Path
    __global_config_path: Path
    __global_data_path: Path
    __global_cache_path: Path
    __loaded_files: Set[Path]
    __config_raw: Dict[str, Any]
    __config: TopLevelConfig

    def __init__(
        self,
        *_,
        local_config_path: Optional[Union[str, Path]] = None,
        project_root_path: Optional[Union[str, Path]] = None,
    ):
        """
        Initialize the `WakeConfig` class. If `project_root_path` is not provided, the current working directory is used.
        If `local_config_path` is not provided, the `wake.toml` file in the project root directory is used.
        """
        system = platform.system()

        try:
            self.__global_config_path = (
                Path(os.environ["XDG_CONFIG_HOME"]) / "wake" / "config.toml"
            )
        except KeyError:
            if system in {"Linux", "Darwin"}:
                self.__global_config_path = (
                    Path.home() / ".config" / "wake" / "config.toml"
                )
            elif system == "Windows":
                self.__global_config_path = (
                    Path(os.environ["LOCALAPPDATA"]) / "wake" / "config.toml"
                )
            else:
                raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

        try:
            self.__global_data_path = Path(os.environ["XDG_DATA_HOME"]) / "wake"
        except KeyError:
            if system in {"Linux", "Darwin"}:
                self.__global_data_path = Path.home() / ".local" / "share" / "wake"
            elif system == "Windows":
                self.__global_data_path = Path(os.environ["LOCALAPPDATA"]) / "wake"
            else:
                raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

        try:
            self.__global_cache_path = Path(os.environ["XDG_CACHE_HOME"]) / "wake"
        except KeyError:
            if system in {"Linux", "Darwin"}:
                self.__global_cache_path = Path.home() / ".cache" / "wake"
            elif system == "Windows":
                self.__global_cache_path = Path(os.environ["TEMP"]) / "wake"
            else:
                raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

        self.__global_config_path.parent.mkdir(parents=True, exist_ok=True)
        self.__global_data_path.mkdir(parents=True, exist_ok=True)

        if project_root_path is None:
            self.__project_root_path = Path.cwd().resolve()
        else:
            self.__project_root_path = Path(project_root_path).resolve()

        if local_config_path is None:
            self.__local_config_path = self.__project_root_path / "wake.toml"
        else:
            self.__local_config_path = Path(local_config_path).resolve()

        if not self.__project_root_path.is_dir():
            raise ValueError(
                f"Project root path '{self.__project_root_path}' is not a directory."
            )

        self.__loaded_files = set()
        with change_cwd(self.__project_root_path):
            self.__config = TopLevelConfig()
        self.__config_raw = self.__config.dict(by_alias=True)

    def __str__(self) -> str:
        """
        Returns:
            JSON representation of the config.
        """
        return self.__config.json(by_alias=True, exclude_unset=True)

    def __repr__(self) -> str:
        """
        Returns:
            String representation of the config.
        """
        config_dict = reprlib.repr(self.__config_raw)
        return f"{self.__class__.__name__}.fromdict({config_dict}, config_path={repr(self.__local_config_path)}, project_root_path={repr(self.__project_root_path)})"

    def __merge_dicts(self, old: Dict[str, Any], new: Dict[str, Any]) -> None:
        for k, v in new.items():
            if k not in old.keys():
                old[k] = v
            else:
                if isinstance(v, dict):
                    self.__merge_dicts(old[k], new[k])
                else:
                    old[k] = v

    def __modified_keys(self, old: Dict, new: Dict, result: Dict) -> None:
        for k, v in new.items():
            if k not in old.keys():
                result[k] = v
            else:
                if isinstance(v, dict):
                    result[k] = {}
                    self.__modified_keys(old[k], new[k], result[k])
                    if not result[k]:
                        del result[k]
                else:
                    if old[k] != v:
                        result[k] = v
        for k, v in old.items():
            if k not in new.keys():
                result[k] = None

    def __load_file(
        self,
        parent: Optional[Path],
        path: Path,
        new_config: Dict[str, Any],
        graph: nx.DiGraph,
    ) -> None:
        if not path.is_file():
            if parent is None:
                logger.info(f"Config file '{path}' does not exist.")
            else:
                logger.warning(
                    f"Config file '{path}' loaded from '{parent}' does not exist."
                )
        else:
            # change the current working dir so that we can resolve relative paths
            with change_cwd(path.parent):
                with path.open("rb") as f:
                    loaded_config = tomli.load(f)

                graph.add_node(path, config=loaded_config)
                if parent is not None:
                    graph.add_edge(parent, path)

                # detect cyclic subconfigs
                if not nx.is_directed_acyclic_graph(graph):
                    cycles = list(nx.simple_cycles(graph))
                    error = f"Found cyclic config subconfigs:"
                    for no, cycle in enumerate(cycles):
                        error += f"\nCycle {no}:\n"
                        for path in cycle:
                            error += f"{path}\n"
                    raise ValueError(error)

                # validate the loaded config
                parsed_config = TopLevelConfig.parse_obj(loaded_config)

                # rebuild the loaded config from the pydantic model
                # this ensures that all stored paths are absolute
                loaded_config = parsed_config.dict(by_alias=True, exclude_unset=True)

                # merge the original config and the newly loaded config
                self.__merge_dicts(new_config, loaded_config)

                for subconfig_path in parsed_config.subconfigs:
                    self.__load_file(path, subconfig_path, new_config, graph)

    @classmethod
    def fromdict(
        cls,
        config_dict: Dict[str, Any],
        *,
        project_root_path: Optional[Union[str, Path]] = None,
    ) -> "WakeConfig":
        """
        Args:
            config_dict: Dictionary containing the config options.
            project_root_path: Path to the project root directory.

        Returns:
            Instance of the `WakeConfig` class with the provided config options.
        """
        instance = cls(project_root_path=project_root_path)
        with change_cwd(instance.project_root_path):
            parsed_config = TopLevelConfig.parse_obj(config_dict)
        instance.__config_raw = parsed_config.dict(by_alias=True, exclude_unset=True)
        instance.__config = parsed_config
        return instance

    def todict(self) -> Dict[str, Any]:
        """
        Returns:
            Dictionary containing the config options.
        """
        return self.__config_raw

    def update(
        self,
        config_dict: Dict[str, Any],
        deleted_options: Iterable[Tuple[Union[int, str], ...]],
    ) -> Dict:
        """
        Update the config with a new dictionary.

        Args:
            config_dict: Dictionary containing the new config options.
            deleted_options: Iterable of config option paths (in the form of tuples of string keys and integer indices) that should be deleted from the config (reset to their default values).

        Returns:
            Dictionary containing the modified config options.
        """
        with change_cwd(self.project_root_path):
            parsed_config = TopLevelConfig.parse_obj(config_dict)
        parsed_config_raw = parsed_config.dict(by_alias=True, exclude_unset=True)

        original_config = deepcopy(self.__config_raw)
        self.__merge_dicts(self.__config_raw, parsed_config_raw)

        for deleted_option in deleted_options:
            conf = self.__config_raw
            skip = False
            for segment in deleted_option[:-1]:
                if segment in conf:
                    conf = conf[segment]  # type: ignore
                else:
                    skip = True
                    break

            if skip:
                continue
            if isinstance(conf, dict):
                conf.pop(deleted_option[-1], None)  # type: ignore
            elif isinstance(conf, list):
                try:
                    conf.remove(deleted_option[-1])
                except ValueError:
                    pass

        self.__config = TopLevelConfig.parse_obj(self.__config_raw)
        modified_keys = {}
        self.__modified_keys(
            original_config,
            self.__config.dict(by_alias=True, exclude_unset=True),
            modified_keys,
        )
        return modified_keys

    def load_configs(self) -> None:
        """
        Clear any previous config options and load both the global config file `config.toml`
        located in the Wake root directory and the project specific local config file.
        Typically, this is expected to be called right after `WakeConfig` instantiation.
        """
        self.__loaded_files = set()
        with change_cwd(self.__project_root_path):
            self.__config = TopLevelConfig()
        self.__config_raw = self.__config.dict(by_alias=True)

        self.load(self.global_config_path)
        self.load(self.local_config_path)

    def load(self, path: Path) -> None:
        """
        Load config from the provided file path. Any already loaded config options are overridden by the options loaded
        from this file.

        Args:
            path: System path to the config file.
        """
        subconfigs_graph = nx.DiGraph()
        config_raw_copy = deepcopy(self.__config_raw)

        self.__load_file(None, path.resolve(), config_raw_copy, subconfigs_graph)

        config = TopLevelConfig.parse_obj(config_raw_copy)
        self.__config_raw = config_raw_copy
        self.__config = config
        self.__loaded_files.update(
            subconfigs_graph.nodes  # pyright: ignore reportGeneralTypeIssues
        )

    @property
    def loaded_files(self) -> FrozenSet[Path]:
        """
        Returns:
            All loaded config files, including files that were loaded using the `subconfigs` config key.
        """
        return frozenset(self.__loaded_files)

    @property
    def local_config_path(self) -> Path:
        """
        Returns:
            System path to the local config file.
        """
        return self.__local_config_path

    @local_config_path.setter
    def local_config_path(self, path: Path) -> None:
        """
        Args:
            path: New system path to the local config file.
        """
        self.__local_config_path = path

    @property
    def global_config_path(self) -> Path:
        """
        Returns:
            System path to the global config file.
        """
        return self.__global_config_path

    @property
    def global_data_path(self) -> Path:
        """
        Returns:
            System path to the global data directory.
        """
        return self.__global_data_path

    @property
    def global_cache_path(self) -> Path:
        """
        Returns:
            System path to the global cache directory.
        """
        return self.__global_cache_path

    @property
    def project_root_path(self) -> Path:
        """
        Returns:
            System path to the project root directory.
        """
        return self.__project_root_path

    @property
    def min_solidity_version(self) -> SolidityVersion:
        """
        Returns:
            Minimum supported Solidity version.
        """
        return SolidityVersion.fromstring("0.6.2")

    @property
    def max_solidity_version(self) -> SolidityVersion:
        """
        Returns:
            Maximum supported Solidity version.
        """
        return SolidityVersion.fromstring("0.8.24")

    @property
    def detectors(self) -> DetectorsConfig:
        """
        Returns:
            General config options for all detectors.
        """
        return self.__config.detectors

    @property
    def detector(self) -> DetectorConfig:
        """
        Returns:
            Per-detector config options.
        """
        return self.__config.detector

    @property
    def api_keys(self) -> Dict[str, str]:
        """
        Returns:
            API keys for various services.
        """
        return self.__config.api_keys

    @property
    def compiler(self) -> CompilerConfig:
        """
        Returns:
            Compiler config options.
        """
        return self.__config.compiler

    @property
    def generator(self) -> GeneratorConfig:
        """
        Returns:
            Config options for specific to assets generated by Wake.
        """
        return self.__config.generator

    @property
    def lsp(self) -> LspConfig:
        """
        Returns:
            LSP config options.
        """
        return self.__config.lsp

    @property
    def testing(self) -> TestingConfig:
        """
        Returns:
            Testing config options.
        """
        return self.__config.testing

    @property
    def deployment(self) -> DeploymentConfig:
        """
        Returns:
            Deployment config options.
        """
        return self.__config.deployment

    @property
    def general(self) -> GeneralConfig:
        """
        Returns:
            General config options.
        """
        return self.__config.general

    @property
    def printers(self) -> PrintersConfig:
        """
        Returns:
            General config options for all printers.
        """
        return self.__config.printers

    @property
    def printer(self) -> PrinterConfig:
        """
        Returns:
            Per-printer config options.
        """
        return self.__config.printer

api_keys: Dict[str, str] property #

Returns:

Type Description
Dict[str, str]

API keys for various services.

compiler: CompilerConfig property #

Returns:

Type Description
CompilerConfig

Compiler config options.

deployment: DeploymentConfig property #

Returns:

Type Description
DeploymentConfig

Deployment config options.

detector: DetectorConfig property #

Returns:

Type Description
DetectorConfig

Per-detector config options.

detectors: DetectorsConfig property #

Returns:

Type Description
DetectorsConfig

General config options for all detectors.

general: GeneralConfig property #

Returns:

Type Description
GeneralConfig

General config options.

generator: GeneratorConfig property #

Returns:

Type Description
GeneratorConfig

Config options for specific to assets generated by Wake.

global_cache_path: Path property #

Returns:

Type Description
Path

System path to the global cache directory.

global_config_path: Path property #

Returns:

Type Description
Path

System path to the global config file.

global_data_path: Path property #

Returns:

Type Description
Path

System path to the global data directory.

loaded_files: FrozenSet[Path] property #

Returns:

Type Description
FrozenSet[Path]

All loaded config files, including files that were loaded using the subconfigs config key.

local_config_path: Path property writable #

Returns:

Type Description
Path

System path to the local config file.

lsp: LspConfig property #

Returns:

Type Description
LspConfig

LSP config options.

max_solidity_version: SolidityVersion property #

Returns:

Type Description
SolidityVersion

Maximum supported Solidity version.

min_solidity_version: SolidityVersion property #

Returns:

Type Description
SolidityVersion

Minimum supported Solidity version.

printer: PrinterConfig property #

Returns:

Type Description
PrinterConfig

Per-printer config options.

printers: PrintersConfig property #

Returns:

Type Description
PrintersConfig

General config options for all printers.

project_root_path: Path property #

Returns:

Type Description
Path

System path to the project root directory.

testing: TestingConfig property #

Returns:

Type Description
TestingConfig

Testing config options.

__init__(*_, local_config_path=None, project_root_path=None) #

Initialize the WakeConfig class. If project_root_path is not provided, the current working directory is used. If local_config_path is not provided, the wake.toml file in the project root directory is used.

Source code in wake/config/wake_config.py
def __init__(
    self,
    *_,
    local_config_path: Optional[Union[str, Path]] = None,
    project_root_path: Optional[Union[str, Path]] = None,
):
    """
    Initialize the `WakeConfig` class. If `project_root_path` is not provided, the current working directory is used.
    If `local_config_path` is not provided, the `wake.toml` file in the project root directory is used.
    """
    system = platform.system()

    try:
        self.__global_config_path = (
            Path(os.environ["XDG_CONFIG_HOME"]) / "wake" / "config.toml"
        )
    except KeyError:
        if system in {"Linux", "Darwin"}:
            self.__global_config_path = (
                Path.home() / ".config" / "wake" / "config.toml"
            )
        elif system == "Windows":
            self.__global_config_path = (
                Path(os.environ["LOCALAPPDATA"]) / "wake" / "config.toml"
            )
        else:
            raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

    try:
        self.__global_data_path = Path(os.environ["XDG_DATA_HOME"]) / "wake"
    except KeyError:
        if system in {"Linux", "Darwin"}:
            self.__global_data_path = Path.home() / ".local" / "share" / "wake"
        elif system == "Windows":
            self.__global_data_path = Path(os.environ["LOCALAPPDATA"]) / "wake"
        else:
            raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

    try:
        self.__global_cache_path = Path(os.environ["XDG_CACHE_HOME"]) / "wake"
    except KeyError:
        if system in {"Linux", "Darwin"}:
            self.__global_cache_path = Path.home() / ".cache" / "wake"
        elif system == "Windows":
            self.__global_cache_path = Path(os.environ["TEMP"]) / "wake"
        else:
            raise UnsupportedPlatformError(f"Platform `{system}` is not supported.")

    self.__global_config_path.parent.mkdir(parents=True, exist_ok=True)
    self.__global_data_path.mkdir(parents=True, exist_ok=True)

    if project_root_path is None:
        self.__project_root_path = Path.cwd().resolve()
    else:
        self.__project_root_path = Path(project_root_path).resolve()

    if local_config_path is None:
        self.__local_config_path = self.__project_root_path / "wake.toml"
    else:
        self.__local_config_path = Path(local_config_path).resolve()

    if not self.__project_root_path.is_dir():
        raise ValueError(
            f"Project root path '{self.__project_root_path}' is not a directory."
        )

    self.__loaded_files = set()
    with change_cwd(self.__project_root_path):
        self.__config = TopLevelConfig()
    self.__config_raw = self.__config.dict(by_alias=True)

__repr__() #

Returns:

Type Description
str

String representation of the config.

Source code in wake/config/wake_config.py
def __repr__(self) -> str:
    """
    Returns:
        String representation of the config.
    """
    config_dict = reprlib.repr(self.__config_raw)
    return f"{self.__class__.__name__}.fromdict({config_dict}, config_path={repr(self.__local_config_path)}, project_root_path={repr(self.__project_root_path)})"

__str__() #

Returns:

Type Description
str

JSON representation of the config.

Source code in wake/config/wake_config.py
def __str__(self) -> str:
    """
    Returns:
        JSON representation of the config.
    """
    return self.__config.json(by_alias=True, exclude_unset=True)

fromdict(config_dict, *, project_root_path=None) classmethod #

Parameters:

Name Type Description Default
config_dict Dict[str, Any]

Dictionary containing the config options.

required
project_root_path Optional[Union[str, Path]]

Path to the project root directory.

None

Returns:

Type Description
WakeConfig

Instance of the WakeConfig class with the provided config options.

Source code in wake/config/wake_config.py
@classmethod
def fromdict(
    cls,
    config_dict: Dict[str, Any],
    *,
    project_root_path: Optional[Union[str, Path]] = None,
) -> "WakeConfig":
    """
    Args:
        config_dict: Dictionary containing the config options.
        project_root_path: Path to the project root directory.

    Returns:
        Instance of the `WakeConfig` class with the provided config options.
    """
    instance = cls(project_root_path=project_root_path)
    with change_cwd(instance.project_root_path):
        parsed_config = TopLevelConfig.parse_obj(config_dict)
    instance.__config_raw = parsed_config.dict(by_alias=True, exclude_unset=True)
    instance.__config = parsed_config
    return instance

load(path) #

Load config from the provided file path. Any already loaded config options are overridden by the options loaded from this file.

Parameters:

Name Type Description Default
path Path

System path to the config file.

required
Source code in wake/config/wake_config.py
def load(self, path: Path) -> None:
    """
    Load config from the provided file path. Any already loaded config options are overridden by the options loaded
    from this file.

    Args:
        path: System path to the config file.
    """
    subconfigs_graph = nx.DiGraph()
    config_raw_copy = deepcopy(self.__config_raw)

    self.__load_file(None, path.resolve(), config_raw_copy, subconfigs_graph)

    config = TopLevelConfig.parse_obj(config_raw_copy)
    self.__config_raw = config_raw_copy
    self.__config = config
    self.__loaded_files.update(
        subconfigs_graph.nodes  # pyright: ignore reportGeneralTypeIssues
    )

load_configs() #

Clear any previous config options and load both the global config file config.toml located in the Wake root directory and the project specific local config file. Typically, this is expected to be called right after WakeConfig instantiation.

Source code in wake/config/wake_config.py
def load_configs(self) -> None:
    """
    Clear any previous config options and load both the global config file `config.toml`
    located in the Wake root directory and the project specific local config file.
    Typically, this is expected to be called right after `WakeConfig` instantiation.
    """
    self.__loaded_files = set()
    with change_cwd(self.__project_root_path):
        self.__config = TopLevelConfig()
    self.__config_raw = self.__config.dict(by_alias=True)

    self.load(self.global_config_path)
    self.load(self.local_config_path)

todict() #

Returns:

Type Description
Dict[str, Any]

Dictionary containing the config options.

Source code in wake/config/wake_config.py
def todict(self) -> Dict[str, Any]:
    """
    Returns:
        Dictionary containing the config options.
    """
    return self.__config_raw

update(config_dict, deleted_options) #

Update the config with a new dictionary.

Parameters:

Name Type Description Default
config_dict Dict[str, Any]

Dictionary containing the new config options.

required
deleted_options Iterable[Tuple[Union[int, str], ...]]

Iterable of config option paths (in the form of tuples of string keys and integer indices) that should be deleted from the config (reset to their default values).

required

Returns:

Type Description
Dict

Dictionary containing the modified config options.

Source code in wake/config/wake_config.py
def update(
    self,
    config_dict: Dict[str, Any],
    deleted_options: Iterable[Tuple[Union[int, str], ...]],
) -> Dict:
    """
    Update the config with a new dictionary.

    Args:
        config_dict: Dictionary containing the new config options.
        deleted_options: Iterable of config option paths (in the form of tuples of string keys and integer indices) that should be deleted from the config (reset to their default values).

    Returns:
        Dictionary containing the modified config options.
    """
    with change_cwd(self.project_root_path):
        parsed_config = TopLevelConfig.parse_obj(config_dict)
    parsed_config_raw = parsed_config.dict(by_alias=True, exclude_unset=True)

    original_config = deepcopy(self.__config_raw)
    self.__merge_dicts(self.__config_raw, parsed_config_raw)

    for deleted_option in deleted_options:
        conf = self.__config_raw
        skip = False
        for segment in deleted_option[:-1]:
            if segment in conf:
                conf = conf[segment]  # type: ignore
            else:
                skip = True
                break

        if skip:
            continue
        if isinstance(conf, dict):
            conf.pop(deleted_option[-1], None)  # type: ignore
        elif isinstance(conf, list):
            try:
                conf.remove(deleted_option[-1])
            except ValueError:
                pass

    self.__config = TopLevelConfig.parse_obj(self.__config_raw)
    modified_keys = {}
    self.__modified_keys(
        original_config,
        self.__config.dict(by_alias=True, exclude_unset=True),
        modified_keys,
    )
    return modified_keys