view update.py @ 45:7d4da4e72d37

fix argv handling
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 05 Jul 2022 10:22:50 +0100
parents fee51ab07d09
children
line wrap: on
line source

#!/bin/python
from sys import stdin,argv
from urllib2 import Request,urlopen, HTTPError
from base64 import b64encode
type='xyzzy'
l=''
year='2014'

def cc(names):
  return ' '.join(map(lambda n:n[0]+n[1:].lower(),names.split()))

cm={'tf':0,
      'uun':1,
      'surname':2,
      'cat':3,
      'stat':4,
      'email':5,
      'country':6,
      'pgm':7,
      'entry':8,
    'forename':9}

eargs=['uun']
eargs.extend(argv[1:])

while l=='':
  l=stdin.readline().rstrip()
if l!="\tPG Applications List for ILCC":
  print "Not what I was expecting: %s"%l
  exit(1)
for l in stdin:
  l=l.rstrip().decode('latin-1');
  if l=='':
    continue
  if l.find('Count:')==0:
    exit()
  if l.find('Pgm Code')==0:
    continue
  if l.find('Entry')==len(l)-5:
    continue
  if l.find(type)==0:
    continue
  if l=='':
    continue
  try: #tf,uun,name,cat,stat,email,country,pgm,entry
    vals=l.split("\t")
  except ValueError:
    print "Bad input: %s"%l
    continue
  if vals[0]!='':
    type=vals[0]
  try:
    (sn,fn)=vals[2].split(", ")
  except ValueError:
    print "Bad input: %s"%l
    exit
  vals[2]=cc(sn)
  vals.append(cc(fn))
  attrs=" ".join(map(lambda n:'%s="%s"'%(n,vals[cm[n]]),eargs))
  req='<update year="%s" %s/>'%(year,attrs)
  print req
  r=Request("http://localhost:8080/exist/apps/phd/updateApp.xq",
            req.encode('utf-8'),headers={'Content-Type':'application/xml;charset=UTF-8'})
  try:
    res=urlopen(r)
  except HTTPError as err:
    print "Error:",err.read()
    print req
    exit(1)
  print res.read()