Coverage for python/felis/visitor.py: 80%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 02:20 -0700

1# This file is part of felis. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

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

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

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

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

21 

22from __future__ import annotations 

23 

24__all__ = ["Visitor"] 

25 

26from abc import ABC, abstractmethod 

27from collections.abc import Iterable, Mapping 

28from typing import Any, Generic, TypeVar 

29 

30_Schema = TypeVar("_Schema") 

31_Table = TypeVar("_Table") 

32_Column = TypeVar("_Column") 

33_PrimaryKey = TypeVar("_PrimaryKey") 

34_Constraint = TypeVar("_Constraint") 

35_Index = TypeVar("_Index") 

36 

37 

38class Visitor(ABC, Generic[_Schema, _Table, _Column, _PrimaryKey, _Constraint, _Index]): 

39 """Abstract interface for visitor classes working on a Felis tree. 

40 

41 Clients will only normally use `visit_schema` method, other methods 

42 defined in this interface should be called by implementation of 

43 `visit_schema` and the methods called from it. 

44 """ 

45 

46 @abstractmethod 

47 def visit_schema(self, schema_obj: Mapping[str, Any]) -> _Schema: 

48 """Visit Felis schema object. 

49 

50 Parameters 

51 ---------- 

52 schema_obj : `Mapping` [ `str`, `Any` ] 

53 Felis object (mapping) representing a schema. 

54 

55 Returns 

56 ------- 

57 schema : `_Schema` 

58 Returns schema representation, type depends on implementation. 

59 """ 

60 raise NotImplementedError() 

61 

62 @abstractmethod 

63 def visit_table(self, table_obj: Mapping[str, Any], schema_obj: Mapping[str, Any]) -> _Table: 

64 """Visit Felis table object, this method is usually called from 

65 `visit_schema`, clients normally do not need to call it directly. 

66 

67 Parameters 

68 ---------- 

69 table_obj : `Mapping` [ `str`, `Any` ] 

70 Felis object (mapping) representing a table. 

71 schema_obj : `Mapping` [ `str`, `Any` ] 

72 Felis object (mapping) representing parent schema. 

73 

74 Returns 

75 ------- 

76 table : `_Table` 

77 Returns table representation, type depends on implementation. 

78 """ 

79 raise NotImplementedError() 

80 

81 @abstractmethod 

82 def visit_column(self, column_obj: Mapping[str, Any], table_obj: Mapping[str, Any]) -> _Column: 

83 """Visit Felis column object, this method is usually called from 

84 `visit_table`, clients normally do not need to call it directly. 

85 

86 Parameters 

87 ---------- 

88 column_obj : `Mapping` [ `str`, `Any` ] 

89 Felis object (mapping) representing a column. 

90 table_obj : `Mapping` [ `str`, `Any` ] 

91 Felis object (mapping) representing parent table. 

92 

93 Returns 

94 ------- 

95 column : `_Column` 

96 Returns column representation, type depends on implementation. 

97 """ 

98 raise NotImplementedError() 

99 

100 @abstractmethod 

101 def visit_primary_key( 

102 self, primary_key_obj: str | Iterable[str], table_obj: Mapping[str, Any] 

103 ) -> _PrimaryKey: 

104 """Visit Felis primary key object, this method is usually called from 

105 `visit_table`, clients normally do not need to call it directly. 

106 

107 Parameters 

108 ---------- 

109 primary_key_obj : `str` or `Mapping` [ `str`, `Any` ] 

110 Felis object (mapping) representing a primary key. 

111 table_obj : `Mapping` [ `str`, `Any` ] 

112 Felis object (mapping) representing parent table. 

113 

114 Returns 

115 ------- 

116 pk : `_PrimaryKey` 

117 Returns primary key representation, type depends on implementation. 

118 """ 

119 raise NotImplementedError() 

120 

121 @abstractmethod 

122 def visit_constraint( 

123 self, constraint_obj: Mapping[str, Any], table_obj: Mapping[str, Any] 

124 ) -> _Constraint: 

125 """Visit Felis constraint object, this method is usually called from 

126 `visit_table`, clients normally do not need to call it directly. 

127 

128 Parameters 

129 ---------- 

130 constraint_obj : `Mapping` [ `str`, `Any` ] 

131 Felis object (mapping) representing a constraint. 

132 table_obj : `Mapping` [ `str`, `Any` ] 

133 Felis object (mapping) representing parent table. 

134 

135 Returns 

136 ------- 

137 constraint : `_Constraint` 

138 Returns primary key representation, type depends on implementation. 

139 """ 

140 raise NotImplementedError() 

141 

142 @abstractmethod 

143 def visit_index(self, index_obj: Mapping[str, Any], table_obj: Mapping[str, Any]) -> _Index: 

144 """Visit Felis index object, this method is usually called from 

145 `visit_table`, clients normally do not need to call it directly. 

146 

147 Parameters 

148 ---------- 

149 index_obj : `Mapping` [ `str`, `Any` ] 

150 Felis object (mapping) representing an index. 

151 table_obj : `Mapping` [ `str`, `Any` ] 

152 Felis object (mapping) representing parent table. 

153 

154 Returns 

155 ------- 

156 index : `_Index` 

157 Returns index representation, type depends on implementation. 

158 """ 

159 raise NotImplementedError()