Skip to content

Commit 376704a

Browse files
committed
Reformat files
1 parent 60ba128 commit 376704a

File tree

3 files changed

+527
-551
lines changed

3 files changed

+527
-551
lines changed

serveradmin/serverdb/query_materializer.py

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
ServertypeAttribute,
1919
Server,
2020
ServerAttribute,
21-
ServerRelationAttribute, ServerInetAttribute,
21+
ServerRelationAttribute,
22+
ServerInetAttribute,
2223
)
2324

2425
logger = logging.getLogger(__package__)
@@ -30,19 +31,16 @@ def __init__(self, servers, joined_attributes, order_by_attributes=[]):
3031
self._joined_attributes = joined_attributes
3132
self._order_by_attributes = order_by_attributes
3233
# XXX: Optimize this query out
33-
self._servertype_lookup = {
34-
servertype.servertype_id: servertype
35-
for servertype in Servertype.objects.all()
36-
}
34+
self._servertype_lookup = {servertype.servertype_id: servertype for servertype in Servertype.objects.all()}
3735

3836
self._server_attributes = {}
3937
servers_by_type = {}
4038
for server in self._servers:
4139
self._server_attributes[server] = {
42-
Attribute.specials["object_id"]: server.server_id,
43-
Attribute.specials["hostname"]: server.hostname,
44-
Attribute.specials["intern_ip"]: server.intern_ip,
45-
Attribute.specials["servertype"]: server.servertype_id,
40+
Attribute.specials['object_id']: server.server_id,
41+
Attribute.specials['hostname']: server.hostname,
42+
Attribute.specials['intern_ip']: server.intern_ip,
43+
Attribute.specials['servertype']: server.servertype_id,
4644
}
4745
servers_by_type.setdefault(server.servertype_id, []).append(server)
4846

@@ -56,18 +54,12 @@ def __iter__(self):
5654
if self._order_by_attributes:
5755

5856
def order_by_key(key):
59-
return tuple(
60-
self._get_order_by_attribute(key, a)
61-
for a in self._order_by_attributes
62-
)
57+
return tuple(self._get_order_by_attribute(key, a) for a in self._order_by_attributes)
6358

6459
servers = sorted(servers, key=order_by_key)
6560

6661
join_results = self._get_join_results()
67-
return (
68-
DatasetObject(self._get_attributes(s, join_results), s.server_id)
69-
for s in servers
70-
)
62+
return (DatasetObject(self._get_attributes(s, join_results), s.server_id) for s in servers)
7163

7264
def _select_attributes(self, servertype_ids):
7365
self._attributes_by_type = {}
@@ -83,9 +75,7 @@ def _select_attributes(self, servertype_ids):
8375

8476
def _select_servertype_attribute(self, attribute, sa):
8577
self._attributes_by_type.setdefault(attribute.type, set()).add(attribute)
86-
self._servertype_ids_by_attribute.setdefault(attribute, []).append(
87-
sa.servertype_id
88-
)
78+
self._servertype_ids_by_attribute.setdefault(attribute, []).append(sa.servertype_id)
8979

9080
related_via_attribute_id = sa.related_via_attribute_id
9181
if related_via_attribute_id:
@@ -103,7 +93,7 @@ def _select_servertype_attribute(self, attribute, sa):
10393
servertype_id=sa.servertype_id,
10494
attribute_id=related_via_attribute_id,
10595
)
106-
.prefetch_related("attribute")
96+
.prefetch_related('attribute')
10797
.get()
10898
)
10999
self._select_servertype_attribute(sa.attribute, sa)
@@ -118,37 +108,29 @@ def _initialize_attributes(self, servers_by_type):
118108
def _add_attributes(self, servers_by_type):
119109
"""Add the attributes to the results"""
120110
for key, attributes in self._attributes_by_type.items():
121-
if key == "supernet":
111+
if key == 'supernet':
122112
for attribute in attributes:
123113
self._add_supernet_attribute(
124114
attribute,
125-
(
126-
s
127-
for st in self._servertype_ids_by_attribute[attribute]
128-
for s in servers_by_type[st]
129-
),
115+
(s for st in self._servertype_ids_by_attribute[attribute] for s in servers_by_type[st]),
130116
)
131-
elif key == "domain":
117+
elif key == 'domain':
132118
for attribute in attributes:
133119
self._add_domain_attribute(
134120
attribute,
135-
[
136-
s
137-
for st in self._servertype_ids_by_attribute[attribute]
138-
for s in servers_by_type[st]
139-
],
121+
[s for st in self._servertype_ids_by_attribute[attribute] for s in servers_by_type[st]],
140122
)
141-
elif key == "reverse":
123+
elif key == 'reverse':
142124
reversed_attributes = {a.reversed_attribute_id: a for a in attributes}
143125
for sa in (
144126
ServerRelationAttribute.objects.filter(
145127
value_id__in=self._server_attributes.keys(),
146128
attribute_id__in=reversed_attributes.keys(),
147129
)
148-
.prefetch_related("server")
130+
.prefetch_related('server')
149131
.defer(
150-
"server__intern_ip",
151-
"server__servertype",
132+
'server__intern_ip',
133+
'server__servertype',
152134
)
153135
):
154136
self._add_attribute_value(
@@ -164,10 +146,10 @@ def _add_attributes(self, servers_by_type):
164146
server__in=self._server_attributes.keys(),
165147
attribute__in=attributes,
166148
)
167-
.prefetch_related("server")
149+
.prefetch_related('server')
168150
.defer(
169-
"server__intern_ip",
170-
"server__servertype",
151+
'server__intern_ip',
152+
'server__servertype',
171153
)
172154
):
173155
self._add_attribute_value(
@@ -181,7 +163,7 @@ def _add_related_attributes(self, servers_by_type):
181163
self._add_related_attribute(attribute, sa, servers_by_type)
182164

183165
def _add_domain_attribute(self, attribute, servers):
184-
domain_names = {s.hostname.split(".", 1)[-1] for s in servers}
166+
domain_names = {s.hostname.split('.', 1)[-1] for s in servers}
185167
domain_lookup = {
186168
domain.hostname: domain
187169
for domain in Server.objects.filter(
@@ -191,9 +173,7 @@ def _add_domain_attribute(self, attribute, servers):
191173
}
192174

193175
for server in servers:
194-
self._server_attributes[server][attribute] = domain_lookup.get(
195-
server.hostname.split(".", 1)[-1]
196-
)
176+
self._server_attributes[server][attribute] = domain_lookup.get(server.hostname.split('.', 1)[-1])
197177

198178
def _add_supernet_attribute(self, attribute: Attribute, servers_in):
199179
"""Calculate the supernet attribute of given servers
@@ -236,15 +216,15 @@ def _add_supernet_attribute(self, attribute: Attribute, servers_in):
236216
supernets = Server.objects.raw(
237217
q,
238218
{
239-
"target_servertype": attribute.target_servertype_id,
240-
"address_family": attribute.inet_address_family,
241-
"hosts": list(servers_by_id.keys()),
219+
'target_servertype': attribute.target_servertype_id,
220+
'address_family': attribute.inet_address_family,
221+
'hosts': list(servers_by_id.keys()),
242222
},
243223
translations={
244-
"net.server_id": "server_id",
245-
"net.hostname": "hostname",
246-
"net.intern_ip": "intern_ip",
247-
"net.servertype_id": "servertype_id",
224+
'net.server_id': 'server_id',
225+
'net.hostname': 'hostname',
226+
'net.intern_ip': 'intern_ip',
227+
'net.servertype_id': 'servertype_id',
248228
},
249229
)
250230
for cur_supernet in supernets:
@@ -254,8 +234,8 @@ def _add_supernet_attribute(self, attribute: Attribute, servers_in):
254234
# TODO: Raise an exception once all data is cleaned up and conflicting
255235
# AF-unaware attributes are removed.
256236
logger.warning(
257-
f"Conflicting supernet {attribute} for {cur_server.hostname}: "
258-
f"{prev_supernet.host_attr_name}->{prev_supernet} vs {cur_supernet.host_attr_name}->{cur_supernet}"
237+
f'Conflicting supernet {attribute} for {cur_server.hostname}: '
238+
f'{prev_supernet.host_attr_name}->{prev_supernet} vs {cur_supernet.host_attr_name}->{cur_supernet}'
259239
)
260240
self._server_attributes[cur_server][attribute] = cur_supernet
261241

@@ -281,10 +261,10 @@ def _add_related_attribute(self, attribute, servertype_attribute, servers_by_typ
281261
server__hostname__in=servers_by_related.keys(),
282262
attribute=attribute,
283263
)
284-
.prefetch_related("server")
264+
.prefetch_related('server')
285265
.defer(
286-
"server__intern_ip",
287-
"server__servertype",
266+
'server__intern_ip',
267+
'server__servertype',
288268
)
289269
):
290270
for target in servers_by_related[sa.server]:
@@ -327,28 +307,24 @@ def _get_attributes(self, server, join_results): # NOQA: C901
327307
if attribute not in self._joined_attributes:
328308
continue
329309

330-
if attribute.type == "inet":
310+
if attribute.type == 'inet':
331311
if value is None:
332312
yield attribute.attribute_id, None
333313
else:
334-
if servertype.ip_addr_type in ("host", "loadbalancer"):
314+
if servertype.ip_addr_type in ('host', 'loadbalancer'):
335315
yield attribute.attribute_id, value.ip
336316
else:
337-
assert servertype.ip_addr_type == "network"
317+
assert servertype.ip_addr_type == 'network'
338318
yield attribute.attribute_id, value.network
339319
elif value is None:
340320
yield attribute.attribute_id, None
341321
elif attribute in join_results:
342322
if attribute.multi:
343-
yield attribute.attribute_id, [
344-
join_results[attribute][v] for v in value
345-
]
323+
yield attribute.attribute_id, [join_results[attribute][v] for v in value]
346324
else:
347325
yield (attribute.attribute_id, join_results[attribute][value])
348326
elif attribute.multi:
349-
yield attribute.attribute_id, {
350-
v.hostname if isinstance(v, Server) else v for v in value
351-
}
327+
yield attribute.attribute_id, {v.hostname if isinstance(v, Server) else v for v in value}
352328
elif isinstance(value, Server):
353329
yield attribute.attribute_id, value.hostname
354330
else:
@@ -396,7 +372,7 @@ def get_default_attribute_values(servertype_id):
396372
attribute_values = {}
397373

398374
for attribute_id in Attribute.specials:
399-
if attribute_id == "servertype":
375+
if attribute_id == 'servertype':
400376
value = servertype_id
401377
else:
402378
value = None

0 commit comments

Comments
 (0)