30 Revert an XML document tree to "iwld:char" format for upload to gracedb.
31 This does not solve the generic problem of document conversion, it is
32 specifically written for the case of gstlal_inspiral's gracedb uploads.
36 from glue.ligolw
import ilwd
37 from glue.ligolw
import ligolw
38 from glue.ligolw
import lsctables
39 from ligo.lw.lsctables
import TableByName
as ligo_lw_TableByName
40 from ligo.lw.param
import Param
as ligo_lw_Param
41 from ligo.lw.table
import Column
as ligo_lw_Column
52 ilwdchar_tables = dict((tblname, dict((ligo_lw_Column.ColumnName(colname),
None)
for colname, coltype
in tblcls.validcolumns.items()
if coltype ==
u"ilwd:char"))
for tblname, tblcls
in lsctables.TableByName.items()
if tblname
in ligo_lw_TableByName
and u"ilwd:char" in tblcls.validcolumns.values())
54 destrip_column = dict((key, dict(value))
for key, value
in ilwdchar_tables.items())
55 for tblname, colnamemap
in destrip_column.items():
56 for colname
in list(colnamemap):
57 colnamemap[colname], = (destripped
for destripped
in ligo_lw_TableByName[tblname].validcolumns
if ligo_lw_Column.ColumnName(destripped) == colname)
59 for tblname, colnamemap
in ilwdchar_tables.items():
60 for colname
in list(colnamemap):
61 destripped = destrip_column[tblname][colname]
63 tblprefix = ligo_lw_Column.ColumnName.table_name(destripped)
68 colnamemap[colname] = ilwd.get_ilwdchar_class(tblprefix, ligo_lw_Column.ColumnName(colname))
82 NOTE: this performs an in-place transcription of the contents of
83 the XML document tree. This should be assumed to be a destructive
84 operation on the contents of the tree. If you wish to hold
85 references to any of the Table elements or other structures in the
86 tree and wish them to remain intact so they can be used afterwards,
93 for table
in list(xmldoc.getElementsByTagName(ligolw.Table.tagName)):
98 if table.Name
not in ilwdchar_tables:
106 newtable = table.parentNode.replaceChild(lsctables.New(lsctables.TableByName[table.Name], table.columnnames), table)
112 if table.Name !=
"coinc_event_map":
113 ilwdclsmap = ilwdchar_tables[table.Name]
114 newrowtype = newtable.RowType
115 def newrow(row, nonilwdcharattrs = tuple(colname
for colname
in table.columnnames
if colname
not in ilwdclsmap), ilwdcharattrs = tuple(colname
for colname
in table.columnnames
if colname
in ilwdclsmap)):
116 kwargs = dict((attr, getattr(row, attr))
for attr
in nonilwdcharattrs)
117 kwargs.update((attr, ilwdclsmap[attr](getattr(row, attr)))
for attr
in ilwdcharattrs)
118 return newrowtype(**kwargs)
122 newrowtype = newtable.RowType
123 def newrow(row, coinc_id_ilwdcls = ilwdchar_tables["coinc_event"]["coinc_event_id"]):
127 if "event_id" in ilwdchar_tables[row.table_name]:
128 event_id = ilwdchar_tables[row.table_name][
"event_id"](row.event_id)
129 elif "simulation_id" in ilwdchar_tables[row.table_name]:
130 event_id = ilwdchar_tables[row.table_name][
"simulation_id"](row.event_id)
131 elif "coinc_event_id" in ilwdchar_tables[row.table_name]:
132 event_id = ilwdchar_tables[row.table_name][
"coinc_event_id"](row.event_id)
134 raise KeyError(
"event_id, simulation_id or coinc_event_id not in " + ilwdchar_tables[row.table_name])
136 table_name = row.table_name,
138 coinc_event_id = coinc_id_ilwdcls(row.coinc_event_id)
145 newtable.extend(newrow(row)
for row
in table)
158 ilwdcls = ilwdchar_tables[
"sngl_inspiral"][
"event_id"]
159 for param
in list(ligo_lw_Param.getParamsByName(xmldoc,
"event_id")):
160 param.Type =
u"ilwd:char"
161 param.pcdata = ilwdcls(param.pcdata)