Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templatefuncs.py @ 43077:687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Done with
python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py')
black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**')
# skip-blame mass-reformatting only
Differential Revision: https://phab.mercurial-scm.org/D6972
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:48:39 -0400 |
parents | 2372284d9457 |
children | d783f945a701 |
line wrap: on
line diff
--- a/mercurial/templatefuncs.py Sun Oct 06 09:45:02 2019 -0400 +++ b/mercurial/templatefuncs.py Sun Oct 06 09:48:39 2019 -0400 @@ -50,21 +50,21 @@ templatefunc = registrar.templatefunc(funcs) -@templatefunc('date(date[, fmt])') +@templatefunc(b'date(date[, fmt])') def date(context, mapping, args): """Format a date. See :hg:`help dates` for formatting strings. The default is a Unix date format, including the timezone: "Mon Sep 04 15:13:13 2006 0700".""" if not (1 <= len(args) <= 2): # i18n: "date" is a keyword - raise error.ParseError(_("date expects one or two arguments")) + raise error.ParseError(_(b"date expects one or two arguments")) date = evaldate( context, mapping, args[0], # i18n: "date" is a keyword - _("date expects a date information"), + _(b"date expects a date information"), ) fmt = None if len(args) == 2: @@ -75,36 +75,36 @@ return dateutil.datestr(date, fmt) -@templatefunc('dict([[key=]value...])', argspec='*args **kwargs') +@templatefunc(b'dict([[key=]value...])', argspec=b'*args **kwargs') def dict_(context, mapping, args): """Construct a dict from key-value pairs. A key may be omitted if a value expression can provide an unambiguous name.""" data = util.sortdict() - for v in args['args']: + for v in args[b'args']: k = templateutil.findsymbolicname(v) if not k: - raise error.ParseError(_('dict key cannot be inferred')) - if k in data or k in args['kwargs']: - raise error.ParseError(_("duplicated dict key '%s' inferred") % k) + raise error.ParseError(_(b'dict key cannot be inferred')) + if k in data or k in args[b'kwargs']: + raise error.ParseError(_(b"duplicated dict key '%s' inferred") % k) data[k] = evalfuncarg(context, mapping, v) data.update( (k, evalfuncarg(context, mapping, v)) - for k, v in args['kwargs'].iteritems() + for k, v in args[b'kwargs'].iteritems() ) return templateutil.hybriddict(data) @templatefunc( - 'diff([includepattern [, excludepattern]])', requires={'ctx', 'ui'} + b'diff([includepattern [, excludepattern]])', requires={b'ctx', b'ui'} ) def diff(context, mapping, args): """Show a diff, optionally specifying files to include or exclude.""" if len(args) > 2: # i18n: "diff" is a keyword - raise error.ParseError(_("diff expects zero, one, or two arguments")) + raise error.ParseError(_(b"diff expects zero, one, or two arguments")) def getpatterns(i): if i < len(args): @@ -113,76 +113,78 @@ return [s] return [] - ctx = context.resource(mapping, 'ctx') - ui = context.resource(mapping, 'ui') + ctx = context.resource(mapping, b'ctx') + ui = context.resource(mapping, b'ui') diffopts = diffutil.diffallopts(ui) chunks = ctx.diff( match=ctx.match([], getpatterns(0), getpatterns(1)), opts=diffopts ) - return ''.join(chunks) + return b''.join(chunks) -@templatefunc('extdata(source)', argspec='source', requires={'ctx', 'cache'}) +@templatefunc( + b'extdata(source)', argspec=b'source', requires={b'ctx', b'cache'} +) def extdata(context, mapping, args): """Show a text read from the specified extdata source. (EXPERIMENTAL)""" - if 'source' not in args: + if b'source' not in args: # i18n: "extdata" is a keyword - raise error.ParseError(_('extdata expects one argument')) + raise error.ParseError(_(b'extdata expects one argument')) - source = evalstring(context, mapping, args['source']) + source = evalstring(context, mapping, args[b'source']) if not source: - sym = templateutil.findsymbolicname(args['source']) + sym = templateutil.findsymbolicname(args[b'source']) if sym: raise error.ParseError( - _('empty data source specified'), - hint=_("did you mean extdata('%s')?") % sym, + _(b'empty data source specified'), + hint=_(b"did you mean extdata('%s')?") % sym, ) else: - raise error.ParseError(_('empty data source specified')) - cache = context.resource(mapping, 'cache').setdefault('extdata', {}) - ctx = context.resource(mapping, 'ctx') + raise error.ParseError(_(b'empty data source specified')) + cache = context.resource(mapping, b'cache').setdefault(b'extdata', {}) + ctx = context.resource(mapping, b'ctx') if source in cache: data = cache[source] else: data = cache[source] = scmutil.extdatasource(ctx.repo(), source) - return data.get(ctx.rev(), '') + return data.get(ctx.rev(), b'') -@templatefunc('files(pattern)', requires={'ctx'}) +@templatefunc(b'files(pattern)', requires={b'ctx'}) def files(context, mapping, args): """All files of the current changeset matching the pattern. See :hg:`help patterns`.""" if not len(args) == 1: # i18n: "files" is a keyword - raise error.ParseError(_("files expects one argument")) + raise error.ParseError(_(b"files expects one argument")) raw = evalstring(context, mapping, args[0]) - ctx = context.resource(mapping, 'ctx') + ctx = context.resource(mapping, b'ctx') m = ctx.match([raw]) files = list(ctx.matches(m)) - return templateutil.compatfileslist(context, mapping, "file", files) + return templateutil.compatfileslist(context, mapping, b"file", files) -@templatefunc('fill(text[, width[, initialident[, hangindent]]])') +@templatefunc(b'fill(text[, width[, initialident[, hangindent]]])') def fill(context, mapping, args): """Fill many paragraphs with optional indentation. See the "fill" filter.""" if not (1 <= len(args) <= 4): # i18n: "fill" is a keyword - raise error.ParseError(_("fill expects one to four arguments")) + raise error.ParseError(_(b"fill expects one to four arguments")) text = evalstring(context, mapping, args[0]) width = 76 - initindent = '' - hangindent = '' + initindent = b'' + hangindent = b'' if 2 <= len(args) <= 4: width = evalinteger( context, mapping, args[1], # i18n: "fill" is a keyword - _("fill expects an integer width"), + _(b"fill expects an integer width"), ) try: initindent = evalstring(context, mapping, args[2]) @@ -193,13 +195,13 @@ return templatefilters.fill(text, width, initindent, hangindent) -@templatefunc('filter(iterable[, expr])') +@templatefunc(b'filter(iterable[, expr])') def filter_(context, mapping, args): """Remove empty elements from a list or a dict. If expr specified, it's applied to each element to test emptiness.""" if not (1 <= len(args) <= 2): # i18n: "filter" is a keyword - raise error.ParseError(_("filter expects one or two arguments")) + raise error.ParseError(_(b"filter expects one or two arguments")) iterable = evalwrapped(context, mapping, args[0]) if len(args) == 1: @@ -210,79 +212,79 @@ def select(w): if not isinstance(w, templateutil.mappable): - raise error.ParseError(_("not filterable by expression")) + raise error.ParseError(_(b"not filterable by expression")) lm = context.overlaymap(mapping, w.tomap(context)) return evalboolean(context, lm, args[1]) return iterable.filter(context, mapping, select) -@templatefunc('formatnode(node)', requires={'ui'}) +@templatefunc(b'formatnode(node)', requires={b'ui'}) def formatnode(context, mapping, args): """Obtain the preferred form of a changeset hash. (DEPRECATED)""" if len(args) != 1: # i18n: "formatnode" is a keyword - raise error.ParseError(_("formatnode expects one argument")) + raise error.ParseError(_(b"formatnode expects one argument")) - ui = context.resource(mapping, 'ui') + ui = context.resource(mapping, b'ui') node = evalstring(context, mapping, args[0]) if ui.debugflag: return node return templatefilters.short(node) -@templatefunc('mailmap(author)', requires={'repo', 'cache'}) +@templatefunc(b'mailmap(author)', requires={b'repo', b'cache'}) def mailmap(context, mapping, args): """Return the author, updated according to the value set in the .mailmap file""" if len(args) != 1: - raise error.ParseError(_("mailmap expects one argument")) + raise error.ParseError(_(b"mailmap expects one argument")) author = evalstring(context, mapping, args[0]) - cache = context.resource(mapping, 'cache') - repo = context.resource(mapping, 'repo') + cache = context.resource(mapping, b'cache') + repo = context.resource(mapping, b'repo') - if 'mailmap' not in cache: - data = repo.wvfs.tryread('.mailmap') - cache['mailmap'] = stringutil.parsemailmap(data) + if b'mailmap' not in cache: + data = repo.wvfs.tryread(b'.mailmap') + cache[b'mailmap'] = stringutil.parsemailmap(data) - return stringutil.mapname(cache['mailmap'], author) + return stringutil.mapname(cache[b'mailmap'], author) @templatefunc( - 'pad(text, width[, fillchar=\' \'[, left=False[, truncate=False]]])', - argspec='text width fillchar left truncate', + b'pad(text, width[, fillchar=\' \'[, left=False[, truncate=False]]])', + argspec=b'text width fillchar left truncate', ) def pad(context, mapping, args): """Pad text with a fill character.""" - if 'text' not in args or 'width' not in args: + if b'text' not in args or b'width' not in args: # i18n: "pad" is a keyword - raise error.ParseError(_("pad() expects two to four arguments")) + raise error.ParseError(_(b"pad() expects two to four arguments")) width = evalinteger( context, mapping, - args['width'], + args[b'width'], # i18n: "pad" is a keyword - _("pad() expects an integer width"), + _(b"pad() expects an integer width"), ) - text = evalstring(context, mapping, args['text']) + text = evalstring(context, mapping, args[b'text']) truncate = False left = False - fillchar = ' ' - if 'fillchar' in args: - fillchar = evalstring(context, mapping, args['fillchar']) + fillchar = b' ' + if b'fillchar' in args: + fillchar = evalstring(context, mapping, args[b'fillchar']) if len(color.stripeffects(fillchar)) != 1: # i18n: "pad" is a keyword - raise error.ParseError(_("pad() expects a single fill character")) - if 'left' in args: - left = evalboolean(context, mapping, args['left']) - if 'truncate' in args: - truncate = evalboolean(context, mapping, args['truncate']) + raise error.ParseError(_(b"pad() expects a single fill character")) + if b'left' in args: + left = evalboolean(context, mapping, args[b'left']) + if b'truncate' in args: + truncate = evalboolean(context, mapping, args[b'truncate']) fillwidth = width - encoding.colwidth(color.stripeffects(text)) if fillwidth < 0 and truncate: @@ -295,7 +297,7 @@ return text + fillchar * fillwidth -@templatefunc('indent(text, indentchars[, firstline])') +@templatefunc(b'indent(text, indentchars[, firstline])') def indent(context, mapping, args): """Indents all non-empty lines with the characters given in the indentchars string. An optional @@ -303,7 +305,7 @@ if present.""" if not (2 <= len(args) <= 3): # i18n: "indent" is a keyword - raise error.ParseError(_("indent() expects two or three arguments")) + raise error.ParseError(_(b"indent() expects two or three arguments")) text = evalstring(context, mapping, args[0]) indent = evalstring(context, mapping, args[1]) @@ -317,14 +319,14 @@ return templatefilters.indent(firstline + text, indent) -@templatefunc('get(dict, key)') +@templatefunc(b'get(dict, key)') def get(context, mapping, args): """Get an attribute/key from an object. Some keywords are complex types. This function allows you to obtain the value of an attribute on these types.""" if len(args) != 2: # i18n: "get" is a keyword - raise error.ParseError(_("get() expects two arguments")) + raise error.ParseError(_(b"get() expects two arguments")) dictarg = evalwrapped(context, mapping, args[0]) key = evalrawexp(context, mapping, args[1]) @@ -332,34 +334,34 @@ return dictarg.getmember(context, mapping, key) except error.ParseError as err: # i18n: "get" is a keyword - hint = _("get() expects a dict as first argument") + hint = _(b"get() expects a dict as first argument") raise error.ParseError(bytes(err), hint=hint) -@templatefunc('config(section, name[, default])', requires={'ui'}) +@templatefunc(b'config(section, name[, default])', requires={b'ui'}) def config(context, mapping, args): """Returns the requested hgrc config option as a string.""" - fn = context.resource(mapping, 'ui').config + fn = context.resource(mapping, b'ui').config return _config(context, mapping, args, fn, evalstring) -@templatefunc('configbool(section, name[, default])', requires={'ui'}) +@templatefunc(b'configbool(section, name[, default])', requires={b'ui'}) def configbool(context, mapping, args): """Returns the requested hgrc config option as a boolean.""" - fn = context.resource(mapping, 'ui').configbool + fn = context.resource(mapping, b'ui').configbool return _config(context, mapping, args, fn, evalboolean) -@templatefunc('configint(section, name[, default])', requires={'ui'}) +@templatefunc(b'configint(section, name[, default])', requires={b'ui'}) def configint(context, mapping, args): """Returns the requested hgrc config option as an integer.""" - fn = context.resource(mapping, 'ui').configint + fn = context.resource(mapping, b'ui').configint return _config(context, mapping, args, fn, evalinteger) def _config(context, mapping, args, configfn, defaultfn): if not (2 <= len(args) <= 3): - raise error.ParseError(_("config expects two or three arguments")) + raise error.ParseError(_(b"config expects two or three arguments")) # The config option can come from any section, though we specifically # reserve the [templateconfig] section for dynamically defining options @@ -373,13 +375,13 @@ return configfn(section, name) -@templatefunc('if(expr, then[, else])') +@templatefunc(b'if(expr, then[, else])') def if_(context, mapping, args): """Conditionally execute based on the result of an expression.""" if not (2 <= len(args) <= 3): # i18n: "if" is a keyword - raise error.ParseError(_("if expects two or three arguments")) + raise error.ParseError(_(b"if expects two or three arguments")) test = evalboolean(context, mapping, args[0]) if test: @@ -388,13 +390,13 @@ return evalrawexp(context, mapping, args[2]) -@templatefunc('ifcontains(needle, haystack, then[, else])') +@templatefunc(b'ifcontains(needle, haystack, then[, else])') def ifcontains(context, mapping, args): """Conditionally execute based on whether the item "needle" is in "haystack".""" if not (3 <= len(args) <= 4): # i18n: "ifcontains" is a keyword - raise error.ParseError(_("ifcontains expects three or four arguments")) + raise error.ParseError(_(b"ifcontains expects three or four arguments")) haystack = evalwrapped(context, mapping, args[1]) try: @@ -409,13 +411,13 @@ return evalrawexp(context, mapping, args[3]) -@templatefunc('ifeq(expr1, expr2, then[, else])') +@templatefunc(b'ifeq(expr1, expr2, then[, else])') def ifeq(context, mapping, args): """Conditionally execute based on whether 2 items are equivalent.""" if not (3 <= len(args) <= 4): # i18n: "ifeq" is a keyword - raise error.ParseError(_("ifeq expects three or four arguments")) + raise error.ParseError(_(b"ifeq expects three or four arguments")) test = evalstring(context, mapping, args[0]) match = evalstring(context, mapping, args[1]) @@ -425,30 +427,30 @@ return evalrawexp(context, mapping, args[3]) -@templatefunc('join(list, sep)') +@templatefunc(b'join(list, sep)') def join(context, mapping, args): """Join items in a list with a delimiter.""" if not (1 <= len(args) <= 2): # i18n: "join" is a keyword - raise error.ParseError(_("join expects one or two arguments")) + raise error.ParseError(_(b"join expects one or two arguments")) joinset = evalwrapped(context, mapping, args[0]) - joiner = " " + joiner = b" " if len(args) > 1: joiner = evalstring(context, mapping, args[1]) return joinset.join(context, mapping, joiner) -@templatefunc('label(label, expr)', requires={'ui'}) +@templatefunc(b'label(label, expr)', requires={b'ui'}) def label(context, mapping, args): """Apply a label to generated content. Content with a label applied can result in additional post-processing, such as automatic colorization.""" if len(args) != 2: # i18n: "label" is a keyword - raise error.ParseError(_("label expects two arguments")) + raise error.ParseError(_(b"label expects two arguments")) - ui = context.resource(mapping, 'ui') + ui = context.resource(mapping, b'ui') thing = evalstring(context, mapping, args[1]) # preserve unknown symbol as literal so effects like 'red', 'bold', # etc. don't need to be quoted @@ -457,7 +459,7 @@ return ui.label(thing, label) -@templatefunc('latesttag([pattern])') +@templatefunc(b'latesttag([pattern])') def latesttag(context, mapping, args): """The global tags matching the given pattern on the most recent globally tagged ancestor of this changeset. @@ -467,7 +469,7 @@ """ if len(args) > 1: # i18n: "latesttag" is a keyword - raise error.ParseError(_("latesttag expects at most one argument")) + raise error.ParseError(_(b"latesttag expects at most one argument")) pattern = None if len(args) == 1: @@ -475,20 +477,20 @@ return templatekw.showlatesttags(context, mapping, pattern) -@templatefunc('localdate(date[, tz])') +@templatefunc(b'localdate(date[, tz])') def localdate(context, mapping, args): """Converts a date to the specified timezone. The default is local date.""" if not (1 <= len(args) <= 2): # i18n: "localdate" is a keyword - raise error.ParseError(_("localdate expects one or two arguments")) + raise error.ParseError(_(b"localdate expects one or two arguments")) date = evaldate( context, mapping, args[0], # i18n: "localdate" is a keyword - _("localdate expects a date information"), + _(b"localdate expects a date information"), ) if len(args) >= 2: tzoffset = None @@ -502,50 +504,50 @@ tzoffset = int(tz) except (TypeError, ValueError): # i18n: "localdate" is a keyword - raise error.ParseError(_("localdate expects a timezone")) + raise error.ParseError(_(b"localdate expects a timezone")) else: tzoffset = dateutil.makedate()[1] return templateutil.date((date[0], tzoffset)) -@templatefunc('max(iterable)') +@templatefunc(b'max(iterable)') def max_(context, mapping, args, **kwargs): """Return the max of an iterable""" if len(args) != 1: # i18n: "max" is a keyword - raise error.ParseError(_("max expects one argument")) + raise error.ParseError(_(b"max expects one argument")) iterable = evalwrapped(context, mapping, args[0]) try: return iterable.getmax(context, mapping) except error.ParseError as err: # i18n: "max" is a keyword - hint = _("max first argument should be an iterable") + hint = _(b"max first argument should be an iterable") raise error.ParseError(bytes(err), hint=hint) -@templatefunc('min(iterable)') +@templatefunc(b'min(iterable)') def min_(context, mapping, args, **kwargs): """Return the min of an iterable""" if len(args) != 1: # i18n: "min" is a keyword - raise error.ParseError(_("min expects one argument")) + raise error.ParseError(_(b"min expects one argument")) iterable = evalwrapped(context, mapping, args[0]) try: return iterable.getmin(context, mapping) except error.ParseError as err: # i18n: "min" is a keyword - hint = _("min first argument should be an iterable") + hint = _(b"min first argument should be an iterable") raise error.ParseError(bytes(err), hint=hint) -@templatefunc('mod(a, b)') +@templatefunc(b'mod(a, b)') def mod(context, mapping, args): """Calculate a mod b such that a / b + a mod b == a""" if not len(args) == 2: # i18n: "mod" is a keyword - raise error.ParseError(_("mod expects two arguments")) + raise error.ParseError(_(b"mod expects two arguments")) func = lambda a, b: a % b return templateutil.runarithmetic( @@ -553,69 +555,70 @@ ) -@templatefunc('obsfateoperations(markers)') +@templatefunc(b'obsfateoperations(markers)') def obsfateoperations(context, mapping, args): """Compute obsfate related information based on markers (EXPERIMENTAL)""" if len(args) != 1: # i18n: "obsfateoperations" is a keyword - raise error.ParseError(_("obsfateoperations expects one argument")) + raise error.ParseError(_(b"obsfateoperations expects one argument")) markers = evalfuncarg(context, mapping, args[0]) try: data = obsutil.markersoperations(markers) - return templateutil.hybridlist(data, name='operation') + return templateutil.hybridlist(data, name=b'operation') except (TypeError, KeyError): # i18n: "obsfateoperations" is a keyword - errmsg = _("obsfateoperations first argument should be an iterable") + errmsg = _(b"obsfateoperations first argument should be an iterable") raise error.ParseError(errmsg) -@templatefunc('obsfatedate(markers)') +@templatefunc(b'obsfatedate(markers)') def obsfatedate(context, mapping, args): """Compute obsfate related information based on markers (EXPERIMENTAL)""" if len(args) != 1: # i18n: "obsfatedate" is a keyword - raise error.ParseError(_("obsfatedate expects one argument")) + raise error.ParseError(_(b"obsfatedate expects one argument")) markers = evalfuncarg(context, mapping, args[0]) try: # TODO: maybe this has to be a wrapped list of date wrappers? data = obsutil.markersdates(markers) - return templateutil.hybridlist(data, name='date', fmt='%d %d') + return templateutil.hybridlist(data, name=b'date', fmt=b'%d %d') except (TypeError, KeyError): # i18n: "obsfatedate" is a keyword - errmsg = _("obsfatedate first argument should be an iterable") + errmsg = _(b"obsfatedate first argument should be an iterable") raise error.ParseError(errmsg) -@templatefunc('obsfateusers(markers)') +@templatefunc(b'obsfateusers(markers)') def obsfateusers(context, mapping, args): """Compute obsfate related information based on markers (EXPERIMENTAL)""" if len(args) != 1: # i18n: "obsfateusers" is a keyword - raise error.ParseError(_("obsfateusers expects one argument")) + raise error.ParseError(_(b"obsfateusers expects one argument")) markers = evalfuncarg(context, mapping, args[0]) try: data = obsutil.markersusers(markers) - return templateutil.hybridlist(data, name='user') + return templateutil.hybridlist(data, name=b'user') except (TypeError, KeyError, ValueError): # i18n: "obsfateusers" is a keyword msg = _( - "obsfateusers first argument should be an iterable of " "obsmakers" + b"obsfateusers first argument should be an iterable of " + b"obsmakers" ) raise error.ParseError(msg) -@templatefunc('obsfateverb(successors, markers)') +@templatefunc(b'obsfateverb(successors, markers)') def obsfateverb(context, mapping, args): """Compute obsfate related information based on successors (EXPERIMENTAL)""" if len(args) != 2: # i18n: "obsfateverb" is a keyword - raise error.ParseError(_("obsfateverb expects two arguments")) + raise error.ParseError(_(b"obsfateverb expects two arguments")) successors = evalfuncarg(context, mapping, args[0]) markers = evalfuncarg(context, mapping, args[1]) @@ -624,33 +627,33 @@ return obsutil.obsfateverb(successors, markers) except TypeError: # i18n: "obsfateverb" is a keyword - errmsg = _("obsfateverb first argument should be countable") + errmsg = _(b"obsfateverb first argument should be countable") raise error.ParseError(errmsg) -@templatefunc('relpath(path)', requires={'repo'}) +@templatefunc(b'relpath(path)', requires={b'repo'}) def relpath(context, mapping, args): """Convert a repository-absolute path into a filesystem path relative to the current working directory.""" if len(args) != 1: # i18n: "relpath" is a keyword - raise error.ParseError(_("relpath expects one argument")) + raise error.ParseError(_(b"relpath expects one argument")) - repo = context.resource(mapping, 'repo') + repo = context.resource(mapping, b'repo') path = evalstring(context, mapping, args[0]) return repo.pathto(path) -@templatefunc('revset(query[, formatargs...])', requires={'repo', 'cache'}) +@templatefunc(b'revset(query[, formatargs...])', requires={b'repo', b'cache'}) def revset(context, mapping, args): """Execute a revision set query. See :hg:`help revset`.""" if not len(args) > 0: # i18n: "revset" is a keyword - raise error.ParseError(_("revset expects one or more arguments")) + raise error.ParseError(_(b"revset expects one or more arguments")) raw = evalstring(context, mapping, args[0]) - repo = context.resource(mapping, 'repo') + repo = context.resource(mapping, b'repo') def query(expr): m = revsetmod.match(repo.ui, expr, lookup=revsetmod.lookupfn(repo)) @@ -660,30 +663,30 @@ formatargs = [evalfuncarg(context, mapping, a) for a in args[1:]] revs = query(revsetlang.formatspec(raw, *formatargs)) else: - cache = context.resource(mapping, 'cache') - revsetcache = cache.setdefault("revsetcache", {}) + cache = context.resource(mapping, b'cache') + revsetcache = cache.setdefault(b"revsetcache", {}) if raw in revsetcache: revs = revsetcache[raw] else: revs = query(raw) revsetcache[raw] = revs - return templatekw.showrevslist(context, mapping, "revision", revs) + return templatekw.showrevslist(context, mapping, b"revision", revs) -@templatefunc('rstdoc(text, style)') +@templatefunc(b'rstdoc(text, style)') def rstdoc(context, mapping, args): """Format reStructuredText.""" if len(args) != 2: # i18n: "rstdoc" is a keyword - raise error.ParseError(_("rstdoc expects two arguments")) + raise error.ParseError(_(b"rstdoc expects two arguments")) text = evalstring(context, mapping, args[0]) style = evalstring(context, mapping, args[1]) - return minirst.format(text, style=style, keep=['verbose']) + return minirst.format(text, style=style, keep=[b'verbose']) -@templatefunc('search(pattern, text)') +@templatefunc(b'search(pattern, text)') def search(context, mapping, args): """Look for the first text matching the regular expression pattern. Groups are accessible as ``{1}``, ``{2}``, ... in %-mapped template.""" @@ -707,7 +710,7 @@ # i18n: "search" is a keyword _(b'invalid group %(group)s in search pattern: %(pat)s') % { - b'group': b', '.join("'%s'" % g for g in sorted(badgroups)), + b'group': b', '.join(b"'%s'" % g for g in sorted(badgroups)), b'pat': pat, } ) @@ -722,16 +725,16 @@ return templateutil.mappingdict(lm, tmpl=b'{0}') -@templatefunc('separate(sep, args...)', argspec='sep *args') +@templatefunc(b'separate(sep, args...)', argspec=b'sep *args') def separate(context, mapping, args): """Add a separator between non-empty arguments.""" - if 'sep' not in args: + if b'sep' not in args: # i18n: "separate" is a keyword - raise error.ParseError(_("separate expects at least one argument")) + raise error.ParseError(_(b"separate expects at least one argument")) - sep = evalstring(context, mapping, args['sep']) + sep = evalstring(context, mapping, args[b'sep']) first = True - for arg in args['args']: + for arg in args[b'args']: argstr = evalstring(context, mapping, arg) if not argstr: continue @@ -742,13 +745,13 @@ yield argstr -@templatefunc('shortest(node, minlength=4)', requires={'repo', 'cache'}) +@templatefunc(b'shortest(node, minlength=4)', requires={b'repo', b'cache'}) def shortest(context, mapping, args): """Obtain the shortest representation of a node.""" if not (1 <= len(args) <= 2): # i18n: "shortest" is a keyword - raise error.ParseError(_("shortest() expects one or two arguments")) + raise error.ParseError(_(b"shortest() expects one or two arguments")) hexnode = evalstring(context, mapping, args[0]) @@ -759,10 +762,10 @@ mapping, args[1], # i18n: "shortest" is a keyword - _("shortest() expects an integer minlength"), + _(b"shortest() expects an integer minlength"), ) - repo = context.resource(mapping, 'repo') + repo = context.resource(mapping, b'repo') if len(hexnode) > 40: return hexnode elif len(hexnode) == 40: @@ -779,20 +782,20 @@ return hexnode if not node: return hexnode - cache = context.resource(mapping, 'cache') + cache = context.resource(mapping, b'cache') try: return scmutil.shortesthexnodeidprefix(repo, node, minlength, cache) except error.RepoLookupError: return hexnode -@templatefunc('strip(text[, chars])') +@templatefunc(b'strip(text[, chars])') def strip(context, mapping, args): """Strip characters from a string. By default, strips all leading and trailing whitespace.""" if not (1 <= len(args) <= 2): # i18n: "strip" is a keyword - raise error.ParseError(_("strip expects one or two arguments")) + raise error.ParseError(_(b"strip expects one or two arguments")) text = evalstring(context, mapping, args[0]) if len(args) == 2: @@ -801,13 +804,13 @@ return text.strip() -@templatefunc('sub(pattern, replacement, expression)') +@templatefunc(b'sub(pattern, replacement, expression)') def sub(context, mapping, args): """Perform text substitution using regular expressions.""" if len(args) != 3: # i18n: "sub" is a keyword - raise error.ParseError(_("sub expects three arguments")) + raise error.ParseError(_(b"sub expects three arguments")) pat = evalstring(context, mapping, args[0]) rpl = evalstring(context, mapping, args[1]) @@ -816,36 +819,36 @@ patre = re.compile(pat) except re.error: # i18n: "sub" is a keyword - raise error.ParseError(_("sub got an invalid pattern: %s") % pat) + raise error.ParseError(_(b"sub got an invalid pattern: %s") % pat) try: yield patre.sub(rpl, src) except re.error: # i18n: "sub" is a keyword - raise error.ParseError(_("sub got an invalid replacement: %s") % rpl) + raise error.ParseError(_(b"sub got an invalid replacement: %s") % rpl) -@templatefunc('startswith(pattern, text)') +@templatefunc(b'startswith(pattern, text)') def startswith(context, mapping, args): """Returns the value from the "text" argument if it begins with the content from the "pattern" argument.""" if len(args) != 2: # i18n: "startswith" is a keyword - raise error.ParseError(_("startswith expects two arguments")) + raise error.ParseError(_(b"startswith expects two arguments")) patn = evalstring(context, mapping, args[0]) text = evalstring(context, mapping, args[1]) if text.startswith(patn): return text - return '' + return b'' -@templatefunc('word(number, text[, separator])') +@templatefunc(b'word(number, text[, separator])') def word(context, mapping, args): """Return the nth word from a string.""" if not (2 <= len(args) <= 3): # i18n: "word" is a keyword raise error.ParseError( - _("word expects two or three arguments, got %d") % len(args) + _(b"word expects two or three arguments, got %d") % len(args) ) num = evalinteger( @@ -853,7 +856,7 @@ mapping, args[0], # i18n: "word" is a keyword - _("word expects an integer index"), + _(b"word expects an integer index"), ) text = evalstring(context, mapping, args[1]) if len(args) == 3: @@ -863,7 +866,7 @@ tokens = text.split(splitter) if num >= len(tokens) or num < -len(tokens): - return '' + return b'' else: return tokens[num]