Skip to content

Commit 5b4a141

Browse files
Update the get method of the Drive class to be able to browser into subfolders.
1 parent a27b324 commit 5b4a141

File tree

6 files changed

+48
-49
lines changed

6 files changed

+48
-49
lines changed

jupyter_drives/base.py

-8
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ class DrivesConfig(Configurable):
5454
help="Custom path of file where credentials are located. Extension automatically checks jupyter_notebook_config.py or directly in ~/.aws/credentials for AWS CLI users."
5555
)
5656

57-
custom_credentials_path = '~/.jupyter/jupyter-notebook-config.py'
58-
5957
@default("api_base_url")
6058
def set_default_api_base_url(self):
6159
# for AWS S3 drives
@@ -80,9 +78,6 @@ def __init__(self, **kwargs):
8078
def _load_credentials(self):
8179
# check if credentials were already set in jupyter_notebook_config.py
8280
if self.access_key_id is not None and self.secret_access_key is not None:
83-
print('WE GOT KEYS FROM CONFIG FILE')
84-
print('access_key:', self.access_key_id)
85-
print('secret_key:', self.secret_access_key)
8681
return
8782

8883
# check if user provided custom path for credentials extraction
@@ -93,10 +88,7 @@ def _load_credentials(self):
9388
# if not, try to load credentials from AWS CLI
9489
aws_credentials_path = "~/.aws/credentials" #add read me about credentials path in windows: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html
9590
if os.path_exists(aws_credentials_path):
96-
print('WE WILL USE THE AWS CREDENTIALS SET WITH AWSCLI')
9791
self.access_key_id, self.secret_access_key, self.session_token = self._extract_credentials_from_file(aws_credentials_path)
98-
print('access_key:', self.access_key_id)
99-
print('secret_key:', self.secret_access_key)
10092
return
10193

10294
def _extract_credentials_from_file(self, file_path):

jupyter_drives/handlers.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ async def get(self):
6060
async def post(self):
6161
body = self.get_json_body()
6262
result = await self._manager.mount_drive(**body)
63-
print('result:', result)
64-
self.finish(json.dump(result))
65-
#self.finish(result)
63+
self.finish(result["message"])
6664

6765
class ContentsJupyterDrivesHandler(JupyterDrivesAPIHandler):
6866
"""
@@ -74,9 +72,7 @@ def initialize(self, logger: logging.Logger, manager: JupyterDrivesManager):
7472
@tornado.web.authenticated
7573
async def get(self, path: str = "", drive: str = ""):
7674
result = await self._manager.get_contents(drive, path)
77-
print("result:", result)
7875
self.finish(result)
79-
8076

8177
@tornado.web.authenticated
8278
async def post(self, path: str = "", drive: str = ""):

jupyter_drives/managers/s3.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,22 @@ async def mount_drive(self, drive_name):
8383
Args:
8484
S3ContentsManager
8585
'''
86-
print('In mount drive:' )
86+
8787
try :
8888
s3_contents_manager = S3ContentsManager(
89-
access_key = self._config.access_key_id,
89+
access_key_id = self._config.access_key_id,
9090
secret_access_key = self._config.secret_access_key,
9191
endpoint_url = self._config.api_base_url,
9292
bucket = drive_name
9393
)
94+
9495
# checking if the drive wasn't mounted already
9596
if drive_name not in self.s3_content_managers or self.s3_content_managers[drive_name] is None:
9697

9798
# dealing with long-term credentials (access key, secret key)
9899
if self._config.session_token is None:
99100
s3_contents_manager = S3ContentsManager(
100-
access_key = self._config.access_key_id,
101+
access_key_id = self._config.access_key_id,
101102
secret_access_key = self._config.secret_access_key,
102103
endpoint_url = self._config.api_base_url,
103104
bucket = drive_name
@@ -106,7 +107,7 @@ async def mount_drive(self, drive_name):
106107
# dealing with short-term credentials (access key, secret key, session token)
107108
else:
108109
s3_contents_manager = S3ContentsManager(
109-
access_key = self._config.access_key_id,
110+
access_key_id = self._config.access_key_id,
110111
secret_access_key = self._config.secret_access_key,
111112
session_token = self._config.session_token,
112113
endpoint_url = self._config.api_base_url,
@@ -126,7 +127,6 @@ async def mount_drive(self, drive_name):
126127
except Exception as e:
127128
response = {"code": 400, "message": e}
128129

129-
print('RESPONSE IN MOUNT_DRIVE:', response)
130130
return response
131131

132132
async def unmount_drive(self, drive_name):
@@ -160,7 +160,6 @@ async def get_contents(self, drive_name, path = ""):
160160
try:
161161
if drive_name in self.s3_content_managers:
162162
contents = self.s3_content_managers[drive_name].fs.ls(path)
163-
print('contents:', contents)
164163
code = 200
165164
response["contents"] = contents
166165
else:

src/contents.ts

+38-22
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,48 @@ export class Drive implements Contents.IDrive {
317317
localPath: string,
318318
options?: Contents.IFetchOptions
319319
): Promise<Contents.IModel> {
320+
let relativePath = '';
321+
if (localPath !== '') {
322+
if (localPath.includes(this.name)) {
323+
relativePath = localPath.split(this.name + '/')[1];
324+
} else {
325+
relativePath = localPath;
326+
}
327+
}
328+
320329
postDriveMounted(this.name);
321-
const response = await getDriveContents(this.name, localPath);
330+
const response = await getDriveContents(this.name, relativePath);
322331
let fileExtension: string = '';
323332
const driveBrowserContents: Array<object> = [];
324333
const driveContents: Array<string> = response['contents'];
325334
driveContents.forEach(content => {
326-
fileExtension = content.split('.')[1];
327-
driveBrowserContents.push({
328-
name: content,
329-
path: this.name + '/' + content,
330-
last_modified: '',
331-
created: '',
332-
content: null,
333-
format: null,
334-
mimetype: fileExtension === 'txt' ? 'text/plain' : '',
335-
size: undefined,
336-
writable: true,
337-
type:
338-
fileExtension === 'txt'
339-
? 'txt'
340-
: fileExtension === 'ipynb'
341-
? 'notebook'
342-
: 'directory'
343-
});
335+
const levels = content.split('/');
336+
const path = this.name + '/' + content;
337+
if (levels[levels.length - 1] !== '') {
338+
const itemName = levels[levels.length - 1];
339+
fileExtension = content.split('.')[1];
340+
driveBrowserContents.push({
341+
name: itemName,
342+
path: path,
343+
last_modified: '',
344+
created: '',
345+
content: null,
346+
format: null,
347+
mimetype: fileExtension === 'txt' ? 'text/plain' : '',
348+
size: undefined,
349+
writable: true,
350+
type:
351+
fileExtension === 'txt'
352+
? 'txt'
353+
: fileExtension === 'ipynb'
354+
? 'notebook'
355+
: 'directory'
356+
});
357+
}
344358
});
345-
const drivePath: Contents.IModel = {
359+
//}
360+
361+
const driveBrowserContentsModel: Contents.IModel = {
346362
name: this.name,
347363
path: this.name,
348364
last_modified: '',
@@ -355,8 +371,8 @@ export class Drive implements Contents.IDrive {
355371
type: 'directory'
356372
};
357373

358-
Contents.validateContentsModel(drivePath);
359-
return drivePath;
374+
Contents.validateContentsModel(driveBrowserContentsModel);
375+
return driveBrowserContentsModel;
360376
}
361377

362378
/**

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ namespace CommandIDs {
4141
async function createDrivesList() {
4242
const response = await getDrivesList();
4343
const bucketList: Array<IBucket> = response['data'];
44-
console.log('bucketList:', bucketList);
4544
const S3Drives: Drive[] = [];
4645
bucketList.forEach(item => {
4746
const drive = new Drive();
@@ -64,7 +63,7 @@ export function camelCaseToDashedCase(name: string) {
6463

6564
function restoreDriveName(id: string) {
6665
const list1 = id.split('-file-');
67-
let driveName = list1[0];
66+
const driveName = list1[0];
6867
return driveName;
6968
}
7069

src/s3requests.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ export async function postDriveMounted(driveName: string) {
2222
}
2323

2424
export async function getDriveContents(driveName: string, path: string) {
25-
return await requestAPI<any>(
26-
'drives' + '/' + driveName + '/' + path,
27-
{
28-
method: 'GET'
29-
}
30-
);
25+
return await requestAPI<any>('drives' + '/' + driveName + '/' + path, {
26+
method: 'GET'
27+
});
3128
}

0 commit comments

Comments
 (0)