http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10917
Revision: 10917 Author: saper Date: 2013-01-14 10:43:29 +0000 (Mon, 14 Jan 2013) Log Message: ----------- Don't fail easily when wikipedia.py version cannot be determined
My script fails with:
Traceback (most recent call last): File "eksport.py", line 4, in <module> import wikipedia File "/home/admini/saper/wikipedia/pywikipedia/wikipedia.py", line 8748, in <module> writeToCommandLogFile() File "/home/admini/saper/wikipedia/pywikipedia/wikipedia.py", line 8685, in writeToCommandLogFile % (isoDate, version.getversiondict()['rev'], File "/home/admini/saper/wikipedia/pywikipedia/pywikibot/version.py", line 43, in getversiondict if len(version) == 4: TypeError: object of type 'NoneType' has no len()
The reason is that my scripts reach pywikipedia via sys.path and are not located in the pywikipedia directory.
This fixes the len(version) == 4 case, since version can be returned None.
A proper fix to determine wikipedia.py is needed, maybe.
Modified Paths: -------------- trunk/pywikipedia/pywikibot/version.py
Modified: trunk/pywikipedia/pywikibot/version.py =================================================================== --- trunk/pywikipedia/pywikibot/version.py 2013-01-14 10:17:17 UTC (rev 10916) +++ trunk/pywikipedia/pywikibot/version.py 2013-01-14 10:43:29 UTC (rev 10917) @@ -29,6 +29,11 @@ data['cmp_ver'] = 'n/a' return '%(tag)s (r%(rev)s, %(date)s, %(cmp_ver)s)' % data
+def unknown_version(): + """Version cannot be determined""" + cache = dict(tag='', rev='-1 (unknown)', date='0 (unknown)') + return cache + def getversiondict(): global cache if cache: @@ -40,16 +45,18 @@ (tag, rev, date) = getversion_nightly() except Exception, e: version = getfileversion('wikipedia.py') - if len(version) == 4: - # the value is most likely '$Id' + '$', it means that - # wikipedia.py got imported without using svn at all - cache = dict(tag='', rev='-1 (unknown)', date='0 (unknown)') - return cache - - id, file, rev, date, ts, author, dollar = version.split(' ') - tag = '' - date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%SZ') - rev += ' (wikipedia.py)' + if version: + try: + id, file, rev, date, ts, author, dollar = version.split(' ') + tag = '' + date = time.strptime('%sT%s' % (date, ts), '%Y-%m-%dT%H:%M:%SZ') + rev += ' (wikipedia.py)' + except ValueError: + # the value is most likely '$Id' + '$', it means that + # wikipedia.py got imported without using svn at all + return unknown_version() + else: + return unknown_version() datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date) cache = dict(tag=tag, rev=rev, date=datestring) return cache