comparison mercurial/extensions.py @ 45942:89a2afe31e82

formating: upgrade to black 20.8b1 This required a couple of small tweaks to un-confuse black, but now it works. Big formatting changes come from: * Dramatically improved collection-splitting logic upstream * Black having a strong (correct IMO) opinion that """ is better than ''' Differential Revision: https://phab.mercurial-scm.org/D9430
author Augie Fackler <raf@durin42.com>
date Fri, 27 Nov 2020 17:03:29 -0500
parents f96059fa519c
children 3db545fccac1
comparison
equal deleted inserted replaced
45941:346af7687c6f 45942:89a2afe31e82
455 if extraobj is not None: 455 if extraobj is not None:
456 getattr(loadermod, loadername)(ui, name, extraobj) 456 getattr(loadermod, loadername)(ui, name, extraobj)
457 457
458 458
459 def afterloaded(extension, callback): 459 def afterloaded(extension, callback):
460 '''Run the specified function after a named extension is loaded. 460 """Run the specified function after a named extension is loaded.
461 461
462 If the named extension is already loaded, the callback will be called 462 If the named extension is already loaded, the callback will be called
463 immediately. 463 immediately.
464 464
465 If the named extension never loads, the callback will be called after 465 If the named extension never loads, the callback will be called after
466 all extensions have been loaded. 466 all extensions have been loaded.
467 467
468 The callback receives the named argument ``loaded``, which is a boolean 468 The callback receives the named argument ``loaded``, which is a boolean
469 indicating whether the dependent extension actually loaded. 469 indicating whether the dependent extension actually loaded.
470 ''' 470 """
471 471
472 if extension in _extensions: 472 if extension in _extensions:
473 # Report loaded as False if the extension is disabled 473 # Report loaded as False if the extension is disabled
474 loaded = _extensions[extension] is not None 474 loaded = _extensions[extension] is not None
475 callback(loaded=loaded) 475 callback(loaded=loaded)
498 % (name, stringutil.forcebytestr(inst)) 498 % (name, stringutil.forcebytestr(inst))
499 ) 499 )
500 500
501 501
502 def bind(func, *args): 502 def bind(func, *args):
503 '''Partial function application 503 """Partial function application
504 504
505 Returns a new function that is the partial application of args and kwargs 505 Returns a new function that is the partial application of args and kwargs
506 to func. For example, 506 to func. For example,
507 507
508 f(1, 2, bar=3) === bind(f, 1)(2, bar=3)''' 508 f(1, 2, bar=3) === bind(f, 1)(2, bar=3)"""
509 assert callable(func) 509 assert callable(func)
510 510
511 def closure(*a, **kw): 511 def closure(*a, **kw):
512 return func(*(args + a), **kw) 512 return func(*(args + a), **kw)
513 513
616 def __exit__(self, exctype, excvalue, traceback): 616 def __exit__(self, exctype, excvalue, traceback):
617 unwrapfunction(self._container, self._funcname, self._wrapper) 617 unwrapfunction(self._container, self._funcname, self._wrapper)
618 618
619 619
620 def wrapfunction(container, funcname, wrapper): 620 def wrapfunction(container, funcname, wrapper):
621 '''Wrap the function named funcname in container 621 """Wrap the function named funcname in container
622 622
623 Replace the funcname member in the given container with the specified 623 Replace the funcname member in the given container with the specified
624 wrapper. The container is typically a module, class, or instance. 624 wrapper. The container is typically a module, class, or instance.
625 625
626 The wrapper will be called like 626 The wrapper will be called like
647 647
648 In general, combining wrapfunction() with subclassing does not 648 In general, combining wrapfunction() with subclassing does not
649 work. Since you cannot control what other extensions are loaded by 649 work. Since you cannot control what other extensions are loaded by
650 your end users, you should play nicely with others by using the 650 your end users, you should play nicely with others by using the
651 subclass trick. 651 subclass trick.
652 ''' 652 """
653 assert callable(wrapper) 653 assert callable(wrapper)
654 654
655 origfn = getattr(container, funcname) 655 origfn = getattr(container, funcname)
656 assert callable(origfn) 656 assert callable(origfn)
657 if inspect.ismodule(container): 657 if inspect.ismodule(container):
666 setattr(container, funcname, wrap) 666 setattr(container, funcname, wrap)
667 return origfn 667 return origfn
668 668
669 669
670 def unwrapfunction(container, funcname, wrapper=None): 670 def unwrapfunction(container, funcname, wrapper=None):
671 '''undo wrapfunction 671 """undo wrapfunction
672 672
673 If wrappers is None, undo the last wrap. Otherwise removes the wrapper 673 If wrappers is None, undo the last wrap. Otherwise removes the wrapper
674 from the chain of wrappers. 674 from the chain of wrappers.
675 675
676 Return the removed wrapper. 676 Return the removed wrapper.
677 Raise IndexError if wrapper is None and nothing to unwrap; ValueError if 677 Raise IndexError if wrapper is None and nothing to unwrap; ValueError if
678 wrapper is not None but is not found in the wrapper chain. 678 wrapper is not None but is not found in the wrapper chain.
679 ''' 679 """
680 chain = getwrapperchain(container, funcname) 680 chain = getwrapperchain(container, funcname)
681 origfn = chain.pop() 681 origfn = chain.pop()
682 if wrapper is None: 682 if wrapper is None:
683 wrapper = chain[0] 683 wrapper = chain[0]
684 chain.remove(wrapper) 684 chain.remove(wrapper)
687 wrapfunction(container, funcname, w) 687 wrapfunction(container, funcname, w)
688 return wrapper 688 return wrapper
689 689
690 690
691 def getwrapperchain(container, funcname): 691 def getwrapperchain(container, funcname):
692 '''get a chain of wrappers of a function 692 """get a chain of wrappers of a function
693 693
694 Return a list of functions: [newest wrapper, ..., oldest wrapper, origfunc] 694 Return a list of functions: [newest wrapper, ..., oldest wrapper, origfunc]
695 695
696 The wrapper functions are the ones passed to wrapfunction, whose first 696 The wrapper functions are the ones passed to wrapfunction, whose first
697 argument is origfunc. 697 argument is origfunc.
698 ''' 698 """
699 result = [] 699 result = []
700 fn = getattr(container, funcname) 700 fn = getattr(container, funcname)
701 while fn: 701 while fn:
702 assert callable(fn) 702 assert callable(fn)
703 result.append(getattr(fn, '_unboundwrapper', fn)) 703 result.append(getattr(fn, '_unboundwrapper', fn))
742 exts[name] = path 742 exts[name] = path
743 return exts 743 return exts
744 744
745 745
746 def _moduledoc(file): 746 def _moduledoc(file):
747 '''return the top-level python documentation for the given file 747 """return the top-level python documentation for the given file
748 748
749 Loosely inspired by pydoc.source_synopsis(), but rewritten to 749 Loosely inspired by pydoc.source_synopsis(), but rewritten to
750 handle triple quotes and to return the whole text instead of just 750 handle triple quotes and to return the whole text instead of just
751 the synopsis''' 751 the synopsis"""
752 result = [] 752 result = []
753 753
754 line = file.readline() 754 line = file.readline()
755 while line[:1] == b'#' or not line.strip(): 755 while line[:1] == b'#' or not line.strip():
756 line = file.readline() 756 line = file.readline()
881 doc = _disabledhelp(path) 881 doc = _disabledhelp(path)
882 return (cmd, name, doc) 882 return (cmd, name, doc)
883 883
884 884
885 def disabledcmd(ui, cmd, strict=False): 885 def disabledcmd(ui, cmd, strict=False):
886 '''find cmd from disabled extensions without importing. 886 """find cmd from disabled extensions without importing.
887 returns (cmdname, extname, doc)''' 887 returns (cmdname, extname, doc)"""
888 888
889 paths = _disabledpaths() 889 paths = _disabledpaths()
890 if not paths: 890 if not paths:
891 raise error.UnknownCommand(cmd) 891 raise error.UnknownCommand(cmd)
892 892