Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/prompt_toolkit/__init__.py: 56%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

25 statements  

1""" 

2prompt_toolkit 

3============== 

4 

5Author: Jonathan Slenders 

6 

7Description: prompt_toolkit is a Library for building powerful interactive 

8 command lines in Python. It can be a replacement for GNU 

9 Readline, but it can be much more than that. 

10 

11See the examples directory to learn about the usage. 

12 

13Probably, to get started, you might also want to have a look at 

14`prompt_toolkit.shortcuts.prompt`. 

15""" 

16 

17from __future__ import annotations 

18 

19from typing import Any 

20 

21from .application import Application 

22from .formatted_text import ANSI, HTML 

23from .shortcuts import PromptSession, choice, print_formatted_text, prompt 

24 

25__version__: str 

26VERSION: tuple[int, int, int] 

27 

28 

29def _load_version() -> None: 

30 """ 

31 Load the package version from importlib.metadata and cache both __version__ 

32 and VERSION in the module globals. 

33 """ 

34 global __version__, VERSION 

35 

36 import re 

37 from importlib import metadata 

38 

39 # note: this is a bit more lax than the actual pep 440 to allow for a/b/rc/dev without a number 

40 pep440_pattern = ( 

41 r"^([1-9]\d*!)?(0|[1-9]\d*)(\.(0|[1-9]\d*))*" 

42 r"((a|b|rc)(0|[1-9]\d*)?)?(\.post(0|[1-9]\d*))?(\.dev(0|[1-9]\d*)?)?$" 

43 ) 

44 

45 version = metadata.version("prompt_toolkit") 

46 assert re.fullmatch(pep440_pattern, version) 

47 

48 # Version string. 

49 __version__ = version 

50 

51 # Version tuple. 

52 parts = [int(v.rstrip("abrc")) for v in version.split(".")] 

53 VERSION = (parts[0], parts[1], parts[2]) 

54 

55 

56def __getattr__(name: str) -> Any: 

57 if name in {"__version__", "VERSION"}: 

58 _load_version() 

59 return globals()[name] 

60 raise AttributeError(f"module {__name__!r} has no attribute {name!r}") 

61 

62 

63def __dir__() -> list[str]: 

64 return sorted( 

65 { 

66 *globals().keys(), 

67 "__version__", 

68 "VERSION", 

69 } 

70 ) 

71 

72 

73__all__ = [ 

74 # Application. 

75 "Application", 

76 # Shortcuts. 

77 "prompt", 

78 "choice", 

79 "PromptSession", 

80 "print_formatted_text", 

81 # Formatted text. 

82 "HTML", 

83 "ANSI", 

84 # Version info. 

85 "__version__", 

86 "VERSION", 

87]