1
|
|
|
#!/usr/bin/python |
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
import datetime |
4
|
|
|
import os |
5
|
|
|
import re |
6
|
|
|
from pathlib import Path |
7
|
|
|
from stat import ST_CTIME, ST_MTIME, ST_SIZE |
8
|
|
|
from typing import Generator |
|
|
|
|
9
|
|
|
|
10
|
|
|
from saucenao.files.constraint import Constraint |
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Filter: |
|
|
|
|
14
|
|
|
""" |
|
|
|
|
15
|
|
|
Filter to apply constraints for multiple metadata entries of the files. |
16
|
|
|
Obviously the files have to exist or they'll automatically get removed. |
17
|
|
|
Returns a generator object |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
def __init__(self, assert_is_folder=False, assert_is_file=False, creation_date=None, modified_date=None, name=None, |
|
|
|
|
21
|
|
|
file_type=None, size=None): |
22
|
|
|
"""Initializing function |
|
|
|
|
23
|
|
|
|
24
|
|
|
:type assert_is_folder: bool |
25
|
|
|
:type assert_is_file: bool |
26
|
|
|
:type creation_date: Constraint |
27
|
|
|
:type modified_date: Constraint |
28
|
|
|
:type name: Constraint |
29
|
|
|
:type file_type: Constraint |
30
|
|
|
:type size: Constraint |
31
|
|
|
""" |
32
|
|
|
self.filter_assert_is_folder = assert_is_folder |
|
|
|
|
33
|
|
|
self.filter_assert_is_file = assert_is_file |
|
|
|
|
34
|
|
|
self.filter_creation_date = creation_date |
|
|
|
|
35
|
|
|
self.filter_modified_date = modified_date |
|
|
|
|
36
|
|
|
self.filter_name = name |
|
|
|
|
37
|
|
|
self.filter_file_type = file_type |
|
|
|
|
38
|
|
|
self.filter_file_size = size |
|
|
|
|
39
|
|
|
|
40
|
|
|
@staticmethod |
|
|
|
|
41
|
|
|
def _get_timestamp_from_datestring(date_string): |
|
|
|
|
42
|
|
|
"""Convert the given date string to timestamp |
|
|
|
|
43
|
|
|
|
44
|
|
|
:param date_string: |
45
|
|
|
:return: |
46
|
|
|
""" |
47
|
|
|
if re.match('\d+.\d+.\d+ \d+:\d+:\d+', date_string): |
|
|
|
|
48
|
|
|
return datetime.datetime.strptime(date_string, "%d.%m.%Y %H:%M:%S").timestamp() |
|
|
|
|
49
|
|
|
elif re.match('\d+.\d+.\d+ \d+:\d+', date_string): |
|
|
|
|
50
|
|
|
return datetime.datetime.strptime(date_string, "%d.%m.%Y %H:%M").timestamp() |
|
|
|
|
51
|
|
|
elif re.match('\d+.\d+.\d+', date_string): |
|
|
|
|
52
|
|
|
return datetime.datetime.strptime(date_string, "%d.%m.%Y").timestamp() |
|
|
|
|
53
|
|
|
else: |
|
|
|
|
54
|
|
|
raise AttributeError("The date doesn't fit the format: d.m.Y[ H:M[:S]]") |
|
|
|
|
55
|
|
|
|
56
|
|
|
def apply(self, directory='', file_system_objects=None): |
|
|
|
|
57
|
|
|
"""Apply the filter values to the given FSOs(File System Objects) |
|
|
|
|
58
|
|
|
|
59
|
|
|
:type directory: str |
60
|
|
|
:type file_system_objects: list|tuple|Generator |
61
|
|
|
:return: |
62
|
|
|
""" |
63
|
|
|
if file_system_objects is None: |
|
|
|
|
64
|
|
|
if directory: |
|
|
|
|
65
|
|
|
file_system_objects = os.listdir(directory) |
|
|
|
|
66
|
|
|
else: |
|
|
|
|
67
|
|
|
file_system_objects = [] |
|
|
|
|
68
|
|
|
|
69
|
|
|
for file_system_object in file_system_objects: |
|
|
|
|
70
|
|
|
abs_path = os.path.join(directory, file_system_object) |
|
|
|
|
71
|
|
|
|
72
|
|
|
# check if the FSO exists, else we can't access the metadata |
73
|
|
|
if not os.path.exists(abs_path): |
|
|
|
|
74
|
|
|
continue |
|
|
|
|
75
|
|
|
|
76
|
|
|
# check if the FSO is a file |
77
|
|
|
if self.filter_assert_is_file and not os.path.isfile(abs_path): |
|
|
|
|
78
|
|
|
continue |
|
|
|
|
79
|
|
|
|
80
|
|
|
# check if the FSO is a folder |
81
|
|
|
if self.filter_assert_is_folder and not os.path.isdir(abs_path): |
|
|
|
|
82
|
|
|
continue |
|
|
|
|
83
|
|
|
|
84
|
|
|
file_stats = os.stat(abs_path) |
|
|
|
|
85
|
|
|
|
86
|
|
|
# check if the FSO creation date matches the constraint |
87
|
|
|
if self.filter_creation_date: |
|
|
|
|
88
|
|
|
creation_time = file_stats[ST_CTIME] |
|
|
|
|
89
|
|
|
expected_creation_time = self._get_timestamp_from_datestring(self.filter_creation_date.value) |
|
|
|
|
90
|
|
|
if not self.filter_creation_date.cmp_func(creation_time, expected_creation_time): |
|
|
|
|
91
|
|
|
continue |
|
|
|
|
92
|
|
|
|
93
|
|
|
# check if the FSO modification date matches the constraint |
94
|
|
|
if self.filter_modified_date: |
|
|
|
|
95
|
|
|
modified_time = file_stats[ST_MTIME] |
|
|
|
|
96
|
|
|
expected_modified_time = self._get_timestamp_from_datestring(self.filter_modified_date.value) |
|
|
|
|
97
|
|
|
if not self.filter_modified_date.cmp_func(modified_time, expected_modified_time): |
|
|
|
|
98
|
|
|
continue |
|
|
|
|
99
|
|
|
|
100
|
|
|
# check if the FSO name matches the constraint |
101
|
|
|
if self.filter_name: |
|
|
|
|
102
|
|
|
if not self.filter_name.cmp_func(file_system_object, self.filter_name.value): |
|
|
|
|
103
|
|
|
continue |
|
|
|
|
104
|
|
|
|
105
|
|
|
# check if the FSO suffix matches the constraint |
106
|
|
|
if self.filter_file_type: |
|
|
|
|
107
|
|
|
if not self.filter_file_type.cmp_func(Path(abs_path).suffix, self.filter_file_type.value): |
|
|
|
|
108
|
|
|
continue |
|
|
|
|
109
|
|
|
|
110
|
|
|
# check if the FSO size matches the constraint |
111
|
|
|
if self.filter_file_size: |
|
|
|
|
112
|
|
|
if not self.filter_file_size.cmp_func(file_stats[ST_SIZE], self.filter_file_size.value): |
|
|
|
|
113
|
|
|
continue |
|
|
|
|
114
|
|
|
|
115
|
|
|
yield file_system_object |
|
|
|
|
116
|
|
|
|
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.