Skip to content

Instantly share code, notes, and snippets.

Created May 23, 2017 17:48
Show Gist options
  • Save anonymous/9c4bfe12471ff1f35ec0d6a7ef702475 to your computer and use it in GitHub Desktop.
Save anonymous/9c4bfe12471ff1f35ec0d6a7ef702475 to your computer and use it in GitHub Desktop.
import {
Contents
} from '@jupyterlab/services';
import {
IDatArchive,
IDATTree,
DATContentModel
} from './dat';
declare var DatArchive: IDatArchive;
import * as validate from '@jupyterlab/services/lib/contents/validate';
const _dat: IDatArchive = new (DatArchive as any)(window.location.origin);
async function aget(path?: string, options?: Contents.IFetchOptions
): Promise<Contents.IModel> {
let stat = await _dat.stat(path);
let tree: IDATTree;
let content: any;
if (stat.type === 'directory') {
tree = await _dat.listFiles(path);
} else if (options && options.content) {
content = await _dat.readFile(path);
}
let contents = new DATContentModel(stat, tree, content);
return contents;
}
async function alistCheckpoints(path: string
): Promise<Contents.ICheckpointModel[]> {
let history = await _dat.listHistory();
let checkpoints = history.filter((op) => {
return op.name === path;
}).map((op) => {
return {
id: `${op.mtime}`,
last_modified: op.mtime ? new Date(op.mtime).toISOString() : null
};
});
console.log(checkpoints);
return checkpoints;
}
async function anewUntitled(options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
let path = `${options.path}Untitled`;
if (options.type === 'directory') {
await _dat.createDirectory(path);
} else {
await _dat.writeFile(path, '');
}
return await aget(path);
}
async function arename(path: string,
newPath: string
): Promise<Contents.IModel> {
// console.debug('//FIXME: implement rename?');
return await aget(path);
}
async function asave(manager: Contents.IManager,
path: string,
options: Contents.IModel = {}
): Promise<Contents.IModel> {
try {
await _dat.writeFile(path, options.content);
} catch (err) {
console.warn(err);
return null;
}
const content = await aget(path);
try {
validate.validateContentsModel(content);
} catch (err) {
console.warn(err);
return null;
}
(manager as any)._fileChanged.emit({
type: 'save',
oldValue: null,
newValue: content
});
return content;
}
// Promise shims
function get(path: string, options?: Contents.IFetchOptions): Promise<Contents.IModel> {
return aget(path, options);
}
function save(path: string, options: Contents.IModel = {}): Promise<Contents.IModel> {
/* tslint:disable */
let manager = this;
/* tslint:enable */
return asave(manager, path, options);
}
function rename(path: string, newPath: string): Promise<Contents.IModel> {
return arename(path, newPath);
}
function newUntitled(options: Contents.ICreateOptions = {}): Promise<Contents.IModel> {
return anewUntitled(options);
}
function listCheckpoints(path: string): Promise<Contents.ICheckpointModel[]> {
return alistCheckpoints(path);
}
// hack a ContentsManager to speak DAT
export function install(contents: Contents.IManager) {
contents.get = get.bind(contents);
contents.listCheckpoints = listCheckpoints.bind(contents);
contents.newUntitled = newUntitled.bind(contents);
contents.save = save.bind(contents);
contents.rename = rename.bind(contents);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment