Skip to content

Commit

Permalink
Add a verbose mode. When off do not print messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pradal committed Dec 3, 2024
1 parent fbb9a08 commit 7014459
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
37 changes: 22 additions & 15 deletions src/openalea/mtg/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,17 @@ def get_properties(name,vid=None, time=False):
args[k] = klass(v)
except:
if vid is not None:
if k =='_line':
continue
print('Args ', v, 'of vertex ', vid, 'of type ', k, 'is not of type ', str(klass))
else:
print('Args ', v, 'of type ', k, 'is not of type ', str(klass))
return args

def add_dynamic_properties(mtg, vid, args):
print("Existing properties at ", vid, " ", mtg.get_vertex_property(vid))
print("New property: ", args)
if mtg.verbose:
log("Existing properties at ", vid, " ", mtg.get_vertex_property(vid))
log("New property: ", args)

# a property can be a list but not a timeserie.
# Create a real timeserie object...
Expand Down Expand Up @@ -167,7 +170,7 @@ def add_dynamic_properties(mtg, vid, args):
implicit_scale = bool(symbol_at_scale)

if debug:
print(list(symbol_at_scale.keys()))
log(list(symbol_at_scale.keys()))

mtg = mtg if mtg else MTG()

Expand Down Expand Up @@ -234,7 +237,7 @@ def add_dynamic_properties(mtg, vid, args):
scale = mtg.scale(vid)
elif tag == '*':
args = get_properties(name, vid=vid, time=True)
print(vid, '*(', args, ')')
log(vid, '*(', args, ')')
# CPL Manage Dynamic_MTG
add_dynamic_properties(mtg, vid, args)
else:
Expand All @@ -252,7 +255,7 @@ def add_dynamic_properties(mtg, vid, args):
try:
new_scale = symbol_at_scale[symbol_class]
except:
print('NODE ',node, bool(tag=='*'))
print('NODE ',symbol_class, node, tag, bool(tag=='*'))
if tag == '/' and new_scale <= scale:
new_scale -= 1
pending_edge = '/'
Expand Down Expand Up @@ -457,7 +460,7 @@ def transform(turtle, mesh):
current_vertex = vid
pending_edge = ''

log('','Cas 1.1', scale,
log('','Case 1.1', scale,
'mtg.scale(vid)', mtg.scale(vid),
'generated vertex', vid)

Expand All @@ -467,7 +470,7 @@ def transform(turtle, mesh):
current_vertex = mtg.add_child(current_vertex,
edge_type=edge_type,
label=name)
log('', 'Cas 1.2', scale,
log('', 'Case 1.2', scale,
'mtg.scale(vid)', mtg.scale(vid),
'generated vertex', current_vertex)
assert mtg.scale(current_vertex) == module_scale
Expand All @@ -481,7 +484,7 @@ def transform(turtle, mesh):
assert vid == current_vertex
vid = mtg.add_component(vid)
current_vertex = vid
log('', '', 'Cas 2.1', scale, 'generate new component', current_vertex)
log('', '', 'Case 2.1', scale, 'generate new component', current_vertex)
scale += 1
if module_scale == scale:
assert mtg.scale(current_vertex) == module_scale
Expand Down Expand Up @@ -893,7 +896,7 @@ class Reader(object):
The code contains topology relations and properties.
"""

def __init__(self, string, has_line_as_param=True, mtg=None, has_date=False):
def __init__(self, string, has_line_as_param=True, mtg=None, has_date=False, verbose=True):
self.mtg = mtg

# First implementation.
Expand All @@ -912,14 +915,17 @@ def __init__(self, string, has_line_as_param=True, mtg=None, has_date=False):
self._no_line = 0
self.warnings = []
self.has_line_as_param = has_line_as_param
self.verbose = verbose

def parse(self):
"""
""" Read the header and parse the code.
"""
self.header()
self.code()

self.errors()
if self.verbose:
self.errors()

return self.mtg

def header(self):
Expand Down Expand Up @@ -1120,6 +1126,7 @@ def errors(self):
print(warning)
else:
print(id, " ", warning)

############################################################################
### Parsing of the MTG code
### That's the real stuff...
Expand Down Expand Up @@ -1270,7 +1277,7 @@ def build_mtg(self):
self.mtg = multiscale_edit(self._new_code, self._symbols, self._features, self.has_date, mtg=self.mtg)
#self.mtg = multiscale_edit(self._new_code, {}, self._features)

def read_mtg(s, mtg=None, has_date=False):
def read_mtg(s, mtg=None, has_date=False, verbose=True):
""" Create an MTG from its string representation in the MTG format.
:Parameter:
Expand All @@ -1290,11 +1297,11 @@ def read_mtg(s, mtg=None, has_date=False):
.. seealso:: :func:`read_mtg_file`.
"""
reader = Reader(s, mtg=mtg, has_date=has_date)
reader = Reader(s, mtg=mtg, has_date=has_date, verbose=verbose)
g = reader.parse()
return g

def read_mtg_file(fn, mtg=None, has_date=False):
def read_mtg_file(fn, mtg=None, has_date=False, verbose=True):
""" Create an MTG from a filename.
:Usage:
Expand All @@ -1306,7 +1313,7 @@ def read_mtg_file(fn, mtg=None, has_date=False):
f = open(fn)
txt = f.read()
f.close()
return read_mtg(txt, mtg=mtg, has_date=has_date)
return read_mtg(txt, mtg=mtg, has_date=has_date, verbose=verbose)


def mtg_display(g, vtx_id, tab=' ', edge_type=None, label=None):
Expand Down
14 changes: 10 additions & 4 deletions src/openalea/mtg/mtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MTG(PropertyTree):
'''

def __init__(self, filename='', has_date=False):
def __init__(self, filename='', has_date=False, verbose=False):
''' Create a new MTG object.
:Usage:
Expand All @@ -78,6 +78,8 @@ def __init__(self, filename='', has_date=False):

super(MTG, self).__init__()

self.verbose = verbose

# Map a vid to its scale
self._scale = {0:0}

Expand All @@ -93,7 +95,9 @@ def __init__(self, filename='', has_date=False):

if filename:
from .io import read_mtg_file
self = read_mtg_file(filename, mtg=self, has_date=has_date)
self = read_mtg_file(filename, mtg=self, has_date=has_date, verbose=verbose)



def __getitem__(self, vtx_id):
"""A simple getitem to extract relevant information on a vertex.
Expand Down Expand Up @@ -2389,7 +2393,8 @@ def _compute_missing_edges(mtg, scale, edge_type_property=None):
for vid in roots:
components = mtg._components.get(vid)
if components is None:
print('ERROR: Missing component for vertex %d'%vid)
if mtg.verbose:
print('ERROR: Missing component for vertex %d'%vid)
continue
#assert len(components) == 1
cid = components[0]
Expand All @@ -2398,7 +2403,8 @@ def _compute_missing_edges(mtg, scale, edge_type_property=None):
parent_id = mtg.complex(mtg.parent(cid))
if parent_id is None:
#roots.append(vid)
print('ERROR: Missing parent for vertex %d'%cid)
if mtg.verbose:
print('ERROR: Missing parent for vertex %d'%cid)
continue
if edge_type_property:
edge_type = edge_type_property.get(cid)
Expand Down

0 comments on commit 7014459

Please sign in to comment.