-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.py
executable file
·130 lines (99 loc) · 3.09 KB
/
watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##########
# IMPORT #
##########
import os
import time
############
# SET UP #
############
# About watching
# --------------
# Path to watch : this is where the script looks for new file/folder
PATH_TO_WATCH = "./watch_folder"
# Refresh every X seconds
REFRESH = 1
# Time between comparaison of PATH_TO_WATCH
TIME_BETWEEN = 1
# folder corresponding to the photo's dimension
PHOTO_DIMENSION = "152x102"
# folder corresponding to the photo's paper's type
PAPER_TYPE = "glossy"
# REGEX ID client ? Todo #
print("--- WATCHER (in python) ---\nYour parameters :")
print("PATH_TO_WATCH = {}".format(PATH_TO_WATCH))
print("REFRESH = {}".format(REFRESH))
print("TIME_BETWEEN = {}".format(TIME_BETWEEN))
print("PHOTO_DIMENSION = {}".format(PHOTO_DIMENSION))
print("PAPER_TYPE = {}".format(PAPER_TYPE))
print("""----------------------------""")
#############
# FUNCTIONS #
#############
# Watch path and compare os.dir(path) with itself 10s later.
# Return new file(s) and folder(s).
def watcher(PATH_TO_WATCH):
before = os.listdir(PATH_TO_WATCH)
while 1:
time.sleep(REFRESH)
after = os.listdir(PATH_TO_WATCH)
if before != after:
print("Il y a du nouveau :")
time.sleep(TIME_BETWEEN)
new_files = []
for afile in after:
exist = 0
for bfile in before:
if bfile == afile:
exist = 1
if exist == 0:
new_files.append(afile)
return new_files
before = after
# Check if new_files contain an unique folder name composed by 6 digits.
# return 0 or 1
def is_it_id(new_files):
if len(new_files) != 1:
return 0
if len(new_files[0]) != 6:
return 0
return 1
# SEARCHED NAME DEDICASSSSSE
# Search searched_name in path
# return 0 or 1
def search_in_folder(searched_name, path):
for file in os.listdir(path):
if file == searched_name:
return 1
return 0
def error_exit(action, *args):
if action == "INVALID_CID":
print("Error: new folder is not a valid client ID.")
elif action == "FOLDER_!EXISTS":
print("Error: folder '{}/' does not exist in '{}'/".format(*args))
print("Exiting...")
exit()
############
# MAIN #
############
new_files = watcher(PATH_TO_WATCH)
# Test if new_files ok.
if not is_it_id(new_files):
error_exit("INVALID_CID")
# Getting cliend_id
cliend_id = new_files[0]
# New path to scan
path_to_scan = PATH_TO_WATCH + "/" + cliend_id
# print path_to_scan # debug
# Searching for PHOTO_DIMENSION in path_to_scan
if not search_in_folder(PHOTO_DIMENSION, path_to_scan):
error_exit("FOLDER_!EXISTS", PHOTO_DIMENSION, path_to_scan)
# New path to scan
path_to_scan = path_to_scan + "/" + PHOTO_DIMENSION
# Searching for PAPER_TYPE in path_to_scan
if not search_in_folder(PAPER_TYPE, path_to_scan):
error_exit("FOLDER_!EXISTS", PAPER_TYPE, path_to_scan)
# New path to scan
path_to_scan = path_to_scan + "/" + PAPER_TYPE
print("Chemin définitif : {}".format(path_to_scan))