template_base.py
· 2.1 KiB · Python
Raw
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
from logging.handlers import RotatingFileHandler
import argparse
# construction du logger
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.ERROR)
#LOGGER.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s :: %(module)s :: %(levelname)s :: %(message)s')
# écriture d'un fichier de log
file_handler = RotatingFileHandler(__file__ + '.log', 'a', 1000000, 1)
file_handler.setFormatter(formatter)
LOGGER.addHandler(file_handler)
# écriture de log sur la sortie standard
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
LOGGER.addHandler(stream_handler)
# gestion des arguments avec argparse, ici on reprend un version modifié mise en place pour agent_dpluzz
# https://docs.python.org/fr/3.6/howto/argparse.html
PARSER = argparse.ArgumentParser(
description="présentation générale \
de l'outil.")
# FLAG
PARSER.add_argument("-d",
"--download",
action="store_true",
help="description de la fonction.")
# Passage de parametre
PARSER.add_argument("-lpf",
"--pattern_load",
type=str,
metavar=('FICHIER'),
help="Charge un fichier de motif (les lignes vide et commençant pas un # sont filtré).")
# Compteur
PARSER.add_argument("-v",
"--verbosity",
action='count',
help="défini le niveau de verbosité à afficher.")
# Parsing des arguments
ARGS = PARSER.parse_args()
# controle du compteur
if ARGS.verbosity == 5:
LOGGER.setLevel(logging.DEBUG)
if ARGS.verbosity == 4:
LOGGER.setLevel(logging.INFO)
if ARGS.verbosity == 3:
LOGGER.setLevel(logging.WARNING)
if ARGS.verbosity == 2:
LOGGER.setLevel(logging.ERROR)
if ARGS.verbosity == 1:
LOGGER.setLevel(logging.CRITICAL)
# controle du FLAG
if ARGS.download:
pritnt("fonction download activé")
exit(0)
# controle du passage de parametre
if ARGS.pattern_load:
print("motif : %s"%ARGS.pattern_load)
exit(0)
| 1 | #!/usr/bin/python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | import logging |
| 5 | from logging.handlers import RotatingFileHandler |
| 6 | import argparse |
| 7 | |
| 8 | # construction du logger |
| 9 | LOGGER = logging.getLogger() |
| 10 | LOGGER.setLevel(logging.ERROR) |
| 11 | #LOGGER.setLevel(logging.DEBUG) |
| 12 | formatter = logging.Formatter('%(asctime)s :: %(module)s :: %(levelname)s :: %(message)s') |
| 13 | |
| 14 | # écriture d'un fichier de log |
| 15 | file_handler = RotatingFileHandler(__file__ + '.log', 'a', 1000000, 1) |
| 16 | file_handler.setFormatter(formatter) |
| 17 | LOGGER.addHandler(file_handler) |
| 18 | |
| 19 | # écriture de log sur la sortie standard |
| 20 | stream_handler = logging.StreamHandler() |
| 21 | stream_handler.setFormatter(formatter) |
| 22 | LOGGER.addHandler(stream_handler) |
| 23 | |
| 24 | # gestion des arguments avec argparse, ici on reprend un version modifié mise en place pour agent_dpluzz |
| 25 | # https://docs.python.org/fr/3.6/howto/argparse.html |
| 26 | |
| 27 | PARSER = argparse.ArgumentParser( |
| 28 | description="présentation générale \ |
| 29 | de l'outil.") |
| 30 | |
| 31 | # FLAG |
| 32 | PARSER.add_argument("-d", |
| 33 | "--download", |
| 34 | action="store_true", |
| 35 | help="description de la fonction.") |
| 36 | |
| 37 | # Passage de parametre |
| 38 | PARSER.add_argument("-lpf", |
| 39 | "--pattern_load", |
| 40 | type=str, |
| 41 | metavar=('FICHIER'), |
| 42 | help="Charge un fichier de motif (les lignes vide et commençant pas un # sont filtré).") |
| 43 | |
| 44 | # Compteur |
| 45 | PARSER.add_argument("-v", |
| 46 | "--verbosity", |
| 47 | action='count', |
| 48 | help="défini le niveau de verbosité à afficher.") |
| 49 | |
| 50 | # Parsing des arguments |
| 51 | ARGS = PARSER.parse_args() |
| 52 | |
| 53 | # controle du compteur |
| 54 | if ARGS.verbosity == 5: |
| 55 | LOGGER.setLevel(logging.DEBUG) |
| 56 | if ARGS.verbosity == 4: |
| 57 | LOGGER.setLevel(logging.INFO) |
| 58 | if ARGS.verbosity == 3: |
| 59 | LOGGER.setLevel(logging.WARNING) |
| 60 | if ARGS.verbosity == 2: |
| 61 | LOGGER.setLevel(logging.ERROR) |
| 62 | if ARGS.verbosity == 1: |
| 63 | LOGGER.setLevel(logging.CRITICAL) |
| 64 | |
| 65 | # controle du FLAG |
| 66 | if ARGS.download: |
| 67 | pritnt("fonction download activé") |
| 68 | exit(0) |
| 69 | |
| 70 | # controle du passage de parametre |
| 71 | if ARGS.pattern_load: |
| 72 | print("motif : %s"%ARGS.pattern_load) |
| 73 | exit(0) |
| 74 |