# Path: lib/util.py
# def getAbsInsensitivePath(abs_insensitive_path, ignoreAbsent=False):
#     """
#     Will sanitize cases and return the correct case.
#     :param abs_insensitive_path:
#     :param ignoreAbsent: if true this will not throw an exception and just return the path
#     :return:
#     """
#     log = Logger("getAbsInsensitivePath")
# 
#     if len(abs_insensitive_path) == 0:
#         raise IOError("Zero-length path used: getAbsInsensitivePath()")
# 
#     if os.path.sep == "/":
#         pathreplaced = abs_insensitive_path.replace("\\", os.path.sep)
#     else:
#         pathreplaced = abs_insensitive_path.replace("/", os.path.sep)
# 
#     parts = pathreplaced.split(os.path.sep)
# 
#     improved_parts = []
# 
#     for part in parts:
#         if part == ".." or part == "." or part == "":
#             improved_parts.append(part)
#         else:
#             improved_path = os.path.sep.join(improved_parts)
#             if len(improved_path) == 0:
#                 improved_path = os.path.sep
#             try:
#                 found = False
#                 for name in os.listdir(improved_path):
#                     if part.lower() == name.lower():
#                         improved_parts.append(name)
#                         found = True
#                 if not found:
#                     raise OSError("Not found")
#             except OSError, e:
#                 if not ignoreAbsent:
#                     raise MissingException("Could not find case-insensitive path: {}".format(abs_insensitive_path))
#                 else:
#                     return abs_insensitive_path
# 
#     finalpath = os.path.sep.join(improved_parts)
# 
#     if (abs_insensitive_path != finalpath):
#         log.warning("Paths do not match: `{}`  != `{}`".format(abs_insensitive_path, finalpath))
# 
#     return finalpath


from os import path
from xml.etree import ElementTree as ET
from exception import DataException, MissingException
from loghelper import Logger
from lib.util import getAbsInsensitivePath

class TopoProject():
    # Dictionary with layer { layernname : layerxpath }
    LAYERS = {
        "DEM": "./Realizations/Topography/TIN[@active='true']/DEM/Path",
        "DetrendedDEM": "./Realizations/Topography/TIN[@active='true']/Detrended/Path",
        "WaterDepth": "./Realizations/Topography/TIN[@active='true']/WaterDepth/Path",
        "ErrorSurface": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/ErrSurface/Path",
        "WaterSurfaceDEM": "./Realizations/Topography/TIN[@active='true']/WaterSurfaceDEM/Path",
        "AssocPointQuality": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/PointQuality3D/Path",
        "AssocSlope": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/Slope/Path",
        "AssocRough": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/Roughness/Path",
        "AssocPointDensity": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/PointDensity/Path",
        "AssocInterpolationError": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/InterpolationError/Path",
        "Topo_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='topo_points']/Path",
        "StreamFeatures": "./Realizations/SurveyData[@projected='true']/Vector[@id='stream_features']/Path",
        "EdgeofWater_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='eow_points']/Path",
        "Control_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='control_points']/Path",
        "Error_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='error_points']/Path",
        "Breaklines": "./Realizations/SurveyData[@projected='true']/Vector[@id='breaklines']/Path",
        "WaterExtent": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='extent']/Path",
        "BankfullExtent": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='extent']/Path",
        "WettedIslands": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='islands']/Path",
        "BankfullIslands": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='islands']/Path",
        "ChannelUnits": "./Realizations/Topography/TIN[@active='true']/ChannelUnits/Path",
        "Thalweg": "./Realizations/Topography/TIN[@active='true']/Thalweg/Path",
        "WettedCenterline": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='centerline']/Path",
        "BankfullCenterline": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='centerline']/Path",
        "WettedCrossSections": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='crosssections']/Path",
        "BankfullCrossSections": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='crosssections']/Path",

# Path: lib/util.py
# def getAbsInsensitivePath(abs_insensitive_path, ignoreAbsent=False):
#     """
#     Will sanitize cases and return the correct case.
#     :param abs_insensitive_path:
#     :param ignoreAbsent: if true this will not throw an exception and just return the path
#     :return:
#     """
#     log = Logger("getAbsInsensitivePath")
# 
#     if len(abs_insensitive_path) == 0:
#         raise IOError("Zero-length path used: getAbsInsensitivePath()")
# 
#     if os.path.sep == "/":
#         pathreplaced = abs_insensitive_path.replace("\\", os.path.sep)
#     else:
#         pathreplaced = abs_insensitive_path.replace("/", os.path.sep)
# 
#     parts = pathreplaced.split(os.path.sep)
# 
#     improved_parts = []
# 
#     for part in parts:
#         if part == ".." or part == "." or part == "":
#             improved_parts.append(part)
#         else:
#             improved_path = os.path.sep.join(improved_parts)
#             if len(improved_path) == 0:
#                 improved_path = os.path.sep
#             try:
#                 found = False
#                 for name in os.listdir(improved_path):
#                     if part.lower() == name.lower():
#                         improved_parts.append(name)
#                         found = True
#                 if not found:
#                     raise OSError("Not found")
#             except OSError, e:
#                 if not ignoreAbsent:
#                     raise MissingException("Could not find case-insensitive path: {}".format(abs_insensitive_path))
#                 else:
#                     return abs_insensitive_path
# 
#     finalpath = os.path.sep.join(improved_parts)
# 
#     if (abs_insensitive_path != finalpath):
#         log.warning("Paths do not match: `{}`  != `{}`".format(abs_insensitive_path, finalpath))
# 
#     return finalpath


# Path: lib/topoproject.py
from os import path
from xml.etree import ElementTree as ET
from exception import DataException, MissingException
from loghelper import Logger
from lib.util import getAbsInsensitivePath


class TopoProject():
    # Dictionary with layer { layernname : layerxpath }
    LAYERS = {
        "DEM": "./Realizations/Topography/TIN[@active='true']/DEM/Path",
        "DetrendedDEM": "./Realizations/Topography/TIN[@active='true']/Detrended/Path",
        "WaterDepth": "./Realizations/Topography/TIN[@active='true']/WaterDepth/Path",
        "ErrorSurface": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/ErrSurface/Path",
        "WaterSurfaceDEM": "./Realizations/Topography/TIN[@active='true']/WaterSurfaceDEM/Path",
        "AssocPointQuality": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/PointQuality3D/Path",
        "AssocSlope": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/Slope/Path",
        "AssocRough": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/Roughness/Path",
        "AssocPointDensity": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/PointDensity/Path",
        "AssocInterpolationError": "./Realizations/Topography/TIN[@active='true']/AssocSurfaces/InterpolationError/Path",
        "Topo_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='topo_points']/Path",
        "StreamFeatures": "./Realizations/SurveyData[@projected='true']/Vector[@id='stream_features']/Path",
        "EdgeofWater_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='eow_points']/Path",
        "Control_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='control_points']/Path",
        "Error_Points": "./Realizations/SurveyData[@projected='true']/Vector[@id='error_points']/Path",
        "Breaklines": "./Realizations/SurveyData[@projected='true']/Vector[@id='breaklines']/Path",
        "WaterExtent": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='extent']/Path",
        "BankfullExtent": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='extent']/Path",
        "WettedIslands": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='islands']/Path",
        "BankfullIslands": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='islands']/Path",
        "ChannelUnits": "./Realizations/Topography/TIN[@active='true']/ChannelUnits/Path",
        "Thalweg": "./Realizations/Topography/TIN[@active='true']/Thalweg/Path",
        "WettedCenterline": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='centerline']/Path",
        "BankfullCenterline": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='centerline']/Path",
        "WettedCrossSections": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='wetted'][@type='crosssections']/Path",
        "BankfullCrossSections": "./Realizations/Topography/TIN[@active='true']/Stages/Vector[@stage='bankfull'][@type='crosssections']/Path",

        "SurveyExtent": "./Realizations/SurveyData/SurveyExtents/Vector[@active='true']/Path", #MR?