Coverage for ase/db/project.py: 97.87%

47 statements  

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

1from pathlib import Path 

2 

3from ase.db.core import KeyDescription 

4from ase.db.row import row2dct 

5from ase.formula import Formula 

6 

7 

8class DatabaseProject: 

9 """Settings for web view of a database. 

10 

11 For historical reasons called a "Project". 

12 """ 

13 

14 _ase_templates = Path('ase/db/templates') 

15 

16 def __init__( 

17 self, name, title, *, key_descriptions, database, default_columns 

18 ): 

19 self.name = name 

20 self.title = title 

21 self.uid_key = 'id' 

22 

23 # The templates loop over "key descriptions" when they want to 

24 # loop over keys. 

25 # 

26 # Therefore, any key without description will not be rendered. 

27 # Therefore, we need to make dummy key descriptions of everything 

28 # in the database, ensuring that all keys are visible. 

29 

30 all_keys = database.get_all_key_names() 

31 

32 key_descriptions = { 

33 **{key: KeyDescription(key) for key in all_keys}, 

34 **key_descriptions, 

35 } 

36 

37 for key, value in key_descriptions.items(): 

38 assert isinstance(key, str), type(key) 

39 assert isinstance(value, KeyDescription), type(value) 

40 

41 self.key_descriptions = key_descriptions 

42 self.database = database 

43 self.default_columns = default_columns 

44 

45 def get_search_template(self): 

46 return self._ase_templates / 'search.html' 

47 

48 def get_row_template(self): 

49 return self._ase_templates / 'row.html' 

50 

51 def get_table_template(self): 

52 return self._ase_templates / 'table.html' 

53 

54 def handle_query(self, args) -> str: 

55 """Convert request args to ase.db query string.""" 

56 return args['query'] 

57 

58 def row_to_dict(self, row): 

59 """Convert row to dict for use in html template.""" 

60 dct = row2dct(row, self.key_descriptions) 

61 dct['formula'] = Formula(row.formula).convert('abc').format('html') 

62 return dct 

63 

64 def uid_to_row(self, uid): 

65 return self.database.get(f'{self.uid_key}={uid}') 

66 

67 @classmethod 

68 def dummyproject(cls, **kwargs): 

69 class DummyDatabase: 

70 def select(self, *args, **kwargs): 

71 return iter([]) 

72 

73 def get_all_key_names(self): 

74 return set() 

75 

76 _kwargs = dict( 

77 name='test', 

78 title='test', 

79 key_descriptions={}, 

80 database=DummyDatabase(), # XXX 

81 default_columns=[], 

82 ) 

83 _kwargs.update(kwargs) 

84 return cls(**_kwargs) 

85 

86 # If we make this a classmethod, and try to instantiate the class, 

87 # it would fail on subclasses. So we use staticmethod 

88 @staticmethod 

89 def load_db_as_ase_project(name, database): 

90 from ase.db.core import get_key_descriptions 

91 from ase.db.table import all_columns 

92 

93 return DatabaseProject( 

94 name=name, 

95 title=database.metadata.get('title', ''), 

96 key_descriptions=get_key_descriptions(), 

97 database=database, 

98 default_columns=all_columns, 

99 )