<s> from __future__ import unicode_literals <EOL> import argparse <EOL> import json <EOL> import sys <EOL> import webbrowser <EOL> import github3 <EOL> import six <EOL> from six . moves import urllib <EOL> MAX_URL_LEN = <NUM_LIT> <EOL> DEFAULT_DOMAIN = '<STR_LIT>' <EOL> def display ( contents , domain = DEFAULT_DOMAIN , force_gist = False ) : <EOL> """<STR_LIT>""" <EOL> url = make_url ( contents , domain , force_gist ) <EOL> webbrowser . open ( url ) <EOL> return url <EOL> to_geojsonio = display <EOL> def embed ( contents = '<STR_LIT>' , width = '<STR_LIT>' , height = <NUM_LIT> , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from IPython . display import HTML <EOL> url = make_url ( contents , * args , ** kwargs ) <EOL> html = '<STR_LIT>' . format ( <EOL> url = url , width = width , height = height ) <EOL> return HTML ( html ) <EOL> def make_url ( contents , domain = DEFAULT_DOMAIN , force_gist = False , <EOL> size_for_gist = MAX_URL_LEN ) : <EOL> """<STR_LIT>""" <EOL> contents = make_geojson ( contents ) <EOL> if len ( contents ) <= size_for_gist and not force_gist : <EOL> url = data_url ( contents , domain ) <EOL> else : <EOL> gist = _make_gist ( contents ) <EOL> url = gist_url ( gist . id , domain ) <EOL> return url <EOL> def make_geojson ( contents ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( contents , six . string_types ) : <EOL> return contents <EOL> if hasattr ( contents , '<STR_LIT>' ) : <EOL> features = [ _geo_to_feature ( contents ) ] <EOL> else : <EOL> try : <EOL> feature_iter = iter ( contents ) <EOL> except TypeError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> features = [ ] <EOL> for i , f in enumerate ( feature_iter ) : <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( i ) ) <EOL> features . append ( _geo_to_feature ( f ) ) <EOL> data = { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : features } <EOL> return json . dumps ( data ) <EOL> def _geo_to_feature ( ob ) : <EOL> """<STR_LIT>""" <EOL> mapping = ob . __geo_interface__ <EOL> if mapping [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> return mapping <EOL> else : <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : mapping } <EOL> def data_url ( contents , domain = DEFAULT_DOMAIN ) : <EOL> """<STR_LIT>""" <EOL> url = ( domain + '<STR_LIT>' + <EOL> urllib . parse . quote ( contents ) ) <EOL> return url <EOL> def _make_gist ( contents , description = '<STR_LIT>' , filename = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ghapi = github3 . GitHub ( ) <EOL> files = { filename : { '<STR_LIT:content>' : contents } } <EOL> gist = ghapi . create_gist ( description , files ) <EOL> return gist <EOL> def gist_url ( gist_id , domain = DEFAULT_DOMAIN ) : <EOL> """<STR_LIT>""" <EOL> url = ( domain + '<STR_LIT>' . format ( gist_id ) ) <EOL> return url <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = DEFAULT_DOMAIN , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:filename>' , <EOL> nargs = '<STR_LIT:?>' , <EOL> type = argparse . FileType ( '<STR_LIT:r>' ) , <EOL> default = sys . stdin , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> contents = args . filename . read ( ) <EOL> url = make_url ( contents , args . domain ) <EOL> if args . do_print : <EOL> print ( url ) <EOL> else : <EOL> webbrowser . open ( url ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
