A modest string writer

dropbox.py 2.9KB

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