view toolfactory/galaxyxml/ @ 35:5d38cb3d9be8 draft

added patched galaxyxml code temporarily until PR accepted
author fubar
date Sat, 08 Aug 2020 19:55:55 -0400
children ce2b1f8ea68d
line wrap: on
line source

from builtins import str
from builtins import object
from lxml import etree

class GalaxyXML(object):

    def __init__(self):
        self.root = etree.Element('root')

    def export(self):
        return etree.tostring(self.root, pretty_print=True, encoding='unicode')

class Util(object):

    def coerce(cls, data, kill_lists=False):
        """Recursive data sanitisation
        if isinstance(data, dict):
            return {k: cls.coerce(v, kill_lists=kill_lists) for k, v in
                    list(data.items()) if v is not None}
        elif isinstance(data, list):
            if kill_lists:
                return cls.coerce(data[0])
                return [cls.coerce(v, kill_lists=kill_lists) for v in data]
            return cls.coerce_value(data)

    def coerce_value(cls, obj):
        """Make everything a string!
        if isinstance(obj, bool):
            if obj:
                return "true"
                return "false"
        elif isinstance(obj, str):
            return obj
            return str(obj)

    def clean_kwargs(cls, params, final=False):
        if 'kwargs' in params:
            kwargs = params['kwargs']
            for k in kwargs:
                params[k] = kwargs[k]
            del params['kwargs']
        if 'self' in params:
            del params['self']

        if '__class__' in params:
            del params['__class__']

        # There will be more params, it would be NICE to use a whitelist
        # instead of a blacklist, but until we have more data let's just
        # blacklist stuff we see commonly.
        if final:
            for blacklist in ('positional',):
                if blacklist in params:
                    del params[blacklist]
        return params