A modest string writer

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