util.py 2.5 KB

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