Skip to content

Commit

Permalink
Merge pull request #1174 from dimaqq/avoid-lexical-sorting-versions-r…
Browse files Browse the repository at this point in the history
…ebased

#1174

Cherry-picked from #1168 and simplified.

Rationale Juju micro versions can get larger than 9, e.g. 2.9.51.

When 3.5.10 comes around, we want it to take precedence over 3.5.9 and not get wedged between 3.5.1 and 3.5.2

Keeping the current codegen mode of operation where it starts with the oldest version and whacks some internal state on encountering a latter version.

(We'll deal with that in a separate PR)
  • Loading branch information
jujubot authored Oct 28, 2024
2 parents c58ec9d + b2f118b commit 74ab0f6
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions juju/client/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
from collections import defaultdict
from glob import glob
from pathlib import Path
from typing import Any, Mapping, Sequence, TypeVar
from typing import Any, Dict, List, Mapping, Sequence

import packaging.version
import typing_inspect

from . import codegen
Expand Down Expand Up @@ -150,7 +151,7 @@ def get(self, name):
# Two way mapping
refname = self.schema.referenceName(name)
if refname not in self:
result = TypeVar(refname)
result = typing.TypeVar(refname)
self[refname] = result
self[result] = refname

Expand Down Expand Up @@ -926,11 +927,11 @@ def generate_definitions(schemas):
return definitions


def generate_facades(schemas):
def generate_facades(schemas: Dict[str, List[Schema]]) -> Dict[str, Dict[int, codegen.Capture]]:
captures = defaultdict(codegen.Capture)

# Build the Facade classes
for juju_version in sorted(schemas.keys()):
for juju_version in sorted(schemas.keys(), key=packaging.version.parse):
for schema in schemas[juju_version]:
cls, source = buildFacade(schema)
cls_name = "{}Facade".format(schema.name)
Expand All @@ -953,18 +954,13 @@ def generate_facades(schemas):

def load_schemas(options):
schemas = {}

for p in sorted(glob(options.schema)):
if 'latest' in p:
juju_version = 'latest'
else:
try:
juju_version = re.search(JUJU_VERSION, p).group()
except AttributeError:
print("Cannot extract a juju version from {}".format(p))
print("Schemas must include a juju version in the filename")
raise SystemExit(1)

try:
juju_version = re.search(JUJU_VERSION, p).group()
except AttributeError:
print("Cannot extract a juju version from {}".format(p))
print("Schemas must include a juju version in the filename")
raise SystemExit(1)
new_schemas = json.loads(Path(p).read_text("utf-8"))
schemas[juju_version] = [Schema(s) for s in new_schemas]
return schemas
Expand Down

0 comments on commit 74ab0f6

Please sign in to comment.