Skip to content

Commit f6ef0e7

Browse files
committed
Remove redundant code in client codegen in facade.py
As long as we keep a tight manifold at the client.py and provide the facade classes from the correct _client modules, then those facades will use the corresponding lookup functions where they're from. This way we're sure the correct _client* will be used, so no need to import everything from old_clients/_client.py into the _client.py. The main separation happens at the client.py.
1 parent 9bc9370 commit f6ef0e7

3 files changed

Lines changed: 10 additions & 71 deletions

File tree

juju/client/_client.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33

44
from juju.client._definitions import *
55

6-
from juju.client.old_clients import _client2 as _2_9_client2
7-
from juju.client.old_clients import _client1 as _2_9_client1
8-
from juju.client.old_clients import _client3 as _2_9_client3
9-
from juju.client.old_clients import _client4 as _2_9_client4
10-
from juju.client.old_clients import _client5 as _2_9_client5
11-
from juju.client.old_clients import _client8 as _2_9_client8
12-
from juju.client.old_clients import _client7 as _2_9_client7
13-
from juju.client.old_clients import _client9 as _2_9_client9
14-
from juju.client.old_clients import _client10 as _2_9_client10
15-
from juju.client.old_clients import _client6 as _2_9_client6
16-
from juju.client.old_clients import _client12 as _2_9_client12
17-
from juju.client.old_clients import _client11 as _2_9_client11
18-
from juju.client.old_clients import _client13 as _2_9_client13
19-
from juju.client.old_clients import _client15 as _2_9_client15
20-
from juju.client.old_clients import _client16 as _2_9_client16
21-
from juju.client.old_clients import _client17 as _2_9_client17
22-
from juju.client.old_clients import _client18 as _2_9_client18
23-
from juju.client.old_clients import _client14 as _2_9_client14
246

257
from juju.client import _client2, _client1, _client3, _client4, _client5, _client8, _client7, _client9, _client10, _client6, _client12, _client11, _client13, _client15, _client16, _client17, _client18, _client14
268

@@ -47,38 +29,15 @@
4729
}
4830

4931

50-
OLD_CLIENTS = {
51-
"2": _2_9_client2,
52-
"1": _2_9_client1,
53-
"3": _2_9_client3,
54-
"4": _2_9_client4,
55-
"5": _2_9_client5,
56-
"8": _2_9_client8,
57-
"7": _2_9_client7,
58-
"9": _2_9_client9,
59-
"10": _2_9_client10,
60-
"6": _2_9_client6,
61-
"12": _2_9_client12,
62-
"11": _2_9_client11,
63-
"13": _2_9_client13,
64-
"15": _2_9_client15,
65-
"16": _2_9_client16,
66-
"17": _2_9_client17,
67-
"18": _2_9_client18,
68-
"14": _2_9_client14
69-
}
70-
71-
72-
def lookup_facade(name, version, is_2_9=False):
32+
def lookup_facade(name, version):
7333
"""
7434
Given a facade name and version, attempt to pull that facade out
7535
of the correct client<version>.py file.
7636
7737
"""
7838
for _version in range(int(version), 0, -1):
7939
try:
80-
client_directory = OLD_CLIENTS if is_2_9 else CLIENTS
81-
facade = getattr(client_directory[str(_version)], name)
40+
facade = getattr(CLIENTS[str(_version)], name)
8241
return facade
8342
except (KeyError, AttributeError):
8443
continue
@@ -107,8 +66,7 @@ def from_connection(cls, connection):
10766
raise Exception('No facade {} in facades {}'.format(facade_name,
10867
connection.facades))
10968

110-
server_version = connection.info['server-version']
111-
c = lookup_facade(cls.__name__, version, server_version.startswith('2.9'))
69+
c = lookup_facade(cls.__name__, version)
11270
c = c()
11371
c.connect(connection)
11472

juju/client/client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
if not a.startswith('_'):
3636
setattr(c_type, a, getattr(o_type, a))
3737
# This unfortunately needs to be a separate loop
38-
for old_client_version in _client.OLD_CLIENTS.values():
38+
for old_client_version in _2_9_client.CLIENTS.values():
3939
try:
4040
c_type = getattr(old_client_version, o)
4141
except AttributeError:
@@ -56,20 +56,16 @@ def __init__(self):
5656
new_client (bool): True if we're working with juju>3.0
5757
"""
5858
self.new_client = None
59-
self.defs = _definitions
6059
self.client_module = _client
6160

6261
__all__ = list(set(vars().keys()) - {'__module__', '__qualname__'})
6362

6463
def __getattr__(self, item):
65-
if 'Facade' in item:
66-
return getattr(self.client_module, item)
67-
return getattr(self.defs, item)
64+
return getattr(self.client_module, item)
6865

6966
def set_new_client(self, server_version):
7067
self.new_client = server_version.startswith('3.')
7168
if not self.new_client:
72-
self.defs = _2_9_definitions
7369
self.client_module = _2_9_client
7470

7571

juju/client/facade.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@
4444

4545
# Classes and helper functions that we'll write to _client.py
4646
LOOKUP_FACADE = '''
47-
def lookup_facade(name, version, is_2_9=False):
47+
def lookup_facade(name, version):
4848
"""
4949
Given a facade name and version, attempt to pull that facade out
5050
of the correct client<version>.py file.
5151
5252
"""
5353
for _version in range(int(version), 0, -1):
5454
try:
55-
client_directory = OLD_CLIENTS if is_2_9 else CLIENTS
56-
facade = getattr(client_directory[str(_version)], name)
55+
facade = getattr(CLIENTS[str(_version)], name)
5756
return facade
5857
except (KeyError, AttributeError):
5958
continue
@@ -84,8 +83,7 @@ def from_connection(cls, connection):
8483
raise Exception('No facade {} in facades {}'.format(facade_name,
8584
connection.facades))
8685
87-
server_version = connection.info['server-version']
88-
c = lookup_facade(cls.__name__, version, server_version.startswith('2.9'))
86+
c = lookup_facade(cls.__name__, version)
8987
c = c()
9088
c.connect(connection)
9189
@@ -116,14 +114,6 @@ def best_facade_version(cls, connection):
116114
'''
117115

118116

119-
OLD_CLIENTS_TABLE = '''
120-
OLD_CLIENTS = {{
121-
{old_clients}
122-
}}
123-
124-
'''
125-
126-
127117
class KindRegistry(dict):
128118

129119
def register(self, name, version, obj):
@@ -882,18 +872,13 @@ def write_client(captures, options):
882872
f.write(HEADER)
883873
f.write("from juju.client._definitions import *\n\n")
884874
clients = ", ".join("_client{}".format(v) for v in captures)
885-
# from juju.client.old_clients import _client{} as _2_9_client{}
886-
for v in captures:
887-
pre = "from juju.client.old_clients import "
888-
f.write(pre + "_client{num} as _2_9_client{num}\n".format(num=v))
875+
889876
# from juju.client import _client2, _client1, _client3 ...
890877
f.write("\nfrom juju.client import " + clients + "\n\n")
891878
# CLIENTS = { ....
892879
f.write(CLIENT_TABLE.format(clients=",\n ".join(
893880
['"{}": _client{}'.format(v, v) for v in captures])))
894-
# OLD_CLIENTS = { ....
895-
f.write(OLD_CLIENTS_TABLE.format(old_clients=",\n ".join(
896-
['"{}": _2_9_client{}'.format(v, v) for v in captures])))
881+
897882
f.write(LOOKUP_FACADE)
898883
f.write(TYPE_FACTORY)
899884
for key in sorted([k for k in factories.keys() if "Facade" in k]):

0 commit comments

Comments
 (0)