diff --git a/CenRa_Metabase/resources/PythonSQL.py b/CenRa_Metabase/resources/PythonSQL.py new file mode 100644 index 00000000..0b556bde --- /dev/null +++ b/CenRa_Metabase/resources/PythonSQL.py @@ -0,0 +1,32 @@ +import sys +import socket +import os +IPAddr=socket.gethostbyname(socket.gethostname()) +#print(IPAddr) +if IPAddr[0:11] == "100.100.100": #4269 + host = "100.100.100.11" + port = "5432" + dbname = "sig4269" + sigdb="sig4269" + refdb="ref_geo4269" + password = "McVities" +if IPAddr[0:9] == "192.168.0": #01 + host = "192.168.0.201" + port = "5432" + dbname = "sig01" + sigdb="sig01" + refdb="ref_geo01" + password = "McVities" +if IPAddr[0:9] == "192.168.1": #0726 + host = "192.168.1.201" + port = "5432" + dbname = "sig0726" + sigdb="sig0726" + refdb="ref_geo0726" + password = "McVities" +if sys.platform == 'linux': + os_user = os.environ['USER'] +else: + os_user = os.environ['USERNAME'] +if os_user == 'STAGE': + os_user='stage' \ No newline at end of file diff --git a/CenRa_Metabase/resources/i18n.py b/CenRa_Metabase/resources/i18n.py new file mode 100644 index 00000000..64a21dac --- /dev/null +++ b/CenRa_Metabase/resources/i18n.py @@ -0,0 +1,49 @@ +"""I18n tools.""" + +from os.path import join + +from qgis.PyQt.QtCore import QLocale, QFileInfo +from qgis.PyQt.QtWidgets import QApplication +from qgis.core import QgsSettings + +from .resources import resources_path + +__copyright__ = "Copyright 2019, 3Liz" +__license__ = "GPL version 3" +__email__ = "info@3liz.org" +__revision__ = "$Format:%H$" + + +def setup_translation(file_pattern="{}.qm", folder=None): + """Find the translation file according to locale. + + :param file_pattern: Custom file pattern to use to find QM files. + :type file_pattern: basestring + + :param folder: Optional folder to look in if it's not the default. + :type folder: basestring + + :return: The locale and the file path to the QM file, or None. + :rtype: (basestring, basestring) + """ + locale = QgsSettings().value("locale/userLocale", QLocale().name()) + + if folder: + ts_file = QFileInfo(join(folder, file_pattern.format(locale))) + else: + ts_file = QFileInfo(resources_path("i18n", file_pattern.format(locale))) + if ts_file.exists(): + return locale, ts_file.absoluteFilePath() + + if folder: + ts_file = QFileInfo(join(folder, file_pattern.format(locale[0:2]))) + else: + ts_file = QFileInfo(resources_path("i18n", file_pattern.format(locale[0:2]))) + if ts_file.exists(): + return locale, ts_file.absoluteFilePath() + + return locale, None + + +def tr(text, context="@default"): + return QApplication.translate(context, text) diff --git a/CenRa_Metabase/resources/lecture_sql.py b/CenRa_Metabase/resources/lecture_sql.py new file mode 100644 index 00000000..06d74050 Binary files /dev/null and b/CenRa_Metabase/resources/lecture_sql.py differ diff --git a/CenRa_Metabase/resources/resources.py b/CenRa_Metabase/resources/resources.py new file mode 100644 index 00000000..c02612bd --- /dev/null +++ b/CenRa_Metabase/resources/resources.py @@ -0,0 +1,164 @@ +"""Tools to work with resource files.""" + +import configparser +import shutil +import tempfile +import base64 +import psycopg2 +import psycopg2.extras +from os.path import abspath, join, pardir, dirname + +from qgis.PyQt import uic + +__copyright__ = "Copyright 2019, 3Liz" +__license__ = "GPL version 3" +__email__ = "info@3liz.org" +__revision__ = "$Format:%H$" + + +def plugin_path(*args): + """Get the path to plugin root folder. + + :param args List of path elements e.g. ['img', 'logos', 'image.png'] + :type args: str + + :return: Absolute path to the plugin path. + :rtype: str + """ + path = dirname(dirname(__file__)) + path = abspath(abspath(join(path, pardir))) + for item in args: + path = abspath(join(path, item)) + + return path + + +def plugin_name(): + """Return the plugin name according to metadata.txt. + + :return: The plugin name. + :rtype: basestring + """ + metadata = metadata_config() + name = metadata["general"]["name"] + return name + + +def metadata_config() -> configparser: + """Get the INI config parser for the metadata file. + + :return: The config parser object. + :rtype: ConfigParser + """ + path = plugin_path("metadata.txt") + config = configparser.ConfigParser() + config.read(path, encoding='utf8') + return config + + +def plugin_test_data_path(*args, copy=False): + """Get the path to the plugin test data path. + + :param args List of path elements e.g. ['img', 'logos', 'image.png'] + :type args: str + + :param copy: If the file must be copied into a temporary directory first. + :type copy: bool + + :return: Absolute path to the resources folder. + :rtype: str + """ + path = abspath(abspath(join(plugin_path(), "test", "data"))) + for item in args: + path = abspath(join(path, item)) + + if copy: + temp = tempfile.mkdtemp() + shutil.copy(path, temp) + return join(temp, args[-1]) + else: + return path + + +def resources_path(*args): + """Get the path to our resources folder. + + :param args List of path elements e.g. ['img', 'logos', 'image.png'] + :type args: str + + :return: Absolute path to the resources folder. + :rtype: str + """ + path = abspath(abspath(join(plugin_path(), "CenRa_Metabase\\resources"))) + for item in args: + path = abspath(join(path, item)) + return path + + +def load_ui(*args): + """Get compile UI file. + + :param args List of path elements e.g. ['img', 'logos', 'image.png'] + :type args: str + + :return: Compiled UI file. + """ + ui_class, _ = uic.loadUiType(resources_path("ui", *args)) + + return ui_class + +def login_base(take=None): + from CenRa_Metabase.resources.PythonSQL import host,port,dbname,password,os_user + first_conn = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=first_cnx password=" + password) + first_cur = first_conn.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_cur.execute("SELECT mdp_w, login_w FROM pg_catalog.pg_user t1, admin_sig.vm_users_sig t2 WHERE t2.oid = t1.usesysid AND (login_w = '" + os_user + "' OR login_w = '" + os_user + "')") + res_ident = first_cur.fetchone() + mdp = base64.b64decode(str(res_ident[0])).decode('utf-8') + user = res_ident[1] + + #con = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=" + user + " password=" + mdp) + con = psycopg2.connect("host=" + '100.100.100.81' + " port=" + port + " dbname="+'postgres'+" user=" + 'tlaveille' + " password=" + 'tlaveille') + cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_conn.close() + + if take: + return cur,con + else: + return cur +def send_issues(url,titre,body,labels): + import requests + import json + import os + import qgis + + usr = os.environ['USERNAME'] + token = '9d0a4e0bea561710e0728f161f7edf4e5201e112' + url=url+'?token='+token + + headers = {'Authorization': 'token ' + token,'accept': 'application/json','Content-Type': 'application/json'} + + + payload = {'title': titre, 'body': body, 'labels': labels} + + r = requests.post(url, data=json.dumps(payload), headers=headers) + print(r.text) + return r + +def maj_verif(NAME): + import qgis + import urllib + from qgis.utils import iface + from qgis.core import Qgis + + url = qgis.utils.pluginMetadata(NAME,'repository') + URL = url+'/raw/branch/main/plugins.xml' +# print(URL) + version = qgis.utils.pluginMetadata(NAME,'version') + len_version = len(version) + version_web = str(urllib.request.urlopen(URL).read()) + plugin_num = version_web.find(NAME) + valeur_version_web = version_web.find('',plugin_num)+9 + version_plugin = version_web[valeur_version_web:valeur_version_web+len_version] + + if version_plugin == version: + iface.messageBar().pushMessage("MAJ :", "Des mise à jour de plugin sont disponibles.", level=Qgis.Info, duration=30) \ No newline at end of file