def test_bad_pattern(self): """Ensure lazy regex handles bad patterns cleanly.""" p = lazy_regex.lazy_compile('RE:[') # As p.match is lazy, we make it into a lambda so its handled # by assertRaises correctly. e = self.assertRaises(errors.InvalidPattern, lambda: p.match('foo')) self.assertEqual(e.msg, '"RE:[" unexpected end of regular expression')
def test_pickle(self): # When pickling, just compile the regex. # Sphinx, which we use for documentation, pickles # some compiled regexes. lazy_pattern = lazy_regex.lazy_compile('[,;]*') pickled = pickle.dumps(lazy_pattern) unpickled_lazy_pattern = pickle.loads(pickled) self.assertEqual(['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
def _add_patterns(self, patterns, translator, prefix=''): while patterns: grouped_rules = ['(%s)' % translator(pat) for pat in patterns[:99]] joined_rule = '%s(?:%s)$' % (prefix, '|'.join(grouped_rules)) # Explicitly use lazy_compile here, because we count on its # nicer error reporting. self._regex_patterns.append( (lazy_regex.lazy_compile(joined_rule, re.UNICODE), patterns[:99])) patterns = patterns[99:]
class Template(object): """A simple template engine. >>> t = Template() >>> t.add('test', 'xxx') >>> print list(t.process('{test}')) ['xxx'] >>> print list(t.process('{test} test')) ['xxx', ' test'] >>> print list(t.process('test {test}')) ['test ', 'xxx'] >>> print list(t.process('test {test} test')) ['test ', 'xxx', ' test'] >>> print list(t.process('{test}\\\\n')) ['xxx', '\\n'] >>> print list(t.process('{test}\\n')) ['xxx', '\\n'] """ _tag_re = lazy_compile('{(\w+)}') def __init__(self): self._data = {} def add(self, name, value): self._data[name] = value def process(self, tpl): tpl = tpl.decode('string_escape') pos = 0 while True: match = self._tag_re.search(tpl, pos) if not match: if pos < len(tpl): yield tpl[pos:] break start, end = match.span() if start > 0: yield tpl[pos:start] pos = end name = match.group(1) try: data = self._data[name] except KeyError: raise errors.MissingTemplateVariable(name) if not isinstance(data, basestring): data = str(data) yield data
def is_pattern_valid(pattern): """Returns True if pattern is valid. :param pattern: Normalized pattern. is_pattern_valid() assumes pattern to be normalized. see: globbing.normalize_pattern """ result = True translator = Globster.pattern_info[Globster.identify( pattern)]["translator"] tpattern = '(%s)' % translator(pattern) try: re_obj = lazy_regex.lazy_compile(tpattern, re.UNICODE) re_obj.search("") # force compile except errors.InvalidPattern, e: result = False
def __call__(self, text): if not self._pat: self._pat = lazy_regex.lazy_compile( u'|'.join([u'(%s)' % p for p in self._pats]), re.UNICODE) return self._pat.sub(self._do_sub, text)
def test_split(self): pattern = lazy_regex.lazy_compile('[,;]*') self.assertEqual(['x', 'y', 'z'], pattern.split('x,y;z'))
'lt':'<', 'gt':'>' } def _unescaper(match, _map=_xml_unescape_map): code = match.group(1) try: return _map[code] except KeyError: if not code.startswith('#'): raise return unichr(int(code[1:])).encode('utf8') _unescape_re = lazy_regex.lazy_compile('\&([^;]*);') def _unescape_xml(data): """Unescape predefined XML entities in a string of data.""" return _unescape_re.sub(_unescaper, data) class Serializer_v8(XMLSerializer): """This serialiser adds rich roots. Its revision format number matches its inventory number. """ __slots__ = [] root_id = None
# You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # """Inventory related helpers for indexing.""" import re from bzrlib import lazy_regex from bzrlib.lazy_import import lazy_import lazy_import(globals(), """ from bzrlib import xml_serializer """) _file_ids_name_regex = lazy_regex.lazy_compile( r'file_id="(?P<file_id>[^"]+)"' r'(?:.* name="(?P<name>[^"]*)")?' r'(?:.* parent_id="(?P<parent_id>[^"]+)")?') _unescape_re = lazy_regex.lazy_compile("&|'|"|<|>") _unescape_map = { '&': '&', "'": "'", """: '"', "<": '<', ">": '>', } def _unescape_replace(match, map=_unescape_map): return map[match.group()]
def test_search(self): pattern = lazy_regex.lazy_compile('fo*') self.assertEqual('foo', pattern.search('baz foo').group()) self.assertEqual('fooo', pattern.search('fooo').group())
class RevisionSpec_dwim(RevisionSpec): """Provides a DWIMish revision specifier lookup. Note that this does not go in the revspec_registry because by definition there is no prefix to identify it. It's solely called from RevisionSpec.from_string() because the DWIMification happen when _match_on is called so the string describing the revision is kept here until needed. """ help_txt = None _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$') # The revspecs to try _possible_revspecs = [] def _try_spectype(self, rstype, branch): rs = rstype(self.spec, _internal=True) # Hit in_history to find out if it exists, or we need to try the # next type. return rs.in_history(branch) def _match_on(self, branch, revs): """Run the lookup and see what we can get.""" # First, see if it's a revno if self._revno_regex.match(self.spec) is not None: try: return self._try_spectype(RevisionSpec_revno, branch) except RevisionSpec_revno.dwim_catchable_exceptions: pass # Next see what has been registered for objgetter in self._possible_revspecs: rs_class = objgetter.get_obj() try: return self._try_spectype(rs_class, branch) except rs_class.dwim_catchable_exceptions: pass # Try the old (deprecated) dwim list: for rs_class in dwim_revspecs: try: return self._try_spectype(rs_class, branch) except rs_class.dwim_catchable_exceptions: pass # Well, I dunno what it is. Note that we don't try to keep track of the # first of last exception raised during the DWIM tries as none seems # really relevant. raise errors.InvalidRevisionSpec(self.spec, branch) @classmethod def append_possible_revspec(cls, revspec): """Append a possible DWIM revspec. :param revspec: Revision spec to try. """ cls._possible_revspecs.append(registry._ObjectGetter(revspec)) @classmethod def append_possible_lazy_revspec(cls, module_name, member_name): """Append a possible lazily loaded DWIM revspec. :param module_name: Name of the module with the revspec :param member_name: Name of the revspec within the module """ cls._possible_revspecs.append( registry._LazyObjectGetter(module_name, member_name))
class Serializer_v8(XMLSerializer): """This serialiser adds rich roots. Its revision format number matches its inventory number. """ __slots__ = [] root_id = None support_altered_by_hack = True # This format supports the altered-by hack that reads file ids directly out # of the versionedfile, without doing XML parsing. supported_kinds = set(['file', 'directory', 'symlink']) format_num = '8' revision_format_num = None # The search regex used by xml based repositories to determine what things # where changed in a single commit. _file_ids_altered_regex = lazy_regex.lazy_compile( r'file_id="(?P<file_id>[^"]+)"' r'.* revision="(?P<revision_id>[^"]+)"' ) def _check_revisions(self, inv): """Extension point for subclasses to check during serialisation. :param inv: An inventory about to be serialised, to be checked. :raises: AssertionError if an error has occurred. """ if inv.revision_id is None: raise AssertionError("inv.revision_id is None") if inv.root.revision is None: raise AssertionError("inv.root.revision is None") def _check_cache_size(self, inv_size, entry_cache): """Check that the entry_cache is large enough. We want the cache to be ~2x the size of an inventory. The reason is because we use a FIFO cache, and how Inventory records are likely to change. In general, you have a small number of records which change often, and a lot of records which do not change at all. So when the cache gets full, you actually flush out a lot of the records you are interested in, which means you need to recreate all of those records. An LRU Cache would be better, but the overhead negates the cache coherency benefit. One way to look at it, only the size of the cache > len(inv) is your 'working' set. And in general, it shouldn't be a problem to hold 2 inventories in memory anyway. :param inv_size: The number of entries in an inventory. """ if entry_cache is None: return # 1.5 times might also be reasonable. recommended_min_cache_size = inv_size * 1.5 if entry_cache.cache_size() < recommended_min_cache_size: recommended_cache_size = inv_size * 2 trace.mutter('Resizing the inventory entry cache from %d to %d', entry_cache.cache_size(), recommended_cache_size) entry_cache.resize(recommended_cache_size) def write_inventory_to_lines(self, inv): """Return a list of lines with the encoded inventory.""" return self.write_inventory(inv, None) def write_inventory_to_string(self, inv, working=False): """Just call write_inventory with a StringIO and return the value. :param working: If True skip history data - text_sha1, text_size, reference_revision, symlink_target. """ sio = cStringIO.StringIO() self.write_inventory(inv, sio, working) return sio.getvalue() def write_inventory(self, inv, f, working=False): """Write inventory to a file. :param inv: the inventory to write. :param f: the file to write. (May be None if the lines are the desired output). :param working: If True skip history data - text_sha1, text_size, reference_revision, symlink_target. :return: The inventory as a list of lines. """ output = [] append = output.append self._append_inventory_root(append, inv) serialize_inventory_flat(inv, append, self.root_id, self.supported_kinds, working) if f is not None: f.writelines(output) # Just to keep the cache from growing without bounds # but we may actually not want to do clear the cache #_clear_cache() return output def _append_inventory_root(self, append, inv): """Append the inventory root to output.""" if inv.revision_id is not None: revid1 = ' revision_id="' revid2 = encode_and_escape(inv.revision_id) else: revid1 = "" revid2 = "" append('<inventory format="%s"%s%s>\n' % ( self.format_num, revid1, revid2)) append('<directory file_id="%s name="%s revision="%s />\n' % ( encode_and_escape(inv.root.file_id), encode_and_escape(inv.root.name), encode_and_escape(inv.root.revision))) def _pack_revision(self, rev): """Revision object -> xml tree""" # For the XML format, we need to write them as Unicode rather than as # utf-8 strings. So that cElementTree can handle properly escaping # them. decode_utf8 = cache_utf8.decode revision_id = rev.revision_id if isinstance(revision_id, str): revision_id = decode_utf8(revision_id) format_num = self.format_num if self.revision_format_num is not None: format_num = self.revision_format_num root = Element('revision', committer = rev.committer, timestamp = '%.3f' % rev.timestamp, revision_id = revision_id, inventory_sha1 = rev.inventory_sha1, format=format_num, ) if rev.timezone is not None: root.set('timezone', str(rev.timezone)) root.text = '\n' msg = SubElement(root, 'message') msg.text = escape_invalid_chars(rev.message)[0] msg.tail = '\n' if rev.parent_ids: pelts = SubElement(root, 'parents') pelts.tail = pelts.text = '\n' for parent_id in rev.parent_ids: _mod_revision.check_not_reserved_id(parent_id) p = SubElement(pelts, 'revision_ref') p.tail = '\n' if isinstance(parent_id, str): parent_id = decode_utf8(parent_id) p.set('revision_id', parent_id) if rev.properties: self._pack_revision_properties(rev, root) return root def _pack_revision_properties(self, rev, under_element): top_elt = SubElement(under_element, 'properties') for prop_name, prop_value in sorted(rev.properties.items()): prop_elt = SubElement(top_elt, 'property') prop_elt.set('name', prop_name) prop_elt.text = prop_value prop_elt.tail = '\n' top_elt.tail = '\n' def _unpack_entry(self, elt, entry_cache=None, return_from_cache=False): # This is here because it's overridden by xml7 return unpack_inventory_entry(elt, entry_cache, return_from_cache) def _unpack_inventory(self, elt, revision_id=None, entry_cache=None, return_from_cache=False): """Construct from XML Element""" inv = unpack_inventory_flat(elt, self.format_num, self._unpack_entry, entry_cache, return_from_cache) self._check_cache_size(len(inv), entry_cache) return inv def _unpack_revision(self, elt): """XML Element -> Revision object""" format = elt.get('format') format_num = self.format_num if self.revision_format_num is not None: format_num = self.revision_format_num if format is not None: if format != format_num: raise BzrError("invalid format version %r on revision" % format) get_cached = get_utf8_or_ascii rev = Revision(committer = elt.get('committer'), timestamp = float(elt.get('timestamp')), revision_id = get_cached(elt.get('revision_id')), inventory_sha1 = elt.get('inventory_sha1') ) parents = elt.find('parents') or [] for p in parents: rev.parent_ids.append(get_cached(p.get('revision_id'))) self._unpack_revision_properties(elt, rev) v = elt.get('timezone') if v is None: rev.timezone = 0 else: rev.timezone = int(v) rev.message = elt.findtext('message') # text of <message> return rev def _unpack_revision_properties(self, elt, rev): """Unpack properties onto a revision.""" props_elt = elt.find('properties') if not props_elt: return for prop_elt in props_elt: if prop_elt.tag != 'property': raise AssertionError( "bad tag under properties list: %r" % prop_elt.tag) name = prop_elt.get('name') value = prop_elt.text # If a property had an empty value ('') cElementTree reads # that back as None, convert it back to '', so that all # properties have string values if value is None: value = '' if name in rev.properties: raise AssertionError("repeated property %r" % name) rev.properties[name] = value def _find_text_key_references(self, line_iterator): """Core routine for extracting references to texts from inventories. This performs the translation of xml lines to revision ids. :param line_iterator: An iterator of lines, origin_version_id :return: A dictionary mapping text keys ((fileid, revision_id) tuples) to whether they were referred to by the inventory of the revision_id that they contain. Note that if that revision_id was not part of the line_iterator's output then False will be given - even though it may actually refer to that key. """ if not self.support_altered_by_hack: raise AssertionError( "_find_text_key_references only " "supported for branches which store inventory as unnested xml" ", not on %r" % self) result = {} # this code needs to read every new line in every inventory for the # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line # not present in one of those inventories is unnecessary but not # harmful because we are filtering by the revision id marker in the # inventory lines : we only select file ids altered in one of those # revisions. We don't need to see all lines in the inventory because # only those added in an inventory in rev X can contain a revision=X # line. unescape_revid_cache = {} unescape_fileid_cache = {} # jam 20061218 In a big fetch, this handles hundreds of thousands # of lines, so it has had a lot of inlining and optimizing done. # Sorry that it is a little bit messy. # Move several functions to be local variables, since this is a long # running loop. search = self._file_ids_altered_regex.search unescape = _unescape_xml setdefault = result.setdefault for line, line_key in line_iterator: match = search(line) if match is None: continue # One call to match.group() returning multiple items is quite a # bit faster than 2 calls to match.group() each returning 1 file_id, revision_id = match.group('file_id', 'revision_id') # Inlining the cache lookups helps a lot when you make 170,000 # lines and 350k ids, versus 8.4 unique ids. # Using a cache helps in 2 ways: # 1) Avoids unnecessary decoding calls # 2) Re-uses cached strings, which helps in future set and # equality checks. # (2) is enough that removing encoding entirely along with # the cache (so we are using plain strings) results in no # performance improvement. try: revision_id = unescape_revid_cache[revision_id] except KeyError: unescaped = unescape(revision_id) unescape_revid_cache[revision_id] = unescaped revision_id = unescaped # Note that unconditionally unescaping means that we deserialise # every fileid, which for general 'pull' is not great, but we don't # really want to have some many fulltexts that this matters anyway. # RBC 20071114. try: file_id = unescape_fileid_cache[file_id] except KeyError: unescaped = unescape(file_id) unescape_fileid_cache[file_id] = unescaped file_id = unescaped key = (file_id, revision_id) setdefault(key, False) if revision_id == line_key[-1]: result[key] = True return result
def test_findall(self): pattern = lazy_regex.lazy_compile('fo*') self.assertEqual(['f', 'fo', 'foo', 'fooo'], pattern.findall('f fo foo fooo'))
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # """Inventory related helpers for indexing.""" import re from bzrlib import lazy_regex from bzrlib.lazy_import import lazy_import lazy_import(globals(), """ from bzrlib import xml_serializer """) _file_ids_name_regex = lazy_regex.lazy_compile( r'file_id="(?P<file_id>[^"]+)"' r'(?:.* name="(?P<name>[^"]*)")?' r'(?:.* parent_id="(?P<parent_id>[^"]+)")?' ) _unescape_re = lazy_regex.lazy_compile("&|'|"|<|>") _unescape_map = { '&': '&', "'": "'", """: '"', "<": '<', ">": '>', } def _unescape_replace(match, map=_unescape_map): return map[match.group()]
def test_simple_acts_like_regex(self): """Test that the returned object has basic regex like functionality""" pattern = lazy_regex.lazy_compile('foo') self.assertIsInstance(pattern, lazy_regex.LazyRegex) self.assertTrue(pattern.match('foo')) self.assertIs(None, pattern.match('bar'))
dialog.connect(chooser, QtCore.SIGNAL("clicked()"), click_handler) def open_browser(url): try: import webbrowser open_func = webbrowser.open except ImportError: try: open_func = os.startfile except AttributeError: open_func = lambda x: None open_func(url) _extract_name_re = lazy_regex.lazy_compile('(.*?) <.*?@.*?>') _extract_email_re = lazy_regex.lazy_compile('<(.*?@.*?)>') def extract_name(author, strict=False): m = _extract_name_re.match(author) if m: name = m.group(1) else: if strict: name = author else: m = _extract_email_re.match(author) if m: name = m.group(1) else:
class _OrderedGlobster(Globster): """A Globster that keeps pattern order.""" def __init__(self, patterns): """Constructor. :param patterns: sequence of glob patterns """ # Note: This could be smarter by running like sequences together self._regex_patterns = [] for pat in patterns: pat = normalize_pattern(pat) t = Globster.identify(pat) self._add_patterns([pat], Globster.pattern_info[t]["translator"], Globster.pattern_info[t]["prefix"]) _slashes = lazy_regex.lazy_compile(r'[\\/]+') def normalize_pattern(pattern): """Converts backslashes in path patterns to forward slashes. Doesn't normalize regular expressions - they may contain escapes. """ if not (pattern.startswith('RE:') or pattern.startswith('!RE:')): pattern = _slashes.sub('/', pattern) if len(pattern) > 1: pattern = pattern.rstrip('/') return pattern
'debianlp:', 'bzrlib.plugins.launchpad.lp_directory', 'LaunchpadDirectory', 'debianlp: shortcut') directories.register_lazy( 'ubuntu:', 'bzrlib.plugins.launchpad.lp_directory', 'LaunchpadDirectory', 'ubuntu: shortcut') _register_directory() # This is kept in __init__ so that we don't load lp_api_lite unless the branch # actually matches. That way we can avoid importing extra dependencies like # json. _package_branch = lazy_regex.lazy_compile( r'bazaar.launchpad.net.*?/' r'(?P<user>~[^/]+/)?(?P<archive>ubuntu|debian)/(?P<series>[^/]+/)?' r'(?P<project>[^/]+)(?P<branch>/[^/]+)?' ) def _get_package_branch_info(url): """Determine the packaging information for this URL. :return: If this isn't a packaging branch, return None. If it is, return (archive, series, project) """ if url is None: return None m = _package_branch.search(url) if m is None: return None archive, series, project, user = m.group('archive', 'series',
If it is Unicode, then we need to encode it. :param a_str: An 8-bit string or Unicode as returned by cElementTree.Element.get() :return: A utf-8 encoded 8-bit string. """ # This is fairly optimized because we know what cElementTree does, this is # not meant as a generic function for all cases. Because it is possible for # an 8-bit string to not be ascii or valid utf8. if a_str.__class__ is unicode: return _encode_utf8(a_str) else: return intern(a_str) _utf8_re = lazy_regex.lazy_compile('[&<>\'\"]|[\x80-\xff]+') _unicode_re = lazy_regex.lazy_compile(u'[&<>\'\"\u0080-\uffff]') _xml_escape_map = { "&": '&', "'": "'", # FIXME: overkill "\"": """, "<": "<", ">": ">", } def _unicode_escape_replace(match, _map=_xml_escape_map): """Replace a string of non-ascii, non XML safe characters with their escape This will escape both Standard XML escapes, like <>"', etc.
def test_extra_args(self): """Test that extra arguments are also properly passed""" pattern = lazy_regex.lazy_compile('foo', re.I) self.assertIsInstance(pattern, lazy_regex.LazyRegex) self.assertTrue(pattern.match('foo')) self.assertTrue(pattern.match('Foo'))
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. from bzrlib import ( bugtracker, config, lazy_regex, ) # XXX use just (\d+) at the end of URL as simpler regexp? _bug_id_re = lazy_regex.lazy_compile( r'(?:' r'bugs/' # Launchpad bugs URL r'|ticket/' # Trac bugs URL r'|show_bug\.cgi\?id=' # Bugzilla bugs URL r'|issues/(?:show/)?' # Redmine bugs URL r'|DispForm.aspx\?ID=' # Microsoft SharePoint URL r'|default.asp\?' # Fogbugz URL r'|issue' # Roundup issue tracker URL r'|view.php\?id=' # Mantis bug tracker URL r'|aid=' # FusionForge bug tracker URL r'|task_id=' # Flyspray bug tracker URL (http://flyspray.org) - old style URLs(?) r'|task/' # Flyspray itself bugtracker (https://bugs.flyspray.org) r')(\d+)(?:\b|$)') _jira_bug_id_re = lazy_regex.lazy_compile( r'(?:.*/browse/)([A-Z][A-Z0-9_]*-\d+)($)') _unique_bugtrackers = ('lp', 'deb', 'gnome') # bugtracker config settings _bugtracker_re = lazy_regex.lazy_compile('(bugtracker|trac|bugzilla)_(.+)_url')
def test_finditer(self): pattern = lazy_regex.lazy_compile('fo*') matches = [(m.start(), m.end(), m.group()) for m in pattern.finditer('foo bar fop')] self.assertEqual([(0, 3, 'foo'), (8, 10, 'fo')], matches)
globals(), """ import time from bzrlib import ( config, errors, osutils, ) """) from bzrlib import ( lazy_regex, ) # the regex removes any weird characters; we don't escape them # but rather just pull them out _file_id_chars_re = lazy_regex.lazy_compile(r'[^\w.]') _rev_id_chars_re = lazy_regex.lazy_compile(r'[^-\w.+@]') _gen_file_id_suffix = None _gen_file_id_serial = 0 def _next_id_suffix(): """Create a new file id suffix that is reasonably unique. On the first call we combine the current time with 64 bits of randomness to give a highly probably globally unique number. Then each call in the same process adds 1 to a serial number we append to that unique value. """ # XXX TODO: change bzrlib.add.smart_add_tree to call workingtree.add() rather # than having to move the id randomness out of the inner loop like this. # XXX TODO: for the global randomness this uses we should add the thread-id
from bzrlib.plugins.qbzr.lib.lazycachedrevloader import (load_revisions, cached_revisions) from bzrlib.plugins.qbzr.lib.util import ( runs_in_loading_queue, format_timestamp, get_message, get_summary, open_browser, ) from bzrlib import foreign from bzrlib.plugins.qbzr.lib.uifactory import ui_current_widget from bzrlib.plugins.qbzr.lib import logmodel _email_re = lazy_regex.lazy_compile(r'([a-z0-9_\-.+]+@[a-z0-9_\-.+]+)', re.IGNORECASE) _link1_re = lazy_regex.lazy_compile( r'([\s>])(https?)://([^\s<>{}()]+[^\s.,<>{}()])', re.IGNORECASE) _link2_re = lazy_regex.lazy_compile( r'(\s)www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^ <>{}()\n\r]*[^., <>{}()\n\r]?)?)', re.IGNORECASE) _tag_re = lazy_regex.lazy_compile(r'[, ]') _start_of_line_whitespace_re = lazy_regex.lazy_compile(r'(?m)^ +') def _dummy_gpg_verify(): return False gpg_verify_available_func = getattr(gpg.GPGStrategy, "verify_signatures_available",
) directories.register_lazy('debianlp:', 'bzrlib.plugins.launchpad.lp_directory', 'LaunchpadDirectory', 'debianlp: shortcut') directories.register_lazy('ubuntu:', 'bzrlib.plugins.launchpad.lp_directory', 'LaunchpadDirectory', 'ubuntu: shortcut') _register_directory() # This is kept in __init__ so that we don't load lp_api_lite unless the branch # actually matches. That way we can avoid importing extra dependencies like # json. _package_branch = lazy_regex.lazy_compile( r'bazaar.launchpad.net.*?/' r'(?P<user>~[^/]+/)?(?P<archive>ubuntu|debian)/(?P<series>[^/]+/)?' r'(?P<project>[^/]+)(?P<branch>/[^/]+)?') def _get_package_branch_info(url): """Determine the packaging information for this URL. :return: If this isn't a packaging branch, return None. If it is, return (archive, series, project) """ if url is None: return None m = _package_branch.search(url) if m is None: return None archive, series, project, user = m.group('archive', 'series', 'project',
class RevisionSpec_date(RevisionSpec): """Selects a revision on the basis of a datestamp.""" help_txt = """Selects a revision on the basis of a datestamp. Supply a datestamp to select the first revision that matches the date. Date can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string. Matches the first entry after a given date (either at midnight or at a specified time). One way to display all the changes since yesterday would be:: bzr log -r date:yesterday.. Examples:: date:yesterday -> select the first revision since yesterday date:2006-08-14,17:10:14 -> select the first revision after August 14th, 2006 at 5:10pm. """ prefix = 'date:' _date_regex = lazy_regex.lazy_compile( r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?' r'(,|T)?\s*' r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?') def _match_on(self, branch, revs): """Spec for date revisions: date:value value can be 'yesterday', 'today', 'tomorrow' or a YYYY-MM-DD string. matches the first entry after a given date (either at midnight or at a specified time). """ # XXX: This doesn't actually work # So the proper way of saying 'give me all entries for today' is: # -r date:yesterday..date:today today = datetime.datetime.fromordinal( datetime.date.today().toordinal()) if self.spec.lower() == 'yesterday': dt = today - datetime.timedelta(days=1) elif self.spec.lower() == 'today': dt = today elif self.spec.lower() == 'tomorrow': dt = today + datetime.timedelta(days=1) else: m = self._date_regex.match(self.spec) if not m or (not m.group('date') and not m.group('time')): raise errors.InvalidRevisionSpec(self.user_spec, branch, 'invalid date') try: if m.group('date'): year = int(m.group('year')) month = int(m.group('month')) day = int(m.group('day')) else: year = today.year month = today.month day = today.day if m.group('time'): hour = int(m.group('hour')) minute = int(m.group('minute')) if m.group('second'): second = int(m.group('second')) else: second = 0 else: hour, minute, second = 0, 0, 0 except ValueError: raise errors.InvalidRevisionSpec(self.user_spec, branch, 'invalid date') dt = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second) branch.lock_read() try: rev = bisect.bisect(_RevListToTimestamps(branch), dt, 1) finally: branch.unlock() if rev == branch.revno(): raise errors.InvalidRevisionSpec(self.user_spec, branch) return RevisionInfo(branch, rev)
def test_match(self): pattern = lazy_regex.lazy_compile('fo*') self.assertIs(None, pattern.match('baz foo')) self.assertEqual('fooo', pattern.match('fooo').group())
If it is Unicode, then we need to encode it. :param a_str: An 8-bit string or Unicode as returned by cElementTree.Element.get() :return: A utf-8 encoded 8-bit string. """ # This is fairly optimized because we know what cElementTree does, this is # not meant as a generic function for all cases. Because it is possible for # an 8-bit string to not be ascii or valid utf8. if a_str.__class__ is unicode: return _encode_utf8(a_str) else: return intern(a_str) _utf8_re = lazy_regex.lazy_compile('[&<>\'\"]|[\x80-\xff]+') _unicode_re = lazy_regex.lazy_compile(u'[&<>\'\"\u0080-\uffff]') _xml_escape_map = { "&":'&', "'":"'", # FIXME: overkill "\"":""", "<":"<", ">":">", } def _unicode_escape_replace(match, _map=_xml_escape_map): """Replace a string of non-ascii, non XML safe characters with their escape
import time from bzrlib import ( config, errors, osutils, ) """) from bzrlib import ( lazy_regex, ) # the regex removes any weird characters; we don't escape them # but rather just pull them out _file_id_chars_re = lazy_regex.lazy_compile(r'[^\w.]') _rev_id_chars_re = lazy_regex.lazy_compile(r'[^-\w.+@]') _gen_file_id_suffix = None _gen_file_id_serial = 0 def _next_id_suffix(): """Create a new file id suffix that is reasonably unique. On the first call we combine the current time with 64 bits of randomness to give a highly probably globally unique number. Then each call in the same process adds 1 to a serial number we append to that unique value. """ # XXX TODO: change bzrlib.add.smart_add_tree to call workingtree.add() rather # than having to move the id randomness out of the inner loop like this. # XXX TODO: for the global randomness this uses we should add the thread-id