Skip to content

Commit

Permalink
Use more concrete types
Browse files Browse the repository at this point in the history
  • Loading branch information
sosna committed May 3, 2024
1 parent 0225e32 commit 374507e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 33 deletions.
11 changes: 6 additions & 5 deletions src/pysdmx/fmr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
from pysdmx.fmr.reader import Deserializer
from pysdmx.fmr.sdmx import deserializers as sdmx_deserializers
from pysdmx.model import (
Agency,
CategoryScheme,
Codelist,
ConceptScheme,
DataflowInfo,
DataProvider,
Hierarchy,
HierarchyAssociation,
MetadataReport,
MultiRepresentationMap,
Organisation,
RepresentationMap,
Schema,
StructureMap,
Expand Down Expand Up @@ -247,7 +248,7 @@ def __get_hierarchies_for_pra(
out = self.__fetch(super()._url("ha_pra", agency, pra, version))
return super()._out(out, self.deser.hier_assoc)

def get_agencies(self, agency: str) -> Sequence[Organisation]:
def get_agencies(self, agency: str) -> Sequence[Agency]:
"""Get the list of **sub-agencies** for the supplied agency.
Args:
Expand All @@ -264,7 +265,7 @@ def get_providers(
self,
agency: str,
with_flows: bool = False,
) -> Sequence[Organisation]:
) -> Sequence[DataProvider]:
"""Get the list of **data providers** for the supplied agency.
Args:
Expand Down Expand Up @@ -579,7 +580,7 @@ async def __get_hierarchies_for_pra(
out = await self.__fetch(super()._url("ha_pra", agency, pra, version))
return super()._out(out, self.deser.hier_assoc)

async def get_agencies(self, agency: str) -> Sequence[Organisation]:
async def get_agencies(self, agency: str) -> Sequence[Agency]:
"""Get the list of **sub-agencies** for the supplied agency.
Args:
Expand All @@ -594,7 +595,7 @@ async def get_agencies(self, agency: str) -> Sequence[Organisation]:

async def get_providers(
self, agency: str, with_flows: bool = False
) -> Sequence[Organisation]:
) -> Sequence[DataProvider]:
"""Get the list of **data providers** for the supplied agency.
Args:
Expand Down
12 changes: 9 additions & 3 deletions src/pysdmx/fmr/fusion/dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from pysdmx.fmr.fusion.core import FusionString
from pysdmx.fmr.fusion.org import FusionProviderScheme
from pysdmx.model import Components, DataflowInfo, DataflowRef, Organisation
from pysdmx.model import (
Agency,
Components,
DataflowInfo,
DataflowRef,
DataProvider,
)


class FusionDataflowRef(Struct, frozen=True, rename={"agency": "agencyId"}):
Expand Down Expand Up @@ -64,7 +70,7 @@ def to_model(
self, components: Components, agency: str, id_: str, version: str
) -> DataflowInfo:
"""Returns the requested dataflow details."""
prvs: List[Organisation] = []
prvs: List[DataProvider] = []
for dps in self.DataProviderScheme:
prvs.extend(dps.to_model([]))
df = list(
Expand All @@ -76,7 +82,7 @@ def to_model(
return DataflowInfo(
df.id,
components,
Organisation(df.agency),
Agency(df.agency),
df.names[0].value,
df.descriptions[0].value if df.descriptions else None,
df.version,
Expand Down
45 changes: 33 additions & 12 deletions src/pysdmx/fmr/fusion/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from msgspec import Struct

from pysdmx.fmr.fusion.core import FusionString
from pysdmx.model import Contact, DataflowRef, Organisation
from pysdmx.model import Agency, Contact, DataflowRef, DataProvider
from pysdmx.util import parse_urn


Expand Down Expand Up @@ -40,25 +40,46 @@ def to_model(self) -> Contact:
)


class FusionOrg(Struct, frozen=True):
class FusionAgency(Struct, frozen=True):
"""Fusion-JSON payload for an organisation."""

id: str
names: Sequence[FusionString]
descriptions: Optional[Sequence[FusionString]] = None
contacts: Sequence[FusionContact] = ()

def to_model(self, owner: Optional[str] = None) -> Organisation:
def to_model(self, owner: Optional[str] = None) -> Agency:
"""Converts a FusionOrg to a standard Organisation."""
d = self.descriptions[0].value if self.descriptions else None
c = [c.to_model() for c in self.contacts]
oid = f"{owner}.{self.id}" if owner and owner != "SDMX" else self.id
if c:
return Organisation(
return Agency(
id=oid, name=self.names[0].value, description=d, contacts=c
)
else:
return Organisation(
return Agency(id=oid, name=self.names[0].value, description=d)


class FusionProvider(Struct, frozen=True):
"""Fusion-JSON payload for an organisation."""

id: str
names: Sequence[FusionString]
descriptions: Optional[Sequence[FusionString]] = None
contacts: Sequence[FusionContact] = ()

def to_model(self, owner: Optional[str] = None) -> DataProvider:
"""Converts a FusionOrg to a standard Organisation."""
d = self.descriptions[0].value if self.descriptions else None
c = [c.to_model() for c in self.contacts]
oid = f"{owner}.{self.id}" if owner and owner != "SDMX" else self.id
if c:
return DataProvider(
id=oid, name=self.names[0].value, description=d, contacts=c
)
else:
return DataProvider(
id=oid, name=self.names[0].value, description=d
)

Expand All @@ -67,9 +88,9 @@ class FusionAgencyScheme(Struct, frozen=True):
"""Fusion-JSON payload for an agency scheme."""

agencyId: str
items: Sequence[FusionOrg]
items: Sequence[FusionAgency]

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[Agency]:
"""Converts a FusionAgencyScheme to a list of Organisations."""
return [o.to_model(self.agencyId) for o in self.items]

Expand All @@ -79,7 +100,7 @@ class FusionAgencyMessage(Struct, frozen=True):

AgencyScheme: Sequence[FusionAgencyScheme]

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[Agency]:
"""Returns the requested list of agencies."""
return self.AgencyScheme[0].to_model()

Expand All @@ -94,15 +115,15 @@ class FusionProvisionAgreement(Struct, frozen=True):
class FusionProviderScheme(Struct, frozen=True):
"""Fusion-JSON payload for a data provider scheme."""

items: Sequence[FusionOrg]
items: Sequence[FusionProvider]

def __get_df_ref(self, ref: str) -> DataflowRef:
a = parse_urn(ref)
return DataflowRef(a.id, a.agency, version=a.version)

def to_model(
self, pas: Sequence[FusionProvisionAgreement]
) -> Sequence[Organisation]:
) -> Sequence[DataProvider]:
"""Converts a FusionProviderScheme to a list of Organisations."""
if pas:
paprs: Dict[str, Set[DataflowRef]] = defaultdict(set)
Expand All @@ -112,7 +133,7 @@ def to_model(
paprs[pr].add(df)
prvs = [o.to_model() for o in self.items]
return [
Organisation(
DataProvider(
id=p.id,
name=p.name,
description=p.description,
Expand All @@ -131,6 +152,6 @@ class FusionProviderMessage(Struct, frozen=True):
DataProviderScheme: Sequence[FusionProviderScheme]
ProvisionAgreement: Sequence[FusionProvisionAgreement] = ()

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[DataProvider]:
"""Returns the requested list of providers."""
return self.DataProviderScheme[0].to_model(self.ProvisionAgreement)
12 changes: 9 additions & 3 deletions src/pysdmx/fmr/sdmx/dataflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from msgspec import Struct

from pysdmx.fmr.sdmx.org import JsonDataProviderScheme
from pysdmx.model import Components, DataflowInfo, DataflowRef, Organisation
from pysdmx.model import (
Agency,
Components,
DataflowInfo,
DataflowRef,
DataProvider,
)


class JsonDataflowRef(Struct, frozen=True, rename={"agency": "agencyID"}):
Expand Down Expand Up @@ -59,7 +65,7 @@ def to_model(
self, components: Components, agency: str, id_: str, version: str
) -> DataflowInfo:
"""Returns the requested dataflow details."""
prvs: List[Organisation] = []
prvs: List[DataProvider] = []
for dps in self.dataProviderSchemes:
prvs.extend(dps.dataProviders)
df = list(
Expand All @@ -71,7 +77,7 @@ def to_model(
return DataflowInfo(
df.id,
components,
Organisation(df.agency),
Agency(df.agency),
df.name,
df.description,
df.version,
Expand Down
20 changes: 10 additions & 10 deletions src/pysdmx/fmr/sdmx/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
from msgspec import Struct

from pysdmx.fmr.sdmx.pa import JsonProvisionAgreement
from pysdmx.model import DataflowRef, Organisation
from pysdmx.model import Agency, DataflowRef, DataProvider
from pysdmx.util import parse_urn


class JsonDataProviderScheme(Struct, frozen=True):
"""SDMX-JSON payload for a data provider scheme."""

dataProviders: Sequence[Organisation]
dataProviders: Sequence[DataProvider]

def __get_df_ref(self, ref: str) -> DataflowRef:
a = parse_urn(ref)
return DataflowRef(a.id, a.agency, version=a.version)

def to_model(
self, pas: Sequence[JsonProvisionAgreement]
) -> Sequence[Organisation]:
) -> Sequence[DataProvider]:
"""Converts a JsonDataProviderScheme to a list of Organisations."""
if pas:
paprs: Dict[str, Set[DataflowRef]] = defaultdict(set)
Expand All @@ -30,7 +30,7 @@ def to_model(
pr = pa.dataProvider[pa.dataProvider.rindex(".") + 1 :]
paprs[pr].add(df)
return [
Organisation(
DataProvider(
id=p.id,
name=p.name,
description=p.description,
Expand All @@ -49,7 +49,7 @@ class JsonDataProviderSchemes(Struct, frozen=True):
dataProviderSchemes: Sequence[JsonDataProviderScheme]
provisionAgreements: Sequence[JsonProvisionAgreement] = ()

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[DataProvider]:
"""Converts a JsonDataProviderSchemes to a list of Organisations."""
return self.dataProviderSchemes[0].to_model(self.provisionAgreements)

Expand All @@ -59,7 +59,7 @@ class JsonProviderMessage(Struct, frozen=True):

data: JsonDataProviderSchemes

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[DataProvider]:
"""Returns the requested list of providers."""
return self.data.to_model()

Expand All @@ -68,7 +68,7 @@ class JsonAgencyScheme(Struct, frozen=True):
"""SDMX-JSON payload for an agency scheme."""

agencyID: str
agencies: Sequence[Organisation]
agencies: Sequence[Agency]


class JsonAgencySchemes(Struct, frozen=True):
Expand All @@ -82,13 +82,13 @@ class JsonAgencyMessage(Struct, frozen=True):

data: JsonAgencySchemes

def __add_owner(self, owner: str, a: Organisation) -> Organisation:
def __add_owner(self, owner: str, a: Agency) -> Agency:
oid = f"{owner}.{a.id}" if owner != "SDMX" else a.id
return Organisation(
return Agency(
id=oid, name=a.name, description=a.description, contacts=a.contacts
)

def to_model(self) -> Sequence[Organisation]:
def to_model(self) -> Sequence[Agency]:
"""Returns the requested list of agencies."""
return [
self.__add_owner(self.data.agencySchemes[0].agencyID, a)
Expand Down

0 comments on commit 374507e

Please sign in to comment.