Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
dicomclean/pass.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (60 sloc)
2.28 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import subprocess | |
from subprocess import check_output # to call: ssh, pwgen | |
import fileinput | |
import itertools | |
import xml.etree.ElementTree as ET | |
from pwgen import pwgen | |
import sys | |
import os | |
import tempfile | |
import random | |
import re | |
def next_fname(basename, ext): | |
actualname = "%s.%s" % (basename, ext) | |
c = itertools.count() | |
while os.path.exists(actualname): | |
actualname = "%s-%d.%s" % (basename, next(c), ext) | |
return actualname | |
parser = argparse.ArgumentParser() | |
parser.add_argument('id', help="ID String which identifies a subject") | |
args = parser.parse_args() | |
pw = pwgen(pw_length=12, num_pw=1, no_capitalize=True, numerals=True, symbols=True, allowed_symbols="-", no_ambiguous=True) | |
# sometimes users are overwhlemed by some special chars and callback, this is why need to focus on common smybols | |
pw = re.sub(r'[' + re.escape(r'`¸,.~\'§"^°}{BIOS') + r']', random.choice("[]-+_#?!bi"), pw) | |
if args.id.startswith('K'): | |
xmlstring = check_output(["ssh", "data", "cat /SCR/master/Child/"+str(args.id)+".xml"]) | |
else: | |
xmlstring = check_output(["ssh", "data", "cat /SCR/master/Proband/Proband"+str(args.id)+".xml"]) | |
tree = ET.ElementTree(ET.fromstring(xmlstring)) | |
mobile = tree.find(".//privat").text | |
SMS = f""" | |
{mobile} | |
You have requested data from MPI CBS. Please use this password for download link in separate email: {pw} | |
""" | |
print(SMS) | |
addr_keys = ['Anrede', 'Vorname', 'Nachname', 'Strasse', 'Ort', 'Plz'] | |
if args.id.startswith('K'): | |
# there is no 'Anrede' | |
addr_keys.remove('Anrede') | |
addr = {key:tree.find(".//"+str(key)).text.strip() for key in addr_keys} | |
addr['PW']=pw | |
PDF = "" | |
with open('template.tex', 'r') as f: | |
for line in f.readlines(): | |
for key in addr.keys(): | |
line = line.replace(f'@{key}@', str(addr[key])) | |
PDF += re.sub('@[^@ ]*@', '', line) # to eliminate unused placeholdes in template.tex | |
fname = next_fname('letter' + args.id, 'tex') | |
with open(fname,'w') as f: | |
f.write(PDF) | |
cmds = [f'xelatex {fname}', | |
f'latexmk -c', | |
] | |
for cmd in cmds: | |
print(cmd) | |
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out,err = proc.communicate() | |
if proc.returncode: | |
print(out.decode()) | |
ValueError('Error {} executing command: {}'.format(proc.returncode, cmd)) |