Coverage for src/bob/cli.py: 92%

26 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-06-16 13:47 +0200

1"""Sets up the click plugin group for packages to plug into.""" 

2 

3from importlib.metadata import entry_points 

4from pathlib import Path 

5 

6import clapper.logging 

7import click 

8import xdg 

9 

10from clapper.click import AliasedGroup, user_defaults_group 

11from clapper.rc import UserDefaults 

12from click_plugins import with_plugins 

13 

14logger = clapper.logging.setup("bob") 

15 

16 

17def legacy_rc_checker(func): 

18 home = xdg.xdg_config_home() 

19 if (Path(home) / "../.bobrc").is_file(): 

20 click.echo( 

21 "You have a legacy .bobrc file. The current configuration file " 

22 "needs to be located in ~/.config/bobrc.toml. Will now attempt to" 

23 "move it to the correct location..." 

24 ) 

25 if not (Path(home) / "bobrc.toml").is_file(): 

26 (Path(home) / "../.bobrc").rename(Path(home) / "bobrc.toml") 

27 else: 

28 click.echo( 

29 "WARNING: You have a legacy ~/.bobrc file but also a new " 

30 "~/.config/bobrc.toml. The configurations files were not " 

31 "altered. Consider removing ~/.bobrc if you don't need it." 

32 ) 

33 return func 

34 

35 

36# Back-compatibility: ~/.bobrc needs to be moved to ~/.config/bobrc.toml 

37@legacy_rc_checker 

38# HACK: entry_points in python <3.10 takes no parameter but returns a dictionary 

39# Replace with entry_points(group="bob.cli") when dropping py3.9 support. 

40@with_plugins(entry_points().get("bob.cli")) 

41@click.group( 

42 cls=AliasedGroup, 

43 context_settings={"help_option_names": ("-h", "--help")}, 

44) 

45def bob_main_cli(): 

46 """The main command line interface for bob.""" 

47 # An entry-point `bob` is created at the package level (pyproject.toml) 

48 # pointing here. 

49 # 

50 # Packages that want to insert their sub-commands in `bob` have to register 

51 # a unique entry-point in the `bob.cli` group in their pyproject.toml: 

52 # [project.entry-points."bob.cli"] 

53 # custom = "bob.custom.cli:custom_command" 

54 # (and install with `pip install -e package_location`) 

55 # 

56 # Then, calling `bob custom` will call the `custom_command` function. 

57 pass 

58 

59 

60@user_defaults_group( 

61 logger=logger, config=UserDefaults("bobrc.toml", logger=logger) 

62) 

63def bob_config(**kwargs): 

64 "Allows reading and writing into the user configuration." 

65 pass 

66 

67 

68bob_main_cli.add_command(bob_config, "config")