Commits

Anonymous committed 6452b28

Use the pythom module urllib2 instead of calling wget.

Comments (0)

Files changed (1)

 import re
 import os
 
+def download_file(url, localFile = None, directory = None):
+    r"""
+    Download a url from the Web, and copy it to the location
+    specified by directory/localFile.
+
+    If directory is None, use the current directory.
+    If localFile is None, use the part of the url after the last '/'
+    as the file name.
+    """
+    import urllib2
+
+    print "Downloading file: ", url
+    try:
+        urlHandle = urllib2.urlopen(url)
+
+        if localFile is None:
+            localFile = url.split('/')[-1]
+
+        if not directory is None:
+            localFile = directory + '/' + localFile
+        localHandle = open(localFile, 'w')
+
+        localHandle.write(urlHandle.read())
+
+        urlHandle.close()
+        localHandle.close()
+    except urllib2.HTTPError, error:
+        print "Unable to download file: ", url
+        print error.msg
+
 def detect_math(text):
     r"""
         Replace TeX math delimiters $...$ and $$...$$ with the corresponding
                 img_options += ["height=%spx" % dict(attrs).get('height')]
             img_options = ",".join(img_options)
             if where_is == 'http':
-                download = os.popen('cd %s; wget %s' % (self.temp_dir,dict(attrs).get('src')))
+                url = dict(attrs).get('src')
+                download_file(url, directory = self.temp_dir)
+
                 file = dict(attrs).get('src').split('/')
                 file = file[len(file)-1]
                 self.latex[-1] += "\\includegraphics[%s]{%s}" % (img_options,file)