Skip to content

Commit

Permalink
Update getAttachments
Browse files Browse the repository at this point in the history
  • Loading branch information
mokko committed Feb 10, 2024
1 parent 6d64928 commit 0cc5119
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
51 changes: 35 additions & 16 deletions src/getAttachments.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
getAttachments downloads attachments from RIA
create a configuration file that describes one or multiple jobs
e.g. getAttachments.jobs
label:
Configuration file format
[label]
type: group
id: 12345
restriction: None | freigegeben
Expand All @@ -22,12 +22,13 @@
import configparser
from datetime import date
from mpapi.client import MpApi
from mpapi.constants import get_credentials
from mpapi.module import Module
from mpapi.search import Search
from pathlib import Path

conf_fn = "getAttachments.jobs"
response_cache = "_ga_response.xml" # for debugging
response_cache = "_ga_response.xml" # cache
NSMAP = {"m": "http://www.zetcom.com/ria/ws/module"}


Expand All @@ -51,7 +52,7 @@ def get_attachment(c: MpApi, ID: int) -> None:
hasAttachments = True
else:
hasAttachments = False
raise ValueError("ERROR: Record has not attachment")
raise ValueError("ERROR: Record has no attachment")

try:
fn = m.xpath(
Expand All @@ -66,21 +67,28 @@ def get_attachment(c: MpApi, ID: int) -> None:


class GetAttachments:
def __init__(self, *, baseURL: str, job: str, user: str, pw: str) -> None:
def __init__(self, *, job: str, cache: bool = False) -> None:
user, pw, baseURL = get_credentials()
self.api = MpApi(baseURL=baseURL, user=user, pw=pw)
self.job = job
self.conf = self.setup_conf()
print(f" type: {self.conf['type']}")
print(f" id: {self.conf['id']}")
print(f" rest: {self.conf['restriction']}")
print(f" name: {self.conf['name']}")

if cache:
print("* loading cached response")
m = Module(file=response_cache)
else:
print(f"* launching new search")
m = self.query()
m.toFile(path=response_cache)

# if Path(response_cache).exists():
# print(f"* loading response cache from '{response_cache}'")
# m = Module(file=response_cache)
# else:
print(f"* launching new query")
m = self.query() # returns response as Module
m.toFile(path=response_cache) # debug
self.process_response(data=m)

def process_response(self, *, data: Module) -> None:
print(f"* processing response")
no = data.actualSize(module="Multimedia")
name_policy = self.conf["name"]
print(f"* {no} digital assets found")
Expand Down Expand Up @@ -136,8 +144,6 @@ def query(self) -> Search:
"""
Restriction: Currently, only gets attachments from Multimedia.
"""
print(f"* type: {self.conf['type']}")
print(f"* id: {self.conf['id']}")
qu = Search(module="Multimedia")
if self.conf["restriction"] == "freigegeben":
qu.AND()
Expand Down Expand Up @@ -194,8 +200,21 @@ def setup_conf(self) -> dict:
Returns configuration for selected job
"""
config = configparser.ConfigParser()
required = ["type", "id", "restriction", "name"]
if not Path(conf_fn).exists():
raise SyntaxError(f"ERROR: conf file not found! {conf_fn}")
config.read(conf_fn)
print(f"* Using job '{self.job}' from {conf_fn}")
return config[self.job]
try:
c = config[self.job]
except:
raise SyntaxError(f"job '{self.job}' not found")

for each in required:
try:
c[each]
except:
raise SyntaxError(f"Config value {each} missing!")

print(f"Conf ok") # : {c}
return c
9 changes: 8 additions & 1 deletion src/mpapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ def getAttachments():
parser.add_argument(
"-j", "--job", required=True, help="pick a job from getAttachments.jobs file"
)
parser.add_argument(
"-c",
"--cache",
action="store_true",
help="New query or load cached query?",
default=False,
)
args = _setup_args(parser)
GetAttachments(baseURL=baseURL, job=args.job, user=user, pw=pw)
GetAttachments(job=args.job, cache=args.cache)


def getDefinition():
Expand Down

0 comments on commit 0cc5119

Please sign in to comment.