Blame view

doc/code_style/package1_name/module1.py 6.03 KB
42cd9e29   Etienne Pallier   code source style...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3


'''
=================================================================
    PACKAGES IMPORT
=================================================================
'''

# --- GENERAL PURPOSE IMPORT ---

import pykwalify.core
import sys
import yaml, logging, os, pickle, time
from datetime import datetime
from pykwalify.errors import PyKwalifyException,SchemaError
from pathlib import Path

# --- PROJECT SPECIFIC IMPORT ---

from django.conf import settings as djangosettings
from common.models import AgentSurvey, AgentCmd, AgentLogs
from src.core.pyros_django.obsconfig.configpyros import ConfigPyros
from device_controller.abstract_component.device_controller import (
    DCCNotFoundException, UnknownGenericCmdException, UnimplementedGenericCmdException, UnknownNativeCmdException
)



'''
=================================================================
    GENERAL MODULE CONSTANTS & FUNCTIONS DEFINITIONS
=================================================================
'''

# - General constants

DEBUG = False

IS_WINDOWS = platform.system() == "Windows"


# - General Functions

def general_function1(arg_a: int=1, arg_b: str='toto', arg_c: float, arg_d: bool) -> float:
    '''
    This function is used for ... blabla ...
    
    Args:
        arg_a: the path of the file to wrap
        arg_b: instance to wrap
        arg_c: toto
        arg_d: whether or not to delete the file when the File instance is destructed

    Returns:
        A buffered writable file descriptor
    
    Raises:
        AttributeError: The ``Raises`` section is a list of all exceptions
            that are relevant to the interface.
        ValueError: If `arg_a` is equal to `arg_b`.
    '''
    
    # comment on a
    a = 1
    
    # comment on b
    b = 2

    return 3.5


def general_function2(a: int, b:int):
    '''
        Commentaires
    '''
    a = 1
    b = 2
    
    return 3



'''
=================================================================
    CLASS MyFirstClass
=================================================================
'''

class MyFirstClass:
    ''' 
    General Comment on the class 
    bla
    bla
    '''

    # The class attributes
    
    # comment on my_attr1
    my_attr1 = {}
    current_file = None
    pickle_file = "obsconfig.p"
    obs_config = None
    devices = None
    computers = None
    agents = None 
    obs_config_file_content = None
    errors = None


    # The class methods

    def __init__(self, a: int, b: float) -> int:
        '''
        Commentaire sur my_method1"

        Args:
            a: blabla

        Returns:
            bool: [description]
        '''

        c = 1
        d = 2
        
        return False


    def my_method2(self, a: int, b: float):
        a = 1
        b = 2


#TODO: *args et **all_kwargs

    def check_and_return_config(self, yaml_file: str, schema_file: str) -> dict:
        '''
        Check if yaml_file is valid for the schema_file and return an dictionary of the config file

        Args:
            yaml_file: Path to the config_file to be validated
            schema_file: Path to the schema file 

        Returns:
            dict: dictionary of the config file (with values)
        '''
        # disable pykwalify error to clean the output
        #####logging.disable(logging.ERROR)
        try:
            can_yaml_file_be_read = False
            while can_yaml_file_be_read != True:
                if os.access(yaml_file, os.R_OK):
                    can_yaml_file_be_read = True
                else:
                    print(f"{yaml_file} can't be accessed, waiting for availability")
                    time.sleep(0.5)

            c = pykwalify.core.Core(source_file=yaml_file, schema_files=[self.SCHEMA_PATH+schema_file])
            return c.validate(raise_exception=True)
        except SchemaError:
            for error in c.errors:
                print("Error :",str(error).split(". Path")[0])
                print("Path to error :",error.path)
                self.errors = c.errors
            return None
        except IOError:
            print("Error when reading the observatory config file")


    #TODO: @classmethod (avec cls)
    @staticmethod
    def check_config(yaml_file: str, schema_file: str) -> any:
        """
        Check if yaml_file is valid for the schema_file and return a boolean or list of errors according the schema

        Args:
            yaml_file (str): Path to the config_file to be validated
            schema_file (str): Path to the schema file 

        Returns:
            any: boolean (True) if the configuration is valid according the schema or a list of error otherwise
        """
        # disable pykwalify error to clean the output
        ####logging.disable(logging.ERROR)
        try:
            can_yaml_file_be_read = False
            while can_yaml_file_be_read != True:
                if os.access(yaml_file, os.R_OK):
                    can_yaml_file_be_read = True
                else:
                    print(f"{yaml_file} can't be accessed, waiting for availability")
                    time.sleep(0.5)
        
            c = pykwalify.core.Core(source_file=yaml_file, schema_files=[schema_file])
            c.validate(raise_exception=True)
            return True
        except SchemaError:
            for error in c.errors:
                print("Error :",str(error).split(". Path")[0])
                print("Path to error :",error.path)
                
            return c.errors
        except IOError:
            print("Error when reading the observatory config file")



class MySecondClass:
    ''' General Comment on the class '''
    pass


#TODO: Exceptions custom
#TODO: générer doc exemple avec Sphinx



'''
=================================================================
    Main function
=================================================================
'''
def main():
    a = 1
    b = 2
    c = 3



'''
=================================================================
    Exec of main function
=================================================================
'''
if __name__ == "__main__": 

    main()