Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ Write the date in place of the "Unreleased" in the case a new version is release

### Added

- Added `SQLAdapter` which can save and interact with table structured data in `sqlite` , `postgresql` and `duckdb` databases using `arrow-adbc` API calls.
- Added `SQLAdapter` which can save and interact with table structured data in
`sqlite` , `postgresql` and `duckdb` databases using `arrow-adbc` API calls.
- New parameter to `tiled.client.tree` `on_error` defaults to skipping errors
and printing the error message. To restore original strict behavior, pass
`on_error="raise"`.

### Changed

Expand Down
44 changes: 29 additions & 15 deletions tiled/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def walk(tree, nodes=None):
yield nodes


def gen_tree(tree, nodes=None, last=None):
def gen_tree(tree, nodes=None, last=None, on_error="skip"):
"A generator of lines for the tree utility"

# Normally, traversing a Tree will cause the structure clients to be
Expand All @@ -355,22 +355,35 @@ def dummy_client(*args, **kwargs):
structure_clients = collections.defaultdict(lambda: dummy_client)
structure_clients["container"] = Container
fast_tree = tree.new_variation(structure_clients=structure_clients)
if nodes is None:
last_index = len(fast_tree) - 1
for index, node in enumerate(fast_tree):
yield from gen_tree(fast_tree, [node], [index == last_index])
else:
value = fast_tree[nodes[-1]]
if hasattr(value, "items"):
yield _line(nodes, last)
last_index = len(value) - 1
for index, (k, v) in enumerate(value.items()):
yield from gen_tree(value, nodes + [k], last + [index == last_index])
try:
if nodes is None:
last_index = len(fast_tree) - 1
for index, node in enumerate(fast_tree):
yield from gen_tree(
fast_tree, [node], [index == last_index], on_error=on_error
)
else:
value = fast_tree[nodes[-1]]
if hasattr(value, "items"):
yield _line(nodes, last)
last_index = len(value) - 1
for index, (k, v) in enumerate(value.items()):
yield from gen_tree(
value,
nodes + [k],
last + [index == last_index],
on_error=on_error,
)
else:
yield _line(nodes, last)
except Exception as err:
if on_error == "skip":
yield f"SKIPPING due to error: {err}"
else:
yield _line(nodes, last)
raise


def tree(tree, max_lines=20):
def tree(tree, max_lines=20, on_error="skip"):
"""
Print a visual sketch of Tree structure akin to UNIX `tree`.

Expand All @@ -380,6 +393,7 @@ def tree(tree, max_lines=20):
max_lines: int or None, optional
By default, output is trucated at 20 lines. ``None`` means "Do not
truncate."
on_error : {"skip", "raise"}

Examples
--------
Expand All @@ -398,7 +412,7 @@ def tree(tree, max_lines=20):
if len(tree) == 0:
print("<Empty>")
return
for counter, line in enumerate(gen_tree(tree), start=1):
for counter, line in enumerate(gen_tree(tree, on_error=on_error), start=1):
if (max_lines is not None) and (counter > max_lines):
print(
f"<Output truncated at {max_lines} lines. "
Expand Down