def _validate(data): "Helper function to convert data to AMP-safe (picketable) values" if isinstance(data, dict): newdict = {} for key, part in data.items(): newdict[key] = _validate(part) return newdict elif hasattr(data, "__iter__"): return [_validate(part) for part in data] elif isinstance(data, str): # make sure strings are in a valid encoding try: data = data and to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) except LookupError: # wrong encoding set on the session. Set it to a safe one session.protocol_flags["ENCODING"] = "utf-8" data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler): # only parse inlinefuncs on the outgoing path (sessionhandler->) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) # At this point the object is certainly the right encoding, but may still be a unicode object-- # to_str does not actually force objects to become bytestrings. # If the unicode object is a subclass of unicode, such as ANSIString, this can cause a problem, # as special behavior for that class will still be in play. Since we're now transferring raw data, # we must now force this to be a proper bytestring. return str(data) elif hasattr(data, "id") and hasattr(data, "db_date_created") \ and hasattr(data, '__dbclass__'): # convert database-object to their string representation. return _validate(unicode(data)) else: return data
def _validate(data): "Helper function to convert data to AMP-safe (picketable) values" if isinstance(data, dict): newdict = {} for key, part in data.items(): newdict[key] = _validate(part) return newdict elif hasattr(data, "__iter__"): return [_validate(part) for part in data] elif isinstance(data, basestring): # make sure strings are in a valid encoding try: data = data and to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) except LookupError: # wrong encoding set on the session. Set it to a safe one session.protocol_flags["ENCODING"] = "utf-8" data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler): # only parse inlinefuncs on the outgoing path (sessionhandler->) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) return data elif hasattr(data, "id") and hasattr(data, "db_date_created") \ and hasattr(data, '__dbclass__'): # convert database-object to their string representation. return _validate(unicode(data)) else: return data
def _validate(data): "Helper function to convert data to AMP-safe (picketable) values" if isinstance(data, dict): newdict = {} for key, part in data.items(): newdict[key] = _validate(part) return newdict elif is_iter(data): return [_validate(part) for part in data] elif isinstance(data, (str, bytes)): data = _utf8(data) if _INLINEFUNC_ENABLED and not raw and isinstance( self, ServerSessionHandler): # only parse inlinefuncs on the outgoing path (sessionhandler->) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) return str(data) elif (hasattr(data, "id") and hasattr(data, "db_date_created") and hasattr(data, "__dbclass__")): # convert database-object to their string representation. return _validate(str(data)) else: return data
def test_escaped2(self): self.assertEqual( inlinefuncs.parse_inlinefunc( 'this should be $pad("""escaped,""" and """instead,""" cropped $crop(with a long,5) text., 80)' ), "this should be escaped, and instead, cropped with text. " )
def _validate(data): "Helper function to convert data to AMP-safe (picketable) values" if isinstance(data, dict): newdict = {} for key, part in data.items(): newdict[key] = _validate(part) return newdict elif hasattr(data, "__iter__"): return [_validate(part) for part in data] elif isinstance(data, basestring): # make sure strings are in a valid encoding try: data = data and to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) except LookupError: # wrong encoding set on the session. Set it to a safe one session.protocol_flags["ENCODING"] = "utf-8" data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) if _INLINEFUNC_ENABLED and not raw and isinstance(self, ServerSessionHandler): # only parse inlinefuncs on the outgoing path (sessionhandler->) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) # At this point the object is certainly the right encoding, but may still be a unicode object-- # to_str does not actually force objects to become bytestrings. # If the unicode object is a subclass of unicode, such as ANSIString, this can cause a problem, # as special behavior for that class will still be in play. Since we're now transferring raw data, # we must now force this to be a proper bytestring. return str(data) elif hasattr(data, "id") and hasattr(data, "db_date_created") \ and hasattr(data, '__dbclass__'): # convert database-object to their string representation. return _validate(unicode(data)) else: return data
def test_nested(self): self.assertEqual( inlinefuncs.parse_inlinefunc( "this $crop(is a test with $pad(padded, 20) text in $pad(pad2, 10) a crop, 80)" ), "this is a test with padded text in pad2 a crop" )
def test_escaped(self): self.assertEqual( inlinefuncs.parse_inlinefunc( "this should be $pad('''escaped,''' and '''instead,''' cropped $crop(with a long,5) text., 80)" ), "this should be escaped, and instead, cropped with text. " )
def _validate(data): "Helper function to convert data to AMP-safe (picketable) values" if isinstance(data, dict): newdict = {} for key, part in data.items(): newdict[key] = _validate(part) return newdict elif hasattr(data, "__iter__"): return [_validate(part) for part in data] elif isinstance(data, basestring): # make sure strings are in a valid encoding try: data = data and to_str( to_unicode(data), encoding=session.protocol_flags["ENCODING"]) except LookupError: # wrong encoding set on the session. Set it to a safe one session.protocol_flags["ENCODING"] = "utf-8" data = to_str(to_unicode(data), encoding=session.protocol_flags["ENCODING"]) if _INLINEFUNC_ENABLED and not raw and isinstance( self, ServerSessionHandler): # only parse inlinefuncs on the outgoing path (sessionhandler->) data = parse_inlinefunc(data, strip=strip_inlinefunc, session=session) return data elif hasattr(data, "id") and hasattr(data, "db_date_created") \ and hasattr(data, '__dbclass__'): # convert database-object to their string representation. return _validate(unicode(data)) else: return data
def protfunc_parser(value, available_functions=None, testing=False, stacktrace=False, **kwargs): """ Parse a prototype value string for a protfunc and process it. Available protfuncs are specified as callables in one of the modules of `settings.PROTFUNC_MODULES`, or specified on the command line. Args: value (any): The value to test for a parseable protfunc. Only strings will be parsed for protfuncs, all other types are returned as-is. available_functions (dict, optional): Mapping of name:protfunction to use for this parsing. If not set, use default sources. testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may behave differently. stacktrace (bool, optional): If set, print the stack parsing process of the protfunc-parser. Kwargs: session (Session): Passed to protfunc. Session of the entity spawning the prototype. protototype (dict): Passed to protfunc. The dict this protfunc is a part of. current_key(str): Passed to protfunc. The key in the prototype that will hold this value. any (any): Passed on to the protfunc. Returns: testresult (tuple): If `testing` is set, returns a tuple (error, result) where error is either None or a string detailing the error from protfunc_parser or seen when trying to run `literal_eval` on the parsed string. any (any): A structure to replace the string on the prototype level. If this is a callable or a (callable, (args,)) structure, it will be executed as if one had supplied it to the prototype directly. This structure is also passed through literal_eval so one can get actual Python primitives out of it (not just strings). It will also identify eventual object #dbrefs in the output from the protfunc. """ if not isinstance(value, basestring): try: value = value.dbref except AttributeError: pass value = to_str(value, force_string=True) available_functions = PROT_FUNCS if available_functions is None else available_functions # insert $obj(#dbref) for #dbref value = _RE_DBREF.sub("$obj(\\1)", value) result = inlinefuncs.parse_inlinefunc( value, available_funcs=available_functions, stacktrace=stacktrace, testing=testing, **kwargs) err = None try: result = literal_eval(result) except ValueError: pass except Exception as err: err = str(err) if testing: return err, result return result
def protfunc_parser(value, available_functions=None, testing=False, stacktrace=False, **kwargs): """ Parse a prototype value string for a protfunc and process it. Available protfuncs are specified as callables in one of the modules of `settings.PROTFUNC_MODULES`, or specified on the command line. Args: value (any): The value to test for a parseable protfunc. Only strings will be parsed for protfuncs, all other types are returned as-is. available_functions (dict, optional): Mapping of name:protfunction to use for this parsing. If not set, use default sources. testing (bool, optional): Passed to protfunc. If in a testing mode, some protfuncs may behave differently. stacktrace (bool, optional): If set, print the stack parsing process of the protfunc-parser. Kwargs: session (Session): Passed to protfunc. Session of the entity spawning the prototype. protototype (dict): Passed to protfunc. The dict this protfunc is a part of. current_key(str): Passed to protfunc. The key in the prototype that will hold this value. any (any): Passed on to the protfunc. Returns: testresult (tuple): If `testing` is set, returns a tuple (error, result) where error is either None or a string detailing the error from protfunc_parser or seen when trying to run `literal_eval` on the parsed string. any (any): A structure to replace the string on the prototype level. If this is a callable or a (callable, (args,)) structure, it will be executed as if one had supplied it to the prototype directly. This structure is also passed through literal_eval so one can get actual Python primitives out of it (not just strings). It will also identify eventual object #dbrefs in the output from the protfunc. """ if not isinstance(value, basestring): try: value = value.dbref except AttributeError: pass value = to_str(value, force_string=True) available_functions = PROT_FUNCS if available_functions is None else available_functions result = inlinefuncs.parse_inlinefunc( value, available_funcs=available_functions, stacktrace=stacktrace, testing=testing, **kwargs) err = None try: result = literal_eval(result) except ValueError: pass except Exception as err: err = str(err) if testing: return err, result return result
def test_nofunc(self): self.assertEqual( inlinefuncs.parse_inlinefunc("as$382ewrw w we w werw,|44943}"), "as$382ewrw w we w werw,|44943}")
def test_single_func(self): self.assertEqual( inlinefuncs.parse_inlinefunc( "this is a test with $pad(centered, 20) text in it."), "this is a test with centered text in it.")
def test_incomplete(self): self.assertEqual(inlinefuncs.parse_inlinefunc( "testing $blah{without an ending."), "testing $blah{without an ending.")
def test_escaped2(self): self.assertEqual(inlinefuncs.parse_inlinefunc( 'this should be $pad("""escaped,""" and """instead,""" cropped $crop(with a long,5) text., 80)'), "this should be escaped, and instead, cropped with text. ")
def test_escaped(self): self.assertEqual(inlinefuncs.parse_inlinefunc( "this should be $pad('''escaped,''' and '''instead,''' cropped $crop(with a long,5) text., 80)"), "this should be escaped, and instead, cropped with text. ")
def test_nested(self): self.assertEqual(inlinefuncs.parse_inlinefunc( "this $crop(is a test with $pad(padded, 20) text in $pad(pad2, 10) a crop, 80)"), "this is a test with padded text in pad2 a crop")
def test_single_func(self): self.assertEqual(inlinefuncs.parse_inlinefunc( "this is a test with $pad(centered, 20) text in it."), "this is a test with centered text in it.")
def test_incomplete(self): self.assertEqual( inlinefuncs.parse_inlinefunc("testing $blah{without an ending."), "testing $blah{without an ending.")
def test_nofunc(self): self.assertEqual(inlinefuncs.parse_inlinefunc( "as$382ewrw w we w werw,|44943}"), "as$382ewrw w we w werw,|44943}")