1 | from dataclasses import dataclass |
||
2 | from typing import Any, Optional, List |
||
3 | from datetime import datetime, timedelta |
||
4 | from models import ( |
||
5 | from_str, from_union, from_none, from_timedelta, |
||
6 | from_int, from_bool, from_datetime, from_list |
||
7 | ) |
||
8 | from models.user import User |
||
9 | |||
10 | |||
11 | @dataclass |
||
12 | class RaceCategory: |
||
13 | name: str |
||
14 | short_name: str |
||
15 | slug: str |
||
16 | url: str |
||
17 | data_url: str |
||
18 | image: str |
||
19 | |||
20 | @staticmethod |
||
21 | def from_dict(obj: Any) -> 'RaceCategory': |
||
22 | if not isinstance(obj, dict): |
||
23 | return None |
||
24 | name = from_str(obj.get("name")) |
||
25 | short_name = from_str(obj.get("short_name")) |
||
26 | slug = from_str(obj.get("slug")) |
||
27 | url = from_str(obj.get("url")) |
||
28 | data_url = from_str(obj.get("data_url")) |
||
29 | image = from_union([from_str, from_none], obj.get("image")) |
||
30 | return RaceCategory(name, short_name, slug, url, data_url, image) |
||
31 | |||
32 | |||
33 | @dataclass |
||
34 | class Status: |
||
35 | value: str |
||
36 | verbose_value: str |
||
37 | help_text: str |
||
38 | |||
39 | @staticmethod |
||
40 | def from_dict(obj: Any) -> 'Status': |
||
41 | if not isinstance(obj, dict): |
||
42 | return None |
||
43 | value = from_str(obj.get("value")) |
||
44 | verbose_value = from_str(obj.get("verbose_value")) |
||
45 | help_text = from_str(obj.get("help_text")) |
||
46 | return Status(value, verbose_value, help_text) |
||
47 | |||
48 | |||
49 | @dataclass |
||
50 | class Entrant: |
||
51 | user: User |
||
52 | status: Status |
||
53 | has_comment: bool |
||
54 | stream_live: bool |
||
55 | stream_override: bool |
||
56 | actions: List[str] |
||
57 | score: Optional[int] = None |
||
58 | finish_time: Optional[timedelta] = None |
||
59 | finished_at: Optional[datetime] = None |
||
60 | place: Optional[int] = None |
||
61 | place_ordinal: Optional[str] = None |
||
62 | score_change: Optional[int] = None |
||
63 | comment: Optional[str] = None |
||
64 | |||
65 | @staticmethod |
||
66 | def from_dict(obj: Any) -> 'Entrant': |
||
67 | if not isinstance(obj, dict): |
||
68 | return None |
||
69 | user = User.from_dict(obj.get("user")) |
||
70 | status = Status.from_dict(obj.get("status")) |
||
71 | finish_time = from_union( |
||
72 | [from_timedelta, from_none], obj.get("finish_time")) |
||
73 | finished_at = from_union( |
||
74 | [from_datetime, from_none], obj.get("finished_at")) |
||
75 | place = from_union([from_int, from_none], obj.get("place")) |
||
76 | place_ordinal = from_union( |
||
77 | [from_str, from_none], obj.get("place_ordinal")) |
||
78 | score_change = from_union( |
||
79 | [from_int, from_none], obj.get("score_change")) |
||
80 | comment = from_union([from_str, from_none], obj.get("comment")) |
||
81 | has_comment = from_bool(obj.get("has_comment")) |
||
82 | stream_live = from_bool(obj.get("stream_live")) |
||
83 | stream_override = from_bool(obj.get("stream_override")) |
||
84 | actions = from_list(from_str, obj.get("actions")) |
||
85 | score = from_union([from_int, from_none], obj.get("score")) |
||
86 | return Entrant( |
||
87 | user=user, status=status, finish_time=finish_time, |
||
88 | finished_at=finished_at, place=place, place_ordinal=place_ordinal, |
||
89 | score_change=score_change, comment=comment, |
||
90 | has_comment=has_comment, stream_live=stream_live, |
||
91 | stream_override=stream_override, actions=actions, score=score |
||
92 | ) |
||
93 | |||
94 | |||
95 | @dataclass |
||
96 | class Goal: |
||
97 | name: str |
||
98 | custom: bool |
||
99 | |||
100 | @staticmethod |
||
101 | def from_dict(obj: Any) -> 'Goal': |
||
102 | if not isinstance(obj, dict): |
||
103 | return None |
||
104 | name = from_str(obj.get("name")) |
||
105 | custom = from_bool(obj.get("custom")) |
||
106 | return Goal(name, custom) |
||
107 | |||
108 | |||
109 | @dataclass |
||
110 | class Race: |
||
111 | name: str |
||
112 | status: Status |
||
113 | url: str |
||
114 | data_url: str |
||
115 | category: RaceCategory |
||
116 | goal: Goal |
||
117 | info: str |
||
118 | entrants_count: int |
||
119 | entrants_count_inactive: int |
||
120 | opened_at: datetime |
||
121 | time_limit: timedelta |
||
122 | slug: Optional[str] = None |
||
123 | entrants: Optional[List[Entrant]] = None |
||
124 | version: Optional[int] = None |
||
125 | started_at: Optional[datetime] = None |
||
126 | ended_at: Optional[datetime] = None |
||
127 | cancelled_at: Optional[datetime] = None |
||
128 | recorded_by: Optional[User] = None |
||
129 | start_delay: Optional[timedelta] = None |
||
130 | streaming_required: Optional[bool] = None |
||
131 | auto_start: Optional[bool] = None |
||
132 | opened_by: Optional[User] = None |
||
133 | monitors: Optional[List[User]] = None |
||
134 | recordable: Optional[bool] = None |
||
135 | recorded: Optional[bool] = None |
||
136 | allow_comments: Optional[bool] = None |
||
137 | hide_comments: Optional[bool] = None |
||
138 | allow_midrace_chat: Optional[bool] = None |
||
139 | allow_non_entrant_chat: Optional[bool] = None |
||
140 | chat_message_delay: Optional[str] = None |
||
141 | unlisted: Optional[bool] = None |
||
142 | entrants_count_finished: Optional[int] = None |
||
143 | websocket_url: Optional[str] = None |
||
144 | websocket_bot_url: Optional[str] = None |
||
145 | websocket_oauth_url: Optional[str] = None |
||
146 | |||
147 | def get_entrant_by_name(self, full_name: str) -> Entrant: |
||
148 | entrant = next( |
||
149 | (x for x in self.entrants if ( |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
150 | x.user.full_name.lower() == full_name.lower())), None) |
||
151 | return entrant |
||
152 | |||
153 | def get_entrant_by_place(self, place: int) -> Entrant: |
||
154 | entrant = next( |
||
155 | (x for x in self.entrants if x.place == place), None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
156 | return entrant |
||
157 | |||
158 | @staticmethod |
||
159 | def from_dict(obj: Any) -> 'Race': |
||
160 | if not isinstance(obj, dict): |
||
161 | return None |
||
162 | name = from_str(obj.get("name")) |
||
163 | slug = from_str(obj.get("slug")) |
||
164 | status = Status.from_dict(obj.get("status")) |
||
165 | url = from_str(obj.get("url")) |
||
166 | data_url = from_str(obj.get("data_url")) |
||
167 | websocket_url = from_union( |
||
168 | [from_str, from_none], obj.get("websocket_url")) |
||
169 | websocket_bot_url = from_union( |
||
170 | [from_str, from_none], obj.get("websocket_bot_url")) |
||
171 | websocket_oauth_url = from_union( |
||
172 | [from_str, from_none], obj.get("websocket_oauth_url")) |
||
173 | category = RaceCategory.from_dict(obj.get("category")) |
||
174 | goal = Goal.from_dict(obj.get("goal")) |
||
175 | info = from_str(obj.get("info")) |
||
176 | entrants_count = from_int(obj.get("entrants_count")) |
||
177 | entrants_count_inactive = from_int(obj.get("entrants_count_inactive")) |
||
178 | entrants = from_union([lambda x: from_list( |
||
179 | Entrant.from_dict, x), from_none], obj.get("entrants")) |
||
180 | opened_at = from_datetime(obj.get("opened_at")) |
||
181 | start_delay = from_union( |
||
182 | [from_timedelta, from_none], obj.get("start_delay")) |
||
183 | started_at = from_union( |
||
184 | [from_datetime, from_none], (obj.get("started_at"))) |
||
185 | ended_at = from_union([from_datetime, from_none], |
||
186 | (obj.get("ended_at"))) |
||
187 | cancelled_at = from_union( |
||
188 | [from_datetime, from_none], (obj.get("cancelled_at"))) |
||
189 | unlisted = from_union([from_bool, from_none], obj.get("unlisted")) |
||
190 | time_limit = from_timedelta(obj.get("time_limit")) |
||
191 | streaming_required = from_union( |
||
192 | [from_bool, from_none], obj.get("streaming_required")) |
||
193 | auto_start = from_union([from_bool, from_none], obj.get("auto_start")) |
||
194 | opened_by = from_union( |
||
195 | [lambda x: User.from_dict(x), from_none], obj.get("opened_by")) |
||
196 | monitors = from_union([lambda x: from_list( |
||
197 | User.from_dict, x), from_none], obj.get("monitors")) |
||
198 | recordable = from_union([from_bool, from_none], obj.get("recordable")) |
||
199 | recorded = from_union([from_bool, from_none], obj.get("recorded")) |
||
200 | recorded_by = from_union( |
||
201 | [lambda x: User.from_dict(x), from_none], obj.get("recorded_by")) |
||
202 | allow_comments = from_union( |
||
203 | [from_bool, from_none], obj.get("allow_comments")) |
||
204 | hide_comments = from_union( |
||
205 | [from_bool, from_none], obj.get("hide_comments")) |
||
206 | allow_midrace_chat = from_union( |
||
207 | [from_bool, from_none], obj.get("allow_midrace_chat")) |
||
208 | allow_non_entrant_chat = from_union( |
||
209 | [from_bool, from_none], obj.get("allow_non_entrant_chat")) |
||
210 | chat_message_delay = from_union( |
||
211 | [from_str, from_none], obj.get("chat_message_delay")) |
||
212 | version = from_union([from_int, from_none], obj.get("version")) |
||
213 | entrants_count_finished = from_union( |
||
214 | [from_int, from_none], obj.get("entrants_count_finished")) |
||
215 | return Race( |
||
216 | name=name, status=status, url=url, data_url=data_url, |
||
217 | websocket_url=websocket_url, websocket_bot_url=websocket_bot_url, |
||
218 | websocket_oauth_url=websocket_oauth_url, category=category, |
||
219 | goal=goal, info=info, entrants_count=entrants_count, |
||
220 | entrants_count_inactive=entrants_count_inactive, |
||
221 | opened_at=opened_at, time_limit=time_limit, entrants=entrants, |
||
222 | version=version, started_at=started_at, ended_at=ended_at, |
||
223 | cancelled_at=cancelled_at, unlisted=unlisted, |
||
224 | streaming_required=streaming_required, auto_start=auto_start, |
||
225 | opened_by=opened_by, monitors=monitors, recordable=recordable, |
||
226 | recorded=recorded, recorded_by=recorded_by, |
||
227 | allow_comments=allow_comments, hide_comments=hide_comments, |
||
228 | allow_midrace_chat=allow_midrace_chat, |
||
229 | allow_non_entrant_chat=allow_non_entrant_chat, |
||
230 | chat_message_delay=chat_message_delay, start_delay=start_delay, |
||
231 | entrants_count_finished=entrants_count_finished, slug=slug |
||
232 | ) |
||
233 | |||
234 | |||
235 | def race_from_dict(s: Any) -> Race: |
||
236 | return Race.from_dict(s) |
||
237 | |||
238 | |||
239 | def races_from_dict(s: Any) -> List[Race]: |
||
240 | assert isinstance(s, dict) |
||
241 | if not s.get("races"): |
||
242 | return [] |
||
243 | return from_union( |
||
244 | [lambda x: from_list(Race.from_dict, x), from_none], s.get("races") |
||
245 | ) |
||
246 |