def validate_encoding(val): # helper: change encoding try: codecs_lookup(val) except LookupError: raise RuntimeError("The encoding '|w%s|n' is invalid. " % val) return val
def func(self): """ Sets the encoding. """ if self.session is None: return sync = False if "clear" in self.switches: # remove customization old_encoding = self.session.protocol_flags.get("ENCODING", None) if old_encoding: string = "Your custom text encoding ('%s') was cleared." % old_encoding else: string = "No custom encoding was set." self.session.protocol_flags["ENCODING"] = "utf-8" sync = True elif not self.args: # just list the encodings supported pencoding = self.session.protocol_flags.get("ENCODING", None) string = "" if pencoding: string += ( "Default encoding: |g%s|n (change with |wencoding <encoding>|n)" % pencoding ) encodings = settings.ENCODINGS if encodings: string += ( "\nServer's alternative encodings (tested in this order):\n |g%s|n" % ", ".join(encodings) ) if not string: string = "No encodings found." else: # change encoding old_encoding = self.session.protocol_flags.get("ENCODING", None) encoding = self.args try: codecs_lookup(encoding) except LookupError: string = ( "|rThe encoding '|w%s|r' is invalid. Keeping the previous encoding '|w%s|r'.|n" % (encoding, old_encoding) ) else: self.session.protocol_flags["ENCODING"] = encoding string = "Your custom text encoding was changed from '|w%s|n' to '|w%s|n'." % ( old_encoding, encoding, ) sync = True if sync: self.session.sessionhandler.session_portal_sync(self.session) self.caller.msg(string.strip())
def do_parse_dict_to_xml(dict_in, file_name, encoding=None): from codecs import lookup as codecs_lookup if encoding is None: encoding = 'utf-8' dom = root_dict_to_xml(dict_in, encoding) #print 'complete',data f = open(file_name, 'wb') writer = codecs_lookup(encoding)[3](f) dom.writexml(writer, encoding=encoding) f.close()
def do_parse_json_file_to_xml(file_path, encoding=None): from codecs import lookup as codecs_lookup f = open(file_path, 'r') str_json = json_load(f) f.close() if encoding is None: encoding = 'utf-8' dom = root_dict_to_xml(str_json, encoding) #print 'complete',data f = open(file_path + '.xml', 'wb') writer = codecs_lookup(encoding)[3](f) dom.writexml(writer, encoding=encoding) f.close()