def _rematcher(pat): m = util.compilere(pat) try: # slightly faster, provided by facebook's re2 bindings return m.test_match except AttributeError: return m.match
def _rematcher(regex): '''compile the regexp with the best available regexp engine and return a matcher function''' m = util.compilere(regex) try: # slightly faster, provided by facebook's re2 bindings return m.test_match except AttributeError: return m.match
def parse(self, src, data, sections=None, remap=None, include=None): sectionre = util.compilere(r'\[([^\[]+)\]') itemre = util.compilere(r'([^=\s][^=]*?)\s*=\s*(.*\S|)') contre = util.compilere(r'\s+(\S|\S.*\S)\s*$') emptyre = util.compilere(r'(;|#|\s*$)') commentre = util.compilere(r'(;|#)') unsetre = util.compilere(r'%unset\s+(\S+)') includere = util.compilere(r'%include\s+(\S|\S.*\S)\s*$') section = "" item = None line = 0 cont = False for l in data.splitlines(True): line += 1 if line == 1 and l.startswith('\xef\xbb\xbf'): # Someone set us up the BOM l = l[3:] if cont: if commentre.match(l): continue m = contre.match(l) if m: if sections and section not in sections: continue v = self.get(section, item) + "\n" + m.group(1) self.set(section, item, v, "%s:%d" % (src, line)) continue item = None cont = False m = includere.match(l) if m: inc = util.expandpath(m.group(1)) base = os.path.dirname(src) inc = os.path.normpath(os.path.join(base, inc)) if include: try: include(inc, remap=remap, sections=sections) except IOError, inst: if inst.errno != errno.ENOENT: raise error.ParseError(_("cannot include %s (%s)") % (inc, inst.strerror), "%s:%s" % (src, line)) continue if emptyre.match(l): continue m = sectionre.match(l) if m: section = m.group(1) if remap: section = remap.get(section, section) if section not in self: self._data[section] = sortdict() continue m = itemre.match(l) if m: item = m.group(1) cont = True if sections and section not in sections: continue self.set(section, item, m.group(2), "%s:%d" % (src, line)) continue m = unsetre.match(l) if m: name = m.group(1) if sections and section not in sections: continue if self.get(section, name) is not None: del self._data[section][name] self._unset.append((section, name)) continue raise error.ParseError(l.rstrip(), ("%s:%s" % (src, line)))