示例#1
0
 def start_library_block(self):
     assert self.currentblock is None
     self.currentblock = LibraryBuilder(library_manager=self)
示例#2
0
class LibraryManager(object):

    _stdlib_cache = False
    _stdlib_cache_loading = False

    # Find the location of the standard library, relative to this directory
    _stdlibdir = os.path.join(os.path.dirname(__file__), '../stdlib/')

    def accept_visitor(self, v, **kwargs):
        return v.VisitLibraryManager(self, **kwargs)

    def __init__(self,
                 backend,
                 working_dir=None,
                 options=None,
                 name=None,
                 src_text=None,
                 is_stdlib_cache=False):
        #assert src_text is None
        from neurounits.neurounitparser import NeuroUnitParserOptions
        self.options = options or NeuroUnitParserOptions()

        # This is only used to make summarising easier:

        self.name = name
        self.src_text = src_text

        self.currentblock = None
        self.backend = backend

        self.libraries = []
        self.eqnsets = []

        # self.working_dir = working_dir or "/tmp/mf_neurounits/"
        # EnsureExisits(self.working_dir)

        if is_stdlib_cache:
            # Load in the standard libraries:
            from neurounits.unit_expr_parsing.units_expr_yacc import parse_expr, ParseTypes
            LibraryManager._stdlib_cache_loading = True
            for f in glob.glob(self._stdlibdir + "/*.eqn"):
                with open(f) as l:
                    print 'Loading StdLib file:', f
                    parse_expr(l.read(),
                               parse_type=ParseTypes.L6_TextBlock,
                               library_manager=self)

            LibraryManager._stdlib_cache_loading = False
            LibraryManager._stdlib_cache = self

        # Ensure the cache is setup:
        if not LibraryManager._stdlib_cache and not is_stdlib_cache:
            LibraryManager(backend=backend, is_stdlib_cache=True)

    def get(self, name, include_stdlibs=True):

        if LibraryManager._stdlib_cache_loading:
            include_stdlibs = False

        if include_stdlibs:
            srcs = chain(self.eqnsets, self.libraries,
                         self._stdlib_cache.libraries)
        else:
            srcs = chain(self.eqnsets, self.libraries)
        return SeqUtils.expect_single([l for l in srcs if l.name == name])

    def get_library(self, libname):

        # print 'Searching for library: ' % libname

        lib = SeqUtils.expect_single([
            l for l in chain(self.libraries, self._stdlib_cache.libraries)
            if l.name == libname
        ])
        return lib

    def get_eqnset(self, libname):
        eqnset = SeqUtils.expect_single(
            [l for l in self.eqnsets if l.name == libname])
        return eqnset

    def get_eqnset_names(self):
        names = [l.name for l in self.eqnsets]
        return names

    def get_library_names(self, include_stdlibs=True):
        return [l.name for l in self.libraries]

    def start_eqnset_block(self):
        assert self.currentblock is None
        self.currentblock = EqnSetBuilder(library_manager=self)

    def end_eqnset_block(self):
        self.currentblock.finalise()
        self.eqnsets.append(self.currentblock._astobject)
        self.currentblock = None
        self.get_eqnset_names()

    def start_library_block(self):
        assert self.currentblock is None
        self.currentblock = LibraryBuilder(library_manager=self)

    def end_library_block(self):
        self.currentblock.finalise()

        # print 'Name',self.currentblock._astobject.name

        assert not self.currentblock._astobject.name \
            in self.get_library_names()

        # if self.load_into_stdlibs:
        #    self.stdlibraries.append( self.currentblock._astobject )
        # else:

        self.libraries.append(self.currentblock._astobject)
        self.currentblock = None

    def get_current_block_builder(self):
        return self.currentblock

    def summary(self, details=True):
        name = self.name if self.name else ''
        simple = '<LibraryManager: %s EqnSets:%d Libraries:%d>' % (
            name, len(self.eqnsets), len(self.libraries))

        return simple
示例#3
0
 def start_eqnset_block(self):
     assert self.currentblock is None
     self.currentblock = EqnSetBuilder(library_manager=self)
示例#4
0
class LibraryManager(object):

    _stdlib_cache = False
    _stdlib_cache_loading = False

    # Find the location of the standard library, relative to this directory
    _stdlibdir = os.path.join(os.path.dirname(__file__), "../stdlib/")

    def accept_visitor(self, v, **kwargs):
        return v.VisitLibraryManager(self, **kwargs)

    def __init__(self, backend, working_dir=None, options=None, name=None, src_text=None, is_stdlib_cache=False):
        # assert src_text is None
        from neurounits.neurounitparser import NeuroUnitParserOptions

        self.options = options or NeuroUnitParserOptions()

        # This is only used to make summarising easier:

        self.name = name
        self.src_text = src_text

        self.currentblock = None
        self.backend = backend

        self.libraries = []
        self.eqnsets = []

        # self.working_dir = working_dir or "/tmp/mf_neurounits/"
        # EnsureExisits(self.working_dir)

        if is_stdlib_cache:
            # Load in the standard libraries:
            from neurounits.unit_expr_parsing.units_expr_yacc import parse_expr, ParseTypes

            LibraryManager._stdlib_cache_loading = True
            for f in glob.glob(self._stdlibdir + "/*.eqn"):
                with open(f) as l:
                    parse_expr(l.read(), parse_type=ParseTypes.L6_TextBlock, library_manager=self)

            LibraryManager._stdlib_cache_loading = False
            LibraryManager._stdlib_cache = self

        # Ensure the cache is setup:
        if not LibraryManager._stdlib_cache and not is_stdlib_cache:
            LibraryManager(backend=backend, is_stdlib_cache=True)

    def get(self, name, include_stdlibs=True):

        if LibraryManager._stdlib_cache_loading:
            include_stdlibs = False

        if include_stdlibs:
            srcs = chain(self.eqnsets, self.libraries, self._stdlib_cache.libraries)
        else:
            srcs = chain(self.eqnsets, self.libraries)
        return SeqUtils.expect_single([l for l in srcs if l.name == name])

    def get_library(self, libname):

        # print 'Searching for library: ' % libname

        lib = SeqUtils.expect_single(
            [l for l in chain(self.libraries, self._stdlib_cache.libraries) if l.name == libname]
        )
        return lib

    def get_eqnset(self, libname):
        eqnset = SeqUtils.expect_single([l for l in self.eqnsets if l.name == libname])
        return eqnset

    def get_eqnset_names(self):
        names = [l.name for l in self.eqnsets]
        return names

    def get_library_names(self, include_stdlibs=True):
        return [l.name for l in self.libraries]

    def start_eqnset_block(self):
        assert self.currentblock is None
        self.currentblock = EqnSetBuilder(library_manager=self)

    def end_eqnset_block(self):
        self.currentblock.finalise()
        self.eqnsets.append(self.currentblock._astobject)
        self.currentblock = None
        self.get_eqnset_names()

    def start_library_block(self):
        assert self.currentblock is None
        self.currentblock = LibraryBuilder(library_manager=self)

    def end_library_block(self):
        self.currentblock.finalise()

        # print 'Name',self.currentblock._astobject.name

        assert not self.currentblock._astobject.name in self.get_library_names()

        # if self.load_into_stdlibs:
        #    self.stdlibraries.append( self.currentblock._astobject )
        # else:

        self.libraries.append(self.currentblock._astobject)
        self.currentblock = None

    def get_current_block_builder(self):
        return self.currentblock
示例#5
0
 def start_library_block(self):
     assert self.currentblock is None
     self.currentblock = LibraryBuilder(library_manager=self)
示例#6
0
 def start_eqnset_block(self):
     assert self.currentblock is None
     self.currentblock = EqnSetBuilder(library_manager=self)
示例#7
0
 def start_library_block(self, name):
     self.open_block(LibraryBuilder(library_manager=self, name=name))