util.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import re
  2. class bcolors:
  3. """
  4. Allows for prettyprinting to the console for debugging.
  5. """
  6. HEADER = '\033[95m'
  7. OKBLUE = '\033[94m'
  8. OKGREEN = '\033[92m'
  9. WARNING = '\033[93m'
  10. CYAN = '\033[36m'
  11. GREEN = '\033[32m'
  12. YELLOW = '\033[33m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. def strip_nick(nick):
  16. """
  17. Clean up nicks of their op levels (&Schooly_D, ~BoneKin, etc)
  18. """
  19. nick = re.sub('[@~+]', '', nick)
  20. return nick
  21. def depends(self, module_name):
  22. for m in self.loaded_modules:
  23. if m.__class__.__name__ == module_name:
  24. return m
  25. return None
  26. def commands(*command_list):
  27. def add_attribute(function):
  28. if not hasattr(function, "commands"):
  29. function.commands = []
  30. function.commands.extend(command_list)
  31. return function
  32. return add_attribute
  33. def parse_line(line):
  34. """
  35. returns an object with a nice set of line-pulled-apart members
  36. """
  37. class Parsed():
  38. def __init(self):
  39. self.first_word = None
  40. self.message = None
  41. self.channel = None
  42. self.user = None
  43. def startswith(self, thing):
  44. if self.message.startswith(thing):
  45. return True
  46. return False
  47. parsed = Parsed()
  48. try:
  49. parsed.first_word = line.split(":", 2)[2]
  50. parsed.message = line.split(":",2)[2]
  51. parsed.channel = line.split()[2]
  52. if "JOIN" in line or "QUIT" in line:
  53. parsed.user = line.split("!")[0].replace(":","")
  54. else:
  55. parsed.user = line.split(":")[1].rsplit("!")[0] # nick is first thing on line
  56. except IndexError:
  57. return None
  58. else:
  59. return parsed
  60. def __prettyDate(time):
  61. """
  62. Similar to Rails's nice time since thing.
  63. """
  64. now = datetime.now()
  65. if type(time) is int:
  66. diff = now - datetime.fromtimestamp(time)
  67. elif isinstance(time,datetime):
  68. diff = now - time
  69. elif not time:
  70. diff = now - now
  71. second_diff = diff.seconds
  72. day_diff = diff.days
  73. if day_diff < 0:
  74. return ''
  75. if day_diff == 0:
  76. if second_diff < 10:
  77. return "just now"
  78. if second_diff < 60:
  79. return str(second_diff) + " seconds ago"
  80. if second_diff < 120:
  81. return "a minute ago"
  82. if second_diff < 3600:
  83. return str( second_diff / 60 ) + " minutes ago"
  84. if second_diff < 7200:
  85. return "an hour ago"
  86. if second_diff < 86400:
  87. return str( second_diff / 3600 ) + " hours ago"
  88. if day_diff == 1:
  89. return "Yesterday"
  90. if day_diff < 7:
  91. return str(day_diff) + " days ago"
  92. if day_diff < 31:
  93. return str(day_diff/7) + " weeks ago"
  94. if day_diff < 365:
  95. return str(day_diff/30) + " months ago"
  96. return str(day_diff/365) + " years ago"