[bugfixes] in Metadata class (not fully tested yet)

This commit is contained in:
Marcel Paffrath 2018-07-06 12:22:24 +02:00
parent d223011f90
commit 689192b25d

View File

@ -15,14 +15,34 @@ from pylot.core.util.utils import key_for_set_value, find_in_list, \
class Metadata(object): class Metadata(object):
def __init__(self, inventory=None): def __init__(self, inventory=None):
self.inventories = [] self.inventories = []
if os.path.isdir(inventory): if inventory:
self.add_inventory(inventory) if os.path.isdir(inventory):
if os.path.isfile(inventory): self.add_inventory(inventory)
self.add_inventory_file(inventory) if os.path.isfile(inventory):
self.add_inventory_file(inventory)
# saves filenames holding metadata for a seed_id
self.seed_ids = {} self.seed_ids = {}
# saves read metadata objects (Parser/inventory) for a filename
self.inventory_files = {} self.inventory_files = {}
def __str__(self):
repr = 'PyLoT Metadata object including the following inventories:\n\n'
ntotal = len(self.inventories)
for index, inventory in enumerate(self.inventories):
if index < 2 or (ntotal - index) < 3:
repr += '{}\n'.format(inventory)
if ntotal > 4 and int(ntotal/2) == index:
repr += '...\n'
if ntotal > 4:
repr += '\nTotal of {} inventories. Use Metadata.inventories to see all.'.format(ntotal)
return repr
def __repr__(self):
return self.__str__()
def add_inventory(self, path_to_inventory): def add_inventory(self, path_to_inventory):
''' '''
add paths to list of inventories add paths to list of inventories
@ -43,12 +63,16 @@ class Metadata(object):
:return: :return:
''' '''
assert (os.path.isfile(path_to_inventory_file)), '{} is no directory'.format(path_to_inventory_file) assert (os.path.isfile(path_to_inventory_file)), '{} is no file'.format(path_to_inventory_file)
self.add_inventory(os.path.split(path_to_inventory_file)[0]) self.add_inventory(os.path.split(path_to_inventory_file)[0])
if not path_to_inventory_file in self.inventory_files.keys(): if not path_to_inventory_file in self.inventory_files.keys():
self.read_single_file(path_to_inventory_file) self.read_single_file(path_to_inventory_file)
def remove_all_inventories(self):
self.__init__()
def remove_inventory(self, path_to_inventory): def remove_inventory(self, path_to_inventory):
''' '''
remove a path from inventories list remove a path from inventories list
@ -60,6 +84,12 @@ class Metadata(object):
print('Path {} not in inventories list.'.format(path_to_inventory)) print('Path {} not in inventories list.'.format(path_to_inventory))
return return
self.inventories.remove(path_to_inventory) self.inventories.remove(path_to_inventory)
for filename in self.inventory_files.keys():
if filename.startswith(path_to_inventory):
del(self.inventory_files[filename])
for seed_id in self.seed_ids.keys():
if self.seed_ids[seed_id].startswith(path_to_inventory):
del(self.seed_ids[seed_id])
def get_metadata(self, seed_id): def get_metadata(self, seed_id):