Exemplo n.º 1
0
 def save(self, url=None, encoding=None):
     """Saves the document to the specified or current url.
     
     Currently only local files are supported. An IOError is raised
     when trying to save a nonlocal URL.
     
     If saving succeeds and an url was specified, the url is made the
     current url (by calling setUrl() internally).
     
     """
     if url is None:
         url = QUrl()
     u = url if not url.isEmpty() else self.url()
     filename = u.toLocalFile()
     # currently, we do not support non-local files
     if not filename:
         raise IOError("not a local file")
     # keep the url if specified when we didn't have one, even if saving
     # would fail
     if self.url().isEmpty() and not url.isEmpty():
         self.setUrl(url)
     with app.documentSaving(self):
         with open(filename, "wb") as f:
             f.write(self.encodedText())
             f.flush()
             os.fsync(f.fileno())
         self.setModified(False)
         if not url.isEmpty():
             self.setUrl(url)
     self.saved()
     app.documentSaved(self)
Exemplo n.º 2
0
 def load(self, url=None, encoding=None, keepUndo=False):
     """Load the specified or current url (if None was specified).
     
     Currently only local files are supported. An IOError is raised
     when trying to load a nonlocal URL.
     
     If loading succeeds and an url was specified, the url is make the
     current url (by calling setUrl() internally).
     
     If keepUndo is True, the loading can be undone (with Ctrl-Z).
     
     """
     if url is None:
         url = QUrl()
     u = url if not url.isEmpty() else self.url()
     text = self.load_data(u, encoding or self._encoding)
     if keepUndo:
         c = QTextCursor(self)
         c.select(QTextCursor.Document)
         c.insertText(text)
     else:
         self.setPlainText(text)
     self.setModified(False)
     if not url.isEmpty():
         self.setUrl(url)
     self.loaded()
     app.documentLoaded(self)
Exemplo n.º 3
0
 def load(self, url=None, encoding=None, keepUndo=False):
     """Load the specified or current url (if None was specified).
     
     Currently only local files are supported. An IOError is raised
     when trying to load a nonlocal URL.
     
     If loading succeeds and an url was specified, the url is make the
     current url (by calling setUrl() internally).
     
     If keepUndo is True, the loading can be undone (with Ctrl-Z).
     
     """
     if url is None:
         url = QUrl()
     u = url if not url.isEmpty() else self.url()
     text = self.load_data(u, encoding or self._encoding)
     if keepUndo:
         c = QTextCursor(self)
         c.select(QTextCursor.Document)
         c.insertText(text)
     else:
         self.setPlainText(text)
     self.setModified(False)
     if not url.isEmpty():
         self.setUrl(url)
     self.loaded()
     app.documentLoaded(self)
Exemplo n.º 4
0
 def save(self, url=None, encoding=None):
     """Saves the document to the specified or current url.
     
     Currently only local files are supported. An IOError is raised
     when trying to save a nonlocal URL.
     
     If saving succeeds and an url was specified, the url is made the
     current url (by calling setUrl() internally).
     
     """
     if url is None:
         url = QUrl()
     u = url if not url.isEmpty() else self.url()
     filename = u.toLocalFile()
     # currently, we do not support non-local files
     if not filename:
         raise IOError("not a local file")
     # keep the url if specified when we didn't have one, even if saving
     # would fail
     if self.url().isEmpty() and not url.isEmpty():
         self.setUrl(url)
     with self.saving(), app.documentSaving(self):
         with open(filename, "wb") as f:
             f.write(self.encodedText())
             f.flush()
             os.fsync(f.fileno())
         self.setModified(False)
         if not url.isEmpty():
             self.setUrl(url)
     self.saved()
     app.documentSaved(self)
Exemplo n.º 5
0
class BaseInventoryProvider(HelpProvider):
    def __init__(self, inventory, parent=None):
        super().__init__(parent)
        self.inventory = QUrl(inventory)

        if not self.inventory.scheme() and not self.inventory.isEmpty():
            self.inventory.setScheme("file")

        self._error = None
        self._fetch_inventory(self.inventory)

    def _fetch_inventory(self, url):
        cache_dir = config.cache_dir()
        cache_dir = os.path.join(cache_dir, "help", type(self).__qualname__)

        try:
            os.makedirs(cache_dir)
        except OSError:
            pass

        url = QUrl(self.inventory)
        if not url.isLocalFile():
            # fetch and cache the inventory file.
            manager = QNetworkAccessManager(self)
            cache = QNetworkDiskCache()
            cache.setCacheDirectory(cache_dir)
            manager.setCache(cache)
            req = QNetworkRequest(url)

            self._reply = manager.get(req)
            manager.finished.connect(self._on_finished)
        else:
            self._load_inventory(open(str(url.toLocalFile()), "rb"))

    def _on_finished(self, reply):
        if reply.error() != QNetworkReply.NoError:
            log.error("An error occurred while fetching "
                      "help inventory '{0}'".format(self.inventory))
            self._error = reply.error(), reply.errorString()

        else:
            contents = bytes(reply.readAll())
            self._load_inventory(io.BytesIO(contents))

    def _load_inventory(self, stream):
        raise NotImplementedError()