123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- # -*- coding: utf-8 -*-
-
- import sardine
-
- import os
- 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 as e:
- self.status = DropboxStatus.error
- else:
- lines = out.split("\n")
- 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')
|