Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically add core schema URL #1238

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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'},
Expand Down
8 changes: 6 additions & 2 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/pynwb/nwb-schema
Submodule nwb-schema updated 1 files
+4 −0 core/nwb.file.yaml
21 changes: 21 additions & 0 deletions tests/unit/test_file.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -60,6 +64,23 @@ 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
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'
Expand Down