Skip to content

Commit 81bb081

Browse files
author
Dominik Fleischmann
committed
Split off metadata function to charm.py
Move logic to retrieve metadata of a local charm to charm.py for futrue reusability.
1 parent d2685f6 commit 81bb081

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

juju/application.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import asyncio
1616
import json
1717
import logging
18-
import zipfile
1918
import os
20-
import yaml
21-
from pathlib import Path
2219

2320
from . import model, tag
2421
from .status import derive_status
@@ -27,6 +24,7 @@
2724
from .errors import JujuError
2825
from .bundle import get_charm_series
2926
from .placement import parse as parse_placement
27+
from .charm import get_local_charm_metadata
3028

3129
log = logging.getLogger(__name__)
3230

@@ -719,16 +717,9 @@ async def local_refresh(
719717
default_series = model_config.get("default-series")
720718
if default_series:
721719
series = default_series.value
722-
723720
charm_url = await self.model.add_local_charm_dir(charm_dir, series)
721+
metadata = get_local_charm_metadata(path)
724722
if resources is not None:
725-
if str(path).endswith('.charm'):
726-
with zipfile.ZipFile(path, 'r') as charm_file:
727-
metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader)
728-
else:
729-
entity_path = Path(path)
730-
metadata_path = entity_path / 'metadata.yaml'
731-
metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader)
732723
resources = await self.model.add_local_resources(self.entity_id,
733724
charm_url,
734725
metadata,

juju/charm.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,31 @@
1313
# limitations under the License.
1414

1515
import logging
16-
16+
import zipfile
17+
import yaml
18+
from pathlib import Path
1719
from . import model
1820

1921
log = logging.getLogger(__name__)
2022

2123

24+
def get_local_charm_metadata(path):
25+
"""Retrieve Metadata of a Charm from its path
26+
27+
:patam str path: Path of charm directory or .charm file
28+
29+
:return: Object of charm metadata
30+
"""
31+
if str(path).endswith('.charm'):
32+
with zipfile.ZipFile(path, 'r') as charm_file:
33+
metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader)
34+
else:
35+
entity_path = Path(path)
36+
metadata_path = entity_path / 'metadata.yaml'
37+
metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader)
38+
39+
return metadata
40+
41+
2242
class Charm(model.ModelEntity):
2343
pass

0 commit comments

Comments
 (0)