Skip to content

Commit

Permalink
Preserve comments in tpv format
Browse files Browse the repository at this point in the history
  • Loading branch information
natefoo committed Nov 12, 2024
1 parent 264f11e commit 78f9fb2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tools:
rank: |
helpers.weighted_random_sampling(candidate_destinations)
toolshed.g2.bx.psu.edu/repos/iuc/mothur_shhh_seqs/mothur_shhh_seqs/.*:
# This is a comment
inherits: wig_to_bigWig
cores: 2
mem: 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tools:
wig_to_bigWig:
mem: 10
toolshed.g2.bx.psu.edu/repos/iuc/mothur_shhh_seqs/mothur_shhh_seqs/.*:
# This is a comment
cores: 2
mem: 20
inherits: wig_to_bigWig
Expand Down
26 changes: 19 additions & 7 deletions tpv/commands/formatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations
import logging

from ruamel.yaml.comments import CommentedMap, CommentedSeq

from tpv.core import util

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -64,14 +66,24 @@ def multi_level_dict_sorter(dict_to_sort, sort_order):
"""
if not sort_order:
return dict_to_sort
if isinstance(dict_to_sort, dict):
if isinstance(dict_to_sort, CommentedMap):
sorted_keys = sorted(dict_to_sort or [], key=TPVConfigFormatter.generic_key_sorter(sort_order.keys()))
return {key: TPVConfigFormatter.multi_level_dict_sorter(dict_to_sort.get(key),
sort_order.get(key, {}) or sort_order.get('*', {}))
for key in sorted_keys}
elif isinstance(dict_to_sort, list):
return [TPVConfigFormatter.multi_level_dict_sorter(item, sort_order.get('*', []))
for item in dict_to_sort]
rval = CommentedMap()
for key in sorted_keys:
sorted_value = TPVConfigFormatter.multi_level_dict_sorter(
dict_to_sort.get(key),
sort_order.get(key, {}) or sort_order.get('*', {})
)
rval[key] = sorted_value
rval.ca.items.update(dict_to_sort.ca.items)
return rval
elif isinstance(dict_to_sort, CommentedSeq):
rval = CommentedSeq()
for item in dict_to_sort:
sorted_item = TPVConfigFormatter.multi_level_dict_sorter(item, sort_order.get('*', []))
rval.append(sorted_item)
rval.ca.items.update(dict_to_sort.ca.items)
return rval
else:
return dict_to_sort

Expand Down

0 comments on commit 78f9fb2

Please sign in to comment.