From b16228c37c1e609af395941590943644a55a3720 Mon Sep 17 00:00:00 2001 From: Jerome Bernardes Date: Mon, 25 May 2020 15:00:40 +0200 Subject: [PATCH 1/2] github token can be provided by the command line to create gist --- README.rst | 1 + geojsonio/geojsonio.py | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index d4574eb..1a05a83 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,7 @@ Options: --print prints the url rather than opening it --domain="http://custominstancedomain.com/" + --github-token Github token used to create gist Installation ------------ diff --git a/geojsonio/geojsonio.py b/geojsonio/geojsonio.py index 8b255a0..0bf8d63 100755 --- a/geojsonio/geojsonio.py +++ b/geojsonio/geojsonio.py @@ -61,7 +61,7 @@ def embed(contents='', width='100%', height=512, *args, **kwargs): def make_url(contents, domain=DEFAULT_DOMAIN, force_gist=False, - size_for_gist=MAX_URL_LEN): + size_for_gist=MAX_URL_LEN, github_token=None): """ Returns the URL to open given the domain and contents. @@ -90,7 +90,7 @@ def make_url(contents, domain=DEFAULT_DOMAIN, force_gist=False, if len(contents) <= size_for_gist and not force_gist: url = data_url(contents, domain) else: - gist = _make_gist(contents) + gist = _make_gist(contents, github_token=github_token) url = gist_url(gist.id, domain) return url @@ -161,13 +161,17 @@ def data_url(contents, domain=DEFAULT_DOMAIN): return url -def _make_gist(contents, description='', filename='data.geojson'): +def _make_gist(contents, description='', filename='data.geojson', + github_token=None): """ - Create and return an anonymous gist with a single file and specified - contents + Create and return a gist with a single file and specified + contents. Gist is created under the account related to the provided + github_token (cf https://github.com/settings/tokens) """ - ghapi = github3.GitHub() + if github_token is None: + raise ValueError("Github token cannot be None") + ghapi = github3.login(token=github_token) files = {filename: {'content': contents}} gist = ghapi.create_gist(description, files) @@ -202,6 +206,11 @@ def main(): default=DEFAULT_DOMAIN, help='Alternate URL instead of http://geojson.io/') + parser.add_argument('-t', '--github-token', + dest='github_token', + default=None, + help='Github token used to create gist') + parser.add_argument('filename', nargs='?', type=argparse.FileType('r'), @@ -211,7 +220,7 @@ def main(): args = parser.parse_args() contents = args.filename.read() - url = make_url(contents, args.domain) + url = make_url(contents, args.domain, github_token=args.github_token) if args.do_print: print(url) else: From ddd26d00b9862317c4d3f89bef8a2b2ab23791e9 Mon Sep 17 00:00:00 2001 From: Jerome Bernardes Date: Mon, 25 May 2020 15:17:33 +0200 Subject: [PATCH 2/2] fix tests --- tests/test_geojsonio.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_geojsonio.py b/tests/test_geojsonio.py index 9b475e6..b17cbd1 100644 --- a/tests/test_geojsonio.py +++ b/tests/test_geojsonio.py @@ -128,7 +128,8 @@ def test_factory_gist(features): class Dummy(object): id = 'abc123' MockInstance.return_value = Dummy() - url = geojsonio.make_url(contents, size_for_gist=size-1) + url = geojsonio.make_url(contents, size_for_gist=size-1, + github_token='my_token') assert url == geojsonio.gist_url(Dummy.id) @@ -142,6 +143,6 @@ class Dummy(object): id = 'abc123' MockInstance.return_value = Dummy() url = geojsonio.make_url(contents, size_for_gist=size+1, - force_gist=True) + force_gist=True, github_token='my_token') assert url == geojsonio.gist_url(Dummy.id)