# Path: bbcflib/common.py
# def unique_filename_in(path=None):
#     """Return a random filename unique in the given path.
# 
#     The filename returned is twenty alphanumeric characters which are
#     not already serving as a filename in *path*.  If *path* is
#     omitted, it defaults to the current working directory.
#     """
#     if path == None: path = os.getcwd()
#     def random_string():
#         return "".join([random.choice(string.letters + string.digits) for x in range(20)])
#     while True:
#         filename = random_string()
#         files = [f for f in os.listdir(path) if f.startswith(filename)]
#         if files == []: break
#     return filename


import os, re, tarfile, gzip, urllib2, StringIO
from urlparse import urlparse
from bbcflib.common import unique_filename_in
                        url = self._open_url(link)
                        tar = None
                        if re.sub('.gz[ip]*','',link).endswith(".tar"):
                            tar = tarfile.open(fileobj=url, mode='r|gz')
                            # Since the tar file contains exactly one file, calling
                            # ``next()`` on the tar gives us the file we want.  We cannot
                            # use ``getnames()[0]`` or similar methods, since they scan
                            # all the way through the file, and we cannot rewind on HTTP
                            # responses.
                            tar_filename = tar.next()
                            # extractfile returns a file-like object we can stream from.
                            input_file = tar.extractfile(tar_filename)
                        elif not(link.endswith(".gz")):
                            input_file = url
                        else:
                            input_file = gzip.GzipFile(fileobj=StringIO.StringIO(url.read()))
                        while True:
                            chunk = input_file.read(4096)
                            if chunk == '':
                                break
                            else:
                                output_file.write(chunk)
                        input_file.close()
                        if tar: tar.close()
                    except Exception as e:
                        raise Exception("Problem with file %s: %s"%(link,e))

        if to == None:
            target = unique_filename_in()
        elif os.path.isdir(to):

# Path: bbcflib/common.py
# def unique_filename_in(path=None):
#     """Return a random filename unique in the given path.
# 
#     The filename returned is twenty alphanumeric characters which are
#     not already serving as a filename in *path*.  If *path* is
#     omitted, it defaults to the current working directory.
#     """
#     if path == None: path = os.getcwd()
#     def random_string():
#         return "".join([random.choice(string.letters + string.digits) for x in range(20)])
#     while True:
#         filename = random_string()
#         files = [f for f in os.listdir(path) if f.startswith(filename)]
#         if files == []: break
#     return filename


# Path: bbcflib/daflims.py
import os, re, tarfile, gzip, urllib2, StringIO
from urlparse import urlparse
from bbcflib.common import unique_filename_in

                        url = self._open_url(link)
                        tar = None
                        if re.sub('.gz[ip]*','',link).endswith(".tar"):
                            tar = tarfile.open(fileobj=url, mode='r|gz')
                            # Since the tar file contains exactly one file, calling
                            # ``next()`` on the tar gives us the file we want.  We cannot
                            # use ``getnames()[0]`` or similar methods, since they scan
                            # all the way through the file, and we cannot rewind on HTTP
                            # responses.
                            tar_filename = tar.next()
                            # extractfile returns a file-like object we can stream from.
                            input_file = tar.extractfile(tar_filename)
                        elif not(link.endswith(".gz")):
                            input_file = url
                        else:
                            input_file = gzip.GzipFile(fileobj=StringIO.StringIO(url.read()))
                        while True:
                            chunk = input_file.read(4096)
                            if chunk == '':
                                break
                            else:
                                output_file.write(chunk)
                        input_file.close()
                        if tar: tar.close()
                    except Exception as e:
                        raise Exception("Problem with file %s: %s"%(link,e))

        if to == None:
            target = unique_filename_in()
        elif os.path.isdir(to):

            target = os.path.join(to, unique_filename_in(to))