-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_watcher.py
executable file
·51 lines (43 loc) · 1.7 KB
/
print_watcher.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
#!/usr/bin/env python2
import argparse
import logging
import os
import time
import sys
from printer import CmdPrinter, PyPrinter
logger = logging.getLogger('photobooth')
file_log_handler = logging.FileHandler('printer.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
file_log_handler.setFormatter(formatter)
logger.addHandler(file_log_handler)
stdout_log_handler = logging.StreamHandler(sys.stdout)
stdout_log_handler.setLevel(logging.WARN)
logger.addHandler(stdout_log_handler)
logger.setLevel(logging.DEBUG)
def watch_for_files(input_folder, printer):
done_file = os.path.join(input_folder, 'done.txt')
if not os.path.exists(done_file):
open(done_file, 'a').close()
with open(done_file) as f:
content = f.readlines()
done_files = set([x.strip() for x in content])
while True:
files = os.listdir(input_folder)
jpgs = set([f for f in files if f.endswith('.jpg')])
for jpg in jpgs:
if jpg not in done_files:
printer.print_image(os.path.join(input_folder, jpg))
with open(done_file, 'a') as f:
f.write(jpg + '\n')
done_files.add(jpg)
time.sleep(1)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("print_from", help="Location to find images")
parser.add_argument("-p", "--print_count",
help="Set number of copies to print", type=int, default=1)
parser.add_argument("-P", "--printer", help="Set printer to use")
args = parser.parse_args()
logger.info("Args were: %s", args)
printer = PyPrinter(args.printer, args.print_count, False)
watch_for_files(args.print_from, printer)