<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

import argparse
import json
import sys
import urllib
import webbrowser

import github3

MAX_URL_LEN = 150e3  # Size threshold above which a gist is created


def display(contents, domain='http://geojson.io/', force_gist=False):
    url = geojsonio_url(contents, domain, force_gist)
    webbrowser.open(url)
    return url
# display() used to be called to_geojsonio. Keep it around for now...
to_geojsonio = display


def embed(contents='', width='100%', height=512, *args, **kwargs):
    """
    Embed geojson.io in an iframe in Jupyter/IPython notebook. 

    Parameters
    ----------
    contents - see geojsonio_url()
    width - string, default '100%' - width of the iframe
    height - string / int, default 512 - height of the iframe

    """
    from IPython.display import HTML
    
    url = geojsonio_url(contents, *args, **kwargs)
    html = '<iframe src={url} width={width} height={height}></iframe>'.format(
            url=url, width=width, height=height)
    return HTML(html)


def geojsonio_url(contents, domain='http://geojson.io/', force_gist=False):
    """
    Returns the URL to open given the domain and contents

    The input contents may be:
    * string - assumed to be GeoJSON
    * an object that implements __geo_interface__
        A FeatureCollection will be constructed with one feature,
        the object.
    * a sequence of objects that each implement __geo_interface__
        A FeatureCollection will be constructed with the objects
        as the features

    For more information about __geo_interface__ see:
    https://gist.github.com/sgillies/2217756

    If the contents are large, then a gist will be created.

    """
    contents = _parse_contents(contents)
    if len(contents) <= MAX_URL_LEN and not force_gist:
        url = _data_url(domain, contents)
    else:
        gist = _create_gist(contents)
        url = _gist_url(domain, gist.id)

    return url


def _parse_contents(contents):
    """
    Return a GeoJSON string from a variety of inputs.
    See the documentation for geojsonio_url for the possible contents
    input.

    Returns
    -------
    GeoJSON string

    """
    if isinstance(contents, basestring):
        return contents

    if hasattr(contents, '__geo_interface__'):
        features = [_geo_to_feature(contents)]
    else:
        try:
            feature_iter = iter(contents)
        except TypeError:
            raise ValueError('Unknown type for input')

        features = []
        for i, f in enumerate(feature_iter):
            if not hasattr(f, '__geo_interface__'):
                raise ValueError('Unknown type at index {}'.format(i))
            features.append(_geo_to_feature(f))

    data = {'type': 'FeatureCollection', 'features': features}
    return json.dumps(data)


def _geo_to_feature(ob):
    """
    Return a GeoJSON Feature from an object that implements
    __geo_interface__

    If the object's type is a geometry, return a Feature with empty
    properties and the object's mapping as the feature geometry. If the
    object's type is a Feature, then return it.

    """
    mapping = ob.__geo_interface__
    if mapping['type'] == 'Feature':
        return mapping
    else:
        return {'type': 'Feature',
                'geometry': mapping}


def _create_gist(contents, description='', filename='data.geojson'):
    """
    Create and return an anonymous gist with a single file and specified
    contents

    """
    ghapi = github3.GitHub()
    files = {filename: {'content': contents}}
    gist = ghapi.create_gist(description, files)

    return gist


def _data_url(domain, contents):
    url = (domain + '#data=data:application/json,' +
           urllib.quote(contents))
    return url


def _gist_url(domain, gist_id):
    url = (domain + '#id=gist:/{}'.format(gist_id))
    return url


def main():
    parser = argparse.ArgumentParser(
        description='Quickly visualize GeoJSON data on geojson.io')

    parser.add_argument('-p', '--print',
                        dest='do_print',
                        action='store_true',
                        help='print the URL')

    parser.add_argument('-d', '--domain',
                        dest='domain',
                        default='http://geojson.io',
                        help='Alternate URL instead of http://geojson.io/')

    parser.add_argument('filename',
                        nargs='?',
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="The file to send to geojson.io")

    args = parser.parse_args()

    contents = args.filename.read()
    url = geojsonio_url(contents, args.domain)
    if args.do_print:
        print(url)
    else:
        webbrowser.open(url)

if __name__ == '__main__':
    main()<commit_msg>Refactored to lots of teensy functions<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

import argparse
import json
import sys
import urllib
import webbrowser

import github3

MAX_URL_LEN = 150e3  # Size threshold above which a gist is created
DEFAULT_DOMAIN = 'http://geojson.io/'

def display(contents, domain=DEFAULT_DOMAIN, force_gist=False):
    url = make_url(contents, domain, force_gist)
    webbrowser.open(url)
    return url
# display() used to be called to_geojsonio. Keep it around for now...
to_geojsonio = display


def embed(contents='', width='100%', height=512, *args, **kwargs):
    """
    Embed geojson.io in an iframe in Jupyter/IPython notebook. 

    Parameters
    ----------
    contents - see make_url()
    width - string, default '100%' - width of the iframe
    height - string / int, default 512 - height of the iframe

    """
    from IPython.display import HTML
    
    url = make_url(contents, *args, **kwargs)
    html = '<iframe src={url} width={width} height={height}></iframe>'.format(
            url=url, width=width, height=height)
    return HTML(html)


def make_url(contents, domain=DEFAULT_DOMAIN, force_gist=False):
    """
    Returns the URL to open given the domain and contents

    The input contents may be:
    * string - assumed to be GeoJSON
    * an object that implements __geo_interface__
        A FeatureCollection will be constructed with one feature,
        the object.
    * a sequence of objects that each implement __geo_interface__
        A FeatureCollection will be constructed with the objects
        as the features

    For more information about __geo_interface__ see:
    https://gist.github.com/sgillies/2217756

    If the contents are large, then a gist will be created.

    """
    contents = parse_contents(contents)
    factory = get_url_factory(contents, force_gist, size_for_gist=MAX_URL_LEN)
    url = factory(contents, domain)

    return url


def parse_contents(contents):
    """
    Return a GeoJSON string from a variety of inputs.
    See the documentation for make_url for the possible contents
    input.

    Returns
    -------
    GeoJSON string

    """
    if isinstance(contents, basestring):
        return contents

    if hasattr(contents, '__geo_interface__'):
        features = [_geo_to_feature(contents)]
    else:
        try:
            feature_iter = iter(contents)
        except TypeError:
            raise ValueError('Unknown type for input')

        features = []
        for i, f in enumerate(feature_iter):
            if not hasattr(f, '__geo_interface__'):
                raise ValueError('Unknown type at index {}'.format(i))
            features.append(_geo_to_feature(f))

    data = {'type': 'FeatureCollection', 'features': features}
    return json.dumps(data)


def _geo_to_feature(ob):
    """
    Return a GeoJSON Feature from an object that implements
    __geo_interface__

    If the object's type is a geometry, return a Feature with empty
    properties and the object's mapping as the feature geometry. If the
    object's type is a Feature, then return it.

    """
    mapping = ob.__geo_interface__
    if mapping['type'] == 'Feature':
        return mapping
    else:
        return {'type': 'Feature',
                'geometry': mapping}


def get_url_factory(contents, force_gist=False, size_for_gist=MAX_URL_LEN):
    """
    Return a function that will generate the geojson.io URL

    Factory functions take the domain and contents and return the appropriate
    URL that geojson.io knows how to handle. The automatic switching logic to
    choose which factory is used belongs here.

    """
    if len(contents) <= size_for_gist and not force_gist:
        func = get_data_url
    else:
        func = make_gist_and_url

    return func


def get_data_url(contents, domain=DEFAULT_DOMAIN):
    url = (domain + '#data=data:application/json,' +
           urllib.quote(contents))
    return url


def make_gist_and_url(contents, domain=DEFAULT_DOMAIN):
    gist = make_gist(contents)
    url = get_gist_url(gist.id, domain)
    return url


def make_gist(contents, description='', filename='data.geojson'):
    """
    Create and return an anonymous gist with a single file and specified
    contents

    """
    ghapi = github3.GitHub()
    files = {filename: {'content': contents}}
    gist = ghapi.create_gist(description, files)

    return gist


def get_gist_url(gist_id, domain=DEFAULT_DOMAIN):
    url = (domain + '#id=gist:/{}'.format(gist_id))
    return url


def main():
    parser = argparse.ArgumentParser(
        description='Quickly visualize GeoJSON data on geojson.io')

    parser.add_argument('-p', '--print',
                        dest='do_print',
                        action='store_true',
                        help='print the URL')

    parser.add_argument('-d', '--domain',
                        dest='domain',
                        default=DEFAULT_DOMAIN,
                        help='Alternate URL instead of http://geojson.io/')

    parser.add_argument('filename',
                        nargs='?',
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="The file to send to geojson.io")

    args = parser.parse_args()

    contents = args.filename.read()
    url = make_url(contents, args.domain)
    if args.do_print:
        print(url)
    else:
        webbrowser.open(url)

if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.1006277160791888, 'fraction_numerical': 0.005021728633510382, 'mean_word_length': 2.528449744463373, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3477011', 'n_tokens_mistral': 3100, 'n_tokens_neox': 2915, 'n_words': 904}