Skip to content

Commit

Permalink
add new script 'getdefinition' and fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mokko committed Oct 13, 2023
1 parent 60656d0 commit f559539
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 36 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"flit",
"lxml >=4.8.0", #pathlike objects
"requests >=2.6",
"pillow", # for exif in record
]
[project.optional-dependencies]
test = [
Expand All @@ -27,6 +28,7 @@ test = [
mink = 'mpapi:mink'
getAttachments = 'mpapi:getAttachments'
getItem = 'mpapi:getItem'
getDefinition = 'mpapi:getDefinition'
updateItem = 'mpapi:updateItem'
validate = 'mpapi:validate'

Expand Down
29 changes: 29 additions & 0 deletions src/mpapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@
user, pw, baseURL = get_credentials()


def getDefinition():
parser = argparse.ArgumentParser(description="getItem for MpApi")
parser.add_argument(
"-t",
"--mtype",
help="module type (Object, Multimedia etc.); optional",
default=None,
)
parser.add_argument("-v", "--version", help="Display version information")
args = parser.parse_args()

if args.version:
print(f"Version: {__version__}")
sys.exit(0)

c = MpApi(baseURL=baseURL, pw=pw, user=user)
print(f"Logging in as '{user}'")
m = c.getDefinition2(mtype=args.mtype)
if args.mtype is None:
fn = f"definition.xml"
else:
fn = f"definition-{args.mtype}.xml"
print(f"About to write to {fn}")
if Path(fn).exists():
print(" overwriting existing file")
m.toFile(path=fn)


def getItem():
parser = argparse.ArgumentParser(description="getItem for MpApi")
parser.add_argument(
Expand All @@ -30,6 +58,7 @@ def getItem():
sys.exit(0)

c = MpApi(baseURL=baseURL, pw=pw, user=user)
print(f"Logging in as '{user}'")
m = c.getItem2(mtype=args.mtype, ID=args.ID)
if args.upload:
fn = f"getItem-{args.mtype}{args.ID}u.xml"
Expand Down
1 change: 1 addition & 0 deletions src/mpapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def getItem2(self, *, mtype: str, ID: int) -> Module:
object.
"""
r = self.getItem(module=mtype, id=ID)
# print(r.text)
return Module(xml=r.text)

def createItem(self, *, module: str, xml: str) -> requests.Response:
Expand Down
21 changes: 16 additions & 5 deletions src/mpapi/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
from collections import namedtuple # experimenting with namedtuples
from copy import deepcopy # for lxml
from lxml import etree # type: ignore
from lxml.etree import XMLSyntaxError
from mpapi.constants import NSMAP, parser
from mpapi.helper import Helper
from pathlib import Path
Expand Down Expand Up @@ -153,11 +154,7 @@ def __init__(self, *, file: PathX = None, tree: ET = None, xml: str = None) -> N
if tree is not None:
self.etree = tree
elif xml is not None:
# fromstring is for xml without encoding declaration
if isinstance(xml, bytes):
self.etree = etree.fromstring(xml, parser)
else:
self.etree = etree.fromstring(bytes(xml, "utf-8"), parser)
self.etree = self._from_xml(xml)
elif file is not None:
self.etree = etree.parse(str(file), parser)
else:
Expand All @@ -168,6 +165,20 @@ def __init__(self, *, file: PathX = None, tree: ET = None, xml: str = None) -> N
</application>"""
self.etree = etree.fromstring(xml, parser)

def _from_xml(self, xml: str):
# fromstring is for xml without encoding declaration
if isinstance(xml, bytes):
try:
ET = etree.fromstring(xml, parser)
except XMLSyntaxError:
raise SyntaxError("Invalid module XML--!")
else:
try:
ET = etree.fromstring(bytes(xml, "utf-8"), parser)
except XMLSyntaxError:
raise SyntaxError("Invalid module XML!")
return ET

def __iter__(self) -> ET:
"""
iterates through all moduleItems, see the method iter if you want to
Expand Down
5 changes: 3 additions & 2 deletions src/mpapi/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from lxml import etree
from mpapi.module import Module
from pathlib import Path
import pyexiv2
import PIL.Image

parser = etree.XMLParser(remove_blank_text=True)

Expand Down Expand Up @@ -159,11 +159,12 @@ def set_dateexif(self, *, path: str) -> None: # alternatively pathlib object
p = Path(path)
dateDT = False
try:
img = pyexiv2.Image(path)
img = PIL.Image.open(path)
except:
print("WARNING: Can't access exif info")
# mtime = p.stat().st_mtime
else:
exif = img._getexif()
try:
date_str = exif["Exif.Image.DateTime"] # 2011:04:12 16:29:52
except:
Expand Down
76 changes: 76 additions & 0 deletions test/mk_test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# Little script that d/l the test data
#

from pathlib import Path
from mpapi.constants import get_credentials
from mpapi.module import Module
from mpapi.search import Search
from mpapi.client import MpApi

user, pw, baseURL = get_credentials()


def get_m39(c) -> None:
"""
What we need is the join file from mink
mink -j m39
jobs.dsl
m39:
getPack exhibit 20222 m39
m39-join-exhibit20222.xml
"""
fn = Path("sdata/m39-join-exhibit20222.xml")
if not fn.exists():
raise TypeError("File missing")


def get_item(c, mtype: str, ID: int) -> None:
# if mtype not in allowed_types:
# raise SyntaxError("Unknown type")
ID = int(ID)

if mtype == "Exhibition":
mtype2 = "exhibit"
else:
mtype2 = mtype

fn = Path(f"sdata/{mtype2}{ID}.xml")
if not fn.exists():
m = c.getItem2(mtype=mtype, ID=ID)
print(f"Writing to {fn}")
m.toFile(path=fn)


def filter_search(c):
fn = "sdata/filter-search.xml"
if not Path(fn).exists():
print("filter search with limit 100...")
q = Search(module="Object", limit=100)
q.AND()
q.addCriterion(
operator="equalsField",
field="ObjPublicationGrp.TypeVoc",
value="2600647", # Daten freigegeben für SMB-digital
)
q.addCriterion(
operator="equalsField",
field="ObjPublicationGrp.PublicationVoc",
value="1810139", # Ja
)
q.addField(field="__id")
q.validate(mode="search") # raises on error
m = c.search2(query=q)
print(f"Writing {fn}")
m.toFile(path=fn)


if __name__ == "__main__":
c = MpApi(baseURL=baseURL, pw=pw, user=user)
print(f"Logging in as '{user}'")
if not Path("sdata"): # assuming we execute in MpApi/test
Path.mkdir()
get_item(c, "Exhibition", 20222)
get_m39(c)
get_item(c, "Object", 739673)
filter_search(c)
65 changes: 36 additions & 29 deletions test/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def test_constructors_only():
# four different constructors, sort of
m: MpApi.Module = Module(file="sdata/exhibit20222.xml")
m: MpApi.Module = Module(file="sdata/m39-join-exhibit20222.xml")
assert m # len(m)>0
xml = """
<application xmlns="http://www.zetcom.com/ria/ws/module">
Expand Down Expand Up @@ -40,27 +40,35 @@ def test_nsmap():


def test_output():
m = Module(file="sdata/exhibit20222.xml")
m = Module(file="sdata/m39-join-exhibit20222.xml")
m.toFile(path="sdata/output.xml") # not sure how to test
xml = m.toString()
assert xml # not sure how to test


def test_inspection():
m = Module(file="sdata/exhibit20222.xml")
# requires scaffold data
m = Module(file="sdata/m39-join-exhibit20222.xml")
with pytest.raises(TypeError) as exc_info:
m.totalSize(module="Object") # none
assert m.totalSize(module="Multimedia") == 642
m.totalSize(module="Objectssss") # none
assert m.totalSize(module="Exhibition") == 1
desc = m.describe()
# print (desc)
assert isinstance(desc, dict)
assert desc == {"Multimedia": 642}
assert desc == {
"Exhibition": 1,
"Multimedia": 677,
"Object": 194,
"Person": 58,
"Registrar": 194,
}
# print(desc)
m.dropUUID()
assert m.validate()


def test_iter():
m = Module(file="sdata/exhibit20222.xml")
m = Module(file="sdata/m39-join-exhibit20222.xml")
for itemN in m:
pass # not sure how to test

Expand Down Expand Up @@ -92,7 +100,7 @@ def test_from_scratch_interface():
def test_drops():
m = Module(file="sdata/exhibit20222.xml")
uuid = m.xpath("/m:application/m:modules/m:module/m:moduleItem/@uuid")[0]
assert uuid == "507201"
assert uuid == "20222"
m.dropUUID()
try:
uuid = m.xpath("/m:application/m:modules/m:module/m:moduleItem/@uuid")
Expand All @@ -103,10 +111,10 @@ def test_drops():


def test_join():
m = Module(file="sdata/exhibit20222.xml")
m = Module(file="sdata/m39-join-exhibit20222.xml")
MM_before = m.totalSize(module="Multimedia")
assert isinstance(MM_before, int)
assert len(m) == 642
assert len(m) == 1124

# first add
ET = etree.parse("sdata/exhibit20222.xml")
Expand All @@ -116,15 +124,15 @@ def test_join():
MM_after = m.totalSize(module="Multimedia")
assert MM_before == MM_after
# print(len(m))
assert len(m) == 642
assert len(m) == 1124

# second add
# print("2nd ADD")
ET2 = etree.parse("data/739673.xml")
# second add: add 2 different documents
ET2 = etree.parse("sdata/object739673.xml")
Obj_before = Module(tree=ET2).actualSize(module="Object")
# print(f"{Obj_before=}")
m.add(doc=ET2)
assert m.totalSize(module="Object") == 1
assert len(m) == 643
assert m.totalSize(module="Object") == 195
assert len(m) == 1125

# there was a version where add(doc=ET) deleted most of the document, so now we
# test that doc remains the "same"
Expand All @@ -137,12 +145,12 @@ def test_join():
def test_length():
m = Module()
assert len(m) == 0
m = Module(file="sdata/exhibit20222.xml")
assert len(m) == 642
m = Module(file="sdata/m39-join-exhibit20222.xml")
assert len(m) == 1124


def test_getItem():
m = Module(file="sdata/exhibit20222.xml")
m = Module(file="sdata/m39-join-exhibit20222.xml")
# from collections import namedtuple
# Item = namedtuple('Item', ['type', 'id'])
itemN = m[("Multimedia", 507201)]
Expand All @@ -151,7 +159,7 @@ def test_getItem():


def test_str():
m1 = Module(file="sdata/exhibit20222.xml")
m1 = Module(file="sdata/m39-join-exhibit20222.xml")
assert isinstance(str(m1), str)
# print (m1.__repr__)

Expand All @@ -160,19 +168,17 @@ def test_add():
m1 = Module(file="sdata/exhibit20222.xml")
m2 = Module(file="sdata/exhibit20222.xml")
m3 = m1 + m2
m4 = Module(file="sdata/Object739673.xml")
m5 = m3 + m4

m1Len = len(m1)
assert isinstance(m3, Module)
assert len(m3) == m1Len
assert len(m3) == len(m1)
assert m3.__repr__ != m1.__repr__

m4 = Module(file="data/739673.xml")
m5 = m3 + m4
assert len(m5) == 643
assert len(m5) == 2
m1 = m1 + m2 # change in place
assert len(m1) == 642
assert len(m1) == 1
m5 = m1 + m2 + m3 # more than 2 additions
assert len(m5) == 642
assert len(m5) == 1


def test_toZip():
Expand All @@ -198,7 +204,8 @@ def test_filter():
# user, pw, baseURL = get_credentials()
# client = MpApi(user=user, baseURL=baseURL, pw=pw)
# m = client.search2(query=q)
m = Module(file="filter-search.xml")

m = Module(file="sdata/filter-search.xml")
new_m = m.filter(
xpath="/m:application/m:modules/m:module/m:moduleItem[@hasAttachments = 'true']"
)
Expand Down

0 comments on commit f559539

Please sign in to comment.