From f1786dbf5461bce20c7e6f166663c73f4a3c9132 Mon Sep 17 00:00:00 2001
From: Kipp Cannon <kipp.cannon@ligo.org>
Date: Mon, 25 Feb 2019 16:40:47 +0900
Subject: [PATCH] ilwdify: try fixing

- this patch fixes ID reconstruction but doesn't get the column names quite
  right.  they're close to being correct, and there is a bug in glue that
  makes it inconvenient to get them any closer, so we'll give this a try
  and see if downstream consumer codes care.  note that ligolw_no_ilwdchar
  does *not* care:  with this patch, file --> ilwdify -->
  ligolw_no_ilwdchar --> file is now an identity transformation.  also note
  that glue's normal document parsing logic does not care:  ligolw_print
  is happy with the result of this transformation.  it's not impossible,
  however, that somebody has written some custom code that adds additional
  safety checks and will notice the column names.
---
 gstlal-inspiral/python/ilwdify.py | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/gstlal-inspiral/python/ilwdify.py b/gstlal-inspiral/python/ilwdify.py
index 7c238c5025..904fb28e5e 100755
--- a/gstlal-inspiral/python/ilwdify.py
+++ b/gstlal-inspiral/python/ilwdify.py
@@ -36,31 +36,36 @@ specifically written for the case of gstlal_inspiral's gracedb uploads.
 from glue.ligolw import ilwd
 from glue.ligolw import ligolw
 from glue.ligolw import lsctables
+from ligo.lw.lsctables import TableByName as ligo_lw_TableByName
 from ligo.lw.param import Param as ligo_lw_Param
 from ligo.lw.table import Column as ligo_lw_Column
 
 
 #
 # dictionary mapping lsctables table class to dictionary mapping column
-# name to ilwd:char class.  note the colum name is initially in the format
-# as it appears in the .validcolumns attribute, which means it is not
-# stripped, then it is stripped for ease of use later
+# name to ilwd:char class.  we only consider tables that are named in both
+# glue.ligolw.lsctables and ligo.lw.lsctables, assuming we won't be
+# converting documents that contain tables that have been removed from the
+# latter.
 #
 
+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())
 
-ilwdchar_tables = dict((tblname, dict((colname, None) for colname, coltype in tblcls.validcolumns.items() if coltype == u"ilwd:char")) for tblname, tblcls in lsctables.TableByName.items() if u"ilwd:char" in tblcls.validcolumns.values())
+destrip_column = dict((key, dict(value)) for key, value in ilwdchar_tables.items())
+for tblname, colnamemap in destrip_column.items():
+	for colname in list(colnamemap):
+		colnamemap[colname], = (destripped for destripped in ligo_lw_TableByName[tblname].validcolumns if ligo_lw_Column.ColumnName(destripped) == colname)
 
-for tblname, colnamemap in list(ilwdchar_tables.items()):
+for tblname, colnamemap in ilwdchar_tables.items():
 	for colname in list(colnamemap):
+		destripped = destrip_column[tblname][colname]
 		try:
-			tblprefix = ligo_lw_Column.ColumnName.table_name(colname)
+			tblprefix = ligo_lw_Column.ColumnName.table_name(destripped)
 		except ValueError:
 			# columns whose names don't have a prefix are in
 			# their own table
 			tblprefix = tblname
 		colnamemap[colname] = ilwd.get_ilwdchar_class(tblprefix, ligo_lw_Column.ColumnName(colname))
-	# strip column names
-	ilwdchar_tables[tblname] = dict((ligo_lw_Column.ColumnName(colname), ilwdcls) for colname, ilwdcls in colnamemap.items())
 
 
 #
-- 
GitLab