Example #1
0
    def write(self, filename):
        """Write config to filename.

        Can raise EnvironmentError
        """

        assert isinstance(filename, fsnative)

        mkdir(os.path.dirname(filename))

        # temporary set the new version for saving
        if self._version is not None:
            self.add_section("__config__")
            self.set("__config__", "version", self._version)
        try:
            with atomic_save(filename, "wb") as fileobj:
                if PY2:
                    self._config.write(fileobj)
                else:
                    temp = StringIO()
                    self._config.write(temp)
                    data = temp.getvalue().encode("utf-8", "surrogateescape")
                    fileobj.write(data)
        finally:
            if self._loaded_version is not None:
                self.set("__config__", "version", self._loaded_version)
Example #2
0
    def setstringlist(self, section, option, values):
        """Saves a list of unicode strings using the csv module"""

        if PY2:
            sw = cBytesIO()
            values = [text_type(v).encode('utf-8') for v in values]
        else:
            sw = StringIO()
            values = [text_type(v) for v in values]

        writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
        writer.writerow(values)
        self.set(section, option, sw.getvalue())
Example #3
0
def _status(app):
    player = app.player
    window = app.window
    f = StringIO()

    if player.paused:
        strings = ["paused"]
    else:
        strings = ["playing"]
    strings.append(type(app.browser).__name__)
    strings.append("%0.3f" % player.volume)
    strings.append(window.order.get_active_name())
    strings.append((window.repeat.get_active() and "on") or "off")
    progress = 0
    if player.info:
        length = player.info.get("~#length", 0)
        if length:
            progress = player.get_position() / (length * 1000.0)
    strings.append("%0.3f" % progress)
    f.write(" ".join(strings) + "\n")
    try:
        f.write(app.browser.status + "\n")
    except AttributeError:
        pass
    return f.getvalue()
Example #4
0
def _status(app):
    player = app.player
    f = StringIO()

    if player.paused:
        strings = ["paused"]
    else:
        strings = ["playing"]
    strings.append(type(app.browser).__name__)
    po = app.player_options
    strings.append("%0.3f" % player.volume)
    strings.append("shuffle" if po.shuffle else "inorder")
    strings.append("on" if po.repeat else "off")
    progress = 0
    if player.info:
        length = player.info.get("~#length", 0)
        if length:
            progress = player.get_position() / (length * 1000.0)
    strings.append("%0.3f" % progress)
    f.write(" ".join(strings) + "\n")
    try:
        f.write(app.browser.status + "\n")
    except AttributeError:
        pass
    return f.getvalue()
Example #5
0
def capture_output():
    """
    with capture_output() as (stdout, stderr):
        some_action()
    print stdout.getvalue(), stderr.getvalue()
    """

    err = StringIO()
    out = StringIO()
    old_err = sys.stderr
    old_out = sys.stdout
    sys.stderr = err
    sys.stdout = out

    try:
        yield (out, err)
    finally:
        sys.stderr = old_err
        sys.stdout = old_out
Example #6
0
    def read(self, filename):
        """Reads the config from `filename` if the file exists,
        otherwise does nothing

        Can raise EnvironmentError, Error.
        """

        try:
            with open(filename, "rb") as fileobj:
                if PY3:
                    fileobj = StringIO(
                        fileobj.read().decode("utf-8", "surrogateescape"))
                self._config.readfp(fileobj, filename)
        except (IOError, OSError):
            return

        # don't upgrade if we just created a new config
        if self._version is not None:
            self._loaded_version = self.getint("__config__", "version", -1)
            for func in self._upgrade_funcs:
                self._do_upgrade(func)
Example #7
0
def _dump_queue(app):
    window = app.window
    f = StringIO()
    for song in window.playlist.q.get():
        f.write(song("~uri") + "\n")
    return f.getvalue()
Example #8
0
def _dump_browsers(app):
    f = StringIO()
    for i, b in enumerate(browsers.browsers):
        if not b.is_empty:
            f.write("%d. %s\n" % (i, browsers.name(b)))
    return f.getvalue()