ingo@2798: import os aheinecke@4874: import sys ingo@3654: from shpimporter import DEBUG, INFO, ERROR ingo@2798: ingo@2798: SHP='.shp' ingo@2798: ingo@2798: def findShapefiles(path): ingo@2798: shapes = [] ingo@2798: ingo@2798: for root, dirs, files in os.walk(path): ingo@2798: if len(files) == 0: ingo@2798: continue ingo@2798: ingo@3654: DEBUG("Processing directory '%s' with %i files " % (root, len(files))) ingo@2798: ingo@2798: for f in files: ingo@2798: idx = f.find(SHP) ingo@2798: if (idx+len(SHP)) == len(f): ingo@2798: shapes.append((f.replace(SHP, ''), root + "/" + f)) ingo@2798: ingo@2798: return shapes ingo@2798: aheinecke@4935: def getUTF8(string): aheinecke@4935: """ aheinecke@4935: Tries to convert the string to a UTF-8 encoding by first checking if it aheinecke@4935: is UTF-8 and then trying cp1252 aheinecke@4935: """ aheinecke@4935: try: aheinecke@4935: return unicode.encode(unicode(string, "UTF-8"), "UTF-8") aheinecke@4935: except UnicodeDecodeError: aheinecke@4935: # Probably European Windows names so lets try again aheinecke@4935: return unicode.encode(unicode(string, "cp1252"), "UTF-8") aheinecke@4935: aheinecke@4874: def getUTF8Path(path): aheinecke@4874: """ aheinecke@4874: Tries to convert path to utf-8 by first checking the filesystemencoding aheinecke@4874: and trying the default windows encoding afterwards. aheinecke@4874: Returns a valid UTF-8 encoded unicode object or throws a UnicodeDecodeError aheinecke@4874: """ aheinecke@4874: try: aheinecke@4874: return unicode.encode(unicode(path, sys.getfilesystemencoding()), "UTF-8") aheinecke@4874: except UnicodeDecodeError: aheinecke@4887: # Probably European Windows names so lets try again aheinecke@4887: return unicode.encode(unicode(path, "cp1252"), "UTF-8")