import logging
import io
import copy
from satella.coding import for_argument


from ..json import is_leaf_node

logger = logging.getLogger(__name__)


class RendererObject(io.StringIO):

    @for_argument(None, copy.copy)
    def render_node(self, tree, prefixes):
        if isinstance(tree, list):
            for elem in tree:
                self.render_node(elem, prefixes=prefixes)
            return

        if is_leaf_node(tree):
            self.write('_'.join(prefix for prefix in prefixes if prefix != ''))
            curly_braces_used = len(tree) > 1
            if curly_braces_used:
                self.write('{')
            main_value = tree.pop('_')
            if curly_braces_used:
                labels = []
                for key, value in tree.items():
                    value = repr(value)
                    logger.warning(f'value before {value}')
                    value = value.replace('\\', '\\\\').replace('"', '\\"').replace("'", '"')
                    logger.warning(f'value after {value}')
                    labels.append('%s=%s' % (key, value))
                self.write(','.join(labels))
                self.write('}')
            self.write(' %s\n' % (repr(main_value), ))

        else:
            for k, v in tree.items():
                if k == '_' and isinstance(v, list):
                    for elem in v:
                        self.render_node(elem, prefixes=prefixes)
                    continue
                self.render_node(v, prefixes=prefixes + [k])


def json_to_prometheus(tree) -> str:
    """
    Render the JSON in the form understandable by Prometheus

    :param tree: JSON returned by the root metric (or any metric for that instance).
    :return: a string output to present to Prometheus
    """
    if not tree:
        return '\n'
    obj = RendererObject()
    obj.render_node(tree, [])
    return obj.getvalue().replace('\r\n', '\n')

@@ -816,219 +816,66 @@
 lue)
-%0A                    logger.warning(f'value before %7Bvalue%7D')%0A                    value = value.replace('%5C%5C', '%5C%5C%5C%5C').replace('%22', '%5C%5C%22').replace(%22'%22, '%22')%0A                    logger.warning(f'value after %7Bvalue%7D
+.replace('%5C%5C', '%5C%5C%5C%5C').replace('%22', '%5C%5C%22').replace(%22'%22, '%22
 ')%0A 

fix for py35