Skip to content

Commit

Permalink
Try to implement the Untitled method in Drive class in contents.ts an…
Browse files Browse the repository at this point in the history
…d remove non unnecessary files.
  • Loading branch information
HaudinFlorence committed Dec 11, 2023
1 parent dbe8a9c commit 0b7f689
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 900 deletions.
3 changes: 2 additions & 1 deletion schema/widget.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"name": "drive",
"command": "drives:open-drives-dialog",
"rank": 35
}
},
{ "name": "fileNameSearcher", "rank": 40 }
]
},
"title": "'@jupyter/drives",
Expand Down
238 changes: 227 additions & 11 deletions src/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import { Signal, ISignal } from '@lumino/signaling';
import { Contents, ServerConnection } from '@jupyterlab/services';
import { DocumentRegistry } from '@jupyterlab/docregistry';
//import { PathExt } from '@jupyterlab/coreutils';

/*export const DRIVE_NAME = 'Drive1';
const DRIVE_PREFIX = `${DRIVE_NAME}:`;*/

const drive1Contents: Contents.IModel = {
name: 'Drive1',
Expand Down Expand Up @@ -94,27 +97,59 @@ const drive1Contents: Contents.IModel = {
size: 153,
writable: true,
type: 'file'
},
{
name: 'untitled.txt',
path: 'Drive1/untitled.txt',
last_modified: '2023-10-25T08:20:09.395167Z',
created: '2023-10-25T08:20:09.395167Z',
content: null,
format: null,
mimetype: 'text/plain',
size: 4772,
writable: true,
type: 'txt'
},
{
name: 'untitled1.txt',
path: 'Drive1/untitled1.txt',
last_modified: '2023-10-25T08:20:09.395167Z',
created: '2023-10-25T08:20:09.395167Z',
content: null,
format: null,
mimetype: 'text/plain',
size: 4772,
writable: true,
type: 'txt'
},
{
name: 'Untitled Folder',
path: 'Drive1/Untitled Folder',
last_modified: '2023-10-25T08:20:09.395167Z',
created: '2023-10-25T08:20:09.395167Z',
content: [],
format: null,
mimetype: '',
size: 0,
writable: true,
type: 'directory'
}
],

format: 'json',
mimetype: '',
size: undefined,
writable: true,
type: 'directory'
};

/**
* A Contents.IDrive implementation that serves as a read-only
* view onto the drive repositories.
*/

export class Drive implements Contents.IDrive {
/**
* Construct a new drive object.
*
* @param options - The options used to initialize the object.
*/
constructor(registry: DocumentRegistry) {
constructor(options: Drive.IOptions = {}) {
this._serverSettings = ServerConnection.makeSettings();
}
/**
Expand Down Expand Up @@ -258,8 +293,125 @@ export class Drive implements Contents.IDrive {
* @returns A promise which resolves with the created file content when the
* file is created.
*/
newUntitled(options: Contents.ICreateOptions = {}): Promise<Contents.IModel> {
return Promise.reject('Repository is read only');

/*async newUntitled(
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
return Promise.reject('Empty Untitled method');
}*/

async newUntitled(
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
let data: Contents.IModel = {
name: '',
path: '',
last_modified: '',
created: '',
content: null,
format: null,
mimetype: '',
size: 0,
writable: true,
type: ''
};

console.log('options:', options);
if (options.type !== undefined) {
if (options.type !== 'directory') {
const name = this.incrementName(drive1Contents, options);
console.log('name:', name);
data = {
name: name,
path: options.path + '/' + name + '.' + options.ext,
last_modified: '2023-12-06T10:37:42.089566Z',
created: '2023-12-06T10:37:42.089566Z',
content: null,
format: null,
mimetype: '',
size: 0,
writable: true,
type: options.type
};
} else {
const name = this.incrementName(drive1Contents, options);
console.log('name:', name);
data = {
name: name,
path: options.path + '/' + name,
last_modified: '2023-12-06T10:37:42.089566Z',
created: '2023-12-06T10:37:42.089566Z',
content: [],
format: 'json',
mimetype: '',
size: undefined,
writable: true,
type: options.type
};
}
} else {
console.warn('Type of new element is undefined');
}

drive1Contents.content.push(data);
console.log('drive1Contents.content:', drive1Contents.content);

Contents.validateContentsModel(data);
this._fileChanged.emit({
type: 'new',
oldValue: null,
newValue: data
});

return data;
}

incrementName(
contents: Contents.IModel,
options: Contents.ICreateOptions
): string {
const content: Array<Contents.IModel> = contents.content;
let name: string = '';
let countText = 0;
let countDir = 0;
let countNotebook = 0;

content.forEach(item => {
if (options.ext !== undefined) {
if (item.name.includes('untitled') && item.name.includes('.txt')) {
countText = countText + 1;
} else if (
item.name.includes('Untitled') &&
item.name.includes('.ipynb')
) {
countNotebook = countNotebook + 1;
}
} else if (item.name.includes('Untitled Folder')) {
countDir = countDir + 1;
}
});

if (options.ext === 'txt') {
if (countText === 0) {
name = 'untitled' + '.' + options.ext;
} else {
name = 'untitled' + countText + '.' + options.ext;
}
}
if (options.ext === 'ipynb') {
if (countNotebook === 0) {
name = 'Untitled' + '.' + options.ext;
} else {
name = 'Untitled' + countNotebook + '.' + options.ext;
}
} else if (options.type === 'directory') {
if (countDir === 0) {
name = 'Untitled Folder';
} else {
name = 'Untitled Folder ' + countDir;
}
}
return name;
}

/**
Expand All @@ -269,8 +421,27 @@ export class Drive implements Contents.IDrive {
*
* @returns A promise which resolves when the file is deleted.
*/
delete(path: string): Promise<void> {
/*delete(path: string): Promise<void> {
return Promise.reject('Repository is read only');
}*/

async delete(localPath: string): Promise<void> {
/*const url = this._getUrl(localPath);
const settings = this.serverSettings;
const init = { method: 'DELETE' };
const response = await ServerConnection.makeRequest(url, init, settings);
// TODO: update IPEP27 to specify errors more precisely, so
// that error types can be detected here with certainty.
if (response.status !== 204) {
const err = await ServerConnection.ResponseError.create(response);
throw err;
}*/

this._fileChanged.emit({
type: 'delete',
oldValue: { path: localPath },
newValue: null
});
}

/**
Expand All @@ -286,7 +457,6 @@ export class Drive implements Contents.IDrive {
rename(path: string, newPath: string): Promise<Contents.IModel> {
return Promise.reject('Repository is read only');
}

/**
* Save a file.
*
Expand Down Expand Up @@ -368,6 +538,13 @@ export class Drive implements Contents.IDrive {
return Promise.reject('Read only');
}

/*private cleanPath(path: string): string {
if (path.includes(DRIVE_PREFIX)) {
return path.replace(DRIVE_PREFIX, '');
}
return path;
}*/

private _serverSettings: ServerConnection.ISettings;
private _name: string = '';
private _provider: string = '';
Expand All @@ -378,3 +555,42 @@ export class Drive implements Contents.IDrive {
private _fileChanged = new Signal<this, Contents.IChangedArgs>(this);
private _isDisposed: boolean = false;
}

export namespace Drive {
/**
* The options used to initialize a `Drive`.
*/
export interface IOptions {
/**
* The name for the `Drive`, which is used in file
* paths to disambiguate it from other drives.
*/
name?: string;

/**
* The server settings for the server.
*/
serverSettings?: ServerConnection.ISettings;

/**
* A REST endpoint for drive requests.
* If not given, defaults to the Jupyter
* REST API given by [Jupyter Notebook API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/contents).
*/
apiEndpoint?: string;
}
}

/*namespace Private {
/**
* Normalize a file extension to be of the type `'.foo'`.
*
* Adds a leading dot if not present and converts to lower case.
*/
/*export function normalizeExtension(extension: string): string {
if (extension.length > 0 && extension.indexOf('.') !== 0) {
extension = `.${extension}`;
}
return extension;
}
}*/
Loading

0 comments on commit 0b7f689

Please sign in to comment.