views:

32

answers:

3

Using pyblog.py, I got the following error, which I then tried to more gracefully handle:

Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster.py", line 11, in <module>
    date = blogurl.get_recent_posts(1)[0]['dateCreated']
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\pyblog.py", line 129, in get_recent_posts
    return self.execute('metaWeblog.getRecentPosts', blogid, self.username, self.password, numposts)
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\pyblog.py", line 93, in execute
    raise BlogError(fault.faultString)
BlogError: XML-RPC services are disabled on this blog.  An admin user can enable them at http://example.com/blogname/wp-admin/options-writing.php
>>> 

So I tried the following code to without crashing the script:

for blog in bloglist:
    try:
        blogurl = pyblog.WordPress('http://example.com' + blog + 'xmlrpc.php', 'admin', 'laxbro24')
        date = blogurl.get_recent_posts(1)[0]['dateCreated']
        print blog + ', ' + str(date.timetuple().tm_mon) + '/' + str(date.timetuple().tm_mday) + '/' + str(date.timetuple().tm_year)
    except BlogError:
        print "Oops! The blog at " + blogurl + " is not configured properly."

Only to get the following error:

Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster.py", line 13, in <module>
    except BlogError:
NameError: name 'BlogError' is not defined

Isn't the name blog error defined by PyBlog, since that's where I got that name in the first place? Am I using "except" wrong? Thanks for any tips!

+2  A: 

Your except is syntactically correct. And yet it fails because you haven't explicitly imported the BlogError exception class into your program's namespace.

To fix this explicitly import the BlogError class. For e.g.

from pyblog import BlogError
try:
    ...
except BlogError:
    ...
Manoj Govindan
Thanks! I really appreciate the answer.
Michael Morisy
+5  A: 

Yes, it is using BlogError, but you have not imported BlogError into your namespace to reference. You instead want to be using pyblog.BlogError:

for blog in bloglist:
    try:
        blogurl = pyblog.WordPress('http://example.com' + blog + 'xmlrpc.php', 'admin', 'laxbro24')
        date = blogurl.get_recent_posts(1)[0]['dateCreated']
        print blog + ', ' + str(date.timetuple().tm_mon) + '/' + str(date.timetuple().tm_mday) + '/' + str(date.timetuple().tm_year)
    except pyblog.BlogError:
        print "Oops! The blog at " + blogurl + " is not configured properly."

Remember that exceptions follow the same rules of scoping that any python object does.

Mike Axiak
Thanks, I really need to brush up on my scoping. Appreciate the help!
Michael Morisy
+2  A: 

the code will be

 from pyblog import BlogError
jknair