def file_is_valid(cls, view, file_path=None): file_path = file_path or view and view.file_name() if not file_path: return None if (cls.get_ext_appendix(file_path) is not None or os.path.splitext(file_path)[1] == '.' + cls.ext): return True # Plists have no scope (syntax definition) since they are XML. # Instead, check for the DOCTYPE in the first three lines. if view: for i in range(3): # This would be a really terrible one-liner text = coorded_substr(view, (i, 0), (i, len(cls.DOCTYPE))) if text == cls.DOCTYPE: return True return False
def get_file_ext(self, v): if not v: return None fn = v.file_name() ext = os.path.splitext(fn)[1] # check for ".plist" ext if ext == ".plist": return ".json" # check for plist namespace (doctype) in first two lines for i in range(2): text = coorded_substr(v, (i, 0), (i, len(self.DOCTYPE))) print "i: %d, text: %s" % (i, text) if text == self.DOCTYPE: return ".JSON-" + ext[1:] # not a plist file, 99% likely return None
def load_options(self, view): """Search for a line comment in the first few lines which starts with ``"[PackageDev]"`` and parse the following things using ``yaml.safe_load`` after wrapping them in "{}". """ # Don't bother reading the file to load its options. if not view: return None # Search for options in the first 3 lines (compatible with xml) for i in range(3): try: line = coorded_substr(view, (i, 0), (i, -1)) optstr = re.search(self.opt_regex, line) # Just parse the string with yaml; wrapped in {} # Yeah, I'm lazy like that, but see, I even put "safe_" in front of it return yaml.safe_load('{%s}' % optstr.group(1)) except: continue return None