aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathieu Blondel <mathieu@mblondel.org>2009-03-27 10:25:04 +0000
committerMathieu BlondelMathieu Blondel <mathieu@mblondel.orgmathieu@mblondel.org>2009-03-27 10:25:04 +0000
commit916e28a1c9e8d221f1c13ad66e5d62aab8a1488f (patch)
treeb4be92544c5012531d76cc80f68bb8e67218895b
parent4e056e323c01e7acc7e91518116e368e818b848e (diff)
downloadwikipediafs-916e28a1c9e8d221f1c13ad66e5d62aab8a1488f.tar.gz
Made logging more flexible in user.py
git-svn-id: http://svn.code.sf.net/p/wikipediafs/code/trunk@67 59acd704-e115-0410-a914-e735a229ed7c
-rw-r--r--ChangeLog4
-rw-r--r--src/wikipediafs/logger.py4
-rw-r--r--src/wikipediafs/user.py55
3 files changed, 45 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index a693719..bd0ee3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-03-27 Mathieu Blondel <mblondel@users.sourceforge.net>
+
+- Made logging more flexible (user.py).
+
2008-09-17 Mathieu Blondel <mblondel@users.sourceforge.net>
- Added a "domain" option in the config file in order to add support
diff --git a/src/wikipediafs/logger.py b/src/wikipediafs/logger.py
index dd8ef81..cbcc420 100644
--- a/src/wikipediafs/logger.py
+++ b/src/wikipediafs/logger.py
@@ -43,3 +43,7 @@ else:
# LOGGER.info('Some information')
# LOGGER.warning('A shot across the bows')
# LOGGER.error('error...')
+
+def printlog(loggerobj, logtype, logmsg):
+ if loggerobj:
+ getattr(loggerobj, logtype)(logmsg)
diff --git a/src/wikipediafs/user.py b/src/wikipediafs/user.py
index 5c972c3..1c198e3 100644
--- a/src/wikipediafs/user.py
+++ b/src/wikipediafs/user.py
@@ -19,6 +19,7 @@
import urllib, re
from http import ExtendedHTTPConnection
+from logger import printlog
class User:
"""
@@ -41,7 +42,6 @@ class User:
cookie_str=None,
dirname=None
):
- logger.info("%s %s" % (username, password))
self.username = username
self.password = password
@@ -95,33 +95,52 @@ class User:
cookie_list.append(it_matches.group(1))
conn.close()
-
+
+ printlog(self.logger, "debug", "cookie_list:")
+ printlog(self.logger, "debug", cookie_list)
+
if len(cookie_list) == 4:
cookie_list.pop()
- if self.logger:
- self.logger.info("Logged in successfully with username %s" % \
- self.username)
+ printlog(self.logger, "info",
+ "Logged in successfully with username %s" % self.username)
#self.logger.info("; ".join(cookie_list))
return "; ".join(cookie_list)
else:
- if self.logger:
- self.logger.warning("Could not log in with username %s" % \
- self.username)
+ printlog(self.logger, "warning",
+ "Could not log in with username %s" % self.username)
return None
if __name__ == "__main__":
import sys
+ import getpass
+ import logging
- params = {
- "host" : "www.mblondel.org",
- "basename" : "/mediawiki/index.php",
- "https" : True
- }
-
- if(len(sys.argv) != 3):
- print "python user.py username password"
+ if len(sys.argv) < 4:
+ print "python user.py host urlbase username "
else:
- user = User(sys.argv[1], sys.argv[2], **params)
- print user.getCookieString()
+ if len(sys.argv) == 4:
+ https = False
+ else:
+ https = True
+
+ logger = logging.getLogger("mydebuglogger")
+ logger.setLevel(logging.DEBUG)
+ hdlr = logging.StreamHandler()
+ hdlr.setLevel(logging.DEBUG)
+ formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
+ hdlr.setFormatter(formatter)
+ logger.addHandler(hdlr)
+
+ params = {
+ "host" : sys.argv[1], # e.g. www.mblondel.org
+ "basename" : sys.argv[2], # e.g. /mediawiki/index.php
+ "https" : https,
+ "logger": logger
+ }
+
+ password = getpass.getpass()
+
+ user = User(sys.argv[3], password, **params)
+ user.getCookieString()
\ No newline at end of file