Commit 42cd9e2949e4587b0dd389d688780eeebb024b0d
1 parent
d123bb31
Exists in
dev
code source style v1 dans /doc/code_style/ (to be continued...)
Showing
2 changed files
with
239 additions
and
0 deletions
Show diff stats
No preview for this file type
... | ... | @@ -0,0 +1,238 @@ |
1 | +#!/usr/bin/env python3 | |
2 | + | |
3 | + | |
4 | +''' | |
5 | +================================================================= | |
6 | + PACKAGES IMPORT | |
7 | +================================================================= | |
8 | +''' | |
9 | + | |
10 | +# --- GENERAL PURPOSE IMPORT --- | |
11 | + | |
12 | +import pykwalify.core | |
13 | +import sys | |
14 | +import yaml, logging, os, pickle, time | |
15 | +from datetime import datetime | |
16 | +from pykwalify.errors import PyKwalifyException,SchemaError | |
17 | +from pathlib import Path | |
18 | + | |
19 | +# --- PROJECT SPECIFIC IMPORT --- | |
20 | + | |
21 | +from django.conf import settings as djangosettings | |
22 | +from common.models import AgentSurvey, AgentCmd, AgentLogs | |
23 | +from src.core.pyros_django.obsconfig.configpyros import ConfigPyros | |
24 | +from device_controller.abstract_component.device_controller import ( | |
25 | + DCCNotFoundException, UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException | |
26 | +) | |
27 | + | |
28 | + | |
29 | + | |
30 | +''' | |
31 | +================================================================= | |
32 | + GENERAL MODULE CONSTANTS & FUNCTIONS DEFINITIONS | |
33 | +================================================================= | |
34 | +''' | |
35 | + | |
36 | +# - General constants | |
37 | + | |
38 | +DEBUG = False | |
39 | + | |
40 | +IS_WINDOWS = platform.system() == "Windows" | |
41 | + | |
42 | + | |
43 | +# - General Functions | |
44 | + | |
45 | +def general_function1(arg_a: int=1, arg_b: str='toto', arg_c: float, arg_d: bool) -> float: | |
46 | + ''' | |
47 | + This function is used for ... blabla ... | |
48 | + | |
49 | + Args: | |
50 | + arg_a: the path of the file to wrap | |
51 | + arg_b: instance to wrap | |
52 | + arg_c: toto | |
53 | + arg_d: whether or not to delete the file when the File instance is destructed | |
54 | + | |
55 | + Returns: | |
56 | + A buffered writable file descriptor | |
57 | + | |
58 | + Raises: | |
59 | + AttributeError: The ``Raises`` section is a list of all exceptions | |
60 | + that are relevant to the interface. | |
61 | + ValueError: If `arg_a` is equal to `arg_b`. | |
62 | + ''' | |
63 | + | |
64 | + # comment on a | |
65 | + a = 1 | |
66 | + | |
67 | + # comment on b | |
68 | + b = 2 | |
69 | + | |
70 | + return 3.5 | |
71 | + | |
72 | + | |
73 | +def general_function2(a: int, b:int): | |
74 | + ''' | |
75 | + Commentaires | |
76 | + ''' | |
77 | + a = 1 | |
78 | + b = 2 | |
79 | + | |
80 | + return 3 | |
81 | + | |
82 | + | |
83 | + | |
84 | +''' | |
85 | +================================================================= | |
86 | + CLASS MyFirstClass | |
87 | +================================================================= | |
88 | +''' | |
89 | + | |
90 | +class MyFirstClass: | |
91 | + ''' | |
92 | + General Comment on the class | |
93 | + bla | |
94 | + bla | |
95 | + ''' | |
96 | + | |
97 | + # The class attributes | |
98 | + | |
99 | + # comment on my_attr1 | |
100 | + my_attr1 = {} | |
101 | + current_file = None | |
102 | + pickle_file = "obsconfig.p" | |
103 | + obs_config = None | |
104 | + devices = None | |
105 | + computers = None | |
106 | + agents = None | |
107 | + obs_config_file_content = None | |
108 | + errors = None | |
109 | + | |
110 | + | |
111 | + # The class methods | |
112 | + | |
113 | + def __init__(self, a: int, b: float) -> int: | |
114 | + ''' | |
115 | + Commentaire sur my_method1" | |
116 | + | |
117 | + Args: | |
118 | + a: blabla | |
119 | + | |
120 | + Returns: | |
121 | + bool: [description] | |
122 | + ''' | |
123 | + | |
124 | + c = 1 | |
125 | + d = 2 | |
126 | + | |
127 | + return False | |
128 | + | |
129 | + | |
130 | + def my_method2(self, a: int, b: float): | |
131 | + a = 1 | |
132 | + b = 2 | |
133 | + | |
134 | + | |
135 | +#TODO: *args et **all_kwargs | |
136 | + | |
137 | + def check_and_return_config(self, yaml_file: str, schema_file: str) -> dict: | |
138 | + ''' | |
139 | + Check if yaml_file is valid for the schema_file and return an dictionary of the config file | |
140 | + | |
141 | + Args: | |
142 | + yaml_file: Path to the config_file to be validated | |
143 | + schema_file: Path to the schema file | |
144 | + | |
145 | + Returns: | |
146 | + dict: dictionary of the config file (with values) | |
147 | + ''' | |
148 | + # disable pykwalify error to clean the output | |
149 | + #####logging.disable(logging.ERROR) | |
150 | + try: | |
151 | + can_yaml_file_be_read = False | |
152 | + while can_yaml_file_be_read != True: | |
153 | + if os.access(yaml_file, os.R_OK): | |
154 | + can_yaml_file_be_read = True | |
155 | + else: | |
156 | + print(f"{yaml_file} can't be accessed, waiting for availability") | |
157 | + time.sleep(0.5) | |
158 | + | |
159 | + c = pykwalify.core.Core(source_file=yaml_file, schema_files=[self.SCHEMA_PATH+schema_file]) | |
160 | + return c.validate(raise_exception=True) | |
161 | + except SchemaError: | |
162 | + for error in c.errors: | |
163 | + print("Error :",str(error).split(". Path")[0]) | |
164 | + print("Path to error :",error.path) | |
165 | + self.errors = c.errors | |
166 | + return None | |
167 | + except IOError: | |
168 | + print("Error when reading the observatory config file") | |
169 | + | |
170 | + | |
171 | + #TODO: @classmethod (avec cls) | |
172 | + @staticmethod | |
173 | + def check_config(yaml_file: str, schema_file: str) -> any: | |
174 | + """ | |
175 | + Check if yaml_file is valid for the schema_file and return a boolean or list of errors according the schema | |
176 | + | |
177 | + Args: | |
178 | + yaml_file (str): Path to the config_file to be validated | |
179 | + schema_file (str): Path to the schema file | |
180 | + | |
181 | + Returns: | |
182 | + any: boolean (True) if the configuration is valid according the schema or a list of error otherwise | |
183 | + """ | |
184 | + # disable pykwalify error to clean the output | |
185 | + ####logging.disable(logging.ERROR) | |
186 | + try: | |
187 | + can_yaml_file_be_read = False | |
188 | + while can_yaml_file_be_read != True: | |
189 | + if os.access(yaml_file, os.R_OK): | |
190 | + can_yaml_file_be_read = True | |
191 | + else: | |
192 | + print(f"{yaml_file} can't be accessed, waiting for availability") | |
193 | + time.sleep(0.5) | |
194 | + | |
195 | + c = pykwalify.core.Core(source_file=yaml_file, schema_files=[schema_file]) | |
196 | + c.validate(raise_exception=True) | |
197 | + return True | |
198 | + except SchemaError: | |
199 | + for error in c.errors: | |
200 | + print("Error :",str(error).split(". Path")[0]) | |
201 | + print("Path to error :",error.path) | |
202 | + | |
203 | + return c.errors | |
204 | + except IOError: | |
205 | + print("Error when reading the observatory config file") | |
206 | + | |
207 | + | |
208 | + | |
209 | +class MySecondClass: | |
210 | + ''' General Comment on the class ''' | |
211 | + pass | |
212 | + | |
213 | + | |
214 | +#TODO: Exceptions custom | |
215 | +#TODO: générer doc exemple avec Sphinx | |
216 | + | |
217 | + | |
218 | + | |
219 | +''' | |
220 | +================================================================= | |
221 | + Main function | |
222 | +================================================================= | |
223 | +''' | |
224 | +def main(): | |
225 | + a = 1 | |
226 | + b = 2 | |
227 | + c = 3 | |
228 | + | |
229 | + | |
230 | + | |
231 | +''' | |
232 | +================================================================= | |
233 | + Exec of main function | |
234 | +================================================================= | |
235 | +''' | |
236 | +if __name__ == "__main__": | |
237 | + | |
238 | + main() | ... | ... |