Coverage for ase/gui/defaults.py: 81.48%

54 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1"""This is a module to handle generic GUI configuration ... 

2 

3... from the global ASE configuration in ase.config or an ~/.ase/gui.py 

4configuration file, for backwards compatibility. The configuration is 

5imported when opening ASE-GUI and can then be modified at runtime, if 

6necessary. 

7""" 

8 

9import ast 

10import runpy 

11import warnings 

12from pathlib import Path 

13 

14from ase.config import ASE_CONFIG_FILE, Config, cfg 

15 

16gui_default_settings = { 

17 'default_graphs_string': 'i, e - E[-1]', 

18 'foreground_color': '#000000', # hex or named color 

19 'background_color': '#ffffff', # hex or named color 

20 'covalent_radii': None, # Dictionary of {atomic number/symbol: radius} 

21 'radii_scale': 0.89, 

22 'force_vector_scale': 1.0, 

23 'velocity_vector_scale': 1.0, 

24 'magmom_vector_scale': 1.0, 

25 'show_unit_cell': True, 

26 'show_axes': True, 

27 'show_bonds': False, 

28 # Performance options for large systems. Both are no-ops for small 

29 # structures; see View._draw_indices(). 

30 # Draw fewer objects while dragging/zooming: 

31 'interactive_level_of_detail': True, 

32 'occlusion_culling': True, # skip atoms fully obscured by nearer atoms 

33 'shift_cell': False, 

34 'swap_mouse': False, 

35 'movie_rate': 'auto', # float or 'auto' 

36 'movie_skip': 'auto', # int or 'auto' 

37 'movie_rock': False, 

38} # end (Marker for a literalinclude in the docs. Please do not remove) 

39 

40 

41class GUIConfig(Config): 

42 """For interfacing with the GUI configuration in a backwards and 

43 hopefully forwards compatible way. Designed to be operated like 

44 a ConfigParser object. 

45 """ 

46 

47 def __init__(self): 

48 super().__init__() 

49 

50 @property 

51 def _parser(self): 

52 if self.parser.has_section('gui'): 

53 return self.parser['gui'] 

54 else: 

55 return self.parser 

56 

57 def __iter__(self): 

58 yield from self._parser 

59 

60 def __getitem__(self, key): 

61 return self._parser[key] 

62 

63 def __len__(self): 

64 return len(self._parser) 

65 

66 def get(self, option, fallback=None): 

67 return self._parser.get(option, fallback=fallback) 

68 

69 def getint(self, option, fallback=None): 

70 return self._parser.getint(option, fallback=fallback) 

71 

72 def getfloat(self, option, fallback=None): 

73 return self._parser.getfloat(option, fallback=fallback) 

74 

75 def getboolean(self, option, fallback=None): 

76 return self._parser.getboolean(option, fallback=fallback) 

77 

78 def getliteral(self, option, fallback=None): 

79 value = self.get(option, fallback) 

80 if value.lower() == 'auto': 

81 return 'auto' 

82 return ast.literal_eval(value) 

83 

84 def set(self, option, value): 

85 if not self.parser.has_section('gui'): 

86 self.parser.add_section('gui') 

87 self.parser.set('gui', option, str(value)) 

88 

89 def read_dict(self, dct: dict) -> None: 

90 self.parser.read_dict({'gui': {k: str(dct[k]) for k in dct}}) 

91 

92 def read_ase_config(self, config: Config) -> None: 

93 self.parser.read_dict(config.as_dict()) 

94 

95 

96def read_defaults( 

97 user_cfg: GUIConfig | Config | dict | None = None, 

98) -> GUIConfig: 

99 """Retrieves configuration from ASE configuration files (see ase.config). 

100 Returns a GUIConfig object from which one can get the settings. If user_cfg 

101 is specified, parses and adapts the provided configuration instead, ignoring 

102 the global config. 

103 """ 

104 old_configfile = Path.home() / '.ase/gui.py' 

105 if old_configfile.exists(): 

106 warnings.warn( 

107 'Found GUI configuration file gui.py which is deprecated' 

108 f'. Use {ASE_CONFIG_FILE} instead.', 

109 FutureWarning, 

110 ) 

111 runpy.run_path( 

112 str(old_configfile), 

113 init_globals={'gui_default_settings': gui_default_settings}, 

114 ) 

115 

116 config = GUIConfig() 

117 config.read_dict(gui_default_settings) 

118 

119 if user_cfg is None: 

120 config.read_ase_config(cfg) 

121 else: 

122 if isinstance(user_cfg, dict): 

123 config.read_dict(user_cfg) 

124 else: 

125 config.read_ase_config(user_cfg) 

126 

127 return config