Coverage for src/jsoncrack_for_sphinx/config/config_utils.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.10.0, created at 2025-07-24 22:26 +0000

1""" 

2Configuration utilities for the JSONCrack Sphinx extension. 

3""" 

4 

5from typing import Any, Union 

6 

7from ..utils.types import Directions, RenderMode, Theme 

8from .config_classes import ContainerConfig, RenderConfig 

9from .config_parser import JsonCrackConfig, parse_config 

10 

11 

12def get_jsoncrack_config(app_config: Any) -> JsonCrackConfig: 

13 """Get JSONCrack configuration from Sphinx app config.""" 

14 

15 # Try to get new-style config first 

16 if hasattr(app_config, "jsoncrack_default_options"): 

17 config_dict = app_config.jsoncrack_default_options 

18 return parse_config(config_dict) 

19 

20 # Fall back to old-style config for backward compatibility 

21 # Parse render mode 

22 render_mode_str = getattr(app_config, "jsoncrack_render_mode", "onclick") 

23 render_mode: Union[RenderMode.OnClick, RenderMode.OnLoad, RenderMode.OnScreen] 

24 if render_mode_str == "onclick": 

25 render_mode = RenderMode.OnClick() 

26 elif render_mode_str == "onload": 

27 render_mode = RenderMode.OnLoad() 

28 elif render_mode_str == "onscreen": 

29 threshold = getattr(app_config, "jsoncrack_onscreen_threshold", 0.1) 

30 margin = getattr(app_config, "jsoncrack_onscreen_margin", "50px") 

31 render_mode = RenderMode.OnScreen(threshold=threshold, margin=margin) 

32 else: 

33 render_mode = RenderMode.OnClick() 

34 

35 # Parse direction 

36 direction_str = getattr(app_config, "jsoncrack_direction", "RIGHT") 

37 direction = Directions(direction_str) 

38 

39 # Parse theme 

40 theme_str = getattr(app_config, "jsoncrack_theme", None) 

41 if theme_str == "light": 

42 theme = Theme.LIGHT 

43 elif theme_str == "dark": 

44 theme = Theme.DARK 

45 else: 

46 theme = Theme.AUTO 

47 

48 # Parse container settings 

49 height = getattr(app_config, "jsoncrack_height", "500") 

50 width = getattr(app_config, "jsoncrack_width", "100%") 

51 

52 # Parse new autodoc settings 

53 disable_autodoc = getattr(app_config, "jsoncrack_disable_autodoc", False) 

54 autodoc_ignore = getattr(app_config, "jsoncrack_autodoc_ignore", []) 

55 

56 return JsonCrackConfig( 

57 render=RenderConfig(render_mode), 

58 container=ContainerConfig(direction=direction, height=height, width=width), 

59 theme=theme, 

60 disable_autodoc=disable_autodoc, 

61 autodoc_ignore=autodoc_ignore, 

62 )