Advertisement
Guest User

Scrambler v1.0

a guest
Feb 7th, 2011
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # coding:utf-8
  3.  
  4. import itertools
  5. import codecs
  6. import sys
  7.  
  8. try:
  9.     import argparse
  10. except:
  11.     print 'In order to use this program, please install python-argparse package!'
  12.     sys.exit()
  13.  
  14. #Setting argument parser and help messages
  15. argpar = argparse.ArgumentParser(description='Scrambler v1.0 is a string permutation tool that gets all words of the same letters, and/or providing all possibilities! It also filters the output using a text-based dictionary! It support UTF-8; most languages are supported including Arabic!',
  16.                                 epilog='Scrambler v1.0 by Mohammed A. Mustafa <Anxious.Nut@Gmail.com>')
  17. argpar.add_argument('--word','-w', metavar='Input', dest='input', default=None, help='User input, the source word that gets scrambled!')
  18. argpar.add_argument('--raw','-r', metavar='True', dest='raw', nargs='?', default=False, help='Data output, output will not be filtered with a dictionary; every combination will be printed if this option is added!')
  19. argpar.add_argument('-d', metavar='Location', dest='dict', default='/usr/share/dict/words', help='Location of an optional dictionary that will be used for filtering the output; defaults to Linux English dictionary, /usr/share/dict/words!')
  20. argpar.add_argument('--output','-o', metavar='File', dest='output', default=None, help='Saves the output in a UTF-8 text file at the specified location!')
  21. args = argpar.parse_args()
  22.  
  23. #Loading dictionary
  24. if args.raw != True:
  25.     try:
  26.         opener = codecs.open(args.dict,'r','utf-8')
  27.     except:
  28.         print 'Error: Dictionary at {0} is not available!'.format(args.dict)
  29.         sys.exit()
  30.     try:
  31.         words_raw = opener.read()
  32.         words = words_raw.splitlines()
  33.         opener.close()
  34.     except:
  35.         print 'Error: Failed to read dictionary!'
  36.         sys.exit()
  37.  
  38. #Getting input from user
  39. if args.input != None:
  40.     word = args.input
  41. else:
  42.     try:
  43.         word = raw_input('Enter a word: ')
  44.     except:
  45.         print '\nError: Noting was entered; exiting!'
  46.         sys.exit()
  47.    
  48. #Permuting
  49. try:
  50.     perms = list(itertools.permutations(list(word.decode('utf-8'))))
  51. except:
  52.     print '\nError: Permutation failed or canceled! Please report this error if this is encountered continually!'
  53.     sys.exit()
  54.  
  55. #Filtering with/without loaded dictionary
  56. matching = []
  57. if args.raw == False: #w/o dictionary
  58.     try:
  59.         for match in perms:
  60.             if (u''.join(match) in words) and (u''.join(match) not in matching):
  61.                matching.append(u''.join(match))
  62.     except:
  63.         print '\nError: Script failed!'
  64.         sys.exit()
  65. else: #w/ dictionary
  66.     for match in perms:
  67.         try: matching.append(u''.join(match))
  68.         except:
  69.             print '\nError: Script failed!'
  70.             sys.exit()
  71.            
  72. #Providing output for user
  73. if len(matching) != 0 and (args.output == None):
  74.     print u'\n'.join(matching)
  75. elif len(matching) != 0 and (args.output != None):
  76.     try:
  77.         op = codecs.open(args.output,'w','utf-8')
  78.         op.write(u'\n'.join(matching))
  79.         op.close()
  80.     except:
  81.         print 'Error: File at {0} is not accessible!'.format(args.output)
  82. else:
  83.     print 'None matched!'
  84.  
  85. ################################ Scrambler v1.0 ################################
  86. # Written by: Mohammed A. Mustafa (AnxiousNut)
  87. # Email: <Anxious.Nut@Gmail.com>
  88. # Blog: http://AnxiousNut.wordpress.com
  89. # License: GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  90. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement