Skip to content

Commit

Permalink
create_bestand: update header to conform to rest of library
Browse files Browse the repository at this point in the history
  • Loading branch information
rien333 committed Dec 6, 2024
1 parent 50127e5 commit e0faa9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 52 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ Je kan op een vergelijkbare manier Bestand objecten bouwen via de `Bestand()` cl


```python
from mdto.gegevensgroepen import *
import mdto

# 'informatieobject_001.xml' is het informatieobject waar het Bestand object een representatie van is
with open("informatieobject_001.mdto.xml") as info_object:
with open("informatieobject_001.mdto.xml") as obj:
bestand = mdto.create_bestand(
"vergunning.pdf",
"34c5-4379-9f1a-5c378",
"Proza (DMS)",
representatievan=info_object,
infile="vergunning.pdf", # bestand waarvoor technische metagegevens moeten worden aangemaakt
identificatie=Identificatiegegevens("34c5-4379-9f1a-5c378", "Proza (DMS)"),
informatieobject=obj,
)

# Sla op als XML bestand
Expand Down
65 changes: 20 additions & 45 deletions mdto/mdto.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,65 +770,40 @@ def pronominfo(path: str) -> BegripGegevens:

def create_bestand(
infile: TextIO | str,
identificatiekenmerken: List[str] | str,
identificatiebronnen: List[str] | str,
informatieobject: TextIO,
identificatie: IdentificatieGegevens | List[IdentificatieGegevens],
informatieobject: TextIO | str,
naam: str = None,
url: str = None,
quiet: bool = False,
force: bool = False,
) -> Bestand:
"""Convenience function for creating Bestand objects. The difference between this function
and calling Bestand() directly is that this function infers most Bestand-related
information for you, based on the characteristics of `infile`.
information for you (checksum, name, and so on), based on the characteristics of `infile`.
Supply a list of strings to `identificatiekenmerken` and `identificatiebronnen`
if multiple <identificatie> tags are desired. Otherwise, a single str suffices.
Args:
infile (TextIO | str): the file the Bestand object should represent
identificatiekenmerken (List[str] | str): str or list of str for <identificatieKenmerk> tags
identificatiebronnen (List[str] | str): str or list of str for <identificatieBron> tags
informatieobject (TextIO | str): path or file-like object that
represents an MDTO Informatieobject in XML form.
Used to infer values for <isRepresentatieVan>.
naam (Optional[str]): value of <naam>. Defaults to the basename of `infile`
url (Optional[str]): value of <URLBestand>
quiet (Optional[bool]): silence non-fatal warnings
force (Optional[bool]): do not exit when encountering would-be invalid tag values
infile (TextIO | str): the file the Bestand object should represent
identificatie (IdentificatieGegevens | List[IdentificatieGegevens]): Identificatiekenmerk
informatieobject (TextIO | str): path or file-like object to a XML file containing an informatieobject.
Used to infer values for <isRepresentatieVan>.
naam (Optional[str]): value of <naam>. Defaults to the basename of `infile`
url (Optional[str]): value of <URLBestand>
Example:
```python
```python
with open('informatieobject_001.xml') as f:
bestand = create_bestand("vergunning.pdf", '34c5-4379-9f1a-5c378', 'Proza (DMS)', informatieobject=f)
xml = bestand.to_xml()
```
"""
global _force, _quiet
_quiet = quiet
_force = force
with open('informatieobject_001.xml') as f:
bestand = create_bestand("vergunning.pdf",
IdentificatieGegevens('34c5-4379-9f1a-5c378', 'Proza (DMS)'),
informatieobject=f)
xml = bestand.to_xml()
```
Returns:
Bestand: new Bestand object
"""
# allow infile to be a path (str)
infile = _process_file(infile)

# permit setting kenmerk and bron to a string
if isinstance(identificatiekenmerken, str):
identificatiekenmerken = [identificatiekenmerken]
if isinstance(identificatiebronnen, str):
identificatiebronnen = [identificatiebronnen]

if len(identificatiekenmerken) != len(identificatiebronnen):
_error(
"number of 'identificatieKenmerk' tags differs from "
"number of 'identificatieBron' tags"
)

ids = [
IdentificatieGegevens(k, b)
for k, b in zip(identificatiekenmerken, identificatiebronnen)
]

if not naam:
naam = os.path.basename(infile.name)

Expand All @@ -843,7 +818,7 @@ def create_bestand(
infile.close()

return Bestand(
ids, naam, omvang, bestandsformaat, checksum, isrepresentatievan, url
identificatie, naam, omvang, bestandsformaat, checksum, isrepresentatievan, url
)


Expand Down
3 changes: 1 addition & 2 deletions tests/test_mdto_xsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def test_automatic_bestand_xml_validity(mdto_xsd, voorbeeld_archiefstuk_xml):
# create Bestand object from example_file + existing informatieobject
bestand = mdto.create_bestand(
example_file,
"abcd-1234",
"Corsa",
IdentificatieGegevens("abcd-1234", "Corsa"),
voorbeeld_archiefstuk_xml,
url="https://example.com/",
)
Expand Down

0 comments on commit e0faa9b

Please sign in to comment.