##// END OF EJS Templates
windows: add win32com.shell to demandimport ignore list...
windows: add win32com.shell to demandimport ignore list Module 'appdirs' tries to import win32com.shell (and catch ImportError as an indication of failure) to check whether some further functionality should be implemented one or another way [1]. Of course, demandimport lets it down, so if we want appdirs to work we have to add it to demandimport's ignore list. The reason we want appdirs to work is becuase it is used by setuptools [2] to determine egg cache location. Only fairly recent versions of setuptools depend on this so people don't see this often. [1] https://github.com/ActiveState/appdirs/blob/master/appdirs.py#L560 [2] https://github.com/pypa/setuptools/blob/aae0a928119d2a178882c32bded02270e30d0273/pkg_resources/__init__.py#L1369

File last commit:

r31886:bdda942f default
r31990:3e03a4b9 default
Show More
registrar.py
261 lines | 7.7 KiB | text/x-python | PythonLexer
FUJIWARA Katsunori
registrar: add funcregistrar class to register function for specific purpose...
r27583 # registrar.py - utilities to register function for specific purpose
#
# Copyright FUJIWARA Katsunori <foozy@lares.dti.ne.jp> and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
from . import (
Pierre-Yves David
registrar: raise a programming error on duplicated registering...
r30608 error,
Augie Fackler
registrar: make format strings unicodes and not bytes...
r30059 pycompat,
FUJIWARA Katsunori
registrar: add funcregistrar class to register function for specific purpose...
r27583 util,
)
FUJIWARA Katsunori
registrar: introduce new class for registration to replace funcregistrar...
r28392 class _funcregistrarbase(object):
Mads Kiilerich
spelling: fixes of non-dictionary words
r30332 """Base of decorator to register a function for specific purpose
FUJIWARA Katsunori
registrar: introduce new class for registration to replace funcregistrar...
r28392
This decorator stores decorated functions into own dict 'table'.
The least derived class can be defined by overriding 'formatdoc',
for example::
class keyword(_funcregistrarbase):
_docformat = ":%s: %s"
This should be used as below:
keyword = registrar.keyword()
@keyword('bar')
def barfunc(*args, **kwargs):
'''Explanation of bar keyword ....
'''
pass
In this case:
- 'barfunc' is stored as 'bar' in '_table' of an instance 'keyword' above
- 'barfunc.__doc__' becomes ":bar: Explanation of bar keyword"
"""
def __init__(self, table=None):
if table is None:
self._table = {}
else:
self._table = table
def __call__(self, decl, *args, **kwargs):
return lambda func: self._doregister(func, decl, *args, **kwargs)
def _doregister(self, func, decl, *args, **kwargs):
name = self._getname(decl)
Pierre-Yves David
registrar: raise a programming error on duplicated registering...
r30608 if name in self._table:
msg = 'duplicate registration for name: "%s"' % name
raise error.ProgrammingError(msg)
FUJIWARA Katsunori
registrar: introduce new class for registration to replace funcregistrar...
r28392 if func.__doc__ and not util.safehasattr(func, '_origdoc'):
Yuya Nishihara
py3: have registrar process docstrings in bytes...
r31820 doc = pycompat.sysbytes(func.__doc__).strip()
FUJIWARA Katsunori
registrar: introduce new class for registration to replace funcregistrar...
r28392 func._origdoc = doc
Yuya Nishihara
py3: have registrar process docstrings in bytes...
r31820 func.__doc__ = pycompat.sysstr(self._formatdoc(decl, doc))
FUJIWARA Katsunori
registrar: introduce new class for registration to replace funcregistrar...
r28392
self._table[name] = func
self._extrasetup(name, func, *args, **kwargs)
return func
def _parsefuncdecl(self, decl):
"""Parse function declaration and return the name of function in it
"""
i = decl.find('(')
if i >= 0:
return decl[:i]
else:
return decl
def _getname(self, decl):
"""Return the name of the registered function from decl
Derived class should override this, if it allows more
descriptive 'decl' string than just a name.
"""
return decl
_docformat = None
def _formatdoc(self, decl, doc):
"""Return formatted document of the registered function for help
'doc' is '__doc__.strip()' of the registered function.
"""
return self._docformat % (decl, doc)
def _extrasetup(self, name, func):
"""Execute exra setup for registered function, if needed
"""
pass
FUJIWARA Katsunori
registrar: define revsetpredicate to decorate revset predicate...
r28393
class revsetpredicate(_funcregistrarbase):
"""Decorator to register revset predicate
Usage::
revsetpredicate = registrar.revsetpredicate()
@revsetpredicate('mypredicate(arg1, arg2[, arg3])')
def mypredicatefunc(repo, subset, x):
'''Explanation of this revset predicate ....
'''
pass
The first string argument is used also in online help.
Optional argument 'safe' indicates whether a predicate is safe for
DoS attack (False by default).
Yuya Nishihara
revset: add 'takeorder' attribute to mark functions that need ordering flag...
r29933 Optional argument 'takeorder' indicates whether a predicate function
takes ordering policy as the last argument.
FUJIWARA Katsunori
registrar: define revsetpredicate to decorate revset predicate...
r28393 'revsetpredicate' instance in example above can be used to
decorate multiple functions.
Decorated functions are registered automatically at loading
extension, if an instance named as 'revsetpredicate' is used for
decorating in extension.
Otherwise, explicit 'revset.loadpredicate()' is needed.
"""
_getname = _funcregistrarbase._parsefuncdecl
Yuya Nishihara
py3: have registrar process docstrings in bytes...
r31820 _docformat = "``%s``\n %s"
FUJIWARA Katsunori
registrar: define revsetpredicate to decorate revset predicate...
r28393
Yuya Nishihara
revset: add 'takeorder' attribute to mark functions that need ordering flag...
r29933 def _extrasetup(self, name, func, safe=False, takeorder=False):
FUJIWARA Katsunori
registrar: define revsetpredicate to decorate revset predicate...
r28393 func._safe = safe
Yuya Nishihara
revset: add 'takeorder' attribute to mark functions that need ordering flag...
r29933 func._takeorder = takeorder
FUJIWARA Katsunori
registrar: add filesetpredicate to mark a function as fileset predicate...
r28447
class filesetpredicate(_funcregistrarbase):
"""Decorator to register fileset predicate
Usage::
filesetpredicate = registrar.filesetpredicate()
@filesetpredicate('mypredicate()')
def mypredicatefunc(mctx, x):
'''Explanation of this fileset predicate ....
'''
pass
The first string argument is used also in online help.
Optional argument 'callstatus' indicates whether a predicate
implies 'matchctx.status()' at runtime or not (False, by
default).
Optional argument 'callexisting' indicates whether a predicate
implies 'matchctx.existing()' at runtime or not (False, by
default).
'filesetpredicate' instance in example above can be used to
decorate multiple functions.
Decorated functions are registered automatically at loading
extension, if an instance named as 'filesetpredicate' is used for
decorating in extension.
Otherwise, explicit 'fileset.loadpredicate()' is needed.
"""
_getname = _funcregistrarbase._parsefuncdecl
Yuya Nishihara
py3: have registrar process docstrings in bytes...
r31820 _docformat = "``%s``\n %s"
FUJIWARA Katsunori
registrar: add filesetpredicate to mark a function as fileset predicate...
r28447
def _extrasetup(self, name, func, callstatus=False, callexisting=False):
func._callstatus = callstatus
func._callexisting = callexisting
FUJIWARA Katsunori
registrar: add templatekeyword to mark a function as template keyword (API)...
r28538
class _templateregistrarbase(_funcregistrarbase):
"""Base of decorator to register functions as template specific one
"""
Yuya Nishihara
py3: have registrar process docstrings in bytes...
r31820 _docformat = ":%s: %s"
FUJIWARA Katsunori
registrar: add templatekeyword to mark a function as template keyword (API)...
r28538
class templatekeyword(_templateregistrarbase):
"""Decorator to register template keyword
Usage::
Mads Kiilerich
spelling: fixes of non-dictionary words
r30332 templatekeyword = registrar.templatekeyword()
FUJIWARA Katsunori
registrar: add templatekeyword to mark a function as template keyword (API)...
r28538
@templatekeyword('mykeyword')
def mykeywordfunc(repo, ctx, templ, cache, revcache, **args):
'''Explanation of this template keyword ....
'''
pass
The first string argument is used also in online help.
'templatekeyword' instance in example above can be used to
decorate multiple functions.
Decorated functions are registered automatically at loading
extension, if an instance named as 'templatekeyword' is used for
decorating in extension.
Otherwise, explicit 'templatekw.loadkeyword()' is needed.
"""
FUJIWARA Katsunori
registrar: add templatefilter to mark a function as template filter (API)...
r28692
class templatefilter(_templateregistrarbase):
"""Decorator to register template filer
Usage::
templatefilter = registrar.templatefilter()
@templatefilter('myfilter')
def myfilterfunc(text):
'''Explanation of this template filter ....
'''
pass
The first string argument is used also in online help.
'templatefilter' instance in example above can be used to
decorate multiple functions.
Decorated functions are registered automatically at loading
extension, if an instance named as 'templatefilter' is used for
decorating in extension.
Otherwise, explicit 'templatefilters.loadkeyword()' is needed.
"""
FUJIWARA Katsunori
registrar: add templatefunc to mark a function as template function (API)...
r28695
class templatefunc(_templateregistrarbase):
"""Decorator to register template function
Usage::
templatefunc = registrar.templatefunc()
Yuya Nishihara
templater: add support for keyword arguments...
r31886 @templatefunc('myfunc(arg1, arg2[, arg3])', argspec='arg1 arg2 arg3')
FUJIWARA Katsunori
registrar: add templatefunc to mark a function as template function (API)...
r28695 def myfuncfunc(context, mapping, args):
'''Explanation of this template function ....
'''
pass
The first string argument is used also in online help.
Yuya Nishihara
templater: add support for keyword arguments...
r31886 If optional 'argspec' is defined, the function will receive 'args' as
a dict of named arguments. Otherwise 'args' is a list of positional
arguments.
FUJIWARA Katsunori
registrar: add templatefunc to mark a function as template function (API)...
r28695 'templatefunc' instance in example above can be used to
decorate multiple functions.
Decorated functions are registered automatically at loading
extension, if an instance named as 'templatefunc' is used for
decorating in extension.
Otherwise, explicit 'templater.loadfunction()' is needed.
"""
_getname = _funcregistrarbase._parsefuncdecl
Yuya Nishihara
templater: add support for keyword arguments...
r31886
def _extrasetup(self, name, func, argspec=None):
func._argspec = argspec