[Python-modules-commits] [nbformat] 01/07: Import nbformat_4.4.0.orig.tar.gz

Gordon Ball chronitis-guest at moszumanska.debian.org
Mon Aug 28 21:45:47 UTC 2017


This is an automated email from the git hooks/post-receive script.

chronitis-guest pushed a commit to branch master
in repository nbformat.

commit c08c300140cb82053678bdf4aebe622cc55bf768
Author: Gordon Ball <gordon at chronitis.net>
Date:   Mon Aug 28 23:13:35 2017 +0200

    Import nbformat_4.4.0.orig.tar.gz
---
 .travis.yml                                |  1 +
 docs/changelog.rst                         | 20 ++++++++++++++++
 docs/conf.py                               |  2 +-
 docs/format_description.rst                | 17 ++++++++++++--
 nbformat/_version.py                       |  3 ++-
 nbformat/notebooknode.py                   | 37 ++++++++++++++++++++++++++----
 nbformat/sign.py                           | 24 ++++++++++---------
 nbformat/tests/test4jupyter_metadata.ipynb | 30 ++++++++++++++++++++++++
 nbformat/tests/test_validator.py           |  7 ++++++
 nbformat/v4/nbbase.py                      | 14 +++++------
 nbformat/v4/nbformat.v4.schema.json        | 33 +++++++++++++++++++++++++-
 nbformat/v4/nbjson.py                      |  2 +-
 nbformat/validator.py                      | 34 ++++++++++++++++++++-------
 package.json                               |  2 +-
 14 files changed, 188 insertions(+), 38 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index be4ef33..3f1d5c2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,7 @@
 language: python
 python:
     - nightly
+    - 3.6
     - 3.5
     - 3.4
     - 3.3
diff --git a/docs/changelog.rst b/docs/changelog.rst
index a8a9da9..6344ea1 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -4,6 +4,24 @@
 Changes in nbformat
 =========================
 
+4.4
+===
+
+`4.4 on GitHub <https://github.com/jupyter/nbformat/milestone/9>`__
+
+- Explicitly state that metadata fields can be ignored.
+- Introduce official jupyter namespace inside metadata (``metadata.jupyter``).
+- Introduce ``source_hidden`` and ``outputs_hidden`` as official front-end
+  metadata fields to indicate hiding source and outputs areas. **NB**: These
+  fields should not be used to hide elements in exported formats.
+- Fix ending the redundant storage of signatures in the signature database. 
+- :func:`nbformat.validate` can be set to not raise a ValidationError if
+  additional properties are included. 
+- Fix for errors with connecting and backing up the signature database.
+- Dict-like objects added to NotebookNode attributes are now transformed to be
+  NotebookNode objects; transformation also works for `.update()`.
+
+
 4.3
 ===
 
@@ -16,6 +34,8 @@ Changes in nbformat
   as well as unicode.
 - Fix for calling :func:`nbformat.validate` on an empty dictionary.
 - Fix for running the tests where the locale makes ASCII the default encoding.
+- Include nbformat-schema files (v3 and v4) in nbformat-schema npm package.
+- Include configuration for appveyor's continuous integration service.
 
 4.2
 ===
diff --git a/docs/conf.py b/docs/conf.py
index 630687b..3f545e4 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -64,7 +64,7 @@ numpydoc_class_members_toctree = False
 # built documents.
 #
 # The short X.Y version.
-version = '4.3'
+version = '4.4'
 # The full version, including alpha/beta/rc tags.
 release = version
 
diff --git a/docs/format_description.rst b/docs/format_description.rst
index e974ae0..8a54ff7 100644
--- a/docs/format_description.rst
+++ b/docs/format_description.rst
@@ -9,8 +9,9 @@ Some general points about the notebook format:
 .. note::
 
     *All* metadata fields are optional.
-    While the type and values of some metadata are defined,
-    no metadata values are required to be defined.
+    While the types and values of some metadata fields are defined,
+    no metadata fields are required to be defined. Any metadata field
+    may also be ignored.
 
 
 Top-level structure
@@ -377,6 +378,9 @@ Additional fields may be added.
 Cell metadata
 -------------
 
+Official Jupyter metadata, as used by Jupyter frontends should be placed in the
+`metadata.jupyter` namespace, for example `metadata.jupyter.foo = "bar"`.
+
 The following metadata keys are defined at the cell level:
 
 =========== =============== ==============
@@ -390,6 +394,15 @@ name        str             A name for the cell. Should be unique
 tags        list of str     A list of string tags on the cell. Commas are not allowed in a tag
 =========== =============== ==============
 
+The following metadata keys are defined at the cell level within the `jupyter` namespace
+
+=============== =============== ==============
+Key             Value           Interpretation
+=============== =============== ==============
+source_hidden   bool            Whether the cell's source should be shown
+outputs_hidden  bool            Whether the cell's outputs should be shown
+=============== =============== ==============
+
 Output metadata
 ---------------
 
diff --git a/nbformat/_version.py b/nbformat/_version.py
index a609314..5ff389c 100644
--- a/nbformat/_version.py
+++ b/nbformat/_version.py
@@ -1,3 +1,4 @@
 # Make sure to update package.json, too!
-version_info = (4, 3, 0)
+# version_info = (4, 5, 0, 'dev')
+version_info = (4, 4, 0)
 __version__ = '.'.join(map(str, version_info))
diff --git a/nbformat/notebooknode.py b/nbformat/notebooknode.py
index f0eaba0..5e1fded 100644
--- a/nbformat/notebooknode.py
+++ b/nbformat/notebooknode.py
@@ -1,23 +1,50 @@
 """NotebookNode - adding attribute access to dicts"""
 
 from ipython_genutils.ipstruct import Struct
+from collections import Mapping
+
 
 class NotebookNode(Struct):
     """A dict-like node with attribute-access"""
-    pass
+
+    def __setitem__(self, key, value):
+        if isinstance(value, Mapping) and not isinstance(value, NotebookNode):
+            value = from_dict(value)
+        super(NotebookNode, self).__setitem__(key, value)
+
+    def update(self, *args, **kwargs):
+        """
+        A dict-like update method based on CPython's MutableMapping `update`
+        method.
+        """
+        if len(args) > 1:
+            raise TypeError('update expected at most 1 arguments, got %d' %
+                            len(args))
+        if args:
+            other = args[0]
+            if isinstance(other, Mapping):
+                for key in other:
+                    self[key] = other[key]
+            elif hasattr(other, "keys"):
+                for key in other.keys():
+                    self[key] = other[key]
+            else:
+                for key, value in other:
+                    self[key] = value
+        for key, value in kwargs.items():
+            self[key] = value
+
 
 def from_dict(d):
     """Convert dict to dict-like NotebookNode
-    
+
     Recursively converts any dict in the container to a NotebookNode.
     This does not check that the contents of the dictionary make a valid
     notebook or part of a notebook.
     """
     if isinstance(d, dict):
-        return NotebookNode({k:from_dict(v) for k,v in d.items()})
+        return NotebookNode({k: from_dict(v) for k, v in d.items()})
     elif isinstance(d, (tuple, list)):
         return [from_dict(i) for i in d]
     else:
         return d
-
-
diff --git a/nbformat/sign.py b/nbformat/sign.py
index b08b998..0d52251 100644
--- a/nbformat/sign.py
+++ b/nbformat/sign.py
@@ -171,7 +171,7 @@ class SQLiteSignatureStore(SignatureStore, LoggingConfigurable):
                     os.rename(db_file, old_db_location)
                     db = sqlite3.connect(db_file, **kwargs)
                     self.init_db(db)
-                except (sqlite3.DatabaseError, sqlite3.OperationalError):
+                except (sqlite3.DatabaseError, sqlite3.OperationalError, OSError):
                     if db is not None:
                         db.close()
                     self.log.warning(
@@ -204,16 +204,18 @@ class SQLiteSignatureStore(SignatureStore, LoggingConfigurable):
     def store_signature(self, digest, algorithm):
         if self.db is None:
             return
-        self.db.execute("""INSERT OR IGNORE INTO nbsignatures
-            (algorithm, signature, last_seen) VALUES (?, ?, ?)""",
-                        (algorithm, digest, datetime.utcnow())
-                        )
-        self.db.execute("""UPDATE nbsignatures SET last_seen = ? WHERE
-            algorithm = ? AND
-            signature = ?;
-            """,
-                        (datetime.utcnow(), algorithm, digest),
-                        )
+        if not self.check_signature(digest, algorithm):
+            self.db.execute("""
+                INSERT INTO nbsignatures (algorithm, signature, last_seen) 
+                VALUES (?, ?, ?)
+                """, (algorithm, digest, datetime.utcnow())
+            )
+        else:
+            self.db.execute("""UPDATE nbsignatures SET last_seen = ? WHERE
+                algorithm = ? AND
+                signature = ?;
+                """, (datetime.utcnow(), algorithm, digest)
+            )
         self.db.commit()
 
         # Check size and cull old entries if necessary
diff --git a/nbformat/tests/test4jupyter_metadata.ipynb b/nbformat/tests/test4jupyter_metadata.ipynb
new file mode 100644
index 0000000..68bf398
--- /dev/null
+++ b/nbformat/tests/test4jupyter_metadata.ipynb
@@ -0,0 +1,30 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {
+    "collapsed": false,
+    "jupyter": {
+      "outputs_hidden": false,
+      "source_hidden": false
+    }
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "hello\n"
+     ]
+    }
+   ],
+   "source": [
+    "print(\"hello\")"
+   ]
+  }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/nbformat/tests/test_validator.py b/nbformat/tests/test_validator.py
index 9732924..4c8ece6 100644
--- a/nbformat/tests/test_validator.py
+++ b/nbformat/tests/test_validator.py
@@ -48,6 +48,13 @@ class TestValidator(TestsBase):
         validate(nb)
         self.assertEqual(isvalid(nb), True)
 
+    def test_nb4jupyter_metadata(self):
+        """Test that a notebook with a jupyter metadata passes validation"""
+        with self.fopen(u'test4jupyter_metadata.ipynb', u'r') as f:
+            nb = read(f, as_version=4)
+        validate(nb)
+        self.assertEqual(isvalid(nb), True)
+
     def test_invalid(self):
         """Test than an invalid notebook does not pass validation"""
         # this notebook has a few different errors:
diff --git a/nbformat/v4/nbbase.py b/nbformat/v4/nbbase.py
index f0294ab..b64eafc 100644
--- a/nbformat/v4/nbbase.py
+++ b/nbformat/v4/nbbase.py
@@ -9,7 +9,7 @@ helpers to build the structs in the right form.
 # Copyright (c) IPython Development Team.
 # Distributed under the terms of the Modified BSD License.
 
-from ..notebooknode import from_dict, NotebookNode
+from ..notebooknode import NotebookNode
 
 # Change this when incrementing the nbformat version
 nbformat = 4
@@ -35,9 +35,9 @@ def new_output(output_type, data=None, **kwargs):
         output.metadata = NotebookNode()
         output.data = NotebookNode()
     # load from args:
-    output.update(from_dict(kwargs))
+    output.update(kwargs)
     if data is not None:
-        output.data = from_dict(data)
+        output.data = data
     # validate
     validate(output, output_type)
     return output
@@ -95,7 +95,7 @@ def new_code_cell(source='', **kwargs):
         source=source,
         outputs=[],
     )
-    cell.update(from_dict(kwargs))
+    cell.update(kwargs)
 
     validate(cell, 'code_cell')
     return cell
@@ -107,7 +107,7 @@ def new_markdown_cell(source='', **kwargs):
         source=source,
         metadata=NotebookNode(),
     )
-    cell.update(from_dict(kwargs))
+    cell.update(kwargs)
 
     validate(cell, 'markdown_cell')
     return cell
@@ -119,7 +119,7 @@ def new_raw_cell(source='', **kwargs):
         source=source,
         metadata=NotebookNode(),
     )
-    cell.update(from_dict(kwargs))
+    cell.update(kwargs)
 
     validate(cell, 'raw_cell')
     return cell
@@ -132,6 +132,6 @@ def new_notebook(**kwargs):
         metadata=NotebookNode(),
         cells=[],
     )
-    nb.update(from_dict(kwargs))
+    nb.update(kwargs)
     validate(nb)
     return nb
diff --git a/nbformat/v4/nbformat.v4.schema.json b/nbformat/v4/nbformat.v4.schema.json
index 8e34431..5c8d49a 100644
--- a/nbformat/v4/nbformat.v4.schema.json
+++ b/nbformat/v4/nbformat.v4.schema.json
@@ -126,6 +126,15 @@
                             "description": "Raw cell metadata format for nbconvert.",
                             "type": "string"
                         },
+                        "jupyter": {
+                          "description": "Official Jupyter Metadata for Raw Cells",
+                          "type": "object",
+                          "additionalProperties": true,
+                            "source_hidden": {
+                                "description": "Whether the source is hidden.",
+                                "type": "boolean"
+                            }
+                        },
                         "name": {"$ref": "#/definitions/misc/metadata_name"},
                         "tags": {"$ref": "#/definitions/misc/metadata_tags"}
                     }
@@ -150,7 +159,16 @@
                     "type": "object",
                     "properties": {
                         "name": {"$ref": "#/definitions/misc/metadata_name"},
-                        "tags": {"$ref": "#/definitions/misc/metadata_tags"}
+                        "tags": {"$ref": "#/definitions/misc/metadata_tags"},
+                        "jupyter": {
+                          "description": "Official Jupyter Metadata for Markdown Cells",
+                          "type": "object",
+                          "additionalProperties": true,
+                            "source_hidden": {
+                                "description": "Whether the source is hidden.",
+                                "type": "boolean"
+                            }
+                        }
                     },
                     "additionalProperties": true
                 },
@@ -174,6 +192,19 @@
                     "type": "object",
                     "additionalProperties": true,
                     "properties": {
+                        "jupyter": {
+                          "description": "Official Jupyter Metadata for Code Cells",
+                          "type": "object",
+                          "additionalProperties": true,
+                            "source_hidden": {
+                                "description": "Whether the source is hidden.",
+                                "type": "boolean"
+                            },
+                            "outputs_hidden": {
+                                "description": "Whether the outputs are hidden.",
+                                "type": "boolean"
+                            }
+                        },
                         "collapsed": {
                             "description": "Whether the cell is collapsed/expanded.",
                             "type": "boolean"
diff --git a/nbformat/v4/nbjson.py b/nbformat/v4/nbjson.py
index 65cc35d..27e5600 100644
--- a/nbformat/v4/nbjson.py
+++ b/nbformat/v4/nbjson.py
@@ -8,7 +8,7 @@ import json
 
 from ipython_genutils import py3compat
 
-from .nbbase import from_dict
+from ..notebooknode import from_dict
 from .rwbase import (
     NotebookReader, NotebookWriter, rejoin_lines, split_lines, strip_transient
 )
diff --git a/nbformat/validator.py b/nbformat/validator.py
index 8f6494c..5ff2e64 100644
--- a/nbformat/validator.py
+++ b/nbformat/validator.py
@@ -50,7 +50,7 @@ def _allow_undefined(schema):
     )
     return schema
 
-def get_validator(version=None, version_minor=None):
+def get_validator(version=None, version_minor=None, relax_add_props=False):
     """Load the JSON schema into a Validator"""
     if version is None:
         from .. import current_nbformat
@@ -65,13 +65,9 @@ def get_validator(version=None, version_minor=None):
 
     if version_tuple not in validators:
         try:
-            v.nbformat_schema
+            schema_json = _get_schema_json(v)
         except AttributeError:
-            # no validator
             return None
-        schema_path = os.path.join(os.path.dirname(v.__file__), v.nbformat_schema)
-        with open(schema_path) as f:
-            schema_json = json.load(f)
 
         if current_minor < version_minor:
             # notebook from the future, relax all `additionalProperties: False` requirements
@@ -80,8 +76,30 @@ def get_validator(version=None, version_minor=None):
             schema_json = _allow_undefined(schema_json)
 
         validators[version_tuple] = Validator(schema_json)
+
+    if relax_add_props:
+        try:
+            schema_json = _get_schema_json(v)
+        except AttributeError:
+            return None
+
+        # this allows properties to be added for intermediate
+        # representations while validating for all other kinds of errors
+        schema_json = _relax_additional_properties(schema_json)
+
+        validators[version_tuple] = Validator(schema_json)
     return validators[version_tuple]
 
+
+def _get_schema_json(v):
+    """
+    Gets the json schema from a given imported library a nbformat version.
+    """
+    schema_path = os.path.join(os.path.dirname(v.__file__), v.nbformat_schema)
+    with open(schema_path) as f:
+        schema_json = json.load(f)
+    return schema_json
+
 def isvalid(nbjson, ref=None, version=None, version_minor=None):
     """Checks whether the given notebook JSON conforms to the current
     notebook format schema. Returns True if the JSON is valid, and
@@ -216,7 +234,7 @@ def better_validation_error(error, version, version_minor):
     return NotebookValidationError(error, ref)
 
 
-def validate(nbjson, ref=None, version=None, version_minor=None):
+def validate(nbjson, ref=None, version=None, version_minor=None, relax_add_props=False):
     """Checks whether the given notebook JSON conforms to the current
     notebook format schema.
 
@@ -226,7 +244,7 @@ def validate(nbjson, ref=None, version=None, version_minor=None):
         from .reader import get_version
         (version, version_minor) = get_version(nbjson)
 
-    validator = get_validator(version, version_minor)
+    validator = get_validator(version, version_minor, relax_add_props=relax_add_props)
 
     if validator is None:
         # no validator
diff --git a/package.json b/package.json
index 5576530..760cd94 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "nbformat-schema",
-  "version": "4.3.0",
+  "version": "4.4.0",
   "description": "JSON schemata for Jupyter notebook formats",
   "main": "index.js",
   "files": [

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/nbformat.git



More information about the Python-modules-commits mailing list