-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint_service_win.py
87 lines (73 loc) · 3.61 KB
/
print_service_win.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
#!/usr/bin/env python
# Requires Python for Windows Extensions: http://python.net/crew/skippy/win32/
# IrfanView applies the last successfully executed print configurations if it is
# called from the commandline, hence you have to print once with your intended
# settings. Moreover, you have to adjust the service configuration to use your
# current user account for execution in order to apply these configs for the
# service. Alternatively you can create a special account for this service.
import subprocess
import os
import glob
import time
import win32serviceutil
import win32service
import win32event
import servicemanager
import traceback
# directory that is monitored for photos to print, printed photos are deleted (just provice a copy)
MONITORED_FOLDER = "D:/test"
MONITORED_FOLDER_INFO_FILE = MONITORED_FOLDER + '/FILES IN THIS DIRECTORY with .jpg EXTENSION are automatically printed and DELETED'
IRFAN_VIEW = "C:\Program Files (x86)\IrfanView\i_view32.exe" # or add i_view32.exe to path
PRINTER_NAME = "Canon Inkjet SELPHY DS810"
MONITORING_TIMEOUT_PERIOD = 10 # seconds
class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "PythonPrintMonitor"
_svc_display_name_ = "Python Print Monitor"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self._running = False
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, ''))
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
# check if folder exists
if not os.path.exists(MONITORED_FOLDER):
os.makedirs(MONITORED_FOLDER)
servicemanager.LogInfoMsg("Monitored folder doesn't exist. Creating: '" + MONITORED_FOLDER + "'")
#create info file in directory
if not os.path.exists(MONITORED_FOLDER_INFO_FILE):
f = open(MONITORED_FOLDER_INFO_FILE, "a")
f.close()
self._running = True
self.main()
def print_photo(self, file):
#this complicated string formatting was necessary to deal with spaces in arguments and process names
p = subprocess.Popen("{0} {1} /print={2}".format(IRFAN_VIEW, r'"%s"' % file, r'"%s"' % PRINTER_NAME))
p.wait()
def main(self):
while self._running:
try:
files = glob.glob(MONITORED_FOLDER + "/*.jpg")
# Enable for debugging
#if len(files)==0:
# servicemanager.LogInfoMsg("No photos found.")
for file in files:
# convert to windows \ convention
photo_file = file.replace("/", "\\")
servicemanager.LogInfoMsg("Printing photo: '" + photo_file+"'")
self.print_photo(photo_file)
os.remove(file)
except Exception as e:
servicemanager.LogMsg(servicemanager.EVENTLOG_ERROR_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ' ' + str(e) + '\n' + traceback.format_exc()))
time.sleep(MONITORING_TIMEOUT_PERIOD)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)