Skip to content

Commit d99c343

Browse files
authored
Merge pull request #2 from sean-morris/main
Added nest_asyncio to plugins
2 parents bb2a30a + e560358 commit d99c343

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ This plugin expects URLs included in the nbgitpuller link to be in the following
1717

1818
In all of these cases, the archive must be publicly available or "shared" with everyone in the case of Google Drive.
1919

20+
## Plugin Development Notes
21+
Please note, that any nbgitpuller-downloader-plugin needs to have the following import and call:
22+
```
23+
import nest_asyncio
24+
nest_asyncio.apply()
25+
```
26+
This is needed to nest the async calls for your downloader-plugin into the current event loop being used in
27+
nbgitpuller. The three plugin's in this repo provide an example of the implementation.
28+
2029
## Installation
2130

2231
```shell

src/nbgitpuller_downloader_dropbox/dropbox_downloader.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
2-
from nbgitpuller.plugin_hook_specs import hookimpl
31
import asyncio
2+
import nest_asyncio
3+
from nbgitpuller.plugin_hook_specs import hookimpl
4+
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
5+
46

7+
# this allows us to nest usage of the event_loop from asyncio
8+
# being used by tornado in jupyter distro
9+
# Ref: https://medium.com/@vyshali.enukonda/how-to-get-around-runtimeerror-this-event-loop-is-already-running-3f26f67e762e
10+
nest_asyncio.apply()
511

612
@hookimpl
713
def handle_files(helper_args, query_line_args):

src/nbgitpuller_downloader_generic_web/generic_web_downloader.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
2-
from nbgitpuller.plugin_hook_specs import hookimpl
31
import asyncio
2+
import nest_asyncio
3+
from nbgitpuller.plugin_hook_specs import hookimpl
4+
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
5+
6+
7+
# this allows us to nest usage of the event_loop from asyncio
8+
# being used by tornado in jupyter distro
9+
# Ref: https://medium.com/@vyshali.enukonda/how-to-get-around-runtimeerror-this-event-loop-is-already-running-3f26f67e762e
10+
nest_asyncio.apply()
411

512
@hookimpl
613
def handle_files(helper_args, query_line_args):
714
"""
15+
This begins the event loop that will both download the compressed archive and send messages
16+
about the progress of the download to the UI.
817
:param dict helper_args: the function, helper_args["progress_func"], that writes messages to
918
the progress stream in the browser window and the download_q, helper_args["download_q"] the progress function uses.
1019
:param dict query_line_args: this includes all the arguments included on the nbgitpuller URL

src/nbgitpuller_downloader_googledrive/googledrive_downloader.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
from nbgitpuller.plugin_hook_specs import hookimpl
2-
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
31
import re
42
import asyncio
53
import aiohttp
4+
import nest_asyncio
5+
from nbgitpuller.plugin_hook_specs import hookimpl
6+
from nbgitpuller_downloader_plugins_util.plugin_helper import HandleFilesHelper
7+
8+
# this allows us to nest usage of the event_loop from asyncio
9+
# being used by tornado in jupyter distro
10+
# Ref: https://medium.com/@vyshali.enukonda/how-to-get-around-runtimeerror-this-event-loop-is-already-running-3f26f67e762e
11+
nest_asyncio.apply()
612

713
DOWNLOAD_URL = "https://docs.google.com/uc?export=download"
814

@@ -78,31 +84,31 @@ async def download_archive_for_google(repo=None, temp_download_file=None):
7884
"""
7985
yield "Downloading archive ...\n"
8086
try:
81-
id = get_id(repo)
82-
CHUNK_SIZE = 1024
87+
file_id = get_id(repo)
88+
chunk_size = 1024
8389
async with aiohttp.ClientSession() as session:
84-
async with session.get(DOWNLOAD_URL, params={'id': id}) as response:
90+
async with session.get(DOWNLOAD_URL, params={'id': file_id}) as response:
8591
token = get_confirm_token(session, repo)
8692
if token:
87-
params = {'id': id, 'confirm': token}
93+
params = {'id': file_id, 'confirm': token}
8894
response = await session.get(repo, params=params)
89-
with open(temp_download_file, 'ab') as fd:
95+
with open(temp_download_file, 'ab') as file_handle:
9096
count_chunks = 1
9197
while True:
9298
count_chunks += 1
9399
if count_chunks % 1000 == 0:
94100
display = count_chunks / 1000
95101
yield f"Downloading Progress ... {display}MB\n"
96-
chunk = await response.content.read(CHUNK_SIZE)
102+
chunk = await response.content.read(chunk_size)
97103
if not chunk:
98104
break
99-
fd.write(chunk)
105+
file_handle.write(chunk)
100106
yield "Archive Downloaded....\n"
101-
except Exception as e:
102-
raise e
107+
except Exception as ex:
108+
raise ex
103109

104110

105-
async def get_response_from_drive(url, id):
111+
async def get_response_from_drive(url, file_id):
106112
"""
107113
You need to check to see that Google Drive has not asked the
108114
request to confirm that they disabled the virus scan on files that
@@ -112,15 +118,15 @@ async def get_response_from_drive(url, id):
112118
parameter.
113119
114120
:param str url: the google download URL
115-
:param str id: the google id of the file to download
121+
:param str file_id: the google id of the file to download
116122
:return response object
117123
:rtype json object
118124
"""
119125
async with aiohttp.ClientSession() as session:
120-
async with session.get(url, params={'id': id}) as response:
126+
async with session.get(url, params={'id': file_id}) as response:
121127
token = get_confirm_token(session, url)
122128
if token:
123-
params = {'id': id, 'confirm': token}
129+
params = {'id': file_id, 'confirm': token}
124130
response = await session.get(url, params=params)
125131
return response
126132
return response
@@ -140,6 +146,6 @@ def determine_file_extension_from_response(response):
140146
ext = fname.split(".")[1]
141147

142148
if ext is None:
143-
m = f"Could not determine compression type of: {content_disposition}"
144-
raise Exception(m)
149+
message = f"Could not determine compression type of: {content_disposition}"
150+
raise Exception(message)
145151
return ext

0 commit comments

Comments
 (0)