def parse(self, *args, **kwargs): text = get_text(self.view) # Parsing will fail if `<?xml version="1.0" encoding="UTF-8"?>` encoding is in the first # line, so strip it. # XXX: Find a better way to fix this misbehaviour of xml stuff in Python # (I mean, plistliv even "writes" that line) if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'): text = text[38:] try: from xml.parsers.expat import ExpatError, ErrorString except ImportError: # xml.parsers.expat is not available on certain Linux dists, use plist_parser then. # See https://github.com/SublimeText/AAAPackageDev/issues/19 import plist_parser print("[AAAPackageDev] Using plist_parser") try: data = plist_parser.parse_string(text) except plist_parser.PropertyListParseError, e: self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0)) else: return data
def load(text, path, out): """Load a tmTheme property list and write errors to an output panel. :param text: The text of the file to be parsed. :param path: The path of the file, for error output purposes. :param out: OutputPanel instance. :return: `None` if errored, the parsed data otherwise (mostly a dict). """ dirname = os.path.dirname(path) out.set_path(dirname, file_regex) if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'): text = text[38:] try: from xml.parsers.expat import ExpatError, ErrorString except ImportError: # TODO: provide a compat plist parser as dependency # xml.parsers.expat is not available on certain Linux dists, try to use plist_parser then. # See https://github.com/SublimeText/AAAPackageDev/issues/19 # Let's just hope AAAPackageDev is installed try: import plist_parser except ImportError: out.write_line( "Unable to load xml.parsers.expat or plist_parser modules.\n" "Please report to the package author.") return else: out.write_line( "Unable to load plistlib, using plist_parser instead\n") try: data = plist_parser.parse_string(text) except plist_parser.PropertyListParseError as e: out.write_line(debug_base_2 % (path, str(e))) else: return data else: import plistlib try: # This will try `from xml.parsers.expat import ParserCreate` # but since it is already tried above it should succeed. return plistlib.readPlistFromBytes(text.encode('utf-8')) except ExpatError as e: out.write_line(debug_base % (path, ErrorString(e.code), e.lineno, e.offset + 1))
def parse(self, *args, **kwargs): # Note: I hate Plist and XML. And it doesn't help a bit that parsing # plist files is a REAL PITA. text = get_text(self.view) # Parsing will fail if `<?xml version="1.0" encoding="UTF-8"?>` encoding is in the first # line, so strip it. # XXX: Find a better way to fix this misbehaviour of xml stuff in Python # (I mean, plistliv even "writes" that line) if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'): text = text[38:] # See https://github.com/SublimeText/AAAPackageDev/issues/34 if ST2 and isinstance(text, unicode): text = text.encode('utf-8') try: from xml.parsers.expat import ExpatError, ErrorString except ImportError: # xml.parsers.expat is not available on certain Linux dists, use plist_parser then. # See https://github.com/SublimeText/AAAPackageDev/issues/19 import plist_parser print("[AAAPackageDev] Using plist_parser") try: data = plist_parser.parse_string(text) except plist_parser.PropertyListParseError as e: self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0)) else: return data else: try: # This will try `from xml.parsers.expat import ParserCreate` # but since it is already tried above it should succeed. if ST2: data = plistlib.readPlistFromString(text) else: data = plistlib.readPlistFromBytes(text.encode('utf-8')) except ExpatError as e: self.output.write_line(self.debug_base % (self.file_path, ErrorString(e.code), e.lineno, e.offset) ) except BaseException as e: # Whatever could happen here ... self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0)) else: return data
def parse(self, *args, **kwargs): # Note: I hate Plist and XML. And it doesn't help a bit that parsing # plist files is a REAL PITA. text = get_text(self.view) # Parsing will fail if `<?xml version="1.0" encoding="UTF-8"?>` encoding is in the first # line, so strip it. # XXX: Find a better way to fix this misbehaviour of xml stuff in Python # (I mean, plistliv even "writes" that line) if text.startswith('<?xml version="1.0" encoding="UTF-8"?>'): text = text[38:] # See https://github.com/SublimeText/AAAPackageDev/issues/34 if ST2 and isinstance(text, unicode): text = text.encode('utf-8') try: from xml.parsers.expat import ExpatError, ErrorString except ImportError: # xml.parsers.expat is not available on certain Linux dists, use plist_parser then. # See https://github.com/SublimeText/AAAPackageDev/issues/19 import plist_parser print("[AAAPackageDev] Using plist_parser") try: data = plist_parser.parse_string(text) except plist_parser.PropertyListParseError as e: self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0)) else: return data else: try: # This will try `from xml.parsers.expat import ParserCreate` # but since it is already tried above it should succeed. if ST2: data = plistlib.readPlistFromString(text) else: data = plistlib.readPlistFromBytes(text.encode('utf-8')) except ExpatError as e: self.output.write_line( self.debug_base % (self.file_path, ErrorString(e.code), e.lineno, e.offset)) except BaseException as e: # Whatever could happen here ... self.output.write_line(self.debug_base % (self.file_path, str(e), 0, 0)) else: return data