flake8 metabase
This commit is contained in:
+64
-109
@@ -14,48 +14,31 @@ from pathlib import Path
|
||||
from xml.dom.minidom import parseString
|
||||
|
||||
from qgis.core import (
|
||||
NULL,
|
||||
QgsApplication,
|
||||
QgsDataSourceUri,
|
||||
QgsProject,
|
||||
QgsProviderConnectionException,
|
||||
QgsProviderRegistry,
|
||||
QgsRasterLayer,
|
||||
QgsSettings,
|
||||
QgsVectorLayer,
|
||||
)
|
||||
from qgis.PyQt.QtCore import QLocale, QUrl
|
||||
from qgis.PyQt.QtGui import QDesktopServices, QIcon
|
||||
from qgis.PyQt.QtPrintSupport import QPrinter
|
||||
#from qgis.PyQt.QtWebKitWidgets import QWebPage
|
||||
# from qgis.PyQt.QtWebKitWidgets import QWebPage
|
||||
from qgis.PyQt.QtWidgets import (
|
||||
QDialog,
|
||||
QAction,
|
||||
QDockWidget,
|
||||
QFileDialog,
|
||||
QInputDialog,
|
||||
QMenu,
|
||||
QToolButton,
|
||||
)
|
||||
from qgis.gui import *
|
||||
from qgis.utils import iface
|
||||
import qgis
|
||||
'''
|
||||
from pg_metadata.connection_manager import (
|
||||
check_pgmetadata_is_installed,
|
||||
connections_list,
|
||||
settings_connections_names,
|
||||
)
|
||||
'''
|
||||
try:
|
||||
from .tools.PythonSQL import login_base
|
||||
except:
|
||||
print('Pas de fichier PythonSQL')
|
||||
|
||||
from .tools.resources import (
|
||||
load_ui,
|
||||
resources_path,
|
||||
)
|
||||
try:
|
||||
from .tools.PythonSQL import login_base
|
||||
except ValueError:
|
||||
print('Pas de fichier PythonSQL')
|
||||
|
||||
DOCK_CLASS = load_ui('CenRa_Metabase_dockwidget_base.ui')
|
||||
LOGGER = logging.getLogger('CenRa_Metabase')
|
||||
@@ -84,8 +67,8 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
self.current_datasource_uri = None
|
||||
self.current_connection = None
|
||||
|
||||
#self.viewer.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
|
||||
#self.viewer.page().linkClicked.connect(self.open_link)
|
||||
# self.viewer.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
|
||||
# self.viewer.page().linkClicked.connect(self.open_link)
|
||||
|
||||
# Help button
|
||||
self.external_help.setText('')
|
||||
@@ -94,13 +77,13 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
|
||||
# Flat table button
|
||||
self.flatten_dataset_table.setText('')
|
||||
#self.flatten_dataset_table.setToolTip(tr("Add the catalog table"))
|
||||
# self.flatten_dataset_table.setToolTip(tr("Add the catalog table"))
|
||||
self.flatten_dataset_table.setIcon(QgsApplication.getThemeIcon("/mActionAddHtml.svg"))
|
||||
#self.flatten_dataset_table.clicked.connect(self.add_flatten_dataset_table)
|
||||
# self.flatten_dataset_table.clicked.connect(self.add_flatten_dataset_table)
|
||||
|
||||
# Settings menu
|
||||
self.config.setAutoRaise(True)
|
||||
#self.config.setToolTip(tr("Settings"))
|
||||
# self.config.setToolTip(tr("Settings"))
|
||||
self.config.setPopupMode(QToolButton.ToolButtonPopupMode(2))
|
||||
self.config.setIcon(QgsApplication.getThemeIcon("/mActionOptions.svg"))
|
||||
|
||||
@@ -118,7 +101,7 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
|
||||
# Setting PDF/HTML menu
|
||||
self.save_button.setAutoRaise(True)
|
||||
#self.save_button.setToolTip(tr("Save metadata"))
|
||||
# self.save_button.setToolTip(tr("Save metadata"))
|
||||
self.save_button.setPopupMode(QToolButton.ToolButtonPopupMode(2))
|
||||
self.save_button.setIcon(QIcon(QgsApplication.iconPath('mActionFileSave.svg')))
|
||||
|
||||
@@ -146,21 +129,21 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
self.metadata = QgsProviderRegistry.instance().providerMetadata('postgres')
|
||||
|
||||
# Display message in the dock
|
||||
#if not settings_connections_names():
|
||||
#self.default_html_content_not_installed()
|
||||
#else:
|
||||
# if not settings_connections_names():
|
||||
# self.default_html_content_not_installed()
|
||||
# else:
|
||||
self.default_html_content_not_pg_layer()
|
||||
|
||||
iface.layerTreeView().currentLayerChanged.connect(self.layer_changed)
|
||||
try:
|
||||
login_base()
|
||||
iface.layerTreeView().currentLayerChanged.connect(self.layer_changed)
|
||||
except:
|
||||
#qgis.utils.plugins['CenRa_METABASE'].initGui()
|
||||
except ValueError:
|
||||
# qgis.utils.plugins['CenRa_METABASE'].initGui()
|
||||
qgis.utils.plugins['CenRa_METABASE'].unload()
|
||||
#self.default_html_content_not_pg_layer()
|
||||
# self.default_html_content_not_pg_layer()
|
||||
|
||||
if iface.activeLayer():
|
||||
layer=iface.activeLayer()
|
||||
layer = iface.activeLayer()
|
||||
iface.layerTreeView().setCurrentLayer(None)
|
||||
iface.layerTreeView().setCurrentLayer(layer)
|
||||
|
||||
@@ -192,7 +175,7 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
if output_format == OutputFormats.PDF:
|
||||
printer = QPrinter()
|
||||
printer.setOutputFormat(QPrinter.OutputFormat(1))
|
||||
#printer.setPageMargins(20,20,20,20,QPrinter.Unit(0))
|
||||
# printer.setPageMargins(20,20,20,20,QPrinter.Unit(0))
|
||||
printer.setOutputFileName(output_file_path)
|
||||
self.viewer.print(printer)
|
||||
iface.messageBar().pushSuccess(
|
||||
@@ -202,7 +185,7 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
"<a href=\"{}\">{}</a>").format(parent_folder, output_file_path)
|
||||
)
|
||||
|
||||
elif output_format in [OutputFormats.HTML,OutputFormats.DCAT]:
|
||||
elif output_format in [OutputFormats.HTML, OutputFormats.DCAT]:
|
||||
if output_format == OutputFormats.HTML:
|
||||
data_str = self.viewer.page().currentFrame().toHtml()
|
||||
else:
|
||||
@@ -215,7 +198,7 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
xml_template = xml_file.read()
|
||||
|
||||
xml = parseString(xml_template.format(language=data[0][0], content=data[0][1]))
|
||||
|
||||
|
||||
data_str = xml.toprettyxml()
|
||||
|
||||
with open(output_file[0], "w", encoding='utf8') as file_writer:
|
||||
@@ -231,43 +214,15 @@ class CenRa_Metabase(QDockWidget, DOCK_CLASS):
|
||||
def save_auto_open_dock(self):
|
||||
""" Save settings about the dock. """
|
||||
self.settings.setValue("pgmetadata/auto_open_dock", self.auto_open_dock_action.isChecked())
|
||||
def sql_to_xml(self,dataall):
|
||||
distribution=''
|
||||
|
||||
def sql_to_xml(self, dataall):
|
||||
distribution = ''
|
||||
for y in dataall[1]:
|
||||
distribution = distribution + (
|
||||
'<dcat:distribution>'+
|
||||
'<dcat:Distribution>'+
|
||||
'<dct:title>{data}</dct:title>'.format(data=y[0])+
|
||||
'<dcat:downloadURL>{data}</dcat:downloadURL>'.format(data=y[1])+
|
||||
'<dcat:mediaType>{data}</dcat:mediaType>'.format(data=y[2])+
|
||||
'<dct:format>{data}</dct:format>'.format(data=y[3])+
|
||||
'<dct:bytesize>{data}</dct:bytesize>'.format(data=y[4])+
|
||||
'</dcat:Distribution>'+
|
||||
'</dcat:distribution>')
|
||||
publisher=''
|
||||
distribution = distribution + ('<dcat:distribution>' + '<dcat:Distribution>' + '<dct:title>{data}</dct:title>'.format(data=y[0]) + '<dcat:downloadURL>{data}</dcat:downloadURL>'.format(data=y[1]) + '<dcat:mediaType>{data}</dcat:mediaType>'.format(data=y[2]) + '<dct:format>{data}</dct:format>'.format(data=y[3]) + '<dct:bytesize>{data}</dct:bytesize>'.format(data=y[4]) + '</dcat:Distribution>' + '</dcat:distribution>')
|
||||
publisher = ''
|
||||
for z in dataall[2]:
|
||||
publisher = publisher + (
|
||||
'<dct:publisher>'+
|
||||
'<foaf:Organization>'+
|
||||
'<foaf:name>{data}</foaf:name>'.format(data=z[1])+
|
||||
'<foaf:mbox>{data}</foaf:mbox>'.format(data=z[3])+
|
||||
'</foaf:Organization>'+
|
||||
'</dct:publisher>')
|
||||
data_str = [[dataall[0][26],
|
||||
'<dct:identifier>{data}</dct:identifier>'.format(data=dataall[0][1])+
|
||||
'<dct:title>{data}</dct:title>'.format(data=dataall[0][4])+
|
||||
'<dct:description>{data}</dct:description>'.format(data=dataall[0][5])+
|
||||
'<dct:language>{data}</dct:language>'.format(data=dataall[0][26])+
|
||||
'<dct:spatial>{data}</dct:spatial>'.format(data=dataall[0][28])+
|
||||
'<dct:created rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:created>'.format(data=dataall[0][20])+
|
||||
'<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:issued>'.format(data=dataall[0][11])+
|
||||
'<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:modified>'.format(data=dataall[0][21])+
|
||||
'<dct:license>{data}</dct:license>'.format(data=dataall[0][13])+
|
||||
distribution+
|
||||
publisher+
|
||||
'<dcat:theme>{data}</dcat:theme>'.format(data=", ".join(str(x) for x in dataall[0][24]))+
|
||||
'<dcat:keyword>{data}</dcat:keyword>'.format(data=", ".join(str(x) for x in dataall[0][6]))+
|
||||
'<dct:accrualPeriodicity>{data}</dct:accrualPeriodicity>'.format(data=dataall[0][12])]]
|
||||
publisher = publisher + ('<dct:publisher>' + '<foaf:Organization>' + '<foaf:name>{data}</foaf:name>'.format(data=z[1]) + '<foaf:mbox>{data}</foaf:mbox>'.format(data=z[3]) + '</foaf:Organization>' + '</dct:publisher>')
|
||||
data_str = [[dataall[0][26], '<dct:identifier>{data}</dct:identifier>'.format(data=dataall[0][1]) + '<dct:title>{data}</dct:title>'.format(data=dataall[0][4]) + '<dct:description>{data}</dct:description>'.format(data=dataall[0][5]) + '<dct:language>{data}</dct:language>'.format(data=dataall[0][26]) + '<dct:spatial>{data}</dct:spatial>'.format(data=dataall[0][28]) + '<dct:created rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:created>'.format(data=dataall[0][20]) + '<dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:issued>'.format(data=dataall[0][11]) + '<dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">{data}</dct:modified>'.format(data=dataall[0][21]) + '<dct:license>{data}</dct:license>'.format(data=dataall[0][13]) + distribution + publisher + '<dcat:theme>{data}</dcat:theme>'.format(data=", ".join(str(x) for x in dataall[0][24])) + '<dcat:keyword>{data}</dcat:keyword>'.format(data=", ".join(str(x) for x in dataall[0][6])) + '<dct:accrualPeriodicity>{data}</dct:accrualPeriodicity>'.format(data=dataall[0][12])]]
|
||||
|
||||
return data_str
|
||||
|
||||
@@ -277,7 +232,7 @@ publisher+
|
||||
locale = QgsSettings().value("locale/userLocale", QLocale().name())
|
||||
locale = locale.split('_')[0].lower()
|
||||
|
||||
if output_format == [OutputFormats.HTML,OutputFormats.DCAT]:
|
||||
if output_format == [OutputFormats.HTML, OutputFormats.DCAT]:
|
||||
sql = (
|
||||
"SELECT pgmetadata.get_dataset_item_html_content('{schema}', '{table}', '{locale}');"
|
||||
).format(schema=uri.schema(), table=uri.table(), locale=locale)
|
||||
@@ -292,7 +247,6 @@ publisher+
|
||||
self.current_connection = None
|
||||
self.ce_trouve_dans_psql(layer)
|
||||
|
||||
|
||||
def add_flatten_dataset_table(self):
|
||||
""" Add a flatten dataset table with all links and contacts. """
|
||||
'''
|
||||
@@ -337,17 +291,17 @@ publisher+
|
||||
|
||||
def set_html_content(self, title=None, body=None):
|
||||
""" Set the content in the dock. """
|
||||
#link_logo=resources_path('icons', 'CEN_RA.png')
|
||||
# ink_logo=resources_path('icons', 'CEN_RA.png')
|
||||
css_file = resources_path('css', 'dock.css')
|
||||
with open(css_file, 'r', encoding='utf8') as f:
|
||||
css = f.read()
|
||||
|
||||
html = '<html><head>'
|
||||
#html += '<script src="http://ignf.github.io/geoportal-sdk/latest/dist/2d/GpSDK2D.js" defer ></script>'
|
||||
# html += '<script src="http://ignf.github.io/geoportal-sdk/latest/dist/2d/GpSDK2D.js" defer ></script>'
|
||||
html += '<style>{css}</style></head><body>'.format(css=css)
|
||||
#html += '<link rel="stylesheet" href="http://ignf.github.io/geoportal-sdk/latest/dist/2d/GpSDK2D.css" >'
|
||||
#html += '<script src="file:///C:/Users/tlaveille/Desktop/maps.js" defer></script>'
|
||||
#html += '<noscript>Your browser does not support JavaScript!</noscript>'
|
||||
# html += '<link rel="stylesheet" href="http://ignf.github.io/geoportal-sdk/latest/dist/2d/GpSDK2D.css" >'
|
||||
# html += '<script src="file:///C:/Users/tlaveille/Desktop/maps.js" defer></script>'
|
||||
# html += '<noscript>Your browser does not support JavaScript!</noscript>'
|
||||
if title:
|
||||
html += '<h2>{title} <img class=logo src=https://i2.wp.com/www.cen-rhonealpes.fr/wp-content/uploads/2013/04/cen-rhonealpes-couleurs1.jpg?w=340&ssl=1></h2>'.format(title=title)
|
||||
if body:
|
||||
@@ -356,16 +310,16 @@ publisher+
|
||||
html += '</body></html>'
|
||||
|
||||
# It must be a file, even if it does not exist on the file system.
|
||||
base_url = QUrl.fromLocalFile(resources_path('images', 'must_be_a_file.png'))
|
||||
self.viewer.setHtml(html)#, base_url)
|
||||
# base_url = QUrl.fromLocalFile(resources_path('images', 'must_be_a_file.png'))
|
||||
self.viewer.setHtml(html)
|
||||
|
||||
def ce_trouve_dans_psql(self,layer):
|
||||
def ce_trouve_dans_psql(self, layer):
|
||||
try:
|
||||
uri = layer.dataProvider().uri()
|
||||
except:
|
||||
except AttributeError:
|
||||
self.default_html_content_not_pg_layer()
|
||||
self.save_button.setEnabled(False)
|
||||
uri=''
|
||||
uri = ''
|
||||
if uri != '':
|
||||
if not uri.table():
|
||||
layertype = layer.providerType().lower()
|
||||
@@ -376,23 +330,23 @@ publisher+
|
||||
self.save_button.setEnabled(False)
|
||||
else:
|
||||
data_count = self.sql_check(uri)
|
||||
#print(data_count)
|
||||
# print(data_count)
|
||||
if data_count == 0:
|
||||
self.default_html_content_not_metadata()
|
||||
self.save_button.setEnabled(False)
|
||||
else:
|
||||
self.build_html_content(layer,uri)
|
||||
self.build_html_content(layer, uri)
|
||||
self.save_button.setEnabled(True)
|
||||
|
||||
def build_html_content(self,layer,uri):
|
||||
def build_html_content(self, layer, uri):
|
||||
body = ''
|
||||
|
||||
dataall=self.sql_info(uri)
|
||||
data=dataall[0]
|
||||
data_url=dataall[1]
|
||||
data_contact=dataall[2]
|
||||
#print(len(data_url))
|
||||
data_collonne=[field.name() for field in layer.dataProvider().fields()]
|
||||
dataall = self.sql_info(uri)
|
||||
data = dataall[0]
|
||||
data_url = dataall[1]
|
||||
data_contact = dataall[2]
|
||||
# print(len(data_url))
|
||||
# data_collonne = [field.name() for field in layer.dataProvider().fields()]
|
||||
|
||||
body += '<div><h3>Identification</h3><table class="table table-condensed">'
|
||||
body += '<tr><th>Titre</th><td>{data[4]}</td></tr>'.format(data=data)
|
||||
@@ -415,7 +369,7 @@ publisher+
|
||||
body += '<tr><th>Emprise</th><td>{data[28]}</td></tr>'.format(data=data)
|
||||
body += '</table></div>'
|
||||
|
||||
#body += '<div id="map"></div>'
|
||||
# body += '<div id="map"></div>'
|
||||
|
||||
body += '<div><h3>Publication</h3><table class="table table-condensed">'
|
||||
body += '<tr><th>Date</th><td>{data[11]}</td></tr>'.format(data=data)
|
||||
@@ -454,45 +408,46 @@ publisher+
|
||||
self.set_html_content(
|
||||
layer.name(), body)
|
||||
|
||||
def set_html_to_wms(self,layer):
|
||||
def set_html_to_wms(self, layer):
|
||||
self.set_html_content(
|
||||
'CenRa Metadata',(layer.htmlMetadata()))
|
||||
'CenRa Metadata', (layer.htmlMetadata()))
|
||||
|
||||
def default_html_content_not_pg_layer(self):
|
||||
""" When it's not a PostgreSQL layer. """
|
||||
self.set_html_content(
|
||||
'CenRa Metadata', ('Vous devez cliquer sur une couche dans la légende qui est stockée dans PostgreSQL.'))
|
||||
|
||||
def default_html_content_not_metadata(self):
|
||||
self.set_html_content(
|
||||
'CenRa Metadata', ('La couche ne contien pas de métadonnée.'))
|
||||
|
||||
def sql_check(self,uri):
|
||||
cur=login_base()
|
||||
def sql_check(self, uri):
|
||||
cur = login_base()
|
||||
table = uri.table()
|
||||
schema = uri.schema()
|
||||
|
||||
sql_count = """SELECT count(uid) FROM metadata.dataset
|
||||
WHERE schema_name LIKE '"""+schema+"""' AND table_name LIKE '"""+table+"""';"""
|
||||
WHERE schema_name LIKE '""" + schema + """' AND table_name LIKE '""" + table + """';"""
|
||||
|
||||
cur.execute(sql_count)
|
||||
data_count = cur.fetchall()
|
||||
cur.close()
|
||||
return data_count[0][0]
|
||||
|
||||
def sql_info(self,uri):
|
||||
cur=login_base()
|
||||
def sql_info(self, uri):
|
||||
cur = login_base()
|
||||
table = uri.table()
|
||||
schema = uri.schema()
|
||||
#[s for s in iface.activeLayer().source().split(" ") if "dbname" in s][0].split("'")[1]
|
||||
# [s for s in iface.activeLayer().source().split(" ") if "dbname" in s][0].split("'")[1]
|
||||
sql_find = """SELECT *,right(left(st_astext(geom,2),-2),-9) FROM metadata.dataset
|
||||
WHERE schema_name LIKE '"""+schema+"""' AND table_name LIKE '"""+table+"""';"""
|
||||
WHERE schema_name LIKE '""" + schema + """' AND table_name LIKE '""" + table + """';"""
|
||||
cur.execute(sql_find)
|
||||
data_general = cur.fetchall()
|
||||
sql_findurl = """SELECT type,url,mime,format,taille FROM metadata.dataurl WHERE schema_name LIKE '"""+schema+"""' AND table_name LIKE '"""+table+"""';"""
|
||||
sql_findurl = """SELECT type,url,mime,format,taille FROM metadata.dataurl WHERE schema_name LIKE '""" + schema + """' AND table_name LIKE '""" + table + """';"""
|
||||
cur.execute(sql_findurl)
|
||||
data_url = cur.fetchall()
|
||||
sql_findcontact = """SELECT role,nom,organisation,email,telephone FROM metadata.datacontact WHERE schema_name LIKE '"""+schema+"""' AND table_name LIKE '"""+table+"""';"""
|
||||
sql_findcontact = """SELECT role,nom,organisation,email,telephone FROM metadata.datacontact WHERE schema_name LIKE '""" + schema + """' AND table_name LIKE '""" + table + """';"""
|
||||
cur.execute(sql_findcontact)
|
||||
data_contact = cur.fetchall()
|
||||
cur.close()
|
||||
return data_general[0],data_url,data_contact
|
||||
|
||||
return data_general[0], data_url, data_contact
|
||||
|
||||
Reference in New Issue
Block a user