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

Loading…
Cancelar
Guardar