Explorar el Código

Annotating storage.py

master
Rocketsoup hace 4 años
padre
commit
0e4a15159b
Se han modificado 1 ficheros con 19 adiciones y 18 borrados
  1. 19
    18
      rocketbot/storage.py

+ 19
- 18
rocketbot/storage.py Ver fichero

@@ -3,6 +3,7 @@ Handles storage of persisted and non-persisted data for the bot.
3 3
 """
4 4
 import json
5 5
 from os.path import exists
6
+from typing import Any, Optional
6 7
 from discord import Guild
7 8
 
8 9
 from config import CONFIG
@@ -22,29 +23,29 @@ class Storage:
22 23
 
23 24
 	# -- Transient state management -----------------------------------------
24 25
 
25
-	__guild_id_to_state = {}
26
+	__guild_id_to_state: dict[int, dict[str, Any]] = {}
26 27
 
27 28
 	@classmethod
28
-	def get_state(cls, guild: Guild) -> dict:
29
+	def get_state(cls, guild: Guild) -> dict[str, Any]:
29 30
 		"""
30 31
 		Returns transient state for the given guild. This state is not preserved
31 32
 		if the bot is restarted.
32 33
 		"""
33
-		state: dict = cls.__guild_id_to_state.get(guild.id)
34
+		state: dict[str, Any] = cls.__guild_id_to_state.get(guild.id)
34 35
 		if state is None:
35 36
 			state = {}
36 37
 			cls.__guild_id_to_state[guild.id] = state
37 38
 		return state
38 39
 
39 40
 	@classmethod
40
-	def get_state_value(cls, guild: Guild, key: str):
41
+	def get_state_value(cls, guild: Guild, key: str) -> Optional[Any]:
41 42
 		"""
42 43
 		Returns a state value for the given guild and key, or `None` if not set.
43 44
 		"""
44 45
 		return cls.get_state(guild).get(key)
45 46
 
46 47
 	@classmethod
47
-	def set_state_value(cls, guild: Guild, key: str, value) -> None:
48
+	def set_state_value(cls, guild: Guild, key: str, value: Optional[Any]) -> None:
48 49
 		"""
49 50
 		Updates a transient value associated with the given guild and key name.
50 51
 		A value of `None` removes any previous value for that key.
@@ -52,7 +53,7 @@ class Storage:
52 53
 		cls.set_state_values(guild, { key: value })
53 54
 
54 55
 	@classmethod
55
-	def set_state_values(cls, guild: Guild, values: dict) -> None:
56
+	def set_state_values(cls, guild: Guild, values: Optional[dict[str, Optional[Any]]]) -> None:
56 57
 		"""
57 58
 		Merges in a set of key-value pairs into the transient state for the
58 59
 		given guild. Any pairs with a value of `None` will be removed from the
@@ -60,7 +61,7 @@ class Storage:
60 61
 		"""
61 62
 		if values is None or len(values) == 0:
62 63
 			return
63
-		state: dict = cls.get_state(guild)
64
+		state: dict[str, Any] = cls.get_state(guild)
64 65
 		for key, value in values.items():
65 66
 			if value is None:
66 67
 				del state[key]
@@ -72,14 +73,14 @@ class Storage:
72 73
 	# -- Persisted configuration management ---------------------------------
73 74
 
74 75
 	# discord.Guild.id -> dict
75
-	__guild_id_to_config = {}
76
+	__guild_id_to_config: dict[int, dict[str, Any]] = {}
76 77
 
77 78
 	@classmethod
78
-	def get_config(cls, guild: Guild) -> dict:
79
+	def get_config(cls, guild: Guild) -> dict[str, Any]:
79 80
 		"""
80 81
 		Returns all persisted configuration for the given guild.
81 82
 		"""
82
-		config: dict = cls.__guild_id_to_config.get(guild.id)
83
+		config: dict[str, Any] = cls.__guild_id_to_config.get(guild.id)
83 84
 		if config is not None:
84 85
 			# Already in memory
85 86
 			return config
@@ -93,7 +94,7 @@ class Storage:
93 94
 		return config
94 95
 
95 96
 	@classmethod
96
-	def get_config_value(cls, guild: Guild, key: str):
97
+	def get_config_value(cls, guild: Guild, key: str) -> Optional[Any]:
97 98
 		"""
98 99
 		Returns a persisted guild config value stored under the given key.
99 100
 		Returns `None` if not present.
@@ -101,7 +102,7 @@ class Storage:
101 102
 		return cls.get_config(guild).get(key)
102 103
 
103 104
 	@classmethod
104
-	def set_config_value(cls, guild: Guild, key: str, value) -> None:
105
+	def set_config_value(cls, guild: Guild, key: str, value: Optional[Any]) -> None:
105 106
 		"""
106 107
 		Adds/updates the given key-value pair to the persisted config for the
107 108
 		given Guild. If `value` is `None` the key will be removed from persisted
@@ -110,7 +111,7 @@ class Storage:
110 111
 		cls.set_config_values(guild, { key: value })
111 112
 
112 113
 	@classmethod
113
-	def set_config_values(cls, guild: Guild, values: dict) -> None:
114
+	def set_config_values(cls, guild: Guild, values: Optional[dict[str, Optional[Any]]]) -> None:
114 115
 		"""
115 116
 		Merges the given `values` dict with the saved config for the given guild
116 117
 		and writes it to disk. `values` must be JSON-encodable or a `ValueError`
@@ -119,7 +120,7 @@ class Storage:
119 120
 		"""
120 121
 		if values is None or len(values) == 0:
121 122
 			return
122
-		config: dict = cls.get_config(guild)
123
+		config: dict[str, Any] = cls.get_config(guild)
123 124
 		try:
124 125
 			json.dumps(values)
125 126
 		except Exception as e:
@@ -132,7 +133,7 @@ class Storage:
132 133
 		cls.__write_guild_config(guild, config)
133 134
 
134 135
 	@classmethod
135
-	def __write_guild_config(cls, guild: Guild, config: dict) -> None:
136
+	def __write_guild_config(cls, guild: Guild, config: dict[str, Any]) -> None:
136 137
 		"""
137 138
 		Saves config for a guild to a JSON file on disk.
138 139
 		"""
@@ -146,7 +147,7 @@ class Storage:
146 147
 		cls.__trace('State saved')
147 148
 
148 149
 	@classmethod
149
-	def __read_guild_config(cls, guild: Guild) -> dict:
150
+	def __read_guild_config(cls, guild: Guild) -> Optional[dict[str, Any]]:
150 151
 		"""
151 152
 		Loads config for a guild from a JSON file on disk, or `None` if not
152 153
 		found.
@@ -171,6 +172,6 @@ class Storage:
171 172
 		return f'{path}guild_{guild.id}.json'
172 173
 
173 174
 	@classmethod
174
-	def __trace(cls, message: str) -> None:
175
-		# print(f'{cls.__name__}: {message}')
175
+	def __trace(cls, message: Any) -> None:
176
+		# print(f'{cls.__name__}: {str(message)}')
176 177
 		pass

Loading…
Cancelar
Guardar