def test_base64encode(): Utils.WriteFile('/tmp/testfile', 'data') == True assert Utils.Base64Encode('/tmp/testfile') == '/tmp/testfile.b64' assert Utils.ReadFile( '/tmp/testfile.b64') == 'data:application/octet-stream;base64,ZGF0YQ==' os.unlink('/tmp/testfile.b64') os.mkdir('/tmp/testfile.b64') assert Utils.Base64Encode('/tmp/testfile') is None os.unlink('/tmp/testfile') os.rmdir('/tmp/testfile.b64') assert Utils.Base64Encode('/tmp/testfile/dsiahdisa') is None
def submitJob(self, jobtype, jobfile, jobdata, jobname, cupsprintername, options=""): """Submits a job to printerid with content of dataUrl. Args: jobtype: string, must match the dictionary keys in content and content_type. jobfile: string, points to source for job. Could be a pathname or id string. jobdata: string, data for print job jobname: string, name of the print job ( usually page name ). options: string, key-value pair of options from print job. Returns: True if submitted, False otherwise """ rotate = 0 # refuse to submit empty jobdata if len(jobdata) == 0: sys.stderr.write("ERROR: Job data is empty\n") return False if jobfile is None or jobfile == "": jobfile = "Unknown" for optiontext in options.split(' '): # landscape if optiontext == 'landscape': # landscape rotate = 90 # nolandscape - already rotates if optiontext == 'nolandscape': # rotate back rotate = 270 if jobtype not in ['png', 'jpeg', 'pdf']: sys.stderr.write("ERROR: Unknown job type: %s\n" % jobtype) return False else: if rotate != 0: command = [ self._CONVERTCOMMAND, '-density', '300x300', '-', '-rotate', str(rotate), '-' ] p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE) newjobdata = p.communicate(jobdata)[0] if p.returncode == 0: jobdata = newjobdata else: logging.error("Failed to rotate") return False if jobname == "": title = "Untitled page" else: title = jobname headers = [('printerid', self['id']), ('title', title), ('content', Utils.Base64Encode(jobdata, jobtype)), ('contentType', 'dataUrl'), ('capabilities', json.dumps(self._getCapabilities(cupsprintername, options)))] logging.info('Capability headers are: %s', headers[4]) data = self._encodeMultiPart(headers) try: responseobj = self.getRequestor().submit(data, self._getMimeBoundary()) if responseobj['success']: return True else: sys.stderr.write( "ERROR: Error response from Cloud Print for type %s: %s\n" % (jobtype, responseobj['message'])) return False except Exception as error_msg: sys.stderr.write("ERROR: Print job %s failed with %s\n" % (jobtype, error_msg)) return False
def test_base64encode(): assert Utils.Base64Encode('data', 'pdf') == 'data:application/pdf;base64,ZGF0YQ==' assert Utils.Base64Encode('data', 'other') == 'data:application/octet-stream;base64,ZGF0YQ==' assert Utils.Base64Encode( 'data', 'something') == 'data:application/octet-stream;base64,ZGF0YQ=='