Advertisement
Guest User

Untitled

a guest
May 26th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """uuid4sym - replace UUID version 4 to an easily distinguishable symbol
  4.  
  5. When looking at a long log file with some UUID4 uuids, you'll have a hard time
  6. distinguishing between two UUIDs.
  7.  
  8. Both 26cb1b84-7748-11e9-9437-2bf0efaa1b2e and 2725e8de-7748-11e9-a9c8-f3e5d10ab3d6
  9. looks similar to the human eye.
  10.  
  11. uuid4sym would replace each UUID with a symbol which is easier to distinguish.
  12.  
  13. uuid4sym replaces the first 50 UUIDs with animals emojis which are not similar.
  14. The next UUIDs will be replaced with UXXX with a ascending serial number and
  15. different color.
  16.  
  17. You can disable emojis with -e/--no-emojis, and disable colors with -C/--no-colors
  18.  
  19. """
  20.  
  21. import re
  22. import sys
  23. import optparse
  24.  
  25. COLORS = range(31, 38)
  26.  
  27. ESC = '\033['
  28.  
  29.  
  30. def color(i, txt):
  31. 'color returns txt surrounded by terminal escape which colors it with color i'
  32. return '{e}{i}m{txt}{e}0m'.format(e=ESC,
  33. i=COLORS[0] + i % len(COLORS),
  34. txt=txt)
  35.  
  36.  
  37. class Animal(object):
  38. 'Animal will replace a given UUID, with the same emoji'
  39. animals = 'πŸ­πŸΏπŸ¦”πŸ¦‡πŸπŸ₯πŸ¦πŸ§πŸΉπŸ˜πŸ¦πŸ°πŸ¦πŸ…πŸ¦›πŸ¦πŸ πŸ‘πŸ¦πŸšπŸŒπŸ¦žπŸ¦…πŸ¦‰πŸ¦œπŸ¦’πŸ¦šπŸ‡πŸ¦—πŸŽ β™˜β™žπŸ½πŸΎπŸ‘£πŸ’πŸ¦€πŸˆπŸ€πŸ¦‹πŸπŸžπŸœπŸ›πŸ•·πŸ•ΈπŸ¦‚πŸ¦ŽπŸ¦ŸπŸ¦ πŸ™πŸŒ°πŸŒ±πŸŒ½πŸ¨'
  40.  
  41. def __init__(self, colors=True, emojis=True):
  42. self.dict = {}
  43. self.i = 0
  44. self.colors = colors
  45. if not emojis:
  46. self.animals = ''
  47.  
  48. def __repr__(self):
  49. return 'Animal(%d UUIDs)'%self.i
  50.  
  51. def __str__(self):
  52. return 'Animal(%s)'%self.dict
  53.  
  54. def size(self):
  55. 'size returns the number of different UUIDs stored'
  56. return self.i
  57.  
  58. def get(self, uuid):
  59. 'get accepts UUID and replaced it with always the same symbol'
  60. animal = self.dict.get(uuid)
  61. if not animal:
  62. if self.i >= len(self.animals):
  63. index = self.i - len(self.animals)
  64. text = 'U%03d' % index
  65. if self.colors:
  66. animal = color(index, text)
  67. else:
  68. animal = text
  69. else:
  70. animal = self.animals[self.i]
  71. self.i += 1
  72. self.dict[uuid] = animal
  73. return animal
  74.  
  75.  
  76. def main():
  77. 'main function, reads from stdin, replaces UUIDv4 with symbols'
  78. parser = optparse.OptionParser(usage="usage: %prog [options]")
  79. parser.add_option("-C",
  80. "--no-colors",
  81. action="store_true",
  82. default=False,
  83. dest='no_colors',
  84. help='do not use terminal colors')
  85. parser.add_option("-e",
  86. "--no-emojis",
  87. action="store_true",
  88. default=False,
  89. dest='no_emojis',
  90. help='do not replace uuid with utf-8 emojis')
  91. options, args = parser.parse_args()
  92. if args:
  93. sys.stderr.write("No args are allowed")
  94. return
  95.  
  96. animals = Animal(colors=not options.no_colors,
  97. emojis=not options.no_emojis)
  98. uuid_re = re.compile('[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}')
  99.  
  100. try:
  101. while True:
  102. line = sys.stdin.readline()
  103. if not line:
  104. break
  105. last_i = 0
  106. for match in uuid_re.finditer(line):
  107. start, end = match.span()
  108. sys.stdout.write(line[last_i:start])
  109. sys.stdout.write(animals.get(match.group(0)))
  110. last_i = end
  111. sys.stdout.write(line[last_i:])
  112. sys.stdout.flush()
  113. except KeyboardInterrupt:
  114. sys.stderr.write('Exit by Ctrl-C')
  115.  
  116.  
  117. if __name__ == '__main__':
  118. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement