Revision: 3901 Author: wikipedian Date: 2007-07-26 15:17:34 +0000 (Thu, 26 Jul 2007)
Log Message: ----------- Added more secure -pass parameter to login.py so that you don't have to run the insecure -pass:XYZ anymore
Added password parameter for wikipedia.input()
Added docu for input methods
Moved lower-casing to wikipedia.input()
Modified Paths: -------------- trunk/pywikipedia/login.py trunk/pywikipedia/userinterfaces/cgi_interface.py trunk/pywikipedia/userinterfaces/terminal_interface.py trunk/pywikipedia/userinterfaces/tkinter_interface.py trunk/pywikipedia/userinterfaces/wxpython_interface.py trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/login.py =================================================================== --- trunk/pywikipedia/login.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/login.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -8,26 +8,30 @@
Parameters:
+ -all Try to log in on all sites where a username is defined in + user-config.py. + + -pass Useful in combination with -all when you have accounts for + several sites and use the same password for all of them. + Asks you for the password, then logs in on all given sites. + -pass:XXXX Uses XXXX as password. Be careful if you use this parameter because your password will be shown on your - screen. - + screen, and will probably be saved in your command line + history. This is NOT RECOMMENDED for use on computers + where others have either physical or remote access. + Use -pass instead. + -sysop Log in with your sysop account. - - -all Try to log in on all sites where a username is defined in - user-config.py. - - -force When doing -all, ignores if the user is already loged in, - and tries to login for all listed sites. - This may be useful if you have changed the account name - and need to aquire new login cookies.
+ -force Ignores if the user is already logged in, and tries to log in. + If not given as parameter, the script will ask for your username and password (password entry will be hidden), log in to your home wiki using this combination, and store the resulting cookies (containing your password hash, so keep it secured!) in a file in the login-data subdirectory.
-All bots in this library will be looking for this cookie file and will use the +All scripts in this library will be looking for this cookie file and will use the login information if it is present.
To log out, throw away the XX-login.data file that is created in the login-data @@ -40,7 +44,7 @@ # __version__='$Id$'
-import re, sys, getpass +import re, sys import httplib, urllib2 import wikipedia, config
@@ -206,13 +210,9 @@
def login(self, retry = False): if not self.password: - # As we don't want the password to appear on the screen, we use getpass(). - s = u'Password for user %s on %s: ' % (self.username, self.site) - self.password = getpass.getpass(s.encode(config.console_encoding)) - # Convert the password from the encoding your shell uses to the one your wiki - # uses, via Unicode. This is the same as wikipedia.input() does with the - # username, but input() uses raw_input() instead of getpass(). - self.password = unicode(self.password, config.console_encoding) + # As we don't want the password to appear on the screen, we set + # password = True + self.password = wikipedia.input(u'Password for user %s on %s:' % (self.username, self.site), password = True)
self.password = self.password.encode(self.site.encoding())
@@ -239,8 +239,11 @@ logall = False forceLogin = False for arg in wikipedia.handleArgs(): - if arg.startswith("-pass:"): - password = arg[6:] + if arg.startswith("-pass"): + if len(arg) == 5: + password = wikipedia.input(u'Password for all accounts:', password = True) + else: + password = arg[6:] elif arg == "-sysop": sysop = True elif arg == "-all":
Modified: trunk/pywikipedia/userinterfaces/cgi_interface.py =================================================================== --- trunk/pywikipedia/userinterfaces/cgi_interface.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/userinterfaces/cgi_interface.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -9,5 +9,6 @@ if toStdout: sys.stdout.write(text.encode('UTF-8', 'replace'))
- def input(self, question, colors = None): + def input(self, question, colors = None, password = False): + # CGI is output-only. self.output(question + ' ', newline = False, toStdout = True)
Modified: trunk/pywikipedia/userinterfaces/terminal_interface.py =================================================================== --- trunk/pywikipedia/userinterfaces/terminal_interface.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/userinterfaces/terminal_interface.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -169,7 +169,7 @@ targetStream = sys.stderr self.printColorized(text, colors, targetStream)
- def input(self, question, colors = None): + def input(self, question, colors = None, password = False): """ Works like raw_input(), but returns a unicode string instead of ASCII.
@@ -184,7 +184,11 @@ self.output(question + ' ', colors = colors + [None], newline=False) else: self.output(question + ' ', newline = False) - text = raw_input() + if password: + import getpass + text = getpass.getpass('') + else: + text = raw_input() text = unicode(text, config.console_encoding) return text
@@ -204,7 +208,7 @@ prompt = '%s (%s)' % (question, ', '.join(options)) answer = self.input(prompt) if answer.lower() in hotkeys or answer.upper() in hotkeys: - return answer.lower() + return answer elif default and answer=='': # empty string entered return default
Modified: trunk/pywikipedia/userinterfaces/tkinter_interface.py =================================================================== --- trunk/pywikipedia/userinterfaces/tkinter_interface.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/userinterfaces/tkinter_interface.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -196,11 +196,11 @@ # auto-scroll down self.logBox.see(END)
- def input(self, question, colors=None): + def input(self, question, colors=None, password = False): """ Returns a unicode string. """ - + # TODO: hide input if password = True answer = tkSimpleDialog.askstring('title', question) return answer
@@ -215,4 +215,4 @@ d = CustomMessageBox(self.parent, question, options, hotkeys) self.parent.wait_window(d.top) answer = d.ask() - return answer.lower() + return answer
Modified: trunk/pywikipedia/userinterfaces/wxpython_interface.py =================================================================== --- trunk/pywikipedia/userinterfaces/wxpython_interface.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/userinterfaces/wxpython_interface.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -23,16 +23,14 @@ # comma at the end means "don't print newline" print text.encode(config.console_encoding, 'replace'),
- def input(self, question, colors = None): + def input(self, question, colors = None, password = False): """ Works like raw_input(), but returns a unicode string instead of ASCII.
Unlike raw_input, this function automatically adds a space after the question. """ - - # sound the terminal bell to notify the user - #sys.stdout.write('\07') + # TODO: hide input if password = True answer = dialog = wxTextEntryDialog ( None, 'question', 'Title Here', '' )
#tkSimpleDialog.askstring('title', question) @@ -54,6 +52,6 @@ prompt = '%s (%s)' % (question, ', '.join(options)) answer = self.input(prompt) if answer.lower() in hotkeys or answer.upper() in hotkeys: - return answer.lower() + return answer ui = UI() print ui.input('Test?')
Modified: trunk/pywikipedia/wikipedia.py =================================================================== --- trunk/pywikipedia/wikipedia.py 2007-07-26 13:06:28 UTC (rev 3900) +++ trunk/pywikipedia/wikipedia.py 2007-07-26 15:17:34 UTC (rev 3901) @@ -4467,7 +4467,7 @@
If toStdout is True, the text will be sent to standard output, so that it can be piped to another process. All other text will - be sent to stderr. + be sent to stderr. See: http://en.wikipedia.org/wiki/Pipeline_%28Unix%29 """ output_lock.acquire() try: @@ -4490,12 +4490,40 @@ finally: output_lock.release()
-def input(question, colors = None): - return ui.input(question, colors) +def input(question, colors = None, password = False): + """ + Asks the user a question, then returns the user's answer.
+ Parameters: + * question - a unicode string that will be shown to the user. Don't add a + space after the question mark/colon, this method will do this + for you. + * colors - same as in output(). + * password - if True, hides the user's input (for password entry). + + Returns a unicode string. + """ + return ui.input(question, colors, password) + def inputChoice(question, answers, hotkeys, default = None): - return ui.inputChoice(question, answers, hotkeys, default) + """ + Asks the user a question and offers several options, then returns the + user's choice. The user's input will be case-insensitive, so the hotkeys + should be distinctive case-insensitively.
+ Parameters: + * question - a unicode string that will be shown to the user. Don't add a + space after the question mark, this method will do this + for you. + * answers - a list of strings that represent the options. + * hotkeys - a list of one-letter strings, one for each answer. + * default - an element of hotkeys, or None. The default choice that will + be returned when the user just presses Enter. + + Returns a one-letter string in lowercase. + """ + return ui.inputChoice(question, answers, hotkeys, default).lower() + def showHelp(moduleName = None): # the parameter moduleName is deprecated and should be left out. moduleName = moduleName or sys.argv[0][:sys.argv[0].rindex('.')]
pywikipedia-l@lists.wikimedia.org