Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import shlex 

2 

3 

4def prefix(data) -> str: 

5 if isinstance(data, str): 

6 if data in ("true", "false", "") or data[0].isdigit() or data.startswith("-"): 

7 return "-s " 

8 return "" 

9 

10 

11def convert(data, pre="", post="") -> str: 

12 if data is None: 

13 return "null" 

14 elif isinstance(data, bool): 

15 if data: 

16 return "true" 

17 else: 

18 return "false" 

19 elif isinstance(data, (int, float)): 

20 return str(data) 

21 elif isinstance(data, (list, tuple)): 

22 return pre + "-a -- " + " ".join( 

23 ["{}{}".format(prefix(x), convert(x, pre="\"$(jo ", post=")\"")) 

24 for x in data]) + post 

25 elif isinstance(data, str): 

26 if data.startswith("@"): 

27 data = "\\"+data 

28 return shlex.quote(data) 

29 elif isinstance(data, dict): 

30 if len(data) == 0: 

31 return '{}' 

32 res = pre + " ".join([ 

33 "{}{}={}".format(prefix(v), convert(k), convert(v, pre="\"$(jo ", post=")\"")) 

34 for k, v in data.items()]) + post 

35 if res.startswith("-"): 

36 return "-- "+res 

37 return res 

38 else: 

39 raise ValueError("unsupported type: {}".format(type(data)))