Bläddra i källkod

Add Dropbox plugin

Check dropbox daemon status and report it in few chars
Alois Mahdal 10 år sedan
förälder
incheckning
6c9d88d840
1 ändrade filer med 100 tillägg och 0 borttagningar
  1. 100
    0
      plugins/dropbox.py

+ 100
- 0
plugins/dropbox.py Visa fil

@@ -0,0 +1,100 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+import sardine
4
+
5
+import os
6
+import subprocess
7
+
8
+
9
+class DropboxStatus(object):
10
+
11
+    up_to_date = 0
12
+    updating = 1
13
+    paused = 2
14
+    stopped = 3
15
+    error = 4
16
+    unknown = 99
17
+
18
+    symbols = {
19
+        'ascii': {
20
+            up_to_date: ".",
21
+            updating: ":",
22
+            paused: "\"",
23
+            stopped: "-",
24
+            error: "X",
25
+            unknown: "?",
26
+        },
27
+        'utf-8': {
28
+            up_to_date: "✓",
29
+            updating: "★",
30
+            paused: "…",
31
+            stopped: "□",
32
+            error: "‼",
33
+            unknown: "▒",
34
+        }
35
+    }
36
+
37
+    def __init__(self):
38
+        self.status = DropboxStatus.unknown
39
+        self.downloading = False
40
+        self.syncing = None
41
+        self.indexing = None
42
+        self.uploading = None
43
+        self.load()
44
+
45
+    def render(self, charset='utf-8'):
46
+        out = DropboxStatus.symbols[charset][self.status]
47
+        if self.downloading:
48
+            out += "v"
49
+        if self.syncing:
50
+            out += 's'
51
+        if self.indexing:
52
+            out += 'i'
53
+        if self.uploading:
54
+            out += 'u'
55
+        return out
56
+
57
+    def load(self):
58
+        cmd = ['dropbox', 'status']
59
+        try:
60
+            out = subprocess.check_output(cmd)
61
+        except OSError as e:
62
+            self.status = DropboxStatus.error
63
+        else:
64
+            lines = out.split("\n")
65
+            if "Up to date" in out:
66
+                self.status = DropboxStatus.up_to_date
67
+                return
68
+            elif "Syncing paused" in out:
69
+                self.status = DropboxStatus.paused
70
+                return
71
+            elif "Dropbox isn't running!" in out:
72
+                self.status = DropboxStatus.stopped
73
+                return
74
+            else:
75
+                try:
76
+                    if "Downloading file list" in out:
77
+                        self.status = DropboxStatus.updating
78
+                        self.downloading = True
79
+                    if "Syncing (" in out:
80
+                        self.status = DropboxStatus.updating
81
+                        self.syncing = True
82
+                    if "Indexing " in out:
83
+                        self.status = DropboxStatus.updating
84
+                        self.indexing = True
85
+                    if "Uploading " in out:
86
+                        self.status = DropboxStatus.updating
87
+                        self.uploading = True
88
+                    if self.status == DropboxStatus.unknown:
89
+                        raise sardine.PluginError("unknown status: %s" % out)
90
+                except ValueError:
91
+                    with open('out.dmp', 'w') as fh:
92
+                        fh.write(out)
93
+
94
+
95
+class Plugin(sardine.BasePlugin):
96
+
97
+    name = "dropbox"
98
+
99
+    def render_plain(self):
100
+        return DropboxStatus().render('ascii')