def anacondaprompt(self): if os.name == "nt": root_dir = utils.get_install_dname("root") my_env = os.environ.copy() my_env["PYTHONPATH"] = os.path.join(root_dir, "Python36_64") my_env["PATH"] = ";".join( [ os.path.join(root_dir, "Python36_64", "Scripts", "conda_exes"), my_env["PATH"], ] ) pydir = utils.get_install_dname("python") my_env["PATH"] = ";".join( [os.path.join(pydir, "Scripts", "conda_exes"), my_env["PATH"]] ) self.ui.actionOpen_sb.triggered.connect(self.open_sb_file) activatebat = os.path.join(pydir, "Scripts", "conda_exe", "activate.bat") msg = "This is experimental functionality used for opening an Anaconda command prompt set to" msg += "\nthe Python environment shipped with the MetadataWizard.\n\n" msg += "The base conda env in this prompt is the one to use, so do not use the activate command." msg += "\nUse: conda install ...package.. to install new packages into the MetadataWizard envronment." QMessageBox.information(self, "Conda instructions", msg) subprocess.Popen( ["start", "cmd", activatebat, pydir], env=my_env, cwd=pydir, shell=True ) else: msg = "This experimental functionality not yet implemented for Mac or Linux builds" QMessageBox.warning(self, "Not implemented", msg)
def launch(self): jupyter_dname = self.ui.dname.currentText() if not os.path.exists(jupyter_dname): msg = 'The selected directory to lauch jupyter in does not exists.' msg += '\nPlease check the location before launching Jupyter.' QMessageBox.information(self, "Missing Directory", msg) return if self.ui.kernel.currentText() == self.default_kernel: # this is pymdwizard specific python_dir = utils.get_install_dname('python') if platform.system() == 'Darwin': jupyterexe = os.path.join(python_dir, 'jupyter') p = Popen([jupyterexe, 'notebook'], cwd=jupyter_dname) else: root_dir = utils.get_install_dname('root') jupyterexe = os.path.join(python_dir, "scripts", "jupyter.exe") my_env = os.environ.copy() my_env["PYTHONPATH"] = os.path.join(root_dir, "Python36_64") p = Popen([jupyterexe, 'notebook'], cwd=jupyter_dname, env=my_env) else: root_dname, envs_dname = self.get_conda_root() activate_fname = os.path.join(root_dname, 'Scripts', 'activate.bat') jupyter_exe_name = os.path.join(envs_dname, self.ui.kernel.currentText(), 'Scripts', 'jupyter.exe') if not os.path.exists(jupyter_exe_name): jupyter_exe_name = os.path.join(root_dname, 'Scripts', 'jupyter.exe') if self.ui.kernel.currentText() == 'root': cmds = ['"{}"'.format(jupyter_exe_name), 'notebook'] else: cmds = [ '"{}"'.format(activate_fname), self.ui.kernel.currentText(), '&&', '"{}"'.format(jupyter_exe_name), 'notebook' ] Popen(" ".join(cmds), cwd=jupyter_dname) msg = 'Jupyter launching...\nJupyter will start momentarily in a new tab in your default internet browser.' QMessageBox.information(self, "Launching Jupyter", msg) self.update_function(self.ui.kernel.currentText(), self.ui.dname.currentText()) self.close()
def update_from_github(self): from subprocess import check_output install_dir = utils.get_install_dname() root_dir = os.path.dirname(install_dir) update_bat = os.path.join(root_dir, 'update_wizard.bat') if os.path.exists(update_bat) and os.path.exists(root_dir): try: QApplication.setOverrideCursor(Qt.WaitCursor) p = check_output([update_bat], cwd=root_dir, shell=False) if p.splitlines()[-1] == b'Already up-to-date.': msg = 'Application already up to date.' else: msg = 'Application updated.\n\n' msg += 'Please close and restart for these updates to take effect' QApplication.restoreOverrideCursor() except BaseException as e: import traceback msg = "Could not update application:\n{}".format(traceback.format_exc()) QMessageBox.warning(self, "Recent Files", msg) else: msg = 'Could not find the batch file to update the application' QMessageBox.information(self, "Update results", msg)
def update_from_github(self): """ Merge the latest version of the Wizard into the local repo Returns ------- None """ try: from git import Repo install_dir = utils.get_install_dname("pymdwizard") repo = Repo(install_dir) fetch = [r for r in repo.remotes if r.name == "usgs_root"][0].fetch() master = [f for f in fetch if f.name == "usgs_root/master"][0] merge_msg = repo.git.merge(master.name) msg = "Updated Successfully from GitHub." QMessageBox.information(self, "Update results", msg) except BaseException as e: msg = ( "Problem Encountered Updating from GitHub\n\n" "Please upgrade to the latest release by reinstalling the " "application from GitHub " "\n(https://github.com/usgs/fort-pymdwizard/releases)\n\n" "Error Message:\n" ) msg += str(e) QMessageBox.information(self, "Update results", msg) QApplication.restoreOverrideCursor()
def populate_kernels(self): self.ui.kernel.addItem("pymdwizard <<default>>") self.kernels["pymdwizard <<default>>"] = utils.get_install_dname( "python") try: conda_exe = os.path.join(self.get_conda_root()[0], "Scripts", "conda.exe") if os.path.exists(conda_exe): kernels = subprocess.check_output([conda_exe, "env", "list"]) else: kernels = subprocess.check_output(["conda", "env", "list"]) for line in kernels.split(b"\n"): if line and not line.strip().startswith(b"#"): try: parts = line.split() if parts[1] == b"*": parts = [parts[0], parts[2]] name, path = parts self.ui.kernel.addItem(str(name)[2:-1]) self.kernels[str(name)[2:-1]] = str(path) except (ValueError, IndexError): pass except: pass
def update_from_github(self): """ Merge the latest version of the Wizard into the local repo Returns ------- None """ try: from git import Repo install_dir = utils.get_install_dname("pymdwizard") repo = Repo(install_dir) fetch = [r for r in repo.remotes if r.name == "origin"][0].fetch() master = [f for f in fetch if f.name == "origin/master"][0] merge_msg = repo.git.merge(master.name) msg = "Updated Successfully from GitHub. Close and re-open Metadata Wizard for changes to be implemented." QMessageBox.information(self, "Update results", msg) except BaseException as e: msg = ( "Problem Encountered Updating from GitHub\n\n" "USGS users, if you experience issues, please try disconnecting/reconnecting to the internal USGS network and re-checking for updates." ) # msg += str(e) QMessageBox.information(self, "Update results", msg) QApplication.restoreOverrideCursor()
def save_file(self): """ Save the current xml document. Prompts for a filename if one has not been set yet. Returns ------- None """ # Test utils.get_install_dname() if not self.cur_fname: fname = self.get_save_name() if not fname: return else: fname = self.cur_fname fname_msg = utils.check_fname(fname) if not fname_msg == "good": msg = "Cannot write to :\n {}.".format(fname) QMessageBox.warning(self, "Metadata Wizard", msg) return tool_comment = ( "Record created using version {} of the " "USGS Metadata Wizard tool. (https://github.com/usgs/" "fort-pymdwizard)".format(__version__) ) xml_contents = self.metadata_root.to_xml() comment = xml_utils.xml_node(tag="", text=tool_comment, index=0, comment=True) xml_contents.addprevious(comment) xml_utils.save_to_file(xml_contents, fname) self.last_updated = time.time() self.set_current_file(fname) self.statusBar().showMessage("File saved", 2000)
def populate_kernels(self): self.ui.kernel.addItem('pymdwizard <<default>>') self.kernels['pymdwizard <<default>>'] = \ utils.get_install_dname('python') kernels = subprocess.check_output(['conda', 'env', 'list']) for line in kernels.split(b'\n'): if line and not line.strip().startswith(b'#'): try: parts = line.split() if parts[1] == b'*': parts = [parts[0], parts[2]] name, path = parts self.ui.kernel.addItem(str(name)[2:-1]) self.kernels[str(name)[2:-1]] = str(path) except (ValueError, IndexError): pass
def launch_help(self): root_dname = utils.get_install_dname('pymdwizard') help_html = os.path.join(root_dname, 'docs', 'html_output', 'index.html') if not os.path.exists(help_html): gui_fname = os.path.dirname(os.path.realpath(__file__)) help_html = os.path.join(gui_fname, '..', '..', 'docs', 'html_output', 'index.html') self.preview = Preview(url=help_html) self.preview_dialog = QDialog(self) self.preview_dialog.setWindowTitle('MetadataWizard Help') self.preview_dialog.setLayout(self.preview.layout()) self.preview_dialog.exec_()
def launch_help(self): root_dname = utils.get_install_dname("pymdwizard") help_html = os.path.join(root_dname, "docs", "html_output", "index.html") if not os.path.exists(help_html): gui_fname = os.path.dirname(os.path.realpath(__file__)) help_html = os.path.join( gui_fname, "..", "..", "docs", "html_output", "index.html" ) self.preview = Preview(url=help_html) self.preview_dialog = QDialog(self) self.preview_dialog.setWindowTitle("MetadataWizard Help") self.preview_dialog.setLayout(self.preview.layout()) self.preview_dialog.exec_()
def check_for_updates(self, e=None, show_uptodate_msg=True): """ Check if the usgs_root repo is at the same commit as this installation Parameters ---------- e : qt event show_uptodate_msg : bool Whether to display a msg if no updates found Returns ------- None """ try: from git import Repo install_dir = utils.get_install_dname("pymdwizard") repo = Repo(install_dir) fetch = [r for r in repo.remotes if r.name == "origin"][0].fetch() master = [f for f in fetch if f.name == "origin/master"][0] if repo.head.commit != master.commit: msg = "An update(s) are available for the Metadata Wizard.\n" msg += "Would you like to install these now?" confirm = QMessageBox.question( self, "Updates Available", msg, QMessageBox.Yes | QMessageBox.No ) if confirm == QMessageBox.Yes: self.update_from_github() elif show_uptodate_msg: msg = "MetadataWizard already up to date." QMessageBox.information(self, "No Update Needed", msg) except BaseException as e: if show_uptodate_msg: msg = ( "Problem Encountered Updating from GitHub\n\n" "USGS users, if you experience issues, please try disconnecting/reconnecting to the internal USGS network and re-checking for updates." ) # msg += str(e) QMessageBox.information(self, "Update results", msg)
def check_for_updates(self): from subprocess import check_output install_dir = utils.get_install_dname() root_dir = os.path.dirname(install_dir) git_exe = os.path.join(root_dir, 'Python36_64', 'Library', 'bin', 'git.exe') try: fetch = check_output([git_exe, 'fetch', 'usgs_root'], cwd=install_dir, shell=True) updates = check_output([git_exe, 'log', 'HEAD..usgs_root/master'], cwd=install_dir, shell=True) if updates: msg = "An update(s) are available for the Metadata Wizard.\n" msg += "Would you like to install these now?" confirm = QMessageBox.question(self, "Updates Available", msg, QMessageBox.Yes|QMessageBox.No) if confirm == QMessageBox.Yes: self.update_from_github() except BaseException as e: pass
def check_for_updates(self, e=None, show_uptodate_msg=True): """ Check if the usgs_root repo is at the same commit as this installation Parameters ---------- e : qt event show_uptodate_msg : bool Whether to display a msg if no updates found Returns ------- None """ try: from git import Repo install_dir = utils.get_install_dname('pymdwizard') repo = Repo(install_dir) fetch = [r for r in repo.remotes if r.name == 'usgs_root'][0].fetch() master = [f for f in fetch if f.name == 'usgs_root/master'][0] if repo.head.commit != master.commit: msg = "An update(s) are available for the Metadata Wizard.\n" msg += "Would you like to install these now?" confirm = QMessageBox.question( self, "Updates Available", msg, QMessageBox.Yes | QMessageBox.No) if confirm == QMessageBox.Yes: self.update_from_github() elif show_uptodate_msg: msg = "MetadataWizard already up to date." QMessageBox.information(self, "No Update Needed", msg) except BaseException as e: if not show_uptodate_msg: msg = 'Problem Encountered Updating from GitHub\n\nError Message:\n' msg += str(e) QMessageBox.information(self, "Update results", msg)
def launch_jupyter(self): """ Launches a jupyter notebook server in our examples directory Returns ------- None """ last_kernel = self.settings.value('last_kernel', '') jupyter_dnames = self.settings.value('jupyter_dnames', []) if not jupyter_dnames: install_dir = utils.get_install_dname() jupyter_dnames = [os.path.join(install_dir, 'examples')] self.settings.setValue('jupyter_dnames', jupyter_dnames) self.jupyter_dialog = JupyterStarter( last_kernel=last_kernel, previous_dnames=jupyter_dnames, update_function=self.update_jupyter_dnames) utils.set_window_icon(self.jupyter_dialog) self.jupyter_dialog.show()
def launch_jupyter(self): """ Launches a jupyter notebook server in our examples directory Returns ------- None """ from subprocess import Popen jupyter_dialog = JupyterLocationDialog() utils.set_window_icon(jupyter_dialog.msgBox) jupyter_dialog.msgBox.setWindowTitle("Where do you want to launch Jupyter?") ret = jupyter_dialog.msgBox.exec_() install_dir = utils.get_install_dname() if ret == 0: jupyter_dname = os.path.join(install_dir, 'examples') elif ret == 1: settings = QSettings('USGS', 'pymdwizard') last_jupyter_dname = settings.value('last_jupyter_dname') if last_jupyter_dname is None: last_jupyter_dname = os.path.join(install_dir, 'examples') jupyter_dname = QFileDialog.getExistingDirectory(self, "Select Directory to launch Jupyter from", last_jupyter_dname) if jupyter_dname: settings.setValue('last_jupyter_dname', jupyter_dname) else: return root_dir = os.path.dirname(install_dir) jupyterexe = os.path.join(root_dir, "Python35_64", "scripts", "jupyter.exe") if os.path.exists(jupyterexe) and os.path.exists(root_dir): p = Popen([jupyterexe, 'notebook'], cwd=jupyter_dname) msg = 'Jupyter launching...\nJupyter will start momentarily in a new tab in your default internet browser.' QMessageBox.information(self, "Launching Jupyter", msg)
Although this program has been used by the U.S. Geological Survey (USGS), no warranty, expressed or implied, is made by the USGS or the U.S. Government as to the accuracy and functioning of the program and related program material nor shall the fact of distribution constitute any such warranty, and no responsibility is assumed by the USGS in connection therewith. ------------------------------------------------------------------------------ """ import os import collections from pymdwizard.core.xml_utils import xml_node from pymdwizard.core import utils try: python_root = utils.get_install_dname('python') gdal_data = os.path.join(python_root, 'Library', 'share', 'gdal') os.environ['GDAL_DATA'] = gdal_data from osgeo import gdal, osr, ogr gdal.UseExceptions() gdal.AllRegister() use_gdal = True except ImportError: print('ERROR Importing GDAL, Spatial functionality limited') use_gdal = False def _get_raster_extent(src): """ extract projected extent from a raster dataset
def launch(self): jupyter_dname = self.ui.dname.currentText() if not os.path.exists(jupyter_dname): msg = "The selected directory to lauch jupyter in does not exists." msg += "\nPlease check the location before launching Jupyter." QMessageBox.information(self, "Missing Directory", msg) return if self.ui.kernel.currentText() == self.default_kernel: # this is pymdwizard specific python_dir = utils.get_install_dname("python") if platform.system() == "Darwin": jupyterexe = os.path.join(python_dir, "jupyter") my_env = os.environ.copy() my_env["PYTHONPATH"] = os.path.join(python_dir, "python") p = Popen([jupyterexe, "notebook"], cwd=jupyter_dname) else: root_dir = utils.get_install_dname("root") jupyterexe = os.path.join(python_dir, "scripts", "jupyter.exe") my_env = os.environ.copy() my_env["PYTHONPATH"] = os.path.join(root_dir, "Python36_64") if self.ui.usejupyterlab.isChecked(): jupytertype = "lab" else: jupytertype = "notebook" p = Popen([jupyterexe, jupytertype], cwd=jupyter_dname, env=my_env) else: root_dname, envs_dname = self.get_conda_root() activate_fname = os.path.join(root_dname, "Scripts", "activate.bat") jupyter_exe_name = os.path.join(envs_dname, self.ui.kernel.currentText(), "Scripts", "jupyter.exe") if not os.path.exists(jupyter_exe_name): jupyter_exe_name = os.path.join(root_dname, "Scripts", "jupyter.exe") if self.ui.usejupyterlab.isChecked(): jupytertype = "lab" else: jupytertype = "notebook" if self.ui.kernel.currentText() == "root": cmds = ['"{}"'.format(jupyter_exe_name), jupytertype] else: cmds = [ '"{}"'.format(activate_fname), self.ui.kernel.currentText(), "&&", '"{}"'.format(jupyter_exe_name), jupytertype, ] Popen(" ".join(cmds), cwd=jupyter_dname) msg = "Jupyter launching...\nJupyter will start momentarily in a new tab in your default internet browser." QMessageBox.information(self, "Launching Jupyter", msg) self.update_function(self.ui.kernel.currentText(), self.ui.dname.currentText()) self.close()