def setAsDefault(self):
        """
        Set this printer as the system default.
        """
        self.connection.setDefault(self.name)

        # Also need to check system-wide lpoptions because that's how
        # previous Fedora versions set the default (bug #217395).
        with tempfile.TemporaryFile() as f:
            try:
                resource = "/admin/conf/lpoptions"
                self.connection.getFile(resource, fd=f.fileno())
            except cups.HTTPError as e:
                (s, ) = e.args
                if s in [
                        cups.HTTP_NOT_FOUND, cups.HTTP_AUTHORIZATION_CANCELED
                ]:
                    return False

                raise cups.HTTPError(s)

            f.seek(0)
            lines = [line.decode('UTF-8') for line in f.readlines()]
            changed = False
            i = 0
            for line in lines:
                if line.startswith("Default "):
                    # This is the system-wide default.
                    name = line.split(' ')[1]
                    if name != self.name:
                        # Stop it from over-riding the server default.
                        lines[i] = "Dest " + line[8:]
                        changed = True
                    i += 1

            if changed:
                f.seek(0)
                f.writelines([line.encode('UTF-8') for line in lines])
                f.truncate()
                f.flush()
                f.seek(0)
                try:
                    self.connection.putFile(resource, fd=f.fileno())
                except cups.HTTPError:
                    return False

            return changed
Example #2
0
    def setAsDefault(self):
        """
        Set this printer as the system default.
        """
        self.connection.setDefault(self.name)

        # Also need to check system-wide lpoptions because that's how
        # previous Fedora versions set the default (bug #217395).
        (tmpfd, tmpfname) = tempfile.mkstemp()
        os.remove(tmpfname)
        try:
            resource = "/admin/conf/lpoptions"
            self.connection.getFile(resource, fd=tmpfd)
        except cups.HTTPError as e:
            (s, ) = e.args
            if s == cups.HTTP_NOT_FOUND:
                return False

            raise cups.HTTPError(s)

        f = os.fdopen(tmpfd, 'r+b')
        f.seek(0)
        lines = [line.decode('UTF-8') for line in f.readlines()]
        changed = False
        i = 0
        for line in lines:
            if line.startswith("Default "):
                # This is the system-wide default.
                name = line.split(' ')[1]
                if name != self.name:
                    # Stop it from over-riding the server default.
                    lines[i] = "Dest " + line[8:]
                    changed = True
                i += 1

        if changed:
            f.seek(0)
            f.writelines([line.encode('UTF-8') for line in lines])
            f.truncate()
            os.lseek(tmpfd, 0, os.SEEK_SET)
            try:
                self.connection.putFile(resource, fd=tmpfd)
            except cups.HTTPError:
                return False

        return changed
Example #3
0
    def setAsDefault(self):
        """
        Set this printer as the system default.
        """
        self.connection.setDefault(self.name)

        # Also need to check system-wide lpoptions because that's how
        # previous Fedora versions set the default (bug #217395).
        (tmpfd, tmpfname) = tempfile.mkstemp()
        os.remove(tmpfname)
        try:
            resource = "/admin/conf/lpoptions"
            self.connection.getFile(resource, fd=tmpfd)
        except cups.HTTPError, (s, ):
            if s == cups.HTTP_NOT_FOUND:
                return False

            raise cups.HTTPError(s)
    def _got_ppd3(self, connection, name, result, callback):
        (status, modtime, filename) = result
        if status in [cups.HTTP_OK, cups.HTTP_NOT_MODIFIED]:
            if status == cups.HTTP_NOT_MODIFIED:
                # The file is no newer than the one we already have.

                # CUPS before 1.5.3 created a temporary file in error
                # in this situation (STR #4018) so remove that.
                try:
                    os.unlink(filename)
                except OSError:
                    pass

            elif status == cups.HTTP_OK:
                # Our version of the file was older.  Cache the new version.

                # Store an open file object, then remove the actual
                # file.  This way we don't leave temporary files
                # around.
                try:
                    self._cache[name] = file(filename)
                    debugprint("%s: caching %s (fd %d) "
                               "(%s) - %s" %
                               (self, filename, self._cache[name].fileno(),
                                modtime, status))
                    os.unlink(filename)
                    self._modtimes[name] = modtime
                except IOError as exc:
                    # File disappeared?
                    debugprint("%s: file %s disappeared? Unable to cache it" %
                               (self, filename))
                    self._schedule_callback(callback, name, None, exc)
                    return

            # Now fetch it from our own cache.
            self.fetch_ppd(name, callback, check_uptodate=False)
        else:
            self._schedule_callback(callback, name, None,
                                    cups.HTTPError(status))