A modest string writer

dropbox.py 2.7KB

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