35
35
import sys
36
36
import re
37
37
import shlex
38
- from collections import UserDict , deque
38
+ from collections import UserDict , UserList , deque
39
39
from subprocess import PIPE , DEVNULL
40
- from typing import Optional
40
+ from typing import Optional , Sequence
41
41
42
42
import SCons .Action
43
43
import SCons .Builder
@@ -1687,17 +1687,23 @@ def Dictionary(self, *args):
1687
1687
return dlist
1688
1688
1689
1689
1690
- def Dump (self , key = None , format : str = 'pretty' ):
1691
- """ Return construction variables serialized to a string.
1690
+ def Dump (self , key : Optional [str ] = None , format : str = 'pretty' ) -> str :
1691
+ """ Returns a dump of serialized construction variables.
1692
+
1693
+ The display formats are intended for humaan readers when
1694
+ debugging - none of the supported formats produce a result that
1695
+ SCons itself can directly make use of. Objects that cannot
1696
+ directly be represented get a placeholder like
1697
+ ``<function foo at 0x123456>`` or ``<<non-serializable: function>>``.
1692
1698
1693
1699
Args:
1694
- key (optional): if None, format the whole dict of variables.
1695
- Else format the value of `key` (Default value = None)
1696
- format (str, optional): specify the format to serialize to.
1697
- `"pretty"` generates a pretty-printed string,
1698
- `"json"` a JSON-formatted string.
1699
- (Default value = `"pretty"`)
1700
+ key: if ``None``, format the whole dict of variables,
1701
+ else format just the value of *key*.
1702
+ format: specify the format to serialize to. ``"pretty"`` generates
1703
+ a pretty-printed string, ``"json"`` a JSON-formatted string.
1700
1704
1705
+ Raises:
1706
+ ValueError: *format* is not a recognized serialization format.
1701
1707
"""
1702
1708
if key :
1703
1709
cvars = self .Dictionary (key )
@@ -1707,9 +1713,9 @@ def Dump(self, key=None, format: str='pretty'):
1707
1713
fmt = format .lower ()
1708
1714
1709
1715
if fmt == 'pretty' :
1710
- import pprint
1711
- pp = pprint .PrettyPrinter (indent = 2 )
1716
+ import pprint # pylint: disable=import-outside-toplevel
1712
1717
1718
+ pp = pprint .PrettyPrinter (indent = 2 )
1713
1719
# TODO: pprint doesn't do a nice job on path-style values
1714
1720
# if the paths contain spaces (i.e. Windows), because the
1715
1721
# algorithm tries to break lines on spaces, while breaking
@@ -1718,26 +1724,33 @@ def Dump(self, key=None, format: str='pretty'):
1718
1724
return pp .pformat (cvars )
1719
1725
1720
1726
elif fmt == 'json' :
1721
- import json
1722
- def non_serializable (obj ):
1723
- return '<<non-serializable: %s>>' % type (obj ).__qualname__
1724
- return json .dumps (cvars , indent = 4 , default = non_serializable )
1727
+ import json # pylint: disable=import-outside-toplevel
1728
+
1729
+ class DumpEncoder (json .JSONEncoder ):
1730
+ """SCons special json Dump formatter."""
1731
+ def default (self , obj ):
1732
+ if isinstance (obj , (UserList , UserDict )):
1733
+ return obj .data
1734
+ return f'<<non-serializable: { type (obj ).__qualname__ } >>'
1735
+
1736
+ return json .dumps (cvars , indent = 4 , cls = DumpEncoder , sort_keys = True )
1725
1737
else :
1726
1738
raise ValueError ("Unsupported serialization format: %s." % fmt )
1727
1739
1728
1740
1729
- def FindIxes (self , paths , prefix , suffix ) :
1730
- """Search a list of paths for something that matches the prefix and suffix.
1741
+ def FindIxes (self , paths : Sequence [ str ] , prefix : str , suffix : str ) -> Optional [ str ] :
1742
+ """Search * paths* for a path that has * prefix* and * suffix* .
1731
1743
1732
- Args:
1733
- paths: the list of paths or nodes.
1734
- prefix: construction variable for the prefix.
1735
- suffix: construction variable for the suffix.
1744
+ Returns on first match.
1736
1745
1737
- Returns: the matched path or None
1746
+ Arguments:
1747
+ paths: the list of paths or nodes.
1748
+ prefix: construction variable for the prefix.
1749
+ suffix: construction variable for the suffix.
1738
1750
1751
+ Returns:
1752
+ The matched path or ``None``
1739
1753
"""
1740
-
1741
1754
suffix = self .subst ('$' + suffix )
1742
1755
prefix = self .subst ('$' + prefix )
1743
1756
0 commit comments