Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/schema_salad/utils.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:32:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:d30785e31577 | 1:56ad4e20f292 |
|---|---|
| 1 from __future__ import absolute_import | |
| 2 | |
| 3 import json | |
| 4 import os | |
| 5 from typing import IO, Any, Dict, Mapping, MutableSequence | |
| 6 | |
| 7 import six | |
| 8 | |
| 9 # move to a regular typing import when Python 3.3-3.6 is no longer supported | |
| 10 | |
| 11 | |
| 12 def add_dictlist(di, key, val): # type: (Dict[Any, Any], Any, Any) -> None | |
| 13 if key not in di: | |
| 14 di[key] = [] | |
| 15 di[key].append(val) | |
| 16 | |
| 17 | |
| 18 def aslist(l): # type: (Any) -> MutableSequence[Any] | |
| 19 """ | |
| 20 Convenience function to wrap single items and lists. | |
| 21 | |
| 22 Return lists unchanged. | |
| 23 """ | |
| 24 | |
| 25 if isinstance(l, MutableSequence): | |
| 26 return l | |
| 27 else: | |
| 28 return [l] | |
| 29 | |
| 30 | |
| 31 # http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html | |
| 32 | |
| 33 | |
| 34 def flatten(l, ltypes=(list, tuple)): | |
| 35 # type: (Any, Any) -> Any | |
| 36 if l is None: | |
| 37 return [] | |
| 38 if not isinstance(l, ltypes): | |
| 39 return [l] | |
| 40 | |
| 41 ltype = type(l) | |
| 42 lst = list(l) | |
| 43 i = 0 | |
| 44 while i < len(lst): | |
| 45 while isinstance(lst[i], ltypes): | |
| 46 if not lst[i]: | |
| 47 lst.pop(i) | |
| 48 i -= 1 | |
| 49 break | |
| 50 else: | |
| 51 lst[i : i + 1] = lst[i] | |
| 52 i += 1 | |
| 53 return ltype(lst) | |
| 54 | |
| 55 | |
| 56 # Check if we are on windows OS | |
| 57 def onWindows(): | |
| 58 # type: () -> (bool) | |
| 59 return os.name == "nt" | |
| 60 | |
| 61 | |
| 62 def convert_to_dict(j4): # type: (Any) -> Any | |
| 63 if isinstance(j4, Mapping): | |
| 64 return {k: convert_to_dict(v) for k, v in j4.items()} | |
| 65 elif isinstance(j4, MutableSequence): | |
| 66 return [convert_to_dict(v) for v in j4] | |
| 67 else: | |
| 68 return j4 | |
| 69 | |
| 70 | |
| 71 def json_dump( | |
| 72 obj, # type: Any | |
| 73 fp, # type: IO[str] | |
| 74 **kwargs # type: Any | |
| 75 ): # type: (...) -> None | |
| 76 """ Force use of unicode. """ | |
| 77 if six.PY2: | |
| 78 kwargs["encoding"] = "utf-8" | |
| 79 json.dump(convert_to_dict(obj), fp, **kwargs) | |
| 80 | |
| 81 | |
| 82 def json_dumps( | |
| 83 obj, # type: Any | |
| 84 **kwargs # type: Any | |
| 85 ): # type: (...) -> str | |
| 86 """ Force use of unicode. """ | |
| 87 if six.PY2: | |
| 88 kwargs["encoding"] = "utf-8" | |
| 89 return json.dumps(convert_to_dict(obj), **kwargs) |
