<s> import errno <EOL> import glob <EOL> import os <EOL> from fnmatch import filter <EOL> import shutil <EOL> import subprocess <EOL> import sys <EOL> import zipfile <EOL> import json <EOL> from PyQt4 . QtCore import QCoreApplication , QDateTime , QDir , QFile , QFileInfo , QIODevice , QObject , QSettings , QUrl , SIGNAL , SLOT <EOL> from PyQt4 . QtGui import QMessageBox <EOL> from PyQt4 . QtXml import QDomNode , QDomElement , QDomDocument , QDomNodeList <EOL> from qgis . core import QgsMapLayerRegistry , QgsMessageLog , QgsVectorLayer , QgsRasterLayer , QgsProviderRegistry <EOL> from qgis . core import QgsLayerTreeGroup , QgsProject <EOL> from qgis . _core import QgsMapLayer <EOL> class Util : <EOL> def __init__ ( self , settings , main_win ) : <EOL> self . main_win = main_win <EOL> self . dlg_caption = settings . DLG_CAPTION <EOL> self . settings = settings <EOL> def tr ( self , message ) : <EOL> """<STR_LIT>""" <EOL> return QCoreApplication . translate ( '<STR_LIT>' , message , encoding = QCoreApplication . UnicodeUTF8 ) <EOL> def create_dir ( self , dir_path ) : <EOL> if not os . path . exists ( dir_path ) : <EOL> try : <EOL> os . makedirs ( dir_path ) <EOL> except OSError as ose : <EOL> if ose . errno != errno . EEXIST : <EOL> return False <EOL> return True <EOL> def check_dir ( self , dir_path ) : <EOL> if ( <EOL> dir_path is None or <EOL> dir_path . isspace ( ) or <EOL> dir_path == '<STR_LIT>' <EOL> ) : <EOL> return False <EOL> if os . path . isdir ( dir_path ) : <EOL> return True <EOL> return self . create_dir ( dir_path ) <EOL> def check_api_url ( self , api_url ) : <EOL> if ( <EOL> not api_url or <EOL> api_url is None or <EOL> api_url . isspace ( ) or <EOL> api_url == '<STR_LIT>' <EOL> ) : <EOL> return False <EOL> else : <EOL> return True <EOL> def extract_zip ( self , archive , dest_dir ) : <EOL> try : <EOL> f = zipfile . ZipFile ( archive , '<STR_LIT:r>' ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( dest_dir ) ) <EOL> for file_info in f . infolist ( ) : <EOL> file_name = os . path . join ( dest_dir , file_info . filename ) <EOL> if file_name [ - <NUM_LIT:1> ] == u'<STR_LIT:/>' : <EOL> if not os . path . exists ( file_name ) : <EOL> os . makedirs ( file_name ) <EOL> continue <EOL> extract_dir = os . path . dirname ( file_name ) <EOL> if not os . path . exists ( extract_dir ) : <EOL> os . makedirs ( ( extract_dir ) ) <EOL> out_file = open ( file_name , '<STR_LIT:wb>' ) <EOL> shutil . copyfileobj ( f . open ( file_info . filename ) , out_file ) <EOL> return True , None <EOL> except UnicodeDecodeError as ude : <EOL> return False , u'<STR_LIT>' . format ( ude . reason ) <EOL> except AttributeError as ae : <EOL> return False , u'<STR_LIT>' . format ( ae . message ) <EOL> except : <EOL> self . msg_log ( u'<STR_LIT>' . format ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) <EOL> return False , u'<STR_LIT>' . format ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> def add_lyrs_from_dir ( self , data_dir , layer_name , layer_url ) : <EOL> try : <EOL> file_types = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> geo_files = [ ] <EOL> for file_type in file_types : <EOL> for root , dir_names , file_names in os . walk ( data_dir ) : <EOL> for file_name in filter ( file_names , file_type ) : <EOL> geo_files . append ( os . path . join ( root , file_name ) ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( data_dir ) ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( '<STR_LIT:\n>' . join ( geo_files ) ) ) <EOL> if len ( geo_files ) < <NUM_LIT:1> : <EOL> self . msg_log ( '<STR_LIT>' ) <EOL> return False , { "<STR_LIT:message>" : "<STR_LIT>" , "<STR_LIT>" : data_dir } <EOL> for geo_file in geo_files : <EOL> if os . path . basename ( geo_file ) . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( geo_file ) ) <EOL> continue <EOL> self . msg_log ( geo_file ) <EOL> full_path = os . path . join ( data_dir , geo_file ) <EOL> full_layer_name = layer_name + '<STR_LIT>' + os . path . basename ( geo_file ) <EOL> low_case = os . path . basename ( geo_file ) . lower ( ) <EOL> lyr = None <EOL> if low_case . endswith ( '<STR_LIT>' ) : <EOL> self . msg_log ( u'<STR_LIT>' ) <EOL> if False is self . __is_geojson ( full_path ) : <EOL> if self . __open_with_system ( full_path ) > <NUM_LIT:0> : <EOL> if QMessageBox . Yes == self . dlg_yes_no ( self . tr ( u'<STR_LIT>' ) . format ( layer_url ) ) : <EOL> self . open_in_manager ( data_dir ) <EOL> continue <EOL> if ( <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) <EOL> ) : <EOL> if self . __open_with_system ( full_path ) > <NUM_LIT:0> : <EOL> if QMessageBox . Yes == self . dlg_yes_no ( self . tr ( u'<STR_LIT>' ) . format ( layer_url ) ) : <EOL> self . open_in_manager ( data_dir ) <EOL> continue <EOL> elif low_case . endswith ( '<STR_LIT>' ) : <EOL> lyr = self . __add_layer_definition_file ( <EOL> full_path , <EOL> QgsProject . instance ( ) . layerTreeRoot ( ) <EOL> ) <EOL> elif low_case . endswith ( '<STR_LIT>' ) or low_case . endswith ( '<STR_LIT>' ) : <EOL> self . msg_log ( u'<STR_LIT>' ) <EOL> self . _open_wmts ( layer_name , layer_url ) <EOL> continue <EOL> elif low_case . endswith ( '<STR_LIT>' ) : <EOL> self . msg_log ( u'<STR_LIT>' ) <EOL> self . _open_wfs ( layer_name , layer_url ) <EOL> continue <EOL> elif low_case . endswith ( '<STR_LIT>' ) : <EOL> self . msg_log ( u'<STR_LIT>' ) <EOL> self . _open_csv ( full_path ) <EOL> continue <EOL> elif ( <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) or <EOL> low_case . endswith ( '<STR_LIT>' ) <EOL> ) : <EOL> lyr = self . __add_raster_layer ( full_path , full_layer_name ) <EOL> else : <EOL> lyr = self . __add_vector_layer ( full_path , full_layer_name ) <EOL> if lyr is not None : <EOL> if not lyr . isValid ( ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( full_path ) ) <EOL> if QMessageBox . Yes == self . dlg_yes_no ( self . tr ( u'<STR_LIT>' ) . format ( layer_url ) ) : <EOL> self . open_in_manager ( data_dir ) <EOL> continue <EOL> QgsMapLayerRegistry . instance ( ) . addMapLayer ( lyr ) <EOL> else : <EOL> self . msg_log ( u'<STR_LIT>' . format ( full_path ) ) <EOL> return True , None <EOL> except AttributeError as ae : <EOL> return False , ae . message <EOL> except TypeError as te : <EOL> return False , te . message <EOL> except : <EOL> return False , sys . exc_info ( ) [ <NUM_LIT:0> ] <EOL> def __add_vector_layer ( self , file_name , full_layer_name ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( file_name ) ) <EOL> lyr = QgsVectorLayer ( <EOL> file_name , <EOL> full_layer_name , <EOL> '<STR_LIT>' <EOL> ) <EOL> return lyr <EOL> def __add_raster_layer ( self , file_name , full_layer_name ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( file_name ) ) <EOL> lyr = QgsRasterLayer ( <EOL> file_name , <EOL> full_layer_name <EOL> ) <EOL> return lyr <EOL> def __add_csv_table ( self , file_name , full_layer_name ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( file_name ) ) <EOL> slashes = '<STR_LIT>' <EOL> if os . name == '<STR_LIT>' : <EOL> slashes += '<STR_LIT:/>' <EOL> lyr_src = u'<STR_LIT>' . format ( <EOL> slashes , <EOL> file_name <EOL> ) <EOL> lyr = QgsVectorLayer ( <EOL> lyr_src , <EOL> full_layer_name , <EOL> '<STR_LIT>' <EOL> ) <EOL> return lyr <EOL> def __add_layer_definition_file ( self , file_name , root_group ) : <EOL> """<STR_LIT>""" <EOL> qfile = QFile ( file_name ) <EOL> if not qfile . open ( QIODevice . ReadOnly ) : <EOL> return None <EOL> doc = QDomDocument ( ) <EOL> if not doc . setContent ( qfile ) : <EOL> return None <EOL> file_info = QFileInfo ( qfile ) <EOL> QDir . setCurrent ( file_info . absoluteDir ( ) . path ( ) ) <EOL> root = QgsLayerTreeGroup ( ) <EOL> ids = doc . elementsByTagName ( '<STR_LIT:id>' ) <EOL> for i in xrange ( <NUM_LIT:0> , ids . size ( ) ) : <EOL> id_node = ids . at ( i ) <EOL> id_elem = id_node . toElement ( ) <EOL> old_id = id_elem . text ( ) <EOL> layer_name = old_id [ : - <NUM_LIT> ] <EOL> date_time = QDateTime . currentDateTime ( ) <EOL> new_id = layer_name + date_time . toString ( '<STR_LIT>' ) <EOL> id_elem . firstChild ( ) . setNodeValue ( new_id ) <EOL> tree_layer_nodes = doc . elementsByTagName ( '<STR_LIT>' ) <EOL> for j in xrange ( <NUM_LIT:0> , tree_layer_nodes . count ( ) ) : <EOL> layer_node = tree_layer_nodes . at ( j ) <EOL> layer_elem = layer_node . toElement ( ) <EOL> if old_id == layer_elem . attribute ( '<STR_LIT:id>' ) : <EOL> layer_node . toElement ( ) . setAttribute ( '<STR_LIT:id>' , new_id ) <EOL> layer_tree_elem = doc . documentElement ( ) . firstChildElement ( '<STR_LIT>' ) <EOL> load_in_legend = True <EOL> if not layer_tree_elem . isNull ( ) : <EOL> root . readChildrenFromXML ( layer_tree_elem ) <EOL> load_in_legend = False <EOL> layers = QgsMapLayer . fromLayerDefinition ( doc ) <EOL> QgsMapLayerRegistry . instance ( ) . addMapLayers ( layers , load_in_legend ) <EOL> nodes = root . children ( ) <EOL> for node in nodes : <EOL> root . takeChild ( node ) <EOL> del root <EOL> root_group . insertChildNodes ( - <NUM_LIT:1> , nodes ) <EOL> return None <EOL> def _open_wmts ( self , name , capabilites_url ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( name , capabilites_url ) ) <EOL> s = QSettings ( ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , '<STR_LIT>' ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , '<STR_LIT>' ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , <NUM_LIT:7> ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , False ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , False ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , False ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , False ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , '<STR_LIT>' ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , False ) <EOL> s . setValue ( u'<STR_LIT>' . format ( name ) , capabilites_url ) <EOL> s . setValue ( u'<STR_LIT>' , name ) <EOL> wms_dlg = QgsProviderRegistry . instance ( ) . selectWidget ( "<STR_LIT>" , self . main_win ) <EOL> QObject . connect ( wms_dlg , SIGNAL ( "<STR_LIT>" ) , <EOL> self . main_win , SLOT ( "<STR_LIT>" ) ) <EOL> wms_dlg . show ( ) <EOL> def _open_wfs ( self , name , capabilites_url ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( name , capabilites_url ) ) <EOL> url = QUrl ( capabilites_url ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT>' ) <EOL> url . removeQueryItem ( '<STR_LIT:version>' ) <EOL> capabilites_url = url . toString ( ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( name , capabilites_url ) ) <EOL> s = QSettings ( ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( s . value ( u'<STR_LIT>' . format ( name ) , '<STR_LIT>' ) ) ) <EOL> key_user = u'<STR_LIT>' . format ( name ) <EOL> key_pwd = u'<STR_LIT>' . format ( name ) <EOL> key_referer = u'<STR_LIT>' . format ( name ) <EOL> key_url = u'<STR_LIT>' . format ( name ) <EOL> s . remove ( key_user ) <EOL> s . remove ( key_pwd ) <EOL> s . remove ( key_referer ) <EOL> s . remove ( key_url ) <EOL> s . sync ( ) <EOL> s . setValue ( key_user , '<STR_LIT>' ) <EOL> s . setValue ( key_pwd , '<STR_LIT>' ) <EOL> s . setValue ( key_referer , '<STR_LIT>' ) <EOL> s . setValue ( key_url , capabilites_url ) <EOL> s . setValue ( u'<STR_LIT>' , name ) <EOL> wfs_dlg = QgsProviderRegistry . instance ( ) . selectWidget ( "<STR_LIT>" , self . main_win ) <EOL> QObject . connect ( <EOL> wfs_dlg <EOL> , SIGNAL ( "<STR_LIT>" ) <EOL> , self . main_win , SLOT ( "<STR_LIT>" ) <EOL> ) <EOL> wfs_dlg . show ( ) <EOL> def _open_csv ( self , full_path ) : <EOL> self . msg_log ( u'<STR_LIT>' . format ( full_path ) ) <EOL> csv_dlg = QgsProviderRegistry . instance ( ) . selectWidget ( "<STR_LIT>" , self . main_win ) <EOL> QObject . connect ( csv_dlg , SIGNAL ( "<STR_LIT>" ) , <EOL> self . main_win , SLOT ( "<STR_LIT>" ) ) <EOL> csv_dlg . children ( ) [ <NUM_LIT:1> ] . children ( ) [ <NUM_LIT:2> ] . setText ( full_path ) <EOL> csv_dlg . show ( ) <EOL> def __open_with_system ( self , file_name ) : <EOL> code = None <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> code = subprocess . call ( ( '<STR_LIT>' , file_name ) ) <EOL> elif os . name == '<STR_LIT>' : <EOL> win_code = os . startfile ( file_name ) <EOL> if win_code != <NUM_LIT:0> : <EOL> code = - <NUM_LIT:1> <EOL> elif os . name == '<STR_LIT>' : <EOL> code = subprocess . call ( ( '<STR_LIT>' , file_name ) ) <EOL> self . msg_log ( u'<STR_LIT>' . format ( code ) ) <EOL> return code <EOL> def __is_geojson ( self , file_path ) : <EOL> try : <EOL> with open ( file_path ) as json_file : <EOL> data = json . load ( json_file ) <EOL> if data . get ( '<STR_LIT>' ) is not None : <EOL> self . msg_log ( '<STR_LIT>' ) <EOL> return True <EOL> elif data . get ( '<STR_LIT:type>' ) == "<STR_LIT>" : <EOL> self . msg_log ( '<STR_LIT>' ) <EOL> return True <EOL> else : <EOL> return False <EOL> except : <EOL> self . msg_log ( u'<STR_LIT>' . format ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) ) <EOL> return False <EOL> def dlg_information ( self , msg ) : <EOL> QMessageBox . information ( self . main_win , self . dlg_caption , msg ) <EOL> def dlg_warning ( self , msg ) : <EOL> QMessageBox . warning ( self . main_win , self . dlg_caption , msg ) <EOL> def dlg_yes_no ( self , msg ) : <EOL> return QMessageBox . question ( <EOL> self . main_win , <EOL> self . dlg_caption , <EOL> msg , <EOL> QMessageBox . Yes , <EOL> QMessageBox . No <EOL> ) <EOL> def msg_log ( self , msg ) : <EOL> if self . settings . debug is True : <EOL> QgsMessageLog . logMessage ( msg , self . dlg_caption ) <EOL> def resolve ( self , name , basepath = None ) : <EOL> """<STR_LIT>""" <EOL> if not basepath : <EOL> basepath = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> return os . path . join ( basepath , name ) <EOL> def open_in_manager ( self , file_path ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , file_path ] ) <EOL> elif sys . platform == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , file_path ] ) <EOL> elif os . name == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , file_path ] ) <EOL> def str2bool ( self , v ) : <EOL> """<STR_LIT>""" <EOL> return v . lower ( ) in ( "<STR_LIT:yes>" , "<STR_LIT:true>" , "<STR_LIT:t>" , "<STR_LIT:1>" ) </s>
