ThePigInMinecraft

NumberMessage.py

Oct 14th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #!/usr/bin/env python2.7
  2.  
  3.  
  4. ''' NumberMessage.py by The Pig in Minecraft
  5. (http://ookiepigster.tumblr.com/ - https://twitter.com/OokiePigster)
  6.  
  7. This program is released in the public domain. You may do whatever you want
  8. with it. '''
  9.  
  10.  
  11. import sys
  12. import base64
  13.  
  14.  
  15. HELP = '''{} [Options]
  16.  
  17.  -t <Message>  --to <Message>   Converts the specified message to a number.
  18.  -f <Number>  --from <Number>   Converts the number back to a string.
  19.  -x  --hex          Switches to hexadecimal mode.
  20.  -o  --oct          Switches to octal mode.
  21.  -b  --bin          Switches to binary mode.
  22.  -d  --dec          Switches to decimal mode (default).
  23.  -F=<File>  --file=<File>   Loads data from the specified filename. If
  24.                 <File> is ommited or '-', it defaults to stdin.
  25.  -h  --help         Prints this help.'''
  26.  
  27. BASE_FORMATS = {2: 'b', 8: 'o', 10: '', 16: 'X'}
  28.  
  29.  
  30. def ToNumber(Str):
  31.  Str = Str.replace('\x00', '').strip()
  32.  if not Str:
  33.   return 0
  34.  Hex = base64.b16encode(Str)
  35.  return int(Hex, 16)
  36.  
  37. def FromNumber(Num):
  38.  if not Num:
  39.   return ''
  40.  Hex = format(Num, 'X')
  41.  return base64.b16decode(Hex)
  42.  
  43. def _MainToNumber(Msg, Base):
  44.  return format(ToNumber(Msg), BASE_FORMATS[Base])
  45.  
  46. def _MainFromNumber(Num, Base):
  47.  return FromNumber(int(Num, Base))
  48.  
  49. def _MainDo(Arg, Mode, Base):
  50.  if Mode == 'To':
  51.   return _MainToNumber(Arg, Base)
  52.  elif Mode == 'From':
  53.   return _MainFromNumber(Arg, Base)
  54.  else:
  55.   raise ValueError
  56.  
  57. def _ToFile(File):
  58.  if File in (None, '-'):
  59.   return sys.stdin
  60.  elif hasattr(File, 'read'):
  61.   return File
  62.  elif isinstance(File, basestring):
  63.   return open(File)
  64.  
  65.  else:
  66.   raise TypeError
  67.  
  68. def Main(argv):
  69.  if len(argv) <= 1:
  70.   print HELP.format(argv[0])
  71.   return
  72.  Mode = None
  73.  Base = 10
  74.  DoubleDash = False
  75.  for I in argv[1:]:
  76.   if DoubleDash:
  77.    print _MainDo(Arg, Mode, Base)
  78.    continue
  79.   if '=' in I and I.startswith('-'):
  80.    Pos = I.find('=')
  81.    Arg = I[:Pos]
  82.    Value = I[Pos + 1:]
  83.   else:
  84.    Arg = I
  85.    Value = None
  86.  
  87.   if Arg in('-t', '--to'):
  88.    Mode = 'To'
  89.   elif Arg in ('-f', '--from'):
  90.    Mode = 'From'
  91.   elif Arg in ('-x', '--hex'):
  92.    Base = 16
  93.   elif Arg in ('-o', '--oct'):
  94.    Base = 8
  95.   elif Arg in ('-b', '--bin'):
  96.    Base = 2
  97.   elif Arg in ('-d', '--dec'):
  98.    Base = 10
  99.   elif Arg in ('-h', '--help'):
  100.    print HELP.format(argv[0])
  101.   elif Arg in ('-F', '--file'):
  102.    print _MainDo(_ToFile(Value).read(), Mode, Base)
  103.   elif Arg == '--':
  104.    DoubleDash = True
  105.   elif Arg.startswith('-'):
  106.    sys.stdout.write('Invalid argument: {}\n{}\n'.format(Arg, HELP.format(argv[0])))
  107.    return 1
  108.   elif Mode in ('To', 'From', None):
  109.    print _MainDo(Arg, Mode, Base)
  110.   else:
  111.    raise Exception('?!')
  112.  
  113.  
  114. if __name__ == '__main__':
  115.  sys.exit(Main(sys.argv) or 0)
Add Comment
Please, Sign In to add comment