<commit_before>""" fURI File access through URIs. """


__author__ = 'amancevice'


import os
import urlparse
from .base import File, RemoteFile
from .s3   import S3File
from .sftp import SftpFile


_DISPATCHER = {
    ''     : File,
    'file' : File,
    's3'   : S3File,
    'sftp' : SftpFile }


def add_handler(scheme, handler_class):
    """
    Add a new class to the dispatcher

    Arguments:
        scheme        (str): URI prefix
        handler_class (class): Reference to class that extends furi.base

    Returns:
        None
    """
    _DISPATCHER[scheme] = handler_class


def open(uri, **kwargs):
    """ Returns a File object given a URI. """
    uri = urlparse.urlparse(os.path.expanduser(uri))
    try:
        return _DISPATCHER[uri.scheme](uri.geturl(), **kwargs)
    except KeyError:
        raise ValueError("Unsupported URI scheme: '%s'" % uri.scheme)


def download(source, target=None, **credentials):
    """ Download contents of a source URI into a target URI. If target URI is omitted,
        source is downloaded to ~/Downloads using the same filename.

        Arguments:
            source      (str):   URI of source file
            target      (str):   URI of target file (optional)
            credentials (dict):  Optional source connection credentials

        Returns:
            Handle to target file """
    src = open(source)
    src.connect(**credentials)
    tgt = open(target or os.path.expanduser("~/Downloads/%s" % src.filename), mode='rw')

    if not isinstance(src, RemoteFile):
        raise TypeError("Cannot download from non-RemoteFile.")
    if isinstance(tgt, RemoteFile):
        raise TypeError("Cannot download RemoteFile to other RemoteFile. Use local URI.")

    return src.download(tgt)
<commit_msg>cleaned up Steve's pydoc<commit_after>""" fURI File access through URIs. """


__author__ = 'amancevice'


import os
import urlparse
from .base import File, RemoteFile
from .s3   import S3File
from .sftp import SftpFile


_DISPATCHER = {
    ''     : File,
    'file' : File,
    's3'   : S3File,
    'sftp' : SftpFile }


def add_handler(scheme, handler_class):
    """ Add a new class to the dispatcher

        Arguments:
            scheme        (str):    URI scheme prefix
            handler_class (class):  Reference to class that extends furi.base """
    _DISPATCHER[scheme] = handler_class


def open(uri, **kwargs):
    """ Returns a File object given a URI. """
    uri = urlparse.urlparse(os.path.expanduser(uri))
    try:
        return _DISPATCHER[uri.scheme](uri.geturl(), **kwargs)
    except KeyError:
        raise ValueError("Unsupported URI scheme: '%s'" % uri.scheme)


def download(source, target=None, **credentials):
    """ Download contents of a source URI into a target URI. If target URI is omitted,
        source is downloaded to ~/Downloads using the same filename.

        Arguments:
            source      (str):   URI of source file
            target      (str):   URI of target file (optional)
            credentials (dict):  Optional source connection credentials

        Returns:
            Handle to target file """
    src = open(source)
    src.connect(**credentials)
    tgt = open(target or os.path.expanduser("~/Downloads/%s" % src.filename), mode='rw')

    if not isinstance(src, RemoteFile):
        raise TypeError("Cannot download from non-RemoteFile.")
    if isinstance(tgt, RemoteFile):
        raise TypeError("Cannot download RemoteFile to other RemoteFile. Use local URI.")

    return src.download(tgt)

{'fraction_non_alphanumeric': 0.10359467874327767, 'fraction_numerical': 0.0022643645626945937, 'mean_word_length': 2.7044025157232703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5168988', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1000, 'n_words': 342}