Example #1
0
    def threejs_offline_scripts(self):
        """
        Three.js scripts for the IPython command line

        OUTPUT:

        String containing script tags

        EXAMPLES::

            sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
            sage: backend = BackendIPythonCommandline()
            sage: backend.threejs_offline_scripts()
            '...<script ...</script>...'
        """
        from sage.env import SAGE_SHARE

        scripts = [
            os.path.join(SAGE_SHARE, 'threejs', script)
            for script in ['three.min.js', 'OrbitControls.js']
        ]

        if sys.platform == 'cygwin':
            import cygwin
            scripts = [cygwin.cygpath(script, 'w') for script in scripts]

        return '\n'.join('<script src="{0}"></script>'.format(script)
                         for script in scripts)
    def add_torrent(self, magnet):
        """
        Initialize settings, set magnet->torrent dir to work_dir
        """
        self.set_setting("torrents_start_stopped", "false")
        torrent_dir = self.work_dir if sys.platform != "cygwin" else cygpath(
            self.work_dir, "w")
        """
        Add torrent from url. To get the torrent info the torrent has to be started then immediately stopped and files deleted.
        """
        params = [("action", "add-url"), ("s", magnet)]
        add_action = self._apirequest(params)
        torrent_info = self.parse_magnet(magnet)
        torrent_hash = torrent_info["hash"]

        torrent_files = self.get_files(torrent_hash)
        self.start_torrent(torrent_hash)
        while not torrent_files or not torrent_files[1]:
            torrent_files = self.get_files(torrent_hash)
            time.sleep(.5)
        self.remove_torrent(torrent_hash, True)

        tor_file = open(
            os.path.join(self.work_dir, torrent_info["dn"] + ".torrent"), "rb")
        self.set_setting("torrents_start_stopped", "true")
        params = [("action", "add-file")]
        self._apirequest(params, "torrent_file", tor_file)

        self.prioritize_download(torrent_hash)

        return torrent_hash
Example #3
0
    def threejs_offline_scripts(self):
        """
        Three.js scripts for the IPython command line

        OUTPUT:

        String containing script tags

        EXAMPLES::

            sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
            sage: backend = BackendIPythonCommandline()
            sage: backend.threejs_offline_scripts()
            '...<script ...</script>...'
        """
        from sage.env import SAGE_SHARE

        scripts = [os.path.join(SAGE_SHARE, 'threejs', script)
                   for script in ['three.min.js', 'OrbitControls.js']]

        if sys.platform == 'cygwin':
            import cygwin
            scripts = [cygwin.cygpath(script, 'w') for script in scripts]

        return '\n'.join('<script src="{0}"></script>'.format(script)
                         for script in scripts)
Example #4
0
    def export_image(
            self,
            targetfile,
            datafile,  #name (path) of data file Jmol can read or script file telling it what to read or load
            datafile_cmd='script',  #"script" or "load"
            image_type='PNG',  #PNG, JPG, GIF
            figsize=5,
            **kwds):
        r"""
        This executes JmolData.jar to make an image file.

        INPUT:

        - targetfile -- the full path to the file where the image
          should be written.

        - datafile -- full path to the data file Jmol can read or
          text of a script telling Jmol what to read or load.
          If it is a script and the platform is cygwin, the filenames in
          the script should be in native windows format.

        - datafile_cmd -- (default ``'script'``)  ``'load'`` or ``'script'``
          should be ``"load"`` for a data file.

        - image_type -- (default ``"PNG"``) ``'PNG'`` ``'JPG'`` or ``'GIF'``

        - figsize -- number (default 5) equal to (pixels/side)/100

        OUTPUT:

        Image file, .png, .gif or .jpg (default .png)

        .. note::

            Examples will generate an error message if a functional Java Virtual Machine (JVM)
            is not installed on the machine the Sage instance is running on.

        .. warning::

            Programmers using this module should check that the JVM is
            available before making calls to avoid the user getting
            error messages.  Check for the JVM using the function
            :meth:`is_jvm_available`, which returns True if a JVM is available.

        EXAMPLES:

        Use Jmol to load a pdb file containing some DNA from a web data
        base and make an image of the DNA. If you execute this in the
        notebook, the image will appear in the output cell::

            sage: from sage.interfaces.jmoldata import JmolData
            sage: JData = JmolData()
            sage: script = "load =1lcd;display DNA;moveto 0.0 { -473 -713 -518 59.94} 100.0 0.0 0.0 {21.17 26.72 27.295} 27.544636 {0.0 0.0 0.0} -25.287832 64.8414 0.0;"
            sage: testfile = tmp_filename(ext="DNA.png")
            sage: JData.export_image(targetfile=testfile,datafile=script,image_type="PNG")  # optional -- java internet
            sage: print(os.path.exists(testfile)) # optional -- java internet
            True

        Use Jmol to save an image of a 3-D object created in Sage.
        This method is used internally by plot3d to generate static images.
        This example doesn't have correct scaling::

            sage: from sage.interfaces.jmoldata import JmolData
            sage: JData = JmolData()
            sage: D = dodecahedron()
            sage: from sage.misc.misc import SAGE_TMP
            sage: archive_name = os.path.join(SAGE_TMP, "archive.jmol.zip")
            sage: D.export_jmol(archive_name)  #not scaled properly...need some more steps.
            sage: archive_native = archive_name
            sage: import sys
            sage: if sys.platform == 'cygwin':
            ....:     import cygwin
            ....:     archive_native = cygwin.cygpath(archive_native, 'w')
            sage: script = 'set defaultdirectory "{0}"\n script SCRIPT\n'.format(archive_native)
            sage: testfile = os.path.join(SAGE_TMP, "testimage.png")
            sage: JData.export_image(targetfile=testfile, datafile=script, image_type="PNG") # optional -- java
            sage: print(os.path.exists(testfile)) # optional -- java
            True
        """
        # Set up paths, file names and scripts
        jmolpath = os.path.join(SAGE_LOCAL, "share", "jmol", "JmolData.jar")
        target_native = targetfile

        if sys.platform == 'cygwin':
            import cygwin
            jmolpath = cygwin.cygpath(jmolpath, 'w')
            target_native = cygwin.cygpath(target_native, 'w')
            if datafile_cmd != 'script':
                datafile = cygwin.cygpath(datafile, 'w')

        launchscript = ""
        if (datafile_cmd != 'script'):
            launchscript = "load "
        launchscript = launchscript + datafile

        imagescript = 'write {} {!r}\n'.format(image_type, target_native)
        size_arg = "%sx%s" % (figsize * 100, figsize * 100)
        # Scratch file for Jmol errors
        scratchout = tmp_filename(ext=".txt")
        with open(scratchout, 'w') as jout:
            # Now call the java application and write the file.
            env = dict(os.environ)
            env['LC_ALL'] = 'C'
            env['LANG'] = 'C'
            subprocess.call([
                "java", "-Xmx512m", "-Djava.awt.headless=true", "-jar",
                jmolpath, "-iox", "-g", size_arg, "-J", launchscript, "-j",
                imagescript
            ],
                            stdout=jout,
                            stderr=jout,
                            env=env)
        if not os.path.isfile(targetfile):
            raise RuntimeError(
                "Jmol failed to create file %s, see %s for details" %
                (repr(targetfile), repr(scratchout)))
        os.unlink(scratchout)
Example #5
0
 def normpath(p):
     return 'file:///' + cygwin.cygpath(p, 'w').replace('\\', '/')