PRODUCT=11/1/1/ab41
NAME="AT Translated Set 2 keyboard"
PHYS="isa0060/serio0/input0"
PROP=0
EV=120013
KEY=402000002 3803078f800d001 feffffdfffefffff fffffffffffffffe
MSC=10
LED=7
MODALIAS=input:b0011v0001p0001eAB41-e0,1,4,11,14,k71,72,73,74,75,76,77,79,7A,7B,7C,7D,7E,7F,80,8C,8E,8F,9B,9C,9D,9E,9F,A3,A4,A5,A6,AC,AD,B7,B8,B9,C1,D9,E2,ram4,l0,1,2,sfw
 ¾    	#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ******************************************************************************
#  $Id$
#
#  Project:  GDAL
#  Purpose:  Dump JPEG2000 file structure
#  Author:   Even Rouault, <even dot rouault at spatialys dot com>
#
# ******************************************************************************
#  Copyright (c) 2015, European Union (European Environment Agency)
#  Copyright (c) 2015, European Union Satellite Centre
#
#  Permission is hereby granted, free of charge, to any person obtaining a
#  copy of this software and associated documentation files (the "Software"),
#  to deal in the Software without restriction, including without limitation
#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
#  and/or sell copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included
#  in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#  DEALINGS IN THE SOFTWARE.
# ******************************************************************************

import sys

from osgeo import gdal


def Usage():
    print("Usage:  dump_jp2 [-dump_gmljp2 out.xml|-] [-dump_crsdictionary out.xml|-]")
    print("                 [-extract_all_xml_boxes filename_prefix]")
    print("                 test.jp2")
    print("")
    print("Options (all are exclusive of the regular dump):")
    print("")
    print("-dump_gmljp2: Writes the content of the GMLJP2 box in the specified")
    print('              file, or on the console if "-" syntax is used.')
    print(
        "-dump_crsdictionary: Writes the content of the GML CRS dictionary box in the specified"
    )
    print('                     file, or on the console if "-" syntax is used.')
    print(
        "-extract_all_xml_boxes: Extract all XML boxes in separate files, and prefix each filename"
    )
    print(
        "                        with the supplied prefix. gmljp2://xml/ link will be replaced by"
    )
    print("                        links to on-disk files.")

    return 2


def dump_gmljp2(filename, out_gmljp2):
    ds = gdal.Open(filename)
    if ds is None:
        print("Cannot open %s" % filename)
        return 1
    mdd = ds.GetMetadata("xml:gml.root-instance")
    if mdd is None:
        print("No GMLJP2 content found in %s" % filename)
        return 1
    if out_gmljp2 == "-":
        print(mdd[0])
    else:
        f = open(out_gmljp2, "wt")
        f.write(mdd[0])
        f.close()
        print("INFO: %s written with content of GMLJP2 box" % out_gmljp2)
    return 0


def dump_crsdictionary(filename, out_crsdictionary):
    ds = gdal.Open(filename)
    if ds is None:
        print("Cannot open %s" % filename)
        return 1
    mdd_list = ds.GetMetadataDomainList()
    if mdd_list is None:
        mdd_list = []
    for domain in mdd_list:
        if domain.startswith("xml:"):
            mdd_item = ds.GetMetadata(domain)[0]
            if "<Dictionary" in mdd_item or "<gml:Dictionary" in mdd_item:
                if out_crsdictionary == "-":
                    print(mdd_item)
                else:
                    f = open(out_crsdictionary, "wt")
  