~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to install/hvpull/browser/httpbrowser.py

  • Committer: Keith Hughitt
  • Date: 2012-04-23 16:02:25 UTC
  • mto: This revision was merged to the branch mainline in revision 732.
  • Revision ID: keith.hughitt@nasa.gov-20120423160225-xzoh82ejf37c8yr7
Incorporated HVPull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""HTTP data browser"""
 
2
import os
 
3
import urllib
 
4
from sgmllib import SGMLParser
 
5
from hvpull.browser.basebrowser import BaseDataBrowser
 
6
 
 
7
class HTTPDataBrowser(BaseDataBrowser):
 
8
    def __init__(self, uri):
 
9
        BaseDataBrowser.__init__(self, uri)
 
10
 
 
11
    def get_directories(self, location):
 
12
        """Get a list of directories at the root of the dataprovider.  
 
13
        We assume that these directories are in fact a list of instrument
 
14
        nicknames."""
 
15
        return filter(lambda url: url.endswith("/"), self._query(location))
 
16
    
 
17
    def get_files(self, location, extension):
 
18
        """Get all the files that end with specified extension at the uri"""
 
19
        return filter(lambda url: url.endswith("." + extension), 
 
20
                      self._query(location))
 
21
    
 
22
    def _query(self, location):
 
23
        """Get a list of files and folders at the specified remote location"""
 
24
        # query the remote location for the list of files and subdirectories 
 
25
        url_lister = URLLister()
 
26
        result = url_lister.read(location)
 
27
        url_lister.close()
 
28
 
 
29
        urls = filter(lambda url: url[0] != "/" and url[0] != "?", result)
 
30
        
 
31
        return [os.path.join(location, url) for url in urls]
 
32
    
 
33
class URLLister(SGMLParser):
 
34
    '''
 
35
    Created on Nov 1, 2011
 
36
    @author: Jack Ireland <jack.ireland@nasa.gov>
 
37
    copied from the original version of the download code.
 
38
    '''
 
39
    def __init__(self):
 
40
        """Create a new URLLister"""
 
41
        SGMLParser.__init__(self)
 
42
        self.urls = []
 
43
 
 
44
    def read(self, uri):
 
45
        """Read a URI and return a list of files/directories"""
 
46
        usock = urllib.urlopen(uri)
 
47
        self.feed(usock.read())
 
48
        usock.close()
 
49
        
 
50
        return self.urls
 
51
        
 
52
    def reset(self):
 
53
        """Reset state of URLLister"""
 
54
        SGMLParser.reset(self)
 
55
        self.urls = []
 
56
 
 
57
    def start_a(self, attrs):
 
58
        href = [v for k, v in attrs if k == 'href']
 
59
        if href:
 
60
            self.urls.extend(href)