A modest string writer

dropbox.py 3.0KB

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