Просмотр исходного кода

Update Pickle format prototype

Lutz Roeder 5 лет назад
Родитель
Сommit
ac074ec6b6
3 измененных файлов с 75 добавлено и 5 удалено
  1. 68 5
      source/pickle.js
  2. 4 0
      test/models.js
  3. 3 0
      test/models.json

+ 68 - 5
source/pickle.js

@@ -16,23 +16,36 @@ pickle.ModelFactory = class {
             return false;
         }
         const tags = context.tags('pkl');
-        if (tags.size === 1 || tags.keys().next().value === '') {
+        if (tags.size === 1) {
             return true;
         }
         return false;
     }
 
-    open(/* context */) {
+    open(context) {
         return new Promise((resolve) => {
-            resolve(new pickle.Model());
+            const value = context.tags('pkl').values().next().value;
+            if (value && value.__module__ && value.__name__) {
+                context.exception(new pickle.Error("Unknown Pickle type '" + value.__module__ + "." + value.__name__ + "'."));
+            }
+            else if (Array.isArray(value)) {
+                context.exception(new pickle.Error('Unknown Pickle array object.'));
+            }
+            else if (value === null || value === undefined) {
+                context.exception(new pickle.Error('Unknown Pickle null object.'));
+            }
+            else {
+                context.exception(new pickle.Error('Unknown Pickle object.'));
+            }
+            resolve(new pickle.Model(value));
         });
     }
 };
 
 pickle.Model = class {
 
-    constructor() {
-        this._graphs = [];
+    constructor(value) {
+        this._graphs = [ new pickle.Graph(value) ];
     }
 
     get format() {
@@ -44,6 +57,56 @@ pickle.Model = class {
     }
 };
 
+pickle.Graph = class {
+
+    constructor(/* value */) {
+        this._inputs = [];
+        this._outputs = [];
+        this._nodes = [ new pickle.Node() ];
+    }
+
+    get inputs() {
+        return this._inputs;
+    }
+
+    get outputs() {
+        return this._outputs;
+    }
+
+    get nodes() {
+        return this._nodes;
+    }
+};
+
+pickle.Node = class {
+
+    constructor(/* value */) {
+        this._inputs = [];
+        this._outputs = [];
+        this._attributes = [];
+    }
+
+    get type() {
+        return '?';
+    }
+
+    get name() {
+        return '';
+    }
+
+    get inputs() {
+        return this._inputs;
+    }
+
+    get outputs() {
+        return this._outputs;
+    }
+
+    get attributes() {
+        return this._attributes;
+    }
+};
+
 
 pickle.Error = class extends Error {
 

+ 4 - 0
test/models.js

@@ -213,6 +213,10 @@ class TestContext {
     require(id) {
         return this._host.require(id);
     }
+
+    exception(error, fatal) {
+        this._host.exception(error, fatal);
+    }
 }
 
 class HTMLDocument {

+ 3 - 0
test/models.json

@@ -4030,6 +4030,7 @@
     "target": "batches.meta",
     "source": "https://raw.githubusercontent.com/MadryLab/cifar10_challenge/master/cifar10_data/batches.meta",
     "format": "Pickle",
+    "error":  "Unknown Pickle object.",
     "link":   "https://github.com/MadryLab/cifar10_challenge"
   },
   {
@@ -4037,6 +4038,7 @@
     "target": "svm.pkl",
     "source": "https://raw.githubusercontent.com/dfridovi/imagelib/master/svm.pkl",
     "format": "Pickle",
+    "error":  "Unknown Pickle array object.",
     "link":   "https://github.com/dfridovi/imagelib/blob/master/svm.pkl"
   },
   {
@@ -4044,6 +4046,7 @@
     "target": "robinhood-portfolio_data_user.pkl.pkl",
     "source": "https://raw.githubusercontent.com/omdv/robinhood-portfolio/4622dc61e0556a85ce52c4de178d01a9838acbc5/data/user.pkl",
     "format": "Pickle",
+    "error":  "Unknown Pickle object.",
     "link":   "https://github.com/omdv/robinhood-portfolio/tree/4622dc61e0556a85ce52c4de178d01a9838acbc5"
   },
   {