#!/home/conda/feedstock_root/build_artifacts/bld/rattler-build_igwn-ligolw_1771054291/host_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_pl/bin/python
#
# Copyright (C) 2017--2020  Kipp Cannon
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#
# =============================================================================
#
#                                   Preamble
#
# =============================================================================
#


import logging
from optparse import OptionParser
from unittest.mock import patch

from igwn_ligolw import __version__, ligolw
from igwn_ligolw import utils as ligolw_utils
from igwn_ligolw.utils import ilwd

#
# =============================================================================
#
#                                 Command Line
#
# =============================================================================
#


def parse_command_line():
    parser = OptionParser(
        version="Name: %%prog\n%s" % __version__,
        usage="%prog [filename ...]",
        description="Convert ilwd:char columns to int_8s.  Also ensure table name prefixes are removed from columns that shouldn't have them, according to the specifications in lsctables.  Files are converted *in place*.  Make a copy before conversion if you wish to preserve the original.  If no filenames are given, stdin is converted to stdout.  Files are overwritten even if no changes are made.",
    )
    parser.add_option("-v", "--verbose", action="store_true", help="Be verbose.")
    options, filenames = parser.parse_args()
    return options, filenames


#
# =============================================================================
#
#                                     Main
#
# =============================================================================
#


options, filenames = parse_command_line()


if options.verbose:
    logging.basicConfig(format="%(asctime)s:%(message)s", level=logging.INFO)
else:
    logging.basicConfig(format="%(asctime)s:%(message)s")


for filename in filenames or [None]:
    # temporarily disable table-by-name look-up
    with patch.dict(ligolw.Table.TableByName, clear=True):
        xmldoc = ligolw_utils.load_filename(filename, verbose=options.verbose)

    ilwd.strip_ilwdchar(xmldoc)

    ligolw_utils.write_filename(xmldoc, filename, verbose=options.verbose)
    xmldoc.unlink()
