def test_mixed2(self): self.assertEqual( string2dict('''key1="'1'" key2='"2"' key3="""'3'""" '''), { 'key3': 3, 'key2': 2, 'key1': 1 })
def test_mixed1(self): self.assertEqual(string2dict('A="B" C=1 D=1.1 E=True F=False G=None'), { 'A': 'B', 'C': 1, 'E': True, 'D': '1.1', 'G': None, 'F': False })
def macro_emit(self, node): #print(node.debug()) macro_name = node.macro_name text = node.content macro = None args = node.macro_args try: macro_kwargs = string2dict(args) except ValueError as e: exc_info = sys.exc_info() return self.error( "Wrong macro arguments: %s for macro '%s' (maybe wrong macro tag syntax?)" % ( json.dumps(args), macro_name ), exc_info ) macro_kwargs["text"] = text exc_info = None if isinstance(self.macros, dict): try: macro = self.macros[macro_name] except KeyError as e: exc_info = sys.exc_info() else: try: macro = getattr(self.macros, macro_name) except AttributeError as e: exc_info = sys.exc_info() if macro == None: return self.error( "Macro '%s' doesn't exist" % macro_name, exc_info ) try: result = macro(**macro_kwargs) except TypeError as err: msg = "Macro '%s' error: %s" % (macro_name, err) exc_info = sys.exc_info() if self.verbose > 1: if self.verbose > 2: raise # Inject more information about the macro in traceback etype, evalue, etb = exc_info import inspect try: filename = inspect.getfile(macro) except TypeError: pass else: try: sourceline = inspect.getsourcelines(macro)[0][0].strip() except IOError as err: evalue = etype("%s (error getting sourceline: %s from %s)" % (evalue, err, filename)) else: evalue = etype("%s (sourceline: %r from %s)" % (evalue, sourceline, filename)) exc_info = etype, evalue, etb return self.error(msg, exc_info) except Exception as err: return self.error( "Macro '%s' error: %s" % (macro_name, err), exc_info=sys.exc_info() ) if not isinstance(result, TEXT_TYPE): msg = "Macro '%s' doesn't return a unicode string!" % macro_name if self.verbose > 1: msg += " - returns: %r, type %r" % (result, type(result)) return self.error(msg) if node.kind == "macro_block": result += "\n" return result
def test_mixed2(self): self.assertEqual( string2dict('''key1="'1'" key2='"2"' key3="""'3'""" '''), {'key3': 3, 'key2': 2, 'key1': 1} )
def test_mixed1(self): self.assertEqual( string2dict('A="B" C=1 D=1.1 E=True F=False G=None'), {'A': 'B', 'C': 1, 'E': True, 'D': '1.1', 'G': None, 'F': False} )
def test_bool(self): self.assertEqual( string2dict('unicode=True'), {'unicode': True} )
def test_basic(self): self.assertEqual( string2dict('key1="value1" key2="value2"'), {'key2': 'value2', 'key1': 'value1'} )
def test_bool(self): self.assertEqual(string2dict('unicode=True'), {'unicode': True})
def test_basic(self): self.assertEqual(string2dict('key1="value1" key2="value2"'), { 'key2': 'value2', 'key1': 'value1' })
def macro_emit(self, node): # print(node.debug()) macro_name = node.macro_name text = node.content macro = None args = node.macro_args try: macro_kwargs = string2dict(args) except ValueError: exc_info = sys.exc_info() return self.error( f"Wrong macro arguments: {json.dumps(args)} for macro '{macro_name}' (maybe wrong macro tag syntax?)", exc_info ) macro_kwargs["text"] = text exc_info = None if isinstance(self.macros, dict): try: macro = self.macros[macro_name] except KeyError: exc_info = sys.exc_info() else: try: macro = getattr(self.macros, macro_name) except AttributeError: exc_info = sys.exc_info() if macro is None: return self.error( f"Macro '{macro_name}' doesn't exist", exc_info ) try: result = macro(**macro_kwargs) except TypeError as err: msg = f"Macro '{macro_name}' error: {err}" exc_info = sys.exc_info() if self.verbose > 1: if self.verbose > 2: raise # Inject more information about the macro in traceback etype, evalue, etb = exc_info import inspect try: filename = inspect.getfile(macro) except TypeError: pass else: try: sourceline = inspect.getsourcelines(macro)[0][0].strip() except OSError as err: evalue = etype(f"{evalue} (error getting sourceline: {err} from {filename})") else: evalue = etype(f"{evalue} (sourceline: {sourceline!r} from {filename})") exc_info = etype, evalue, etb return self.error(msg, exc_info) except Exception as err: return self.error( f"Macro '{macro_name}' error: {err}", exc_info=sys.exc_info() ) if not isinstance(result, str): msg = f"Macro '{macro_name}' doesn't return a unicode string!" if self.verbose > 1: msg += f" - returns: {result!r}, type {type(result)!r}" return self.error(msg) if node.kind == "macro_block": result += "\n" return result