[file handler] add entity buffer
This commit is contained in:
parent
bfeec0a041
commit
a93a02d9ac
@ -38,6 +38,31 @@ class Singleton(type):
|
||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
||||
return cls._instances[cls]
|
||||
|
||||
class EntityBuffer():
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._buffer = {}
|
||||
|
||||
def put(self, entity):
|
||||
if not hasattr(entity, "id"):
|
||||
return
|
||||
id = entity.id
|
||||
if id not in self._buffer.keys():
|
||||
self._buffer[id] = entity
|
||||
|
||||
def has(self, id):
|
||||
return id in self._buffer.keys()
|
||||
|
||||
def get(self, id):
|
||||
if self.has(id):
|
||||
return self._buffer[id]
|
||||
else:
|
||||
return None
|
||||
|
||||
def clear(self):
|
||||
self._buffer.clear()
|
||||
|
||||
|
||||
class FileHandler(metaclass=Singleton):
|
||||
|
||||
@ -46,6 +71,7 @@ class FileHandler(metaclass=Singleton):
|
||||
self._filename = None
|
||||
self._nix_file = None
|
||||
self._file_requests = []
|
||||
self._entity_buffer = EntityBuffer()
|
||||
|
||||
def open(self, filename):
|
||||
"""[summary]
|
||||
@ -72,6 +98,7 @@ class FileHandler(metaclass=Singleton):
|
||||
self._nix_file.close()
|
||||
self._nix_file = None
|
||||
self._file_requests = []
|
||||
self._entity_buffer.clear()
|
||||
|
||||
def file_descriptor(self):
|
||||
|
||||
@ -95,6 +122,7 @@ class FileHandler(metaclass=Singleton):
|
||||
def get_subsections(section):
|
||||
sub_sections = []
|
||||
for s in section.sections:
|
||||
self._entity_buffer.put(s)
|
||||
sub_sections.append(ItemDescriptor(s.name, s.id, s.type, definition=s.definition, entity_type=NodeType.Section))
|
||||
return sub_sections
|
||||
|
||||
@ -110,15 +138,20 @@ class FileHandler(metaclass=Singleton):
|
||||
if root_id is None:
|
||||
sections = get_subsections(self._nix_file)
|
||||
else:
|
||||
found_section = self._nix_file.find_sections(lambda s: s.id == root_id)
|
||||
for fs in found_section:
|
||||
sections.extend(get_subsections(fs))
|
||||
properties.extend(get_properties(fs))
|
||||
fs = self._entity_buffer.get(root_id)
|
||||
if fs is None:
|
||||
found_section = self._nix_file.find_sections(lambda s: s.id == root_id)
|
||||
fs = found_section[0] if len(found_section) > 0 else None
|
||||
if fs is None:
|
||||
return sections, properties
|
||||
sections.extend(get_subsections(fs))
|
||||
properties.extend(get_properties(fs))
|
||||
return sections, properties
|
||||
|
||||
def _entity_info(self, entities, block_id, entity_type):
|
||||
infos = []
|
||||
for e in entities:
|
||||
self._entity_buffer.put(e)
|
||||
itd = ItemDescriptor(e.name, e.id, e.type, definition=e.definition, entity_type=entity_type, block_id=block_id)
|
||||
infos.append(itd)
|
||||
# TODO set the value to something meaningful for the various entity types
|
||||
@ -127,30 +160,40 @@ class FileHandler(metaclass=Singleton):
|
||||
def request_blocks(self):
|
||||
return self._entity_info(self._nix_file.blocks, None, NodeType.Block)
|
||||
|
||||
def get_block(self, id):
|
||||
b = b = self._entity_buffer.get(id)
|
||||
if not b:
|
||||
b = self._nix_file.blocks[id]
|
||||
return b
|
||||
|
||||
def request_data_arrays(self, block_id):
|
||||
return self._entity_info(self._nix_file.blocks[block_id].data_arrays, block_id, NodeType.DataArray)
|
||||
b = self.get_block(block_id)
|
||||
return self._entity_info(b.data_arrays, block_id, NodeType.DataArray)
|
||||
|
||||
def request_tags(self, block_id):
|
||||
tags = self._entity_info(self._nix_file.blocks[block_id].tags, block_id, NodeType.Tag)
|
||||
tags.extend(self._entity_info(self._nix_file.blocks[block_id].multi_tags, block_id, NodeType.MultiTag))
|
||||
b = self.get_block(block_id)
|
||||
tags = self._entity_info(b.tags, block_id, NodeType.Tag)
|
||||
tags.extend(self._entity_info(b.multi_tags, block_id, NodeType.MultiTag))
|
||||
return tags
|
||||
|
||||
def request_references(self, block_id, tag_id, is_mtag):
|
||||
b = self._nix_file.blocks[block_id]
|
||||
t = None
|
||||
if is_mtag:
|
||||
t = b.multi_tags[tag_id]
|
||||
else:
|
||||
t = b.tags[tag_id]
|
||||
b = self.get_block(block_id)
|
||||
t = self._entity_buffer.get(tag_id)
|
||||
if t is None:
|
||||
if is_mtag:
|
||||
t = b.multi_tags[tag_id]
|
||||
else:
|
||||
t = b.tags[tag_id]
|
||||
return self._entity_info(t.references, block_id, NodeType.DataArray)
|
||||
|
||||
def request_features(self, block_id, tag_id, is_mtag):
|
||||
b = self._nix_file.blocks[block_id]
|
||||
t = None
|
||||
if is_mtag:
|
||||
t = b.multi_tags[tag_id]
|
||||
else:
|
||||
t = b.tags[tag_id]
|
||||
b = self.get_block(block_id)
|
||||
t = self._entity_buffer.get(tag_id)
|
||||
if t is None:
|
||||
if is_mtag:
|
||||
t = b.multi_tags[tag_id]
|
||||
else:
|
||||
t = b.tags[tag_id]
|
||||
feats = []
|
||||
for f in t.features:
|
||||
itd = ItemDescriptor(f.data.name, f.id, f.link_type, definition=f.data.definition, block_id=block_id, entity_type=NodeType.Feature)
|
||||
@ -158,8 +201,12 @@ class FileHandler(metaclass=Singleton):
|
||||
return feats
|
||||
|
||||
def request_dimensions(self, block_id, array_id):
|
||||
da = self._entity_buffer.get(array_id)
|
||||
if da is None:
|
||||
b = self.get_block(block_id)
|
||||
da = b.data_arrays[array_id]
|
||||
dimensions = []
|
||||
for i, d in enumerate(self._nix_file.blocks[block_id].data_arrays[array_id].dimensions):
|
||||
for i, d in enumerate(da.dimensions):
|
||||
dim_name = "%i. dim: %s" % (i+1, d.label if hasattr(d, "label") else "")
|
||||
dim_type= "%s %s" % (d.dimension_type, "dimension")
|
||||
dimensions.append(ItemDescriptor(dim_name, type=dim_type, entity_type=NodeType.Dimension, block_id=block_id))
|
||||
@ -167,23 +214,25 @@ class FileHandler(metaclass=Singleton):
|
||||
|
||||
def request_data_frames(self, block_id):
|
||||
if self._nix_file.version[1] >= 2:
|
||||
return self._entity_info(self._nix_file.blocks[block_id].data_frames, block_id, NodeType.DataFrame)
|
||||
b = self.get_block(block_id)
|
||||
return self._entity_info(b.data_frames, block_id, NodeType.DataFrame)
|
||||
return []
|
||||
|
||||
def request_groups(self, block_id):
|
||||
return self._entity_info(self._nix_file.blocks[block_id].groups, block_id, NodeType.Group)
|
||||
b = self.get_block(block_id)
|
||||
return self._entity_info(b.groups, block_id, NodeType.Group)
|
||||
|
||||
def request_sources(self, block_id, parent_source_id=None):
|
||||
def get_subsources(src):
|
||||
sub_sources = []
|
||||
for s in src.sources:
|
||||
self._entity_buffer.put(s)
|
||||
sub_sources.append(ItemDescriptor(s.name, s.id, s.type, definition=s.definition, entity_type=NodeType.Source))
|
||||
return sub_sources
|
||||
|
||||
b = self.get_block(block_id)
|
||||
if parent_source_id is None:
|
||||
return self._entity_info(self._nix_file.blocks[block_id].sources, block_id, NodeType.Source)
|
||||
return self._entity_info(b.sources, block_id, NodeType.Source)
|
||||
else:
|
||||
b = self._nix_file.blocks[block_id]
|
||||
srcs = b.find_sources(lambda s: s.id == parent_source_id)
|
||||
sources = []
|
||||
for src in srcs:
|
||||
|
Loading…
Reference in New Issue
Block a user