Skip to content

Commit

Permalink
update BASE: check for unique subkeys; new append_to_block routine
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Aug 22, 2019
1 parent 94ec5c5 commit efc9aac
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions ogs5py/fileclasses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,32 @@ def get_block_no(self):
"""Get the number of blocks in the file."""
return len(self.mainkw)

def get_multi_keys(self, index=None):
"""State if a block has a unique set of sub keywords."""
index = len(self.mainkw) - 1 if index is None else int(index)
if -len(self.mainkw) <= index < len(self.mainkw):
sub_keys = self.subkw[index]
else:
print(
"ogs5py "
+ self.get_file_type()
+ ": get_multi_keys index out of bounds - "
+ str(index)
)
return {}
result = {}
for key in sub_keys:
if not key:
continue
count = sub_keys.count(key)
if count > 1 and key not in result:
result[key] = count
return result

def is_block_unique(self, index=None):
"""State if a block has a unique set of sub keywords."""
return not bool(self.get_multi_keys(index))

def get_block(self, index=None, as_dict=True):
"""
Get a Block from the actual file.
Expand All @@ -433,8 +459,7 @@ def get_block(self, index=None, as_dict=True):
Default: True
"""
# set the main keyword index
if index is None:
index = len(self.mainkw) - 1
index = len(self.mainkw) - 1 if index is None else int(index)

if -len(self.mainkw) <= index < len(self.mainkw):
main_key = self.mainkw[index]
Expand All @@ -451,11 +476,18 @@ def get_block(self, index=None, as_dict=True):
return {}
return None, [], []

if as_dict:
if as_dict and self.is_block_unique(index):
out = {"main_key": main_key}
for sub, con in zip(sub_key, cont):
out[sub] = con
return out
elif as_dict:
raise ValueError(
"ogs5py "
+ self.get_file_type()
+ ": get_block - block has no unique sub-keys and can not be "
+ "represented as dict."
)

return main_key, sub_key, cont

Expand Down Expand Up @@ -576,6 +608,57 @@ def add_block(self, index=None, main_key=None, **block):
self.add_sub_keyword(skey, main_index=index)
self.add_multi_content(block[skey], main_index=index)

def append_to_block(self, index=None, **block):
"""
Append data to an existing Block in the actual file.
Keywords are the sub keywords of the actual file type:
#MAIN_KEY
$SUBKEY1
content1 ...
$SUBKEY2
content2 ...
which looks like the following:
``FILE.append_to_block(SUBKEY1=content1, SUBKEY2=content2)``
Parameters
----------
index : int or None, optional
Positional index, where to insert the given Block.
As default, it will be added at the end. Default: None.
**block : keyword dict
here the dict-keywords are the ogs-subkeywords and the value is
the content that should be added with this ogs-subkeyword
If a block should contain content directly connected to a main
keyword, use this main keyword as input-keyword and the content as
value: ``SUBKEY=content``
"""
if not self.mainkw:
raise ValueError("append_to_block: No block present")
index = len(self.mainkw) - 1 if index is None else int(index)
if index >= len(self.mainkw) or index < -len(self.mainkw):
raise ValueError("append_to_block: index out of range")
# workaround for main keywords with directly connected content
main_block = set(block) & set(self.MKEYS)
if main_block:
raise ValueError(
"append_to_block: Use add_block with: " + str(main_block)
)
# get the index of the main keyword
mindex = self.MKEYS.index(self.mainkw[index])
# iterate over sub keywords to prevent order
# since the kwargs dict doesn't prevent the order of the input
# this can lead to errors, if the keywords are not added in the right
# order (for example MMP with PERMEABILITY_TENSOR and _DISTRIBUTION)
for skey in self.SKEYS[mindex]:
if skey not in block:
continue
self.add_sub_keyword(skey, main_index=index)
self.add_multi_content(block[skey], main_index=index)

def add_main_keyword(self, key, main_index=None):
"""
Add a new main keyword (#key) to the actual file.
Expand Down

0 comments on commit efc9aac

Please sign in to comment.