def iriToUri(iri):
def urlEncodeNonAscii(b):<EOL><INDENT>"""<STR_LIT>"""<EOL>import re<EOL>return re.sub('<STR_LIT>', lambda c: '<STR_LIT>' % ord(c.group(<NUM_LIT:0>)), b)<EOL><DEDENT>parts = urlparse.urlparse(iri)<EOL>if sys.version_info < (<NUM_LIT:3>, <NUM_LIT:0>):<EOL><INDENT>partes = []<EOL>for parti, part in enumerate(parts):<EOL><INDENT>try:<EOL><INDENT>if parti != <NUM_LIT:1>:<EOL><INDENT>partes.append(urlEncodeNonAscii(part.encode('<STR_LIT:utf-8>')))<EOL><DEDENT>else:<EOL><INDENT>partes.append(part.encode('<STR_LIT>'))<EOL><DEDENT><DEDENT>except UnicodeDecodeError as ue:<EOL><INDENT>partes.append(urlEncodeNonAscii(part.decode('<STR_LIT>')))<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT>return urlparse.urlunparse(partes)<EOL><DEDENT>else:<EOL><INDENT>uri = [part.decode('<STR_LIT:utf8>') for parti, part in enumerate(parts.encode('<STR_LIT:utf8>'))]<EOL>return urlparse.urlunparse(uri)<EOL><DEDENT>
Change an IRI (internationalized R) to an URI.

    Used at MambuStruct.connect() method for any requests done to Mambu.

    Perfect example of unicode getting in the way.

    Illustrative (I hope) example: I have Mambu usernames with special
    chars in them. When retrieving them and then trying to build a
    MambuUser object with them, I get a BIG problem because of the
    unicode chars there. Using this I solved the problem.