Skip to content

Commit 487a2c1

Browse files
Improve readability by extracting code
Only looking over the code did I see that there already exists a charm store and so we should make it easier for others reading this to see that this exists along with charm hub. Extracting the contents into another file shows this. This is a mechanical change that helps with readability, nothing else.
1 parent d6d157f commit 487a2c1

1 file changed

Lines changed: 8 additions & 40 deletions

File tree

juju/model.py

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
from pathlib import Path
1616

1717
import yaml
18-
19-
import theblues.charmstore
20-
import theblues.errors
2118
import websockets
2219

2320
from . import provisioner, tag, utils
2421
from .annotationhelper import _get_annotations, _set_annotations
2522
from .bundle import BundleHandler, get_charm_series
2623
from .charmhub import CharmHub
24+
from .charmstore import CharmStore
2725
from .client import client, connector
2826
from .client.client import ConfigValue, Value
2927
from .client.overrides import Caveat, Macaroon
@@ -461,6 +459,8 @@ def __init__(
461459
self._watch_stopped = asyncio.Event(loop=self._connector.loop)
462460
self._watch_received = asyncio.Event(loop=self._connector.loop)
463461
self._watch_stopped.set()
462+
463+
self._charmhub = CharmHub(self)
464464
self._charmstore = CharmStore(self._connector.loop)
465465

466466
def is_connected(self):
@@ -783,7 +783,11 @@ def charmhub(self):
783783
the charm-hub-url model config.
784784
785785
"""
786-
return CharmHub(self)
786+
return self._charmhub
787+
788+
@property
789+
def charmstore(self):
790+
return self._charmstore
787791

788792
async def get_info(self):
789793
"""Return a client.ModelInfo object for this Model.
@@ -1968,10 +1972,6 @@ def upload_backup(self, archive_path):
19681972
"""
19691973
raise NotImplementedError()
19701974

1971-
@property
1972-
def charmstore(self):
1973-
return self._charmstore
1974-
19751975
async def get_metrics(self, *tags):
19761976
"""Retrieve metrics.
19771977
@@ -2186,38 +2186,6 @@ def _create_consume_args(offer, macaroon, controller_info):
21862186
return arg
21872187

21882188

2189-
class CharmStore:
2190-
"""
2191-
Async wrapper around theblues.charmstore.CharmStore
2192-
"""
2193-
def __init__(self, loop, cs_timeout=20):
2194-
self.loop = loop
2195-
self._cs = theblues.charmstore.CharmStore(timeout=cs_timeout)
2196-
2197-
def __getattr__(self, name):
2198-
"""
2199-
Wrap method calls in coroutines that use run_in_executor to make them
2200-
async.
2201-
"""
2202-
attr = getattr(self._cs, name)
2203-
if not callable(attr):
2204-
wrapper = partial(getattr, self._cs, name)
2205-
setattr(self, name, wrapper)
2206-
else:
2207-
async def coro(*args, **kwargs):
2208-
method = partial(attr, *args, **kwargs)
2209-
for attempt in range(1, 4):
2210-
try:
2211-
return await self.loop.run_in_executor(None, method)
2212-
except theblues.errors.ServerError:
2213-
if attempt == 3:
2214-
raise
2215-
await asyncio.sleep(1, loop=self.loop)
2216-
setattr(self, name, coro)
2217-
wrapper = coro
2218-
return wrapper
2219-
2220-
22212189
class CharmArchiveGenerator:
22222190
"""
22232191
Create a Zip archive of a local charm directory for upload to a controller.

0 commit comments

Comments
 (0)