Skip to content

Commit

Permalink
io.mpfile: make_general_section and with_comments option
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Jul 14, 2015
1 parent 17d9e9c commit 5108c63
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions mpcontribs/io/mpfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,21 @@ def apply_general_section(self):
for k in self.document:
self.document[k].rec_update({general_title: general_data})

def get_string(self):
def make_general_section(self):
"""if possible, make general level-0 section from general subsections of
all other level-1 sections"""
# FIXME: how about general sections overridden locally?
if mp_level01_titles[0] in self.document: return # don't overwrite if exists
if all([
bool(mp_level01_titles[0] in self.document[mp_cat_id].keys())
for mp_cat_id in self.document
]):
for idx, mp_cat_id in enumerate(self.document.keys()):
general_section = self.document[mp_cat_id].pop(mp_level01_titles[0])
if not idx:
self.document[mp_level01_titles[0]] = general_section

def get_string(self, with_comments=False):
"""Returns a string to be written as a file"""
lines = []
min_indentor = get_indentor()
Expand All @@ -97,17 +111,18 @@ def get_string(self):
if lines and key == min_indentor:
lines.append('')
lines.append(make_pair(key, value, sep=sep))
for idx_str, comment in self.comments.iteritems():
if idx_str[-1] == '*':
lines.insert(int(idx_str[:-1]), '#'+comment)
else:
idx = int(idx_str)
line = lines[idx]
table_start = ' '.join([get_indentor(1), 'data_'])
if table_start in line:
table_name = line[len(table_start):]
line = ' '.join([get_indentor(1), table_name])
lines[idx] = ' #'.join([line, comment])
if with_comments:
for idx_str, comment in self.comments.iteritems():
if idx_str[-1] == '*':
lines.insert(int(idx_str[:-1]), '#'+comment)
else:
idx = int(idx_str)
line = lines[idx]
table_start = ' '.join([get_indentor(1), 'data_'])
if table_start in line:
table_name = line[len(table_start):]
line = ' '.join([get_indentor(1), table_name])
lines[idx] = ' #'.join([line, comment])
return '\n'.join(lines).decode('utf-8')

def __repr__(self):
Expand Down

0 comments on commit 5108c63

Please sign in to comment.