chiark / gitweb /
ae2d5e88e80a93889f47f1e320317c31d44c3f72
[cura.git] / Cura / util / printerConnection / dummyConnection.py
1 __copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
2
3 import threading
4 import json
5 import httplib as httpclient
6 import urllib
7 import time
8
9 from Cura.util.printerConnection import printerConnectionBase
10
11 #Dummy printer class which is always
12 class dummyConnection(printerConnectionBase.printerConnectionBase):
13         def __init__(self):
14                 super(dummyConnection, self).__init__()
15
16                 self._printing = False
17                 self._lineCount = 0
18                 self._progressLine = 0
19
20                 self.printThread = threading.Thread(target=self._dummyThread)
21                 self.printThread.daemon = True
22                 self.printThread.start()
23
24         #Load the file into memory for printing.
25         def loadFile(self, filename):
26                 if self._printing:
27                         return False
28                 self._lineCount = 0
29                 f = open(filename, "r")
30                 for line in f:
31                         #Strip out comments, we do not need to send comments
32                         if ';' in line:
33                                 line = line[:line.index(';')]
34                         #Strip out whitespace at the beginning/end this saves data to send.
35                         line = line.strip()
36
37                         if len(line) < 1:
38                                 continue
39                         self._lineCount += 1
40                 self._doCallback()
41                 return True
42
43         #Start printing the previously loaded file
44         def startPrint(self):
45                 if self._printing or self._lineCount < 1:
46                         return
47                 self._progressLine = 0
48                 self._printing = True
49
50         #Abort the previously loaded print file
51         def cancelPrint(self):
52                 self._printing = False
53
54         def isPrinting(self):
55                 return self._printing
56
57         #Amount of progression of the current print file. 0.0 to 1.0
58         def getPrintProgress(self):
59                 if self._lineCount < 1:
60                         return 0.0
61                 return float(self._progressLine) / float(self._lineCount)
62
63         # Return if the printer with this connection type is available
64         def isAvailable(self):
65                 return True
66
67         # Get the connection status string. This is displayed to the user and can be used to communicate
68         #  various information to the user.
69         def getStatusString(self):
70                 return "DUMMY!\n%i %i\n%i" % (self._progressLine, self._lineCount, self._printing)
71
72         def _dummyThread(self):
73                 while True:
74                         if not self._printing:
75                                 time.sleep(5)
76                                 self._doCallback()
77                         else:
78                                 time.sleep(0.01)
79                                 self._progressLine += 1
80                                 if self._progressLine == self._lineCount:
81                                         self._printing = False
82                                 self._doCallback()