121 lines
3.0 KiB
Python
121 lines
3.0 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
""" Module docstring
|
|
"""
|
|
|
|
import os
|
|
import time
|
|
import json
|
|
import requests
|
|
|
|
__author__ = "Guillaume Ryckelynck"
|
|
__copyright__ = "Copyright 2023, Guillaume Ryckelynck"
|
|
__credits__ = ["Guillaume Ryckelynck"]
|
|
__license__ = "GPL"
|
|
__version__ = "0.1"
|
|
__maintainer__ = "Guillaume Ryckelynck"
|
|
__email__ = "guillaume.ryckelynck@grandest.fr"
|
|
__status__ = "Developement"
|
|
|
|
|
|
class Audits(object):
|
|
"""Audits object.
|
|
|
|
Utilisation:
|
|
|
|
``` python
|
|
# Get all audits
|
|
audits = Audits().list
|
|
# ou
|
|
audits = Audits().filter(search='test').list
|
|
```
|
|
|
|
Liste des propriétés
|
|
``` python
|
|
audits.list # liste des audits
|
|
```
|
|
|
|
Liste des méthodes publiques
|
|
``` python
|
|
# A compléter
|
|
```
|
|
"""
|
|
|
|
file = None
|
|
is_url = False
|
|
ssl_verify = True
|
|
audits = []
|
|
result = []
|
|
search = ''
|
|
|
|
|
|
def __init__(self, file=None):
|
|
"""Initialize Audits object."""
|
|
self.load(file=file)
|
|
|
|
|
|
def load(self, file=None):
|
|
if file is not None:
|
|
self.file = file
|
|
if self.file.startswith('http'):
|
|
self.is_url = True
|
|
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
|
|
r = requests.get(self.file, verify=self.ssl_verify)
|
|
if r.status_code == 200 and r.headers['Content-Type'] == 'application/json':
|
|
self.audits = r.json()
|
|
else:
|
|
self.is_url = False
|
|
if os.path.isfile(self.file):
|
|
with open(self.file, 'r') as f:
|
|
self.audits = json.load(f)
|
|
|
|
return self
|
|
|
|
|
|
def get(self, search=None, id=None):
|
|
if id is not None:
|
|
self.result = self.audits[id]
|
|
elif search is not None:
|
|
self.result = [audit for audit in self.audits if search in audit['name']]
|
|
else:
|
|
self.result = self.audits
|
|
|
|
return self
|
|
|
|
|
|
def add(self, audit=None):
|
|
if audit is not None:
|
|
self.audits.append(audit)
|
|
self.save()
|
|
return self
|
|
|
|
|
|
def edit(self, id=None, audit=None):
|
|
is_id = id is not None and isinstance(id, int) and id < len(self.audits)
|
|
is_audit = audit is not None
|
|
if is_id and is_audit:
|
|
self.audits[id] = audit
|
|
self.save()
|
|
return self
|
|
|
|
|
|
def delete(self, id=None):
|
|
if id is not None and isinstance(id, int) and id < len(self.audits):
|
|
del self.audits[id]
|
|
self.save()
|
|
return self
|
|
|
|
|
|
def save(self, audits=None, file=None):
|
|
if file is not None and os.path.isfile(file):
|
|
self.file = file
|
|
if audits is not None:
|
|
self.audits = audits
|
|
|
|
with open(self.file, 'w') as f:
|
|
json.dump(self.audits, f, indent=4)
|
|
return self
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pass |