Coverage for klayout_pex/env.py: 93%

43 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-12 13:45 +0000

1#! /usr/bin/env python3 

2# 

3# -------------------------------------------------------------------------------- 

4# SPDX-FileCopyrightText: 2024 Martin Jan Köhler and Harald Pretl 

5# Johannes Kepler University, Institute for Integrated Circuits. 

6# 

7# This file is part of KPEX 

8# (see https://github.com/martinjankoehler/klayout-pex). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22# SPDX-License-Identifier: GPL-3.0-or-later 

23# -------------------------------------------------------------------------------- 

24# 

25 

26from __future__ import annotations 

27 

28from enum import StrEnum 

29import os 

30from typing import * 

31 

32 

33class EnvVar(StrEnum): 

34 FASTCAP_EXE = 'KPEX_FASTCAP_EXE' 

35 FASTERCAP_EXE = 'KPEX_FASTERCAP_EXE' 

36 KLAYOUT_EXE = 'KPEX_KLAYOUT_EXE' 

37 MAGIC_EXE = 'KPEX_MAGIC_EXE' 

38 PDK_PATH = 'PDKPATH' 

39 PDK = 'PDK' 

40 

41 @property 

42 def default_value(self) -> Optional[str]: 

43 match self: 

44 case EnvVar.FASTCAP_EXE: return 'fastcap' 

45 case EnvVar.FASTERCAP_EXE: return 'FasterCap' 

46 case EnvVar.KLAYOUT_EXE: 

47 return 'klayout_app' if os.name == 'nt' \ 

48 else 'klayout' 

49 case EnvVar.MAGIC_EXE: return 'magic' 

50 case EnvVar.PDK_PATH: return None 

51 case EnvVar.PDK: return None 

52 case _: raise NotImplementedError(f"Unexpected env var '{self.name}'") 

53 

54 @classmethod 

55 def help_epilog_table(cls) -> str: 

56 return f""" 

57| Variable | Description | 

58| ------------------ | ----------------------------------------------------------------------------- | 

59| KPEX_FASTCAP_EXE | Path to FastCap2 Executable. Defaults to '{cls.FASTCAP_EXE.default_value}' | 

60| KPEX_FASTERCAP_EXE | Path to FasterCap Executable. Defaults to '{cls.FASTERCAP_EXE.default_value}' | 

61| KPEX_KLAYOUT_EXE | Path to KLayout Executable. Defaults to '{cls.KLAYOUT_EXE.default_value}' | 

62| KPEX_MAGIC_EXE | Path to MAGIC Executable. Defaults to '{cls.MAGIC_EXE.default_value}' | 

63| PDKPATH | Optional (required for default magicrc), e.g. $HOME/.volare | 

64| PDK | Optional (required for default magicrc), (e.g. sky130A) | 

65""" 

66 

67 

68class Env: 

69 def __init__(self, env_dict: Dict[str, Optional[str]]): 

70 self._data = env_dict 

71 

72 @classmethod 

73 def from_os_environ(cls) -> Env: 

74 d = {} 

75 for env_var in EnvVar: 

76 value = os.environ.get(env_var.name, None) 

77 if value is None: 

78 value = env_var.default_value 

79 d[env_var] = value 

80 return Env(d) 

81 

82 def __getitem__(self, env_var: EnvVar) -> Optional[str]: 

83 return self._data[env_var] 

84 

85 def __contains__(self, env_var): 

86 return env_var in self._data 

87 

88 def __repr__(self): 

89 return f"Env({self._data})" 

90