Coverage for src\cognitivefactory\interactive_clustering_gui\cli.py: 100.00%

13 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-22 23:23 +0100

1# -*- coding: utf-8 -*- 

2 

3""" 

4* Name: cognitivefactory.interactive_clustering_gui.cli 

5* Description: Module that contains the command line application. 

6* Author: Erwan Schild 

7* Created: 22/10/2021 

8* Licence: CeCILL-C License v1.0 (https://cecill.info/licences.fr.html) 

9 

10Why does this file exist, and why not put this in `__main__`? 

11 

12You might be tempted to import things from `__main__` later, but that will cause problems: the code will get executed twice: 

13- When you run `python -m cognitivefactory.interactive_clustering_gui` python will execute `__main__.py` as a script. That means there won't be any `cognitivefactory.interactive_clustering_gui.__main__` in `sys.modules`. 

14- When you import `__main__` it will get executed again (as a module) because there's no `cognitivefactory.interactive_clustering_gui.__main__` in `sys.modules`. 

15""" 

16 

17 

18# ============================================================================== 

19# IMPORT PYTHON DEPENDENCIES 

20# ============================================================================== 

21 

22import argparse 

23from typing import List, Optional 

24 

25from uvicorn import Config, Server 

26 

27 

28# ============================================================================== 

29# DEFINE CLI ARGUMENTS PARSER 

30# ============================================================================== 

31def get_parser() -> argparse.ArgumentParser: 

32 """ 

33 Define possible arguments of the CLI argument parser. 

34 

35 Returns: 

36 An argparse parser. 

37 """ 

38 parser = argparse.ArgumentParser( 

39 prog="cognitivefactory-interactive-clustering-gui", 

40 description="A web application designed for NLP data annotation using Interactive Clustering methodology.", 

41 epilog="For more details, https://cognitivefactory.github.io/interactive-clustering-gui/", 

42 ) 

43 parser.add_argument( 

44 "--host", 

45 type=str, 

46 default="127.0.0.1", 

47 help="The host to bind. Defaults to `127.0.0.1`.", 

48 ) 

49 parser.add_argument( 

50 "--port", 

51 type=int, 

52 default=8080, 

53 help="The port to use. Defaults to `8080`.", 

54 ) 

55 parser.add_argument( 

56 "--log-level", 

57 type=str, 

58 choices=["critical", "error", "warning", "info", "debug", "trace"], 

59 default="info", 

60 help="The log level. Defaults to `info`.", 

61 ) 

62 return parser 

63 

64 

65# ============================================================================== 

66# MAIN TO RUN WEB APPLICATION 

67# ============================================================================== 

68 

69 

70def main(args: Optional[List[str]] = None) -> int: 

71 """ 

72 Run the main program. 

73 

74 This function is executed when you type `cognitivefactory-interactive-clustering-gui` or `python -m cognitivefactory.interactive_clustering_gui`. 

75 

76 Args: 

77 args: Arguments passed from the command line. 

78 

79 Returns: 

80 A default exit code. 

81 """ 

82 # Parse CLI arguments. 

83 parser = get_parser() 

84 opts = parser.parse_args(args=args) 

85 

86 # Config the serveur. 

87 config = Config( # pragma: nocover 

88 "cognitivefactory.interactive_clustering_gui.app:app", 

89 host=opts.host, 

90 port=opts.port, 

91 log_level=opts.log_level, 

92 ) 

93 server = Server(config) # pragma: nocover 

94 

95 # Launch the server. 

96 server.run() # pragma: nocover 

97 

98 # Return a default exit code. 

99 return 0 # pragma: nocover