Skip to content

Commit 322d6b1

Browse files
authored
Merge pull request #90 from DenisaCG/include_exclude_config
Update backend configuration for including and excluding buckets
2 parents 270e724 + e18c1ed commit 322d6b1

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ c.DrivesConfig.access_key_id = "<Drives Access Key ID / IAM Access Key ID>"
5454
c.DrivesConfig.secret_access_key = "<Drives Secret Access Key / IAM Secret>"
5555
c.DrivesConfig.session_token = "<Drives Session Token / IAM Session Token (optional)>"
5656
c.DrivesConfig.provider = "<Drives provider e.g.: s3, gcs>"
57+
c.DrivesConfig.included_drives = "<List of the drives to be shown in the drives filebrowser (optional)>"
58+
c.DrivesConfig.excluded_drives = "<List of the drives to be excluded from the drives filebrowser listing (optional)>"
5759
```
5860

5961
### Custom credentials file path
@@ -76,6 +78,8 @@ export JP_DRIVES_ACCESS_KEY_ID="<Drives Access Key ID>"
7678
export JP_DRIVES_SECRET_ACCESS_KEY="<Drives Secret Access Key>"
7779
export JP_DRIVES_SESSION_TOKEN="<Drives Session Token (optional)>"
7880
export JP_DRIVES_CUSTOM_CREDENTIALS_PATH="<Path to local file which contains credentials (optional)>"
81+
export JP_DRIVES_INCLUDED_DRIVES ="<Names of the drives to be shown in the drives filebrowser, separated by spaces (optional)>"
82+
export JP_DRIVES_EXCLUDED_DRIVES="<Names of the drives to be excluded from the drives filebrowser listing, separated by spaces (optional)>"
7983
```
8084

8185
## Uninstall

jupyter_drives/base.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from sys import platform
33
import entrypoints
4-
from traitlets import Enum, Unicode, default
4+
from traitlets import Enum, Unicode, default, Set
55
from traitlets.config import Configurable
66
import boto3
77

@@ -71,10 +71,27 @@ def set_default_api_base_url(self):
7171
help="The source control provider.",
7272
)
7373

74+
excluded_drives = Set(
75+
trait=Unicode(),
76+
config=True,
77+
help="List of drives that should be excluded from drive browser listing. Drive names should be separated by spaces.",
78+
)
79+
80+
included_drives = Set(
81+
trait=Unicode(),
82+
config=True,
83+
help="List of drives that should be included in drive browser listing. Drive names should be separated by spaces.",
84+
)
85+
7486
def __init__(self, **kwargs):
7587
super().__init__(**kwargs)
7688
# check if credentials were already set in jupyter_notebook_config.py
7789
self.credentials_already_set = self.access_key_id is not None and self.secret_access_key is not None
90+
91+
# check list of excluded and included drives
92+
self.check_excluded_and_included_drives()
93+
94+
# load credentials
7895
self.load_credentials()
7996

8097
def load_credentials(self):
@@ -111,4 +128,26 @@ def load_credentials(self):
111128
self.access_key_id = c.access_key
112129
self.secret_access_key = c.secret_key
113130
self.region_name = s.region_name
114-
self.session_token = c.token
131+
self.session_token = c.token
132+
133+
def check_excluded_and_included_drives(self):
134+
# list of drives to exclude was provided
135+
if len(self.excluded_drives) != 0:
136+
return
137+
138+
# list of what drives to include was provided
139+
if len(self.included_drives) != 0:
140+
return
141+
142+
# check environment variables for excluded or included drives configuration, only one is taken into account
143+
if "JP_DRIVES_EXCLUDED_DRIVES" in os.environ:
144+
drives = os.environ["JP_DRIVES_EXCLUDED_DRIVES"]
145+
self.excluded_drives = drives.split(' ')
146+
return
147+
148+
if "JP_DRIVES_INCLUDED_DRIVES" in os.environ:
149+
drives = os.environ["JP_DRIVES_INCLUDED_DRIVES"]
150+
self.included_drives = drives.split(' ')
151+
return
152+
153+
return

jupyter_drives/manager.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def __init__(self, config: traitlets.config.Config) -> None:
5353
self._max_files_listed = 1025
5454
self._drives = None
5555
self._external_drives = {}
56-
self._excluded_drives = set()
57-
56+
self._excluded_drives = self._config.excluded_drives if len(self._config.excluded_drives) != 0 else set()
57+
self._included_drives = self._config.included_drives if len(self._config.included_drives) != 0 else set()
58+
5859
# instate fsspec file system
5960
self._file_system = fsspec.filesystem(self._config.provider, asynchronous=True)
6061

@@ -255,7 +256,14 @@ async def list_drives(self):
255256
reason=f"The following error occured when listing drives: {e}",
256257
)
257258

258-
for result in results:
259+
if len(self._included_drives) != 0:
260+
for result in results:
261+
if result.name not in self._included_drives:
262+
self._excluded_drives.add(result.name)
263+
# clear list once initialized
264+
self._included_drives.clear()
265+
266+
for result in results:
259267
if result.name not in self._excluded_drives:
260268
data.append(
261269
{

0 commit comments

Comments
 (0)