Mercurial > repos > guerler > springsuite
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/planemo/lib/python3.7/site-packages/schema_salad/utils.py Fri Jul 31 00:32:28 2020 -0400 @@ -0,0 +1,89 @@ +from __future__ import absolute_import + +import json +import os +from typing import IO, Any, Dict, Mapping, MutableSequence + +import six + +# move to a regular typing import when Python 3.3-3.6 is no longer supported + + +def add_dictlist(di, key, val): # type: (Dict[Any, Any], Any, Any) -> None + if key not in di: + di[key] = [] + di[key].append(val) + + +def aslist(l): # type: (Any) -> MutableSequence[Any] + """ + Convenience function to wrap single items and lists. + + Return lists unchanged. + """ + + if isinstance(l, MutableSequence): + return l + else: + return [l] + + +# http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html + + +def flatten(l, ltypes=(list, tuple)): + # type: (Any, Any) -> Any + if l is None: + return [] + if not isinstance(l, ltypes): + return [l] + + ltype = type(l) + lst = list(l) + i = 0 + while i < len(lst): + while isinstance(lst[i], ltypes): + if not lst[i]: + lst.pop(i) + i -= 1 + break + else: + lst[i : i + 1] = lst[i] + i += 1 + return ltype(lst) + + +# Check if we are on windows OS +def onWindows(): + # type: () -> (bool) + return os.name == "nt" + + +def convert_to_dict(j4): # type: (Any) -> Any + if isinstance(j4, Mapping): + return {k: convert_to_dict(v) for k, v in j4.items()} + elif isinstance(j4, MutableSequence): + return [convert_to_dict(v) for v in j4] + else: + return j4 + + +def json_dump( + obj, # type: Any + fp, # type: IO[str] + **kwargs # type: Any +): # type: (...) -> None + """ Force use of unicode. """ + if six.PY2: + kwargs["encoding"] = "utf-8" + json.dump(convert_to_dict(obj), fp, **kwargs) + + +def json_dumps( + obj, # type: Any + **kwargs # type: Any +): # type: (...) -> str + """ Force use of unicode. """ + if six.PY2: + kwargs["encoding"] = "utf-8" + return json.dumps(convert_to_dict(obj), **kwargs)