# ==============================================================================
"""SCRAMBLE : scramble the lines of a text file according to a password"""
# ==============================================================================
__author__  = "Christophe Schlick"
__version__ = "1.0"
__date__    = "2015-09-01"
__usage__   = """
User input : <password> <filename> [filename...]
App output : scramble the content of all 'filenames' according to 'password'
Note: use quotes if 'password' contains space characters"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def scramble(password, name):
  """scramble the content of file 'name' by using characters of 'password'"""
  text, n = '', len(password) # the length of 'password' is needed for cycling 
  for p, char in enumerate(read_txt(name)): # enumerate all chars from file
    # cycle around all characters in password, keep its 6 least significant bits
    # and use them to scramble each file character by applying an XOR operator
    code_char, code_pass = ord(char), 63 & ord(password[p % n])
    text += chr(code_char ^ code_pass) # scramble code with XOR operator
  write_txt(name, text) # replace file content by scrambled text
  return "File %r has been scrambled" % name
# ------------------------------------------------------------------------------
def parser(command):
  """extract arguments from 'command' before calling 'scramble()'"""
  password, *names = parse(command); #inspect()
  return '\n'.join(scramble(password, name) for name in names)
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter <password> <filename> [filename...]")
# ==============================================================================
