Ver Fonte

Use string literals

Steven Silvester há 9 anos atrás
pai
commit
e87a7d74b2

+ 1 - 1
package.json

@@ -36,7 +36,7 @@
     "style-loader": "^0.13.0",
     "tsconfig-cli": "^0.1.1",
     "typedoc": "^0.3.11",
-    "typescript": "^1.7.0",
+    "typescript": "^1.8.0",
     "watch": "^0.17.1",
     "webpack": "^1.12.9"
   },

+ 1 - 2
src/notebook/notebook/model.ts

@@ -37,8 +37,7 @@ import {
 import {
   OutputAreaModel,
   DisplayDataModel, ExecuteResultModel,
-  ExecuteErrorModel, StreamModel,
-  StreamName
+  ExecuteErrorModel, StreamModel
 } from '../output-area';
 
 import {

+ 13 - 9
src/notebook/notebook/nbformat.ts

@@ -24,36 +24,40 @@ export
 const MINOR_VERSION = 0;
 
 
+export
+type OutputType = "execute_result" | "display_data" | "stream" | "error";
+
+
 export
 interface BaseOutput {
-  output_type: string;
+  output_type: OutputType;
 }
 
 export
-interface ExecuteResult extends BaseOutput {
-  output_type: string; // "execute_result"
-  execution_count: number;
-  data:  MimeBundle;
+interface DisplayData extends BaseOutput {
+  output_type: "display_data";
+  data: MimeBundle;
   metadata: {};
 }
 
 export
-interface DisplayData extends BaseOutput {
-  output_type: string; // "display_data"
+interface ExecuteResult extends BaseOutput {
+  output_type: "execute_result";
+  execution_count: number;
   data: MimeBundle;
   metadata: {};
 }
 
 export
 interface Stream extends BaseOutput {
-  output_type: string; // "stream"
+  output_type: "stream";
   name: string;
   text: multilineString;
 }
 
 export
 interface JupyterError extends BaseOutput {
-  output_type: string; // "error"
+  output_type: "error";
   ename: string;
   evalue: string;
   traceback: string[];

+ 4 - 14
src/notebook/notebook/serialize.ts

@@ -27,7 +27,7 @@ import {
   IOutputAreaModel, OutputAreaModel,
   DisplayDataModel, ExecuteResultModel,
   ExecuteErrorModel, StreamModel,
-  StreamName, OutputType, OutputModel
+  StreamType, OutputType, OutputModel
 } from '../output-area';
 
 import {
@@ -92,16 +92,7 @@ function buildOutputModel(out: Output): OutputModel {
   } 
   if (isStream(out)) {
     let outmodel = new StreamModel();
-    switch (out.name) {
-    case 'stdout':
-      outmodel.name = StreamName.StdOut;
-      break;
-    case 'stderr':
-      outmodel.name = StreamName.StdErr;
-      break;
-    default:
-      console.error('Unrecognized stream name: %s', out.name);
-    }
+    outmodel.name = out.name as StreamType;
     outmodel.text = out.text;
     return outmodel;
   } 
@@ -128,7 +119,7 @@ function buildOutputModel(out: Output): OutputModel {
 export
 function messageToModel(msg: IKernelMessage) {
   let m: Output = msg.content;
-  let type = msg.header.msg_type;
+  let type = msg.header.msg_type as OutputType;
   if (type === 'execute_result') {
     m.output_type = 'display_data';
   } else {
@@ -201,10 +192,9 @@ function getOutputData(cell: CodeCellModel): Output[] {
         metadata: output.metadata
       });
     } else if (output instanceof StreamModel) {
-      let name = output.name === StreamName.StdOut ? 'stdout' : 'stderr';
       outputs.push({
         output_type: 'stream',
-        name: name,
+        name: output.name,
         text: output.text
       });
     } else if (output instanceof DisplayDataModel) {

+ 37 - 50
src/notebook/output-area/model.ts

@@ -24,44 +24,34 @@ export interface MimeBundle {
 }
 
 
+/**
+ * The valid output type strings.
+ */
 export
-enum OutputType {
-  /**
-   * The "execute_result" message type from the message spec.
-   */
-  ExecuteResult,
-
-  /**
-   * The "display_data" message type from the message spec.
-   */
-  DisplayData,
+type OutputType = "execute_result" | "display_data" | "stream" | "error";
 
-  /**
-   * The "stream" message type from the message spec.
-   */
-  Stream,
 
-  /**
-   * The "error" message type from the message spec.
-   */
-  Error
-}
+/**
+ * The valid stream type strings.
+ */
+export 
+type StreamType = "stdout" | "stderr";
 
 
 /**
-* The base interface for an output model.
-*/
+ * The base interface for an output model.
+ */
 export
 class OutputBaseModel {
 
   /**
-  * A signal emitted when state of the output changes.
-  */
+   * A signal emitted when state of the output changes.
+   */
   stateChanged: ISignal<OutputBaseModel, IChangedArgs<any>>;
 
   /**
-  * The output type.
-  */
+   * The output type.
+   */
   outputType: OutputType;
 }
 
@@ -72,19 +62,19 @@ class OutputBaseModel {
 export
 class DisplayDataModel extends OutputBaseModel {
   /**
-  * The raw data for the output.
-  */
+   * The raw data for the output.
+   */
   data: MimeBundle;
 
   /**
-  * Metadata about the output.
-  */
+   * Metadata about the output.
+   */
   metadata: any;
 
   /**
-   * Output type
+   * The output type.
    */
-  outputType: OutputType = OutputType.DisplayData;
+  outputType: "display_data";
 }
 
 
@@ -92,33 +82,30 @@ class DisplayDataModel extends OutputBaseModel {
 * An output model for an execute result.
 */
 export
-class ExecuteResultModel extends DisplayDataModel {
+class ExecuteResultModel extends OutputBaseModel {
   /**
-  * The current execution count.
-  */
-  executionCount: number; // this is also a property on the cell?
+   * The raw data for the output.
+   */
+  data: MimeBundle;
 
   /**
-   * Output type
+   * Metadata about the output.
    */
-  outputType: OutputType = OutputType.ExecuteResult;
-}
-
+  metadata: any;
 
-export
-enum StreamName {
   /**
-   * The "stdout" stream name from the message spec.
+   * The current execution count.
    */
-  StdOut,
+  executionCount: number; // this is also a property on the cell?
 
   /**
-   * The "stderr" stream name from the message spec.
+   * The output type.
    */
-  StdErr
+  outputType: "execute_result";
 }
 
 
+
 /**
 * An output model for stream data.
 */
@@ -127,7 +114,7 @@ class StreamModel extends OutputBaseModel {
   /**
   * The type of stream.
   */
-  name: StreamName;
+  name: StreamType;
 
   /**
   * The text from the stream.
@@ -135,14 +122,14 @@ class StreamModel extends OutputBaseModel {
   text: string;
 
   /**
-   * Output type
+   * The output type.
    */
-  outputType: OutputType = OutputType.Stream;
+  outputType: "stream";
 }
 
 
 function isStreamModel(model: OutputBaseModel): model is StreamModel {
-  return model.outputType === OutputType.Stream;
+  return model.outputType === "stream";
 }
 
 
@@ -172,7 +159,7 @@ class ExecuteErrorModel extends OutputBaseModel {
   /**
    * Output type
    */
-  outputType: OutputType = OutputType.Error;
+  outputType: OutputType = "error";
 }
 
 

+ 4 - 4
src/notebook/output-area/widget.ts

@@ -102,16 +102,16 @@ class OutputAreaWidget extends Panel {
   renderItem(output: OutputModel): Promise<HTMLElement> {
     let bundle: MimeBundle;
     switch(output.outputType) {
-    case OutputType.ExecuteResult:
+    case "execute_result":
       bundle = (output as ExecuteResultModel).data;
       break;
-    case OutputType.DisplayData:
+    case "display_data":
       bundle = (output as DisplayDataModel).data;
       break;
-    case OutputType.Stream:
+    case "stream":
       bundle = {'jupyter/console-text': (output as StreamModel).text};
       break;
-    case OutputType.Error:
+    case "error":
       let out: ExecuteErrorModel = output as ExecuteErrorModel;
       bundle = {'jupyter/console-text': out.traceback || `${out.ename}: ${out.evalue}`};
       break;