Example #1
0
    def ensure_new_type(obj):
        from future.types.newbytes import newbytes
        from future.types.newstr import newstr
        from future.types.newint import newint
        from future.types.newdict import newdict

        native_type = type(native(obj))

        # Upcast only if the type is already a native (non-future) type
        if issubclass(native_type, type(obj)):
            # Upcast
            if native_type == str:  # i.e. Py2 8-bit str
                return newbytes(obj)
            elif native_type == unicode:
                return newstr(obj)
            elif native_type == int:
                return newint(obj)
            elif native_type == long:
                return newint(obj)
            elif native_type == dict:
                return newdict(obj)
            else:
                return NotImplementedError('type %s not supported' % type(obj))
        else:
            # Already a new type
            assert type(obj) in [newbytes, newstr]
            return obj
Example #2
0
    def ensure_new_type(obj):
        from future.types.newbytes import newbytes
        from future.types.newstr import newstr
        from future.types.newint import newint
        from future.types.newdict import newdict

        native_type = type(native(obj))

        # Upcast only if the type is already a native (non-future) type
        if issubclass(native_type, type(obj)):
            # Upcast
            if native_type == str:  # i.e. Py2 8-bit str
                return newbytes(obj)
            elif native_type == unicode:
                return newstr(obj)
            elif native_type == int:
                return newint(obj)
            elif native_type == long:
                return newint(obj)
            elif native_type == dict:
                return newdict(obj)
            else:
                return obj
        else:
            # Already a new type
            assert type(obj) in [newbytes, newstr]
            return obj
Example #3
0
    def submit_printjob(
            self,
            computer=None,
            printer=None,
            job_type='pdf',  # PDF|RAW
            title='PrintJob',
            options=None,
            authentication=None,
            uri=None,
            base64=None,
            binary=None):
        printers = self.get_printers(computer=computer, printer=printer)
        assert isinstance(printers, (list, Printer))
        if isinstance(printers, list):
            if len(printers) == 0:
                raise LookupError('printer not found')
            elif len(printers) > 1:
                printer_ids = ','.join(p.id for p in printers)
                msg = 'multiple printers match destination: {}'.format(
                    printer_ids)
                raise LookupError(msg)
            printer_id = printers[0].id
        else:
            printer_id = printers.id

        if job_type not in ['pdf', 'raw', 'binary']:
            raise ValueError('only support job_type of pdf or raw')
        if len([x for x in [uri, base64, binary] if x is not None]) != 1:
            raise ValueError('one and only one of the following parameters '
                             'is needed: uri, base64, or binary')

        if binary is not None:
            if sys.version_info[0] < 3:
                binary_bytes = newbytes(binary)
            else:
                if isinstance(binary, str):
                    binary_bytes = binary.encode('latin-1')
                else:
                    binary_bytes = binary
            base64 = base_64.b64encode(binary_bytes)
            base64 = base64.decode('utf-8')

        printjob_data = {
            'printerId': printer_id,
            'title': title,
            'contentType': job_type + '_' + ('uri' if uri else 'base64'),
            'content': uri or base64,
            'source': 'PythonApiClient'
        }

        if authentication is not None:
            printjob_data.update({"authentication": authentication})

        if options is not None:
            printjob_data.update({"options": options})

        printjob_id = self._auth.post('/printjobs', printjob_data)

        return printjob_id
Example #4
0
    def submit_printjob(
            self,
            computer=None,
            printer=None,
            job_type='pdf',  # PDF|RAW
            title='PrintJob',
            options=None,
            authentication=None,
            uri=None,
            base64=None,
            binary=None):
        printers = self.get_printers(computer=computer, printer=printer)
        assert isinstance(printers, (list, Printer))
        if isinstance(printers, list):
            if len(printers) == 0:
                raise LookupError('printer not found')
            elif len(printers) > 1:
                printer_ids = ','.join(p.id for p in printers)
                msg = 'multiple printers match destination: {}'.format(
                    printer_ids)
                raise LookupError(msg)
            printer_id = printers[0].id
        else:
            printer_id = printers.id

        if job_type not in ['pdf', 'raw', 'binary']:
            raise ValueError('only support job_type of pdf or raw')
        if len([x for x in [uri, base64, binary] if x is not None]) != 1:
            raise ValueError('one and only one of the following parameters '
                             'is needed: uri, base64, or binary')

        if binary is not None:
            if sys.version_info[0] < 3:
                binary_bytes = newbytes(binary)
            else:
                binary_bytes = binary.encode('latin-1')
            base64 = base_64.b64encode(binary_bytes)
            base64 = base64.decode('utf-8')

        printjob_data = {
            'printerId': printer_id,
            'title': title,
            'contentType': job_type + '_' + ('uri' if uri else 'base64'),
            'content': uri or base64,
            'source': 'PythonApiClient'}

        if authentication is not None:
            printjob_data.update({"authentication": authentication})

        if options is not None:
            printjob_data.update({"options": options})

        printjob_id = self._auth.post('/printjobs', printjob_data)

        return printjob_id
Example #5
0
 def native_str_to_bytes(s, encoding=None):
     from future.types import newbytes    # to avoid a circular import
     return newbytes(s)
Example #6
0
 def native_str_to_bytes(s, encoding=None):
     from future.types import newbytes  # pylint: disable=import-outside-toplevel
     return newbytes(s, encoding=encoding)
Example #7
0
 def native_str_to_bytes(s, encoding=None):
     from future.types import newbytes    # to avoid a circular import
     return newbytes(s)