2
2
import sys
3
3
import json
4
4
import logging
5
+ from typing import Dict
6
+ import tempfile
5
7
6
8
import jinja2
7
9
8
- from src . grafana_dashboard .model import Model
10
+ from .model import Model
9
11
10
12
11
13
class Dashboard :
@@ -23,18 +25,17 @@ def __init__(self, dashboard_model: Model):
23
25
self .dashboard_model = dashboard_model
24
26
self .logging = logging .Logger
25
27
26
- def get_dashboard_json (self , template_values : dict ) -> dict :
27
- """The method includes a functionality to template the selected dashboard and return the corresponding dashboard
28
- as dictionary
28
+ def get_dashboard_json (self , template_values : Dict ) -> Dict :
29
+ """The method includes a functionality to template the selected dashboard and return the corresponding dashboard as dictionary
29
30
30
31
Args:
31
- template_values (dict ): Specify the inserted templating values as dict
32
+ template_values (Dict ): Specify the inserted templating values as dict
32
33
33
34
Raises:
34
- Exception: Unspecified error by executing the functionality
35
+ jinja2.TemplateNotFound: Jinja2 template not found
35
36
36
37
Returns:
37
- json_dashboard (dict ): Returns the dashboard as dict
38
+ json_dashboard (Dict ): Returns the dashboard as dict
38
39
"""
39
40
40
41
env = jinja2 .Environment (loader = jinja2 .FileSystemLoader ("/" ))
@@ -52,26 +53,59 @@ def get_dashboard_json(self, template_values: dict) -> dict:
52
53
logging .error ("Please define templating values." )
53
54
sys .exit (1 )
54
55
55
- temp_path : str = "/tmp/dashboard.json"
56
+ with tempfile .NamedTemporaryFile () as tmp_file :
57
+ self .__write_tmp_dashboard_json (
58
+ tmp_file .name , template_dashboard , template_values
59
+ )
60
+ return self .__get_dashboard_json (tmp_file .name )
61
+
62
+ @staticmethod
63
+ def __write_tmp_dashboard_json (
64
+ temp_path : str , template_dashboard : jinja2 .Template , template_values : Dict
65
+ ):
66
+ """The method includes a functionality to write a templated json of the selected dashboard to a temporary file
67
+
68
+ Args:
69
+ temp_path (str): Specify the temporary path as string
70
+ template_dashboard (jinja2.Template): Specify the Jinja2 templated dashboard
71
+ template_values (Dict): Specify the template values
72
+
73
+ Raises:
74
+ FileNotFoundError: The corresponding temporary file is not available
75
+ ValueError: There is an error inside the values
76
+ AttributeError: You missed to add a specific attribute
77
+
78
+ Returns:
79
+ None
80
+ """
56
81
57
82
try :
58
83
fw = open (temp_path , "w" )
59
84
fw .write (template_dashboard .render (template_values ))
60
85
fw .close ()
61
- except Exception as e :
86
+ except ( FileNotFoundError , ValueError , AttributeError ) as e :
62
87
logging .error (f"Please, check the error: { e } ." )
63
88
raise e
64
89
65
- try :
66
- with open (temp_path ) as file :
67
- json_dashboard = json .load (file )
68
- except Exception as e :
69
- logging .error (f"Please, check the error: { e } ." )
70
- raise e
90
+ @staticmethod
91
+ def __get_dashboard_json (temp_path : str ) -> Dict :
92
+ """The method includes a functionality to get the corresponding templated dashboard JSON as Dict
93
+
94
+ Args:
95
+ temp_path (str): Specify the temporary path as string
96
+
97
+ Raises:
98
+ FileNotFoundError: The corresponding temporary file is not available
99
+ ValueError: There is an error inside the values
100
+
101
+ Returns:
102
+ json_dashboard (Dict): Returns the dashboard JSON as dict
103
+ """
71
104
72
105
try :
73
- os .remove (temp_path )
74
- except Exception as e :
106
+ with open (temp_path ) as file :
107
+ json_dashboard : Dict = json .load (file )
108
+ except (FileNotFoundError , ValueError ) as e :
75
109
logging .error (f"Please, check the error: { e } ." )
76
110
raise e
77
111
0 commit comments