# -*- coding: utf-8 -*- import sardine import subprocess class DropboxStatus(object): up_to_date = 0 updating = 1 paused = 2 stopped = 3 error = 4 starting = 5 unknown = 99 symbols = { 'ascii': { up_to_date: ".", starting: ">", updating: ":", paused: "\"", stopped: "-", error: "X", unknown: "?", }, 'utf-8': { up_to_date: "✓", starting: "➤", updating: "★", paused: "…", stopped: "⬛", error: "‼", unknown: "▒", } } def __init__(self): self.status = DropboxStatus.unknown self.downloading = False self.syncing = None self.indexing = None self.uploading = None self.load() def render(self, charset='utf-8'): out = DropboxStatus.symbols[charset][self.status] if self.downloading: out += "v" if self.syncing: out += 's' if self.indexing: out += 'i' if self.uploading: out += 'u' return out def load(self): cmd = ['dropbox', 'status'] try: out = subprocess.check_output(cmd) except OSError: self.status = DropboxStatus.error else: if "Up to date" in out: self.status = DropboxStatus.up_to_date return elif "Starting..." in out: self.status = DropboxStatus.starting return elif "Syncing paused" in out: self.status = DropboxStatus.paused return elif "Dropbox isn't running!" in out: self.status = DropboxStatus.stopped return else: try: if "Downloading file list" in out: self.status = DropboxStatus.updating self.downloading = True if "Syncing (" in out: self.status = DropboxStatus.updating self.syncing = True if "Indexing " in out: self.status = DropboxStatus.updating self.indexing = True if "Uploading " in out: self.status = DropboxStatus.updating self.uploading = True if self.status == DropboxStatus.unknown: raise sardine.PluginError("unknown status: %s" % out) except ValueError: with open('out.dmp', 'w') as fh: fh.write(out) class Plugin(sardine.BasePlugin): name = "dropbox" def render_plain(self): return DropboxStatus().render('ascii')