Coverage for src/bob/bio/vein/script/validate.py: 0%
33 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-12 23:27 +0200
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-12 23:27 +0200
1#!/usr/bin/env python
2# vim: set fileencoding=utf-8 :
5"""Utilities for command-line option validation"""
8import glob
9import logging
10import os
12import schema
14logger = logging.getLogger(__name__)
17def setup_logger(name, level):
18 """Sets up and checks a verbosity level respects min and max boundaries
21 Parameters:
23 name (str): The name of the logger to setup
25 v (int): A value indicating the verbosity that must be set
28 Returns:
30 logging.Logger: A standard Python logger that can be used to log messages
33 Raises:
35 schema.SchemaError: If the verbosity level exceeds the maximum allowed of 4
37 """
39 import clapper.logging
41 logger = clapper.logging.setup(name)
43 if not (0 <= level < 4):
44 raise schema.SchemaError(
45 "there can be only up to 3 -v's in a command-line"
46 )
48 # Sets-up logging
49 clapper.logging.set_verbosity_level(logger, level)
51 return logger
54def make_dir(p):
55 """Checks if a path exists, if it doesn't, creates it
58 Parameters:
60 p (str): The path to check
63 Returns
65 bool: ``True``, always
67 """
69 if not os.path.exists(p):
70 logger.info("Creating directory `%s'...", p)
71 os.makedirs(p)
73 return True
76def check_path_does_not_exist(p):
77 """Checks if a path exists, if it does, raises an exception
80 Parameters:
82 p (str): The path to check
85 Returns:
87 bool: ``True``, always
90 Raises:
92 schema.SchemaError: if the path exists
94 """
96 if os.path.exists(p):
97 raise schema.SchemaError("path to {} exists".format(p))
99 return True
102def check_path_exists(p):
103 """Checks if a path exists, if it doesn't, raises an exception
106 Parameters:
108 p (str): The path to check
111 Returns:
113 bool: ``True``, always
116 Raises:
118 schema.SchemaError: if the path doesn't exist
120 """
122 if not os.path.exists(p):
123 raise schema.SchemaError("path to {} does not exist".format(p))
125 return True
128def check_model_does_not_exist(p):
129 """Checks if the path to any potential model file does not exist
132 Parameters:
134 p (str): The path to check
137 Returns:
139 bool: ``True``, always
142 Raises:
144 schema.SchemaError: if the path exists
146 """
148 files = glob.glob(p + ".*")
149 if files:
150 raise schema.SchemaError("{} already exists".format(files))
152 return True
155def open_multipage_pdf_file(s):
156 """Returns an opened matplotlib multi-page file
159 Parameters:
161 p (str): The path to the file to open
164 Returns:
166 matplotlib.backends.backend_pdf.PdfPages: with the handle to the multipage
167 PDF file
170 Raises:
172 schema.SchemaError: if the path exists
174 """
175 from matplotlib.backends.backend_pdf import PdfPages
177 return PdfPages(s)