1
|
|
|
import os |
|
|
|
|
2
|
|
|
import time |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
from fastest.file_handler.read_file import read_file |
6
|
|
|
from fastest.code_assets.function import get_functions |
7
|
|
|
from fastest.test_compiler import test_writer |
8
|
|
|
from watchdog.observers import Observer |
9
|
|
|
from watchdog.events import PatternMatchingEventHandler |
10
|
|
|
from fastest.logger.logger import logger |
11
|
|
|
from fastest.cli.args import cli_args |
12
|
|
|
from fastest.file_handler import make_test_module, execute_coverage, \ |
13
|
|
|
execute_tests, is_path_to_be_ignored |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
monitor_path = cli_args.path if cli_args.path is not None else os.getcwd() |
17
|
|
|
|
18
|
|
|
poll_duration = int(cli_args.poll_duration) \ |
19
|
|
|
if type(cli_args.poll_duration) is str and \ |
20
|
|
|
cli_args.poll_duration.isdigit() \ |
21
|
|
|
else 1 |
22
|
|
|
|
23
|
|
|
make_test_module() |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def main(): |
27
|
|
|
logger.info('Monitoring started...') |
28
|
|
|
|
29
|
|
|
class PyFileHandler(PatternMatchingEventHandler): |
30
|
|
|
patterns = ['*.py'] |
31
|
|
|
exclude_files = cli_args.exclude if cli_args.exclude is not None else '' |
32
|
|
|
ignore_patterns = [path.strip() for path in exclude_files.split(',')] |
33
|
|
|
ignore_patterns += ['test/*', '__pycache__', '*.pyc', '*__test.py'] |
34
|
|
|
|
35
|
|
|
def process(self, event): |
36
|
|
|
if is_path_to_be_ignored(event.src_path, monitor_path, self.ignore_patterns): |
37
|
|
|
return None |
38
|
|
|
|
39
|
|
|
page = read_file(monitor_path, event.src_path) |
40
|
|
|
functions = get_functions(page) |
41
|
|
|
test_writer.build(functions, event.src_path, monitor_path) |
42
|
|
|
execute_tests(monitor_path) |
43
|
|
|
execute_coverage(monitor_path) |
44
|
|
|
|
45
|
|
|
def on_modified(self, event): |
46
|
|
|
self.process(event) |
47
|
|
|
|
48
|
|
|
def on_created(self, event): |
49
|
|
|
self.process(event) |
50
|
|
|
|
51
|
|
|
observer = Observer() |
52
|
|
|
observer.schedule(PyFileHandler(), monitor_path, recursive=True) |
53
|
|
|
observer.start() |
54
|
|
|
|
55
|
|
|
try: |
56
|
|
|
while True: |
57
|
|
|
time.sleep(poll_duration) |
58
|
|
|
except KeyboardInterrupt: |
59
|
|
|
observer.stop() |
60
|
|
|
observer.join() |
61
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.