Advertisement
linuxlizard

make import everything

Mar 6th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # parse the python module index
  4. # import everything
  5. # and
  6. # from everything import *
  7. #
  8. # because I was curious, that's why
  9. #
  10. # davep 06-Mar-2014
  11.  
  12. from HTMLParser import HTMLParser
  13. from urlparse import urlparse
  14. import os.path
  15. import importlib
  16.  
  17. # use the requests library instead of urllib
  18. # http://docs.python-requests.org/en/latest/
  19. import requests
  20.  
  21. url = "http://docs.python.org/2/py-modindex.html"
  22.  
  23. class MyHTMLParser(HTMLParser):
  24.     def handle_starttag(self,tag,attr_list):
  25.         if tag=="a":
  26.             for attr in attr_list :
  27.                 if "href" not in attr :
  28.                     return
  29.             href = attr_list[0][1]
  30.             if not href.startswith("library"):
  31.                 return
  32.  
  33.             url = urlparse(href)
  34.             modulename = os.path.split(url.path)[-1].replace(".html","")
  35.  
  36.             try :
  37.                 importlib.import_module(modulename)
  38.             except ImportError:
  39.                 pass
  40.             else:
  41.                 print "from {0} import *".format(modulename)
  42. #                print "import", modulename
  43.  
  44. def main():
  45.     # only do the URL fetch once (let's be polite to the server)
  46.     cachefilename = "modindex.html"
  47.     if not os.path.exists(cachefilename) :
  48.         r = requests.get(url)
  49.         with open(cachefilename,"w") as outfile:
  50.             outfile.write(r.content)
  51.         html_str = r.content
  52.     else :
  53.         with open("modindex.html","r") as outfile:
  54.             html_str = "".join(outfile.readlines())
  55.        
  56.     parser = MyHTMLParser()
  57.     parser.feed(html_str)
  58.  
  59. if __name__=='__main__':
  60.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement