def convert(strings, fmt, tz): if tz: tz = make_timezone(tz) # Might raise ValueError else: tz = None if not fmt: try: py_import( "dateutil", { "pip": "python-dateutil", "linux-debian": "python-dateutil", "linux-ubuntu": "python-dateutil", "linux-fedora": "python-dateutil", }, ) except ImportError: # pragma: no cover raise ValueError("can't read dates without a format without " "the dateutil package") from dateutil import parser result = [parser.parse(s, ignoretz=tz is not None) for s in strings] else: result = [datetime.datetime.strptime(s, fmt) for s in strings] if tz is None: result = [dt.replace(tzinfo=None) for dt in result] elif hasattr(tz, "localize"): result = [tz.localize(dt) for dt in result] else: # Compute the time without daylight saving result = [dt.replace(tzinfo=tz) for dt in result] # Check if it is in fact during daylight saving if hasattr(tz, "normalize"): for i in xrange(len(result)): dt = result[i] dst = tz.dst(dt.replace(tzinfo=None)) if dst: result[i] = tz.normalize(dt) - dst # This is close enough but the way this work is by # essence ambiguous # If the given datetime falls right during the time # change period (one hour, two times a year): # For standard -> dst (spring): the time will be in # dst, although it doesn't actually exist (we stepped # over it by changing the clock) # For dst -> standard (fall): the time will be in dst, # although it could also have been standard (there is # noway to know which one was meant) return result
def convert(strings, fmt, tz): if tz: tz = make_timezone(tz) # Might raise ValueError else: tz = None if not fmt: try: py_import( 'dateutil', { 'pip': 'python-dateutil', 'linux-debian': 'python-dateutil', 'linux-ubuntu': 'python-dateutil', 'linux-fedora': 'python-dateutil' }) except ImportError: # pragma: no cover raise ValueError("can't read dates without a format without " "the dateutil package") from dateutil import parser result = [ parser.parse(s, ignoretz=tz is not None) for s in strings ] else: result = [datetime.datetime.strptime(s, fmt) for s in strings] if tz is None: result = [dt.replace(tzinfo=None) for dt in result] elif hasattr(tz, 'localize'): result = [tz.localize(dt) for dt in result] else: # Compute the time without daylight saving result = [dt.replace(tzinfo=tz) for dt in result] # Check if it is in fact during daylight saving if hasattr(tz, 'normalize'): for i in xrange(len(result)): dt = result[i] dst = tz.dst(dt.replace(tzinfo=None)) if dst: result[i] = tz.normalize(dt) - dst # This is close enough but the way this work is by # essence ambiguous # If the given datetime falls right during the time # change period (one hour, two times a year): # For standard -> dst (spring): the time will be in # dst, although it doesn't actually exist (we stepped # over it by changing the clock) # For dst -> standard (fall): the time will be in dst, # although it could also have been standard (there is # noway to know which one was meant) return result
def compute(self): try: py_import('matplotlib', { 'pip': 'matplotlib', 'linux-debian': 'python-matplotlib', 'linux-ubuntu': 'python-matplotlib', 'linux-fedora': 'python-matplotlib'}) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") timestamps = self.get_input('timestamps') result = self.convert(timestamps) self.set_output('dates', result)
def compute(self): try: py_import('matplotlib', { 'pip': 'matplotlib', 'linux-debian': 'python-matplotlib', 'linux-ubuntu': 'python-matplotlib', 'linux-fedora': 'python-matplotlib'}) except ImportError: raise ModuleError(self, "matplotlib is not available") datetimes = self.getInputFromPort('datetimes') result = self.convert(datetimes) self.setResult('dates', result)
def compute(self): try: py_import( 'matplotlib', { 'pip': 'matplotlib', 'linux-debian': 'python-matplotlib', 'linux-ubuntu': 'python-matplotlib', 'linux-fedora': 'python-matplotlib' }) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") timestamps = self.get_input('timestamps') result = self.convert(timestamps) self.set_output('dates', result)
def make_timezone(s): if s == "local": return LocalTimezone() match = _decimal_fmt.match(s) if match is not None: sign, hours, minutes = match.groups("") sign = -1 if sign == "-" else 1 hours = int(hours) minutes = int(minutes) if minutes else 0 offset = datetime.timedelta(minutes=sign * (hours * 60 + minutes)) name = "%s%02d%02d" % ("-" if sign == -1 else "+", hours, minutes) return FixedOffset(offset, name) try: pytz = py_import( "pytz", {"pip": "pytz", "linux-debian": "python-tz", "linux-ubuntu": "python-tz", "linux-fedora": "pytz"} ) except ImportError: # pragma: no cover raise ValueError("can't understand timezone %r (maybe you should " "install pytz?)" % s) else: ver = LooseVersion(pytz.__version__) if ver < PYTZ_MIN_VER: # pragma: no cover warnings.warn( "You are using an old version of pytz (%s). You might " "run into some issues with daylight saving handling." % pytz.__version__, category=VistrailsWarning, ) try: return pytz.timezone(s) except KeyError: raise ValueError("can't understand timezone %r (defaulted to " "pytz, which gave no answer)" % s)
def XDestroyWindow(displayId, windowId): """ XDestroyWindow(displayId: void_p_str, windowId: void_p_str) -> None Destroy the X window specified by two strings displayId and windowId containing void pointer string of (Display*) and (Window) type. This is specific for VTKCell to remove the top shell window. Since VTK does not expose X11-related functions to Python, we have to use ctypes to hi-jack X11 library and call XDestroyWindow to kill the top-shell widget after reparent the OpenGL canvas to another Qt widget """ from vistrails.core.bundles import py_import ctypes = py_import( 'ctypes', { 'pip': 'ctypes', 'linux-debian': 'python-ctypes', 'linux-ubuntu': 'python-ctypes', 'linux-fedora': 'python-ctypes' }) c_void_p = ctypes.c_void_p displayPtr = c_void_p(int(displayId[1:displayId.find('_void_p')], 16)) windowPtr = c_void_p(int(windowId[1:windowId.find('_void_p')], 16)) libx = get_libX11() libx.XDestroyWindow(displayPtr, windowPtr)
def compute(self): try: py_import( "matplotlib", { "pip": "matplotlib", "linux-debian": "python-matplotlib", "linux-ubuntu": "python-matplotlib", "linux-fedora": "python-matplotlib", }, ) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") timestamps = self.getInputFromPort("timestamps") result = self.convert(timestamps) self.setResult("dates", result)
def compute(self): try: py_import('matplotlib', { 'pip': 'matplotlib', 'linux-debian': 'python-matplotlib', 'linux-ubuntu': 'python-matplotlib', 'linux-fedora': 'python-matplotlib'}) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") tz = self.get_input('timezone') strings = self.get_input('strings') fmt = self.get_input('format') try: result = self.convert(strings, fmt, tz) except ValueError, e: raise ModuleError(self, e.message)
def compute(self): try: py_import( 'matplotlib', { 'pip': 'matplotlib', 'linux-debian': 'python-matplotlib', 'linux-ubuntu': 'python-matplotlib', 'linux-fedora': 'python-matplotlib' }) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") tz = self.get_input('timezone') strings = self.get_input('strings') fmt = self.get_input('format') try: result = self.convert(strings, fmt, tz) except ValueError, e: raise ModuleError(self, e.message)
def get_libX11(): """ get_libX11() -> CDLL Return the X11 library loaded with ctypes. Only available on Linux. We also need a way to find the correct X11 library name on different machines. Right now, libX11.so.6 is used. """ from vistrails.core.bundles import py_import ctypes = py_import("ctypes", {"pip": "ctypes", "linux-debian": "python-ctypes", "linux-ubuntu": "python-ctypes"}) c_void_p = ctypes.c_void_p CDLL = ctypes.CDLL return CDLL("libX11.so.6")
def compute(self): try: py_import( "matplotlib", { "pip": "matplotlib", "linux-debian": "python-matplotlib", "linux-ubuntu": "python-matplotlib", "linux-fedora": "python-matplotlib", }, ) except ImportError: # pragma: no cover raise ModuleError(self, "matplotlib is not available") tz = self.getInputFromPort("timezone") strings = self.getInputFromPort("strings") fmt = self.getInputFromPort("format") try: result = self.convert(strings, fmt, tz) except ValueError, e: raise ModuleError(self, e.message)
def get_libX11(): """ get_libX11() -> CDLL Return the X11 library loaded with ctypes. Only available on Linux. We also need a way to find the correct X11 library name on different machines. Right now, libX11.so.6 is used. """ from vistrails.core.bundles import py_import ctypes = py_import('ctypes', { 'pip': 'ctypes', 'linux-debian': 'python-ctypes', 'linux-ubuntu': 'python-ctypes', 'linux-fedora': 'python-ctypes'}) c_void_p = ctypes.c_void_p CDLL = ctypes.CDLL return CDLL('libX11.so.6')
def XDestroyWindow(displayId, windowId): """ XDestroyWindow(displayId: void_p_str, windowId: void_p_str) -> None Destroy the X window specified by two strings displayId and windowId containing void pointer string of (Display*) and (Window) type. This is specific for VTKCell to remove the top shell window. Since VTK does not expose X11-related functions to Python, we have to use ctypes to hi-jack X11 library and call XDestroyWindow to kill the top-shell widget after reparent the OpenGL canvas to another Qt widget """ from vistrails.core.bundles import py_import ctypes = py_import("ctypes", {"pip": "ctypes", "linux-debian": "python-ctypes", "linux-ubuntu": "python-ctypes"}) c_void_p = ctypes.c_void_p displayPtr = c_void_p(int(displayId[1 : displayId.find("_void_p")], 16)) windowPtr = c_void_p(int(windowId[1 : windowId.find("_void_p")], 16)) libx = get_libX11() libx.XDestroyWindow(displayPtr, windowPtr)
def make_timezone(s): if s == 'local': return LocalTimezone() match = _decimal_fmt.match(s) if match is not None: sign, hours, minutes = match.groups('') sign = -1 if sign == '-' else 1 hours = int(hours) minutes = int(minutes) if minutes else 0 offset = datetime.timedelta( minutes=sign * (hours*60 + minutes)) name = '%s%02d%02d' % ( '-' if sign == -1 else '+', hours, minutes) return FixedOffset(offset, name) try: pytz = py_import('pytz', { 'pip': 'pytz', 'linux-debian': 'python-tz', 'linux-ubuntu': 'python-tz', 'linux-fedora': 'pytz'}) except ImportError: raise ValueError("can't understand timezone %r (maybe you should " "install pytz?)" % s) else: ver = LooseVersion(pytz.__version__) if ver < LooseVersion('2012'): warnings.warn( "You are using an old version of pytz (%s). You might " "run into some issues with daylight saving handling." % pytz.__version__, category=VistrailsWarning) try: return pytz.timezone(s) except KeyError: raise ValueError("can't understand timezone %r (defaulted to " "pytz, which gave no answer)" % s)
def make_timezone(s): if s == 'local': return LocalTimezone() match = _decimal_fmt.match(s) if match is not None: sign, hours, minutes = match.groups('') sign = -1 if sign == '-' else 1 hours = int(hours) minutes = int(minutes) if minutes else 0 offset = datetime.timedelta(minutes=sign * (hours * 60 + minutes)) name = '%s%02d%02d' % ('-' if sign == -1 else '+', hours, minutes) return FixedOffset(offset, name) try: pytz = py_import( 'pytz', { 'pip': 'pytz', 'linux-debian': 'python-tz', 'linux-ubuntu': 'python-tz', 'linux-fedora': 'pytz' }) except ImportError: # pragma: no cover raise ValueError("can't understand timezone %r (maybe you should " "install pytz?)" % s) else: ver = LooseVersion(pytz.__version__) if ver < PYTZ_MIN_VER: # pragma: no cover warnings.warn( "You are using an old version of pytz (%s). You might " "run into some issues with daylight saving handling." % pytz.__version__, category=VistrailsWarning) try: return pytz.timezone(s) except KeyError: raise ValueError("can't understand timezone %r (defaulted to " "pytz, which gave no answer)" % s)
import contextlib import io import logging import os import urllib from vistrails.core import debug from vistrails.core.bundles import py_import from vistrails.core.modules.basic_modules import PathObject from vistrails.core.modules.config import ModuleSettings from vistrails.core.modules.vistrails_module import Module, ModuleError, \ ModuleSuspended from vistrails.core.vistrail.job import JobMixin tej = py_import('tej', {'pip': 'tej'}) assert __name__.endswith('.init') this_pkg = __name__[:-5] class ServerLogger(tej.ServerLogger): def __init__(self): self.hidden = False tej.ServerLogger.__init__(self) @contextlib.contextmanager def hide_output(self): self.hidden = True try:
import io import logging import os import shutil import urllib import sys from vistrails.core import debug from vistrails.core.bundles import py_import from vistrails.core.modules.basic_modules import PathObject from vistrails.core.modules.config import ModuleSettings from vistrails.core.modules.vistrails_module import Module, ModuleError, \ ModuleSuspended from vistrails.core.vistrail.job import JobMixin tej = py_import('tej', {'pip': 'tej'}) assert __name__.endswith('.init') this_pkg = __name__[:-5] class ServerLogger(tej.ServerLogger): """Subclass of tej server logger that can be toggled on and off. This is used to hide server messages from the VisTrails console when running tej commands. """ def __init__(self): self.hidden = False tej.ServerLogger.__init__(self)
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ## ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ## ############################################################################### from vistrails.core.bundles import py_import py_import( 'dulwich', { 'pip': 'dulwich', 'linux-debian': 'python-dulwich', 'linux-ubuntu': 'python-dulwich', 'linux-fedora': 'python-dulwich' }) from vistrails.core import debug from dulwich.errors import NotCommitError, NotGitRepository from dulwich.repo import Repo from dulwich.objects import Commit, Blob, Tree, object_header from dulwich.pack import iter_sha1 from dulwich.walk import Walker from itertools import chain import os import shutil import stat import tempfile
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR ## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, ## EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, ## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ## ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ## ############################################################################### from vistrails.core.bundles import py_import py_import('dulwich', { 'pip': 'dulwich', 'linux-debian': 'python-dulwich', 'linux-ubuntu': 'python-dulwich', 'linux-fedora': 'python-dulwich'}) from vistrails.core import debug from dulwich.errors import NotCommitError, NotGitRepository from dulwich.repo import Repo from dulwich.objects import Commit, Blob, Tree, object_header from dulwich.pack import iter_sha1 from dulwich.walk import Walker from itertools import chain import os import shutil import stat import tempfile
## ############################################################################### from vistrails.core.bundles import py_import from PyQt4 import QtCore, QtGui import urllib from vistrails.core import debug from vistrails.core.modules.vistrails_module import Module, ModuleError, NotCacheable from vistrails.gui.modules.source_configure import SourceConfigurationWidget from vistrails.core.upgradeworkflow import UpgradeWorkflowHandler from vistrails.core.utils import PortAlreadyExists from vistrails.gui.theme import CurrentTheme MySQLdb = py_import('MySQLdb', { 'pip': 'mysql-python', 'linux-debian': 'python-mysqldb', 'linux-ubuntu': 'python-mysqldb', 'linux-fedora': 'MySQL-python'}) psycopg2 = py_import('psycopg2', { 'pip': 'psycopg2', 'linux-debian':'python-psycopg2', 'linux-ubuntu':'python-psycopg2', 'linux-fedora':'python-psycopg2'}) class QPasswordEntry(QtGui.QDialog): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.setModal(True) self.setWindowTitle("Enter Password:")
def get_shell_dialog(): global _shell_dialog if _shell_dialog is not None: return _shell_dialog try: deps = { 'pip': 'ipython>=1.0', 'linux-ubuntu': 'ipython-qtconsole', 'linux-debian': 'ipython-qtconsole' } IPython = py_import('IPython.qt.console.rich_ipython_widget', deps, True) RichIPythonWidget = \ IPython.qt.console.rich_ipython_widget.RichIPythonWidget py_import('IPython.qt.inprocess', deps, True) QtInProcessKernelManager = \ IPython.qt.inprocess.QtInProcessKernelManager except ImportError: return None km = QtInProcessKernelManager() km.start_kernel() kernel = km.kernel kernel.gui = 'qt4' kernel_client = km.client() kernel_client.start_channels() class IPythonDialog(RichIPythonWidget, QVistrailsPaletteInterface): """This class incorporates an IPython shell into a dockable widget for use in the VisTrails environment""" def __init__(self, parent=None): RichIPythonWidget.__init__(self, parent) self.old_streams = None self.running_workflow = False self.kernel_manager = km self.kernel_client = kernel_client self.exit_requested.connect(self.stop) self.setWindowTitle("Console") self.vistrails_interpreter = get_default_interpreter() def visibility_changed(self, visible): QVistrailsPaletteInterface.visibility_changed(self, visible) if visible: self.show() else: self.hide() def stop(self): kernel_client.stop_channels() km.shutdown_kernel() def hide(self): """suspend() -> None Called when hiding the parent window in order to recover the previous state. """ #recovering the state if self.old_streams is not None: sys.stdout, sys.stderr, sys.stdin = self.old_streams self.old_streams = None RichIPythonWidget.hide(self) def show(self): """show() -> None Store previous state and starts capturing all interactive input and output. """ # capture all interactive input/output if self.old_streams is None: self.old_streams = sys.stdout, sys.stderr, sys.stdin sys.stdout = self sys.stderr = self sys.stdin = self RichIPythonWidget.show(self) def showEvent(self, e): """showEvent(e) -> None Event handler called when the dialog acquires focus """ self.show() def flush(self): """flush() -> None. Simulate stdin, stdout, and stderr. """ pass def isatty(self): """isatty() -> int Simulate stdin, stdout, and stderr. """ return 1 def readline(self): """readline() -> str Simulate stdin, stdout, and stderr. """ return "" def write(self, text): """write(text: str) -> None Simulate stdin, stdout, and stderr. """ self.input_buffer = '' if not self.running_workflow: self.running_workflow = True # make text blue self._append_plain_text("\n\x1b[34m<STANDARD OUTPUT>\x1b[0m\n", True) self._append_plain_text(text, True) self._prompt_pos = self._get_end_cursor().position() self._control.ensureCursorVisible() self._control.moveCursor(QtGui.QTextCursor.End) def eventFilter(self, obj, event): """ Reimplemented to ensure a console-like behavior in the underlying text widgets. """ etype = event.type() if etype == QtCore.QEvent.KeyPress: self.running_workflow = False return RichIPythonWidget.eventFilter(self, obj, event) _shell_dialog = IPythonDialog return IPythonDialog
def get_shell_dialog(): global _shell_dialog if _shell_dialog is not None: return _shell_dialog try: deps = {'pip': 'ipython>=1.0', 'linux-ubuntu': 'ipython-qtconsole', 'linux-debian': 'ipython-qtconsole'} IPython = py_import('IPython.qt.console.rich_ipython_widget', deps, True) RichIPythonWidget = \ IPython.qt.console.rich_ipython_widget.RichIPythonWidget py_import('IPython.qt.inprocess', deps, True) QtInProcessKernelManager = \ IPython.qt.inprocess.QtInProcessKernelManager except ImportError: return None km = QtInProcessKernelManager() km.start_kernel() kernel = km.kernel kernel.gui = 'qt4' kernel_client = km.client() kernel_client.start_channels() class IPythonDialog(RichIPythonWidget, QVistrailsPaletteInterface): """This class incorporates an IPython shell into a dockable widget for use in the VisTrails environment""" def __init__(self, parent=None): RichIPythonWidget.__init__(self, parent) self.old_streams = None self.running_workflow = False self.kernel_manager = km self.kernel_client = kernel_client self.exit_requested.connect(self.stop) self.setWindowTitle("Console") self.vistrails_interpreter = get_default_interpreter() def visibility_changed(self, visible): QVistrailsPaletteInterface.visibility_changed(self, visible) if visible: self.show() else: self.hide() def stop(self): kernel_client.stop_channels() km.shutdown_kernel() def hide(self): """suspend() -> None Called when hiding the parent window in order to recover the previous state. """ #recovering the state if self.old_streams is not None: sys.stdout, sys.stderr, sys.stdin = self.old_streams self.old_streams = None RichIPythonWidget.hide(self) def show(self): """show() -> None Store previous state and starts capturing all interactive input and output. """ # capture all interactive input/output if self.old_streams is None: self.old_streams = sys.stdout, sys.stderr, sys.stdin sys.stdout = self sys.stderr = self sys.stdin = self RichIPythonWidget.show(self) def showEvent(self, e): """showEvent(e) -> None Event handler called when the dialog acquires focus """ self.show() def flush(self): """flush() -> None. Simulate stdin, stdout, and stderr. """ pass def isatty(self): """isatty() -> int Simulate stdin, stdout, and stderr. """ return 1 def readline(self): """readline() -> str Simulate stdin, stdout, and stderr. """ return "" def write(self, text): """write(text: str) -> None Simulate stdin, stdout, and stderr. """ self.input_buffer = '' if not self.running_workflow: self.running_workflow = True # make text blue self._append_plain_text("\n\x1b[34m<STANDARD OUTPUT>\x1b[0m\n", True) self._append_plain_text(text, True) self._prompt_pos = self._get_end_cursor().position() self._control.ensureCursorVisible() self._control.moveCursor(QtGui.QTextCursor.End) def eventFilter(self, obj, event): """ Reimplemented to ensure a console-like behavior in the underlying text widgets. """ etype = event.type() if etype == QtCore.QEvent.KeyPress: self.running_workflow = False return RichIPythonWidget.eventFilter(self, obj, event) _shell_dialog = IPythonDialog return IPythonDialog