# Path: gsdview/utils.py
# def data_uuid(path, prefixlen=23):
#     path = os.path.normpath(os.path.abspath(os.path.expanduser(path)))
#     st = os.stat(path)
#     if os.path.isdir(path):
#         size = get_tree_size(path)
#     else:
#         size = st[stat.ST_SIZE]
#     uid = '{}:{}:{}'.format(path, st[stat.ST_MTIME], size)
#     uid = uuid.uuid5(uuid.NAMESPACE_URL, uid)
#     if prefixlen > 0:
#         prefix = os.path.basename(path)[:prefixlen]
#         return f'{prefix}-{uid}'
#     else:
#         return str(uid)


import os
import shutil
import logging
import numpy as np
        import copy
    from lxml import etree
    from xml.etree import ElementTree as etree
from osgeo import gdal
from osgeo import osr
from gsdview.utils import data_uuid
        from osgeo import ogr
    # @TODO: use also gdal.Band.Checksum or similia

    d = prod.GetDriver()
    driver_name = d.GetDescription()
    _log.debug('driver_name = %s', driver_name)
    if driver_name == 'SAR_CEOS':
        try:
            # 'CEOS_LOGICAL_VOLUME_ID'
            metadata = prod.GetMetadata()
            prod_id = '{}-{}'.format(
                metadata['CEOS_SOFTWARE_ID'].strip(),
                metadata['CEOS_ACQUISITION_TIME'].strip())
        except KeyError:
            prod_id = os.path.basename(prod.GetDescription())
    elif driver_name == 'ESAT':
        metadata = prod.GetMetadata()
        prod_id = os.path.splitext(metadata['MPH_PRODUCT'])[0]
    # elif driver_name = 'GTiff':
    #     # ERS BTIF
    #     pass
    elif driver_name.startswith('HDF5') or driver_name.startswith('CSK'):
        # @TODO: update this case using data_uuid
        prod_id = prod.GetDescription()
        parts = prod_id.split(':')
        if len(parts) == 4:
            filename = ':'.join(parts[1:3])
            parts = (parts[0], filename, parts[2])
        if len(parts) == 3:
            filename = os.path.basename(parts[1].strip('"'))
            h5path = parts[2].replace('//', '/')

# Path: gsdview/utils.py
# def data_uuid(path, prefixlen=23):
#     path = os.path.normpath(os.path.abspath(os.path.expanduser(path)))
#     st = os.stat(path)
#     if os.path.isdir(path):
#         size = get_tree_size(path)
#     else:
#         size = st[stat.ST_SIZE]
#     uid = '{}:{}:{}'.format(path, st[stat.ST_MTIME], size)
#     uid = uuid.uuid5(uuid.NAMESPACE_URL, uid)
#     if prefixlen > 0:
#         prefix = os.path.basename(path)[:prefixlen]
#         return f'{prefix}-{uid}'
#     else:
#         return str(uid)


# Path: gsdview/gdalbackend/gdalsupport.py
import os
import shutil
import logging
import numpy as np
        import copy
    from lxml import etree
    from xml.etree import ElementTree as etree
from osgeo import gdal
from osgeo import osr
from gsdview.utils import data_uuid
        from osgeo import ogr

    # @TODO: use also gdal.Band.Checksum or similia

    d = prod.GetDriver()
    driver_name = d.GetDescription()
    _log.debug('driver_name = %s', driver_name)
    if driver_name == 'SAR_CEOS':
        try:
            # 'CEOS_LOGICAL_VOLUME_ID'
            metadata = prod.GetMetadata()
            prod_id = '{}-{}'.format(
                metadata['CEOS_SOFTWARE_ID'].strip(),
                metadata['CEOS_ACQUISITION_TIME'].strip())
        except KeyError:
            prod_id = os.path.basename(prod.GetDescription())
    elif driver_name == 'ESAT':
        metadata = prod.GetMetadata()
        prod_id = os.path.splitext(metadata['MPH_PRODUCT'])[0]
    # elif driver_name = 'GTiff':
    #     # ERS BTIF
    #     pass
    elif driver_name.startswith('HDF5') or driver_name.startswith('CSK'):
        # @TODO: update this case using data_uuid
        prod_id = prod.GetDescription()
        parts = prod_id.split(':')
        if len(parts) == 4:
            filename = ':'.join(parts[1:3])
            parts = (parts[0], filename, parts[2])
        if len(parts) == 3:
            filename = os.path.basename(parts[1].strip('"'))
            h5path = parts[2].replace('//', '/')

            prod_id = filename + h5path.replace('/', '_')