# Path: lingtools/util/datamanager.py
# def download(url, path=None):
# def unzip(filepath, destpath='.'):


import sys
import os
import argparse
import errno
import urllib2
from urlparse import urljoin
from HTMLParser import HTMLParser
from lingtools.util import datamanager

class LinkDownloader(HTMLParser):
    """A parser that downloads files with the specified exension."""

    def __init__(self, ext):
        self.last_tag = None
        self.files = []
        self.ext = ext
        HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
        self.last_tag = tag

    def handle_endtag(self, tag):
        self.last_tag = None

    def handle_data(self, data):
        if self.last_tag == "a" and data.endswith(self.ext):
            self.files.append(data)

    def download_files(self, url, path):
        """Download all the files of the given extension linked from a URL."""
        listing = urllib2.urlopen(url).read()
        self.feed(listing)
        self._download(url, path)

    def _download(self, base_url, path):
        """Download and unzip all files we found."""
        for afile in self.files:
            file_url = urljoin(base_url, afile)

# Path: lingtools/util/datamanager.py
# def download(url, path=None):
# def unzip(filepath, destpath='.'):


# Path: download_linked_files.py
import sys
import os
import argparse
import errno
import urllib2
from urlparse import urljoin
from HTMLParser import HTMLParser
from lingtools.util import datamanager


class LinkDownloader(HTMLParser):
    """A parser that downloads files with the specified exension."""

    def __init__(self, ext):
        self.last_tag = None
        self.files = []
        self.ext = ext
        HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
        self.last_tag = tag

    def handle_endtag(self, tag):
        self.last_tag = None

    def handle_data(self, data):
        if self.last_tag == "a" and data.endswith(self.ext):
            self.files.append(data)

    def download_files(self, url, path):
        """Download all the files of the given extension linked from a URL."""
        listing = urllib2.urlopen(url).read()
        self.feed(listing)
        self._download(url, path)

    def _download(self, base_url, path):
        """Download and unzip all files we found."""
        for afile in self.files:
            file_url = urljoin(base_url, afile)

            file_path = datamanager.download(file_url,