Commit f160060c authored by Martin Schorb's avatar Martin Schorb
Browse files

initial xml support

parent 8efe4051
......@@ -34,16 +34,7 @@ newnavf = navfile#[:-4] + '_clean.nav'
print('Navigator items marked with Acquire were duplicated and output is written as: ' + newnavf)
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
# fill the new file
for nitem in newnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
em.write_navfile(newnavf,newnav,xml=False)
\ No newline at end of file
......@@ -27,29 +27,26 @@ acq = filter(lambda item:item.get('Acquire'),allitems)
acq = list(filter(lambda item:item['Acquire']==['1'],acq))
non_acq = [x for x in allitems if x not in acq]
newnav = non_acq
#newnav = non_acq
# create new file by copying the header of the input file
newnavf = navfile[:-4] + '_clean.nav'
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
outnav = list()
#
#nnf = open(newnavf,'w')
#nnf.write("%s\n" % navlines[0])
#nnf.write("%s\n" % navlines[1])
# fill the new file
for item in acq:
labelstr = item['# Item']
searchstr = labelstr[1:4]
include = list(filter(lambda item:item['# Item'].find(searchstr)>0,allitems))
searchstr = labelstr#[1:4]
include = list(filter(lambda item:item['# Item'].find(searchstr)>0,non_acq))
for navitem in include:
out = em.itemtonav(navitem,navitem['# Item'])
for s_item in out: nnf.write("%s\n" % s_item)
outnav.extend(include)
em.write_navfile(newnavf,outnav,xml=False)
nnf.close()
......@@ -30,16 +30,6 @@ newnavf = navfile[:-4] + '_duplicated.nav'
print('Navigator items marked with Acquire were duplicated and output is written as: ' + newnavf)
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
# fill the new file
for nitem in newnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
em.write_navfile(newnavf,newnav,xml=False)
......@@ -54,9 +54,9 @@ targetheader = target_merge['mergeheader']
t_mat = em.map_matrix(targetitem)
newnavf = navname[:-4] + '_automaps.nav'
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
#nnf = open(newnavf,'w')
#nnf.write("%s\n" % navlines[0])
#nnf.write("%s\n" % navlines[1])
allitems = em.fullnav(navlines)
......@@ -205,9 +205,5 @@ for idx,acq_item in enumerate(acq):
for nitem in outnav:
newnav.append(nitem)
for nitem in newnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
em.write_navfile(newnavf,newnav,xml=False)
......@@ -63,9 +63,9 @@ targetheader = target_merge['mergeheader']
t_mat = em.map_matrix(targetitem)
newnavf = navname[:-4] + '_automaps.nav'
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
#nnf = open(newnavf,'w')
#nnf.write("%s\n" % navlines[0])
#nnf.write("%s\n" % navlines[1])
allitems = em.fullnav(navlines)
......@@ -213,10 +213,5 @@ for idx,acq_item in enumerate(acq):
for nitem in outnav:
newnav.append(nitem)
for nitem in newnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
em.write_navfile(newnavf,newnav,xml=False)
......@@ -30,16 +30,5 @@ newnavf = navfile[:-4] + '_sorted.nav'
print('Navigator file was sorted and output is written as: ' + newnavf)
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n" % navlines[1])
# fill the new file
for nitem in newnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
em.write_navfile(newnavf,newnav,xml=False)
nnf.close()
......@@ -199,9 +199,9 @@ navlines = em.loadtext(navname)
newnavf = navname[:-4] + '_automaps.nav'
nnf = open(newnavf,'w')
nnf.write("%s\n" % navlines[0])
nnf.write("%s\n\n" % navlines[1])
#nnf = open(newnavf,'w')
#nnf.write("%s\n" % navlines[0])
#nnf.write("%s\n\n" % navlines[1])
allitems = em.fullnav(navlines)
......@@ -247,12 +247,8 @@ for idx,acq_item in enumerate(acq):
on1=em.ordernav(outnav1, delim='_')
finalnav = maps['mapnav'].copy()
finalnav.extend(on1.copy())
finalnav.extend(on1.copy())
for nitem in finalnav:
out = em.itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
em.write_navfile(newnavf,finalnav,xml=False)
......@@ -41,6 +41,7 @@ import time
from operator import itemgetter
import fnmatch
from subprocess import Popen, PIPE
import xml.etree.ElementTree as ET
# get python version
py_ver = sys.version_info
......@@ -90,25 +91,37 @@ def nav_item(inlines,label):
lines=inlines[:]
if lines[-1] != '':
lines = lines+['']
# search for a navigator item of given label
searchstr = '[Item = ' + label + ']'
if not searchstr in lines:
print('ERROR: Navigator Item ' + label + ' not found!')
result=[]
else:
itemstartline = lines.index(searchstr)+1
itemendline = lines[itemstartline:].index('')
item = lines[itemstartline:itemstartline+itemendline]
result = parse_adoc(item)
# create a dictionary entry that contains the label. Comment # is given in case it is exported directly.
result['# Item']=lines[itemstartline-1][lines[itemstartline-1].find(' = ') + 3:-1]
lines[itemstartline-1:itemstartline+itemendline+1]=[]
if(lines[0]=='<?xml version="1.0" encoding="utf-8"?>'):
#load XML
root = ET.fromstringlist(lines)
el = root.findall('*[@name="%s"]' %label)
root.remove(el[0])
newroot = ET.Element('navigator')
newroot.append(el[0])
result = xmltonav(ET.tostringlist(newroot))
lines = ET.tostringlist(root)
else:
if lines[-1] != '':
lines = lines+['']
# search for a navigator item of given label
searchstr = '[Item = ' + label + ']'
if not searchstr in lines:
print('ERROR: Navigator Item ' + label + ' not found!')
result=[]
else:
itemstartline = lines.index(searchstr)+1
itemendline = lines[itemstartline:].index('')
item = lines[itemstartline:itemstartline+itemendline]
result = parse_adoc(item)
# create a dictionary entry that contains the label. Comment # is given in case it is exported directly.
result['# Item']=lines[itemstartline-1][lines[itemstartline-1].find(' = ') + 3:-1]
lines[itemstartline-1:itemstartline+itemendline+1]=[]
return result,lines
......@@ -252,6 +265,28 @@ def itemtonav(item,name):
dlist.append('')
return dlist
# -------------------------------
#%%
def write_navfile(filename,allitems,xml=False):
# creates a new navigator file from a list of navItems (default is mdoc format)
if not xml:
head0 = 'AdocVersion = 2.00'
head1 = 'LastSavedAs = '+filename
nnf = open(filename,'w')
nnf.write("%s\n" % head0)
nnf.write("%s\n" % head1)
nnf.write("\n")
# fill the new file
for nitem in allitems:
out = itemtonav(nitem,nitem['# Item'])
for item in out: nnf.write("%s\n" % item)
nnf.close()
# -------------------------------
#%%
......@@ -289,19 +324,44 @@ def newreg(navitems):
def fullnav(inlines,header=False):
# parses a full nav file and returns a list of dictionaries
navlines=inlines[:]
c=[]
for index,item in enumerate(navlines):
if item.find('[')>-1:
if header:
return inlines[:index-1]
(b,navlines)=nav_item(navlines,item[item.find(' = ') + 3:-1])
b['# Item']=item[item.find(' = ') + 3:-1]
c.append(b)
navlines=inlines[:]
if(navlines[0]=='<?xml version="1.0" encoding="utf-8"?>'):
#load XML
c = xmltonav(navlines)
else:
# mdoc format
c=[]
for index,item in enumerate(navlines):
if item.find('[')>-1:
if header:
return inlines[:index-1]
(b,navlines)=nav_item(navlines,item[item.find(' = ') + 3:-1])
b['# Item']=item[item.find(' = ') + 3:-1]
c.append(b)
return c
# -------------------------------
#%%
def xmltonav(navlines):
root = ET.fromstringlist(navlines)
c=[];
for child in root:
if child.tag == 'Item':
item = dict({'# Item':child.attrib['name']})
for prop in child:
item[prop.tag] = prop.text.split(' ')
c.append(item)
return c
# -------------------------------
#%%
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment