From a4ac009502aea22b6120982ffba7e6f8598e98bc Mon Sep 17 00:00:00 2001 From: Andrew Tritt Date: Mon, 18 May 2020 16:23:58 -0700 Subject: [PATCH 1/2] automatically add core schema URL --- setup.py | 22 +++++++++++++++++++++- src/pynwb/__init__.py | 8 +++++++- src/pynwb/file.py | 8 ++++++-- src/pynwb/nwb-schema | 2 +- tests/unit/test_file.py | 22 ++++++++++++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 3b8bf8420..756d94e52 100755 --- a/setup.py +++ b/setup.py @@ -4,6 +4,26 @@ import versioneer +import configparser +from os.path import join as pjoin + + +def get_schema_sha(url_path): + parser = configparser.RawConfigParser() + config_path = pjoin('.git', 'modules', 'src', 'pynwb', 'nwb-schema', 'config') + parser.read(config_path) + url = parser.get('remote "origin"', 'url') + sha_path = pjoin('.git', 'modules', 'src', 'pynwb', 'nwb-schema', 'HEAD') + sha = open(sha_path, 'r').read()[:-1] + url = "%s/tree/%s" % (url[:-4], sha) + with open(url_path, 'w') as f: + print(url, file=f) + + +schema_url_path = pjoin('src', 'pynwb', 'core_schema_url') +print('writing schema URL to %s' % schema_url_path) +get_schema_sha(schema_url_path) + with open('README.rst', 'r') as fp: readme = fp.read() @@ -33,7 +53,7 @@ 'install_requires': reqs, 'packages': pkgs, 'package_dir': {'': 'src'}, - 'package_data': {'pynwb': ["%s/*.yaml" % schema_dir, "%s/*.json" % schema_dir]}, + 'package_data': {'pynwb': ["%s/*.yaml" % schema_dir, "%s/*.json" % schema_dir, schema_url_path]}, 'classifiers': [ "Programming Language :: Python", "Programming Language :: Python :: 3.5", diff --git a/src/pynwb/__init__.py b/src/pynwb/__init__.py index cddbbbdfb..ce3cb5f65 100644 --- a/src/pynwb/__init__.py +++ b/src/pynwb/__init__.py @@ -22,9 +22,10 @@ def __get_resources(): - from pkg_resources import resource_filename + from pkg_resources import resource_filename, resource_string ret = dict() ret['namespace_path'] = os.path.join(resource_filename(__name__, 'nwb-schema/core'), __core_ns_file_name) + ret['core_schema_url'] = resource_string(__name__, 'core_schema_url')[:-1].decode('utf-8') return ret @@ -114,6 +115,11 @@ def available_namespaces(): return __NS_CATALOG.namespaces +def core_schema_url(): + """Returns the Git URL of the current core schema""" + return __resources['core_schema_url'] + + # a function to register a container classes with the global map @docval({'name': 'neurodata_type', 'type': str, 'doc': 'the neurodata_type to get the spec for'}, {'name': 'namespace', 'type': str, 'doc': 'the name of the namespace'}, diff --git a/src/pynwb/file.py b/src/pynwb/file.py index 90ceaee24..db5db2b1e 100644 --- a/src/pynwb/file.py +++ b/src/pynwb/file.py @@ -9,7 +9,7 @@ from hdmf.utils import docval, getargs, call_docval_func, get_docval -from . import register_class, CORE_NAMESPACE +from . import register_class, CORE_NAMESPACE, core_schema_url from .base import TimeSeries, ProcessingModule from .device import Device from .epoch import TimeIntervals @@ -198,6 +198,7 @@ class NWBFile(MultiContainerInterface): {'name': 'subject', 'child': True, 'required_name': 'subject'}, {'name': 'sweep_table', 'child': True, 'required_name': 'sweep_table'}, {'name': 'invalid_times', 'child': True, 'required_name': 'invalid_times'}, + 'core_schema_url', 'epoch_tags',) @docval({'name': 'session_description', 'type': str, @@ -290,7 +291,9 @@ class NWBFile(MultiContainerInterface): {'name': 'scratch', 'type': (list, tuple), 'doc': 'scratch data', 'default': None}, {'name': 'icephys_electrodes', 'type': (list, tuple), - 'doc': 'IntracellularElectrodes that belong to this NWBFile.', 'default': None}) + 'doc': 'IntracellularElectrodes that belong to this NWBFile.', 'default': None}, + {'name': 'core_schema_url', 'type': str, 'default': core_schema_url(), + 'doc': 'the URL to the schema used to generate this file'}) def __init__(self, **kwargs): kwargs['name'] = 'root' call_docval_func(super(NWBFile, self).__init__, kwargs) @@ -350,6 +353,7 @@ def __init__(self, **kwargs): 'surgery', 'virus', 'stimulus_notes', + 'core_schema_url', ] for attr in fieldnames: setattr(self, attr, kwargs.get(attr, None)) diff --git a/src/pynwb/nwb-schema b/src/pynwb/nwb-schema index 7931e59ad..1941a8910 160000 --- a/src/pynwb/nwb-schema +++ b/src/pynwb/nwb-schema @@ -1 +1 @@ -Subproject commit 7931e59ad1e97433ce4a450fa5bc2dab81af6f8d +Subproject commit 1941a89100427436b7815c322f50db5b30ac9615 diff --git a/tests/unit/test_file.py b/tests/unit/test_file.py index 638e5e3b8..41e0946dc 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -1,6 +1,10 @@ import numpy as np import pandas as pd +import configparser +import os.path + + from datetime import datetime from dateutil.tz import tzlocal, tzutc @@ -60,6 +64,24 @@ def test_constructor(self): self.assertEqual(self.nwbfile.keywords, ('these', 'are', 'keywords')) self.assertEqual(self.nwbfile.timestamps_reference_time, self.ref_time) + # check GIT schema URL + breakpoint() + parser = configparser.RawConfigParser() + module_path = os.path.join(os.path.dirname(__file__), + '..', + '..', + '.git', + 'modules', + 'src', + 'pynwb', + 'nwb-schema') + parser.read(os.path.join(module_path, 'config')) + url = parser.get('remote "origin"', 'url') + sha_path = os.path.join(module_path, 'HEAD') + sha = open(sha_path, 'r').read()[:-1] + url = "%s/tree/%s" % (url[:-4], sha) + self.assertEqual(self.nwbfile.core_schema_url, url) + def test_create_electrode_group(self): name = 'example_electrode_group' desc = 'An example electrode' From 133bdaaa6bfb346b3c095984a1614c81f3b0af2d Mon Sep 17 00:00:00 2001 From: Andrew Tritt Date: Mon, 18 May 2020 16:30:11 -0700 Subject: [PATCH 2/2] remove breakpoint --- tests/unit/test_file.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_file.py b/tests/unit/test_file.py index 41e0946dc..6ba14cf2d 100644 --- a/tests/unit/test_file.py +++ b/tests/unit/test_file.py @@ -65,7 +65,6 @@ def test_constructor(self): self.assertEqual(self.nwbfile.timestamps_reference_time, self.ref_time) # check GIT schema URL - breakpoint() parser = configparser.RawConfigParser() module_path = os.path.join(os.path.dirname(__file__), '..',