125 | | if not files : |
126 | | files = [ sys.stdin ] |
| 128 | if (not sys.stdin.isatty()) and ("-" not in files) : |
| 129 | files.append("-") |
| 130 | pipe = os.popen("pkpgcounter %s" % " ".join([' "%s"' % f for f in files])) |
| 131 | answer = pipe.readline().strip() |
| 132 | status = pipe.close() |
| 133 | try : |
| 134 | if status is None : |
| 135 | nbpages = int(answer) |
| 136 | else : |
| 137 | raise ValueError, "ERROR in pkpgcounter" |
| 138 | except ValueError, msg : |
| 139 | sys.stderr.write(_("Unable to compute size of %s in number of pages.\n") % files) |
| 140 | sys.stderr.write("%s\n" % msg) |
| 141 | else : |
| 142 | # get current user |
| 143 | uid = os.geteuid() |
| 144 | username = pwd.getpwuid(uid)[0] |
| 145 | user = self.storage.getUser(username) |
| 146 | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
| 147 | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
| 148 | |
| 149 | printers = self.storage.getMatchingPrinters(options["printer"]) |
| 150 | if not printers : |
| 151 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
| 152 | |
| 153 | print _("Job size : %i pages") % nbpages |
| 154 | for printer in printers : |
| 155 | userpquota = self.storage.getUserPQuota(user, printer) |
| 156 | cost = userpquota.computeJobPrice(nbpages) |
| 157 | print _("Cost on printer %s : %.2f") % (printer.Name, cost) |
128 | | nbpages = 0 |
129 | | for filename in files : |
130 | | if filename is sys.stdin : |
131 | | stream = filename |
132 | | else : |
133 | | if filename == "-" : |
134 | | stream = sys.stdin |
135 | | else : |
136 | | try : |
137 | | stream = open(filename, "rb") |
138 | | except IOError, msg : |
139 | | sys.stderr.write("%s\n" % msg) |
140 | | continue |
141 | | nb = self.getNbPages(stream) |
142 | | if nb is None : |
143 | | sys.stderr.write(_("Unable to compute size of %s in number of pages.\n") % filename) |
144 | | else : |
145 | | nbpages += nb |
146 | | if not (filename is sys.stdin) : |
147 | | stream.close() |
148 | | |
149 | | # get current user |
150 | | uid = os.geteuid() |
151 | | username = pwd.getpwuid(uid)[0] |
152 | | user = self.storage.getUser(username) |
153 | | if user.Exists and user.LimitBy and (user.LimitBy.lower() == "balance"): |
154 | | print _("Your account balance : %.2f") % (user.AccountBalance or 0.0) |
155 | | |
156 | | printers = self.storage.getMatchingPrinters(options["printer"]) |
157 | | if not printers : |
158 | | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
159 | | |
160 | | print _("Job size : %i pages") % nbpages |
161 | | for printer in printers : |
162 | | userpquota = self.storage.getUserPQuota(user, printer) |
163 | | cost = userpquota.computeJobPrice(nbpages) |
164 | | print _("Cost on printer %s : %.2f") % (printer.Name, cost) |
165 | | |
166 | | def getNbPages(self, stream) : |
167 | | """Tries to compute the file's size in number of pages, the best we can.""" |
168 | | nbpages = [] # in case some user wants to put several %%Pages: entries in the file |
169 | | for line in stream.xreadlines() : |
170 | | line = line.strip() |
171 | | if line.startswith("%%Pages: ") : |
172 | | try : |
173 | | nbpages.append(int(line[9:])) |
174 | | except ValueError : |
175 | | pass |
176 | | if nbpages : |
177 | | return max(nbpages) |
178 | | |