Skip to content

Commit be8b2cb

Browse files
committed
fix: redo the local_refresh method signature
1 parent 1a44592 commit be8b2cb

5 files changed

Lines changed: 61 additions & 28 deletions

File tree

juju/application.py

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import hashlib
55
import json
66
import logging
7-
from typing import Dict, List, Union
7+
from typing import Dict, List, Optional, Union
88
from pathlib import Path
99

1010
from typing_extensions import deprecated
1111

1212
from . import jasyncio, model, tag, utils
1313
from .annotationhelper import _get_annotations, _set_annotations
1414
from .bundle import get_charm_series, is_local_charm
15-
from .client import client
15+
from .client import client, _definitions
1616
from .errors import JujuApplicationConfigError, JujuError
1717
from .origin import Channel
1818
from .placement import parse as parse_placement
@@ -706,20 +706,28 @@ async def set_constraints(self, constraints):
706706
return await app_facade.SetConstraints(application=self.name, constraints=constraints)
707707

708708
async def refresh(
709-
self, channel=None, force=False, force_series=False, force_units=False,
710-
path=None, resources=None, revision=None, switch=None):
709+
self,
710+
channel: Optional[str] = None,
711+
force: bool = False,
712+
force_series: bool = False,
713+
force_units: bool = False,
714+
path: Optional[Union[Path, str]] = None,
715+
resources: Optional[Dict[str, str]] = None,
716+
revision: Optional[int] = None,
717+
switch: Optional[str] = None,
718+
):
711719
"""Refresh the charm for this application.
712720
713-
:param str channel: Channel to use when getting the charm from the
721+
:param str|None channel: Channel to use when getting the charm from the
714722
charm store, e.g. 'development'
715723
:param bool force_series: Refresh even if series of deployed
716724
application is not supported by the new charm
717725
:param bool force_units: Refresh all units immediately, even if in
718726
error state
719-
:param str path: Refresh to a charm located at path
720-
:param dict resources: Dictionary of resource name/filepath pairs
721-
:param int revision: Explicit refresh revision
722-
:param str switch: Crossgrade charm url
727+
:param Path|str|None path: Refresh to a charm located at path
728+
:param dict[str,str]|None resources: Dictionary of resource name/filepath pairs
729+
:param int|None revision: Explicit refresh revision
730+
:param str|None switch: URL of a different charm to cross-grade to
723731
724732
"""
725733
if switch is not None and path is not None:
@@ -743,8 +751,17 @@ async def refresh(
743751

744752
current_origin = charm_url_origin_result.charm_origin
745753
if path is not None or (switch is not None and is_local_charm(switch)):
746-
await self.local_refresh(current_origin, force, force_series,
747-
force_units, path or switch, resources)
754+
local_path = path or switch
755+
assert local_path
756+
757+
await self.local_refresh(
758+
charm_origin=current_origin,
759+
force=force,
760+
force_series=force_series,
761+
force_units=force_units,
762+
path=local_path,
763+
resources=resources,
764+
)
748765
return
749766

750767
origin = _refresh_origin(current_origin, channel, revision)
@@ -870,9 +887,15 @@ async def refresh(
870887
upgrade_charm = refresh
871888

872889
async def local_refresh(
873-
self, charm_origin=None, force=False, force_series=False,
874-
force_units=False,
875-
path=None, resources=None):
890+
self,
891+
*,
892+
charm_origin: _definitions.CharmOrigin,
893+
force: bool,
894+
force_series: bool,
895+
force_units: bool,
896+
path: Union[Path, str],
897+
resources: Optional[Dict[str, str]],
898+
):
876899
"""Refresh the charm for this application with a local charm.
877900
878901
:param dict charm_origin: The charm origin of the destination charm
@@ -882,16 +905,16 @@ async def local_refresh(
882905
application is not supported by the new charm
883906
:param bool force_units: Refresh all units immediately, even if in
884907
error state
885-
:param str path: Refresh to a charm located at path
908+
:param Path|str path: Refresh to a charm located at path
886909
:param dict resources: Dictionary of resource name/filepath pairs
887910
888911
"""
889912
app_facade = self._facade()
890913

891914
if isinstance(path, str) and path.startswith("local:"):
892915
path = path[6:]
893-
path = Path(path)
894-
charm_dir = path.expanduser().resolve()
916+
local_path = Path(path)
917+
charm_dir = local_path.expanduser().resolve()
895918
model_config = await self.get_config()
896919

897920
series = (
@@ -907,7 +930,7 @@ async def local_refresh(
907930
if default_series:
908931
series = default_series.value
909932
charm_url = await self.model.add_local_charm_dir(charm_dir, series)
910-
metadata = utils.get_local_charm_metadata(path)
933+
metadata = utils.get_local_charm_metadata(local_path)
911934
if resources is not None:
912935
resources = await self.model.add_local_resources(self.entity_id,
913936
charm_url,
@@ -956,14 +979,17 @@ async def get_metrics(self):
956979
return await self.model.get_metrics(self.tag)
957980

958981

959-
def _refresh_origin(current_origin: client.CharmOrigin, channel=None, revision=None) -> client.CharmOrigin:
960-
if channel is not None:
961-
channel = Channel.parse(channel).normalize()
982+
def _refresh_origin(
983+
current_origin: client.CharmOrigin,
984+
channel: Optional[str] = None,
985+
revision: Optional[int] = None,
986+
) -> client.CharmOrigin:
987+
chan = None if channel is None else Channel.parse(channel).normalize()
962988

963989
return client.CharmOrigin(
964990
source=current_origin.source,
965-
track=channel.track if channel else current_origin.track,
966-
risk=channel.risk if channel else current_origin.risk,
991+
track=chan.track if chan else current_origin.track,
992+
risk=chan.risk if chan else current_origin.risk,
967993
revision=revision if revision is not None else current_origin.revision,
968994
base=current_origin.base,
969995
architecture=current_origin.get('architecture', DEFAULT_ARCHITECTURE),

juju/bundle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def resolve(self, reference):
440440
return reference
441441

442442

443-
def is_local_charm(charm_url):
443+
def is_local_charm(charm_url: str):
444444
return charm_url.startswith('.') or charm_url.startswith('local:') or os.path.isabs(charm_url)
445445

446446

juju/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,8 @@ async def add_local_resources(self, application, entity_url, metadata, resources
20132013
:param str application: the name of the application
20142014
:param client.CharmURL entity_url: url for the charm that we add resources for
20152015
:param [string]string metadata: metadata for the charm that we add resources for
2016-
:param [string] resources: the paths for the local files (or oci-images) to be added as
2017-
local resources
2016+
:param dict[str, str] resources: the paths for the local files (or oci-images) to
2017+
be added as local resources
20182018
20192019
:returns [string]string resource_map that is a map of resources to their assigned
20202020
pendingIDs.

juju/origin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(self, track=None, risk=None):
6969
self.risk = risk
7070

7171
@staticmethod
72-
def parse(s):
72+
def parse(s: str):
7373
"""parse a channel from a given string.
7474
Parse does not take into account branches.
7575

tests/integration/test_application.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,14 @@ async def test_local_refresh():
280280
branch="deadbeef", hash_="hash", id_="id", revision=12,
281281
base=client.Base("20.04", "ubuntu"))
282282

283-
await app.local_refresh(charm_origin=origin, path=charm_path)
283+
await app.local_refresh(
284+
charm_origin=origin,
285+
force=False,
286+
force_series=False,
287+
force_units=False,
288+
path=charm_path,
289+
resources=None,
290+
)
284291

285292
assert origin == client.CharmOrigin(source="local", revision=0,
286293
base=client.Base("20.04", "ubuntu"))

0 commit comments

Comments
 (0)