示例#1
0
def test_illegal_mode():
    try:
        builder.QRCodeBuilder('test', 1, mode='murks', error='M')
        raise Exception('Expected an error for illegal mode')
    except ValueError as ex:
        ok_('murks' in str(ex))
示例#2
0
    def __init__(self, content, error='H', version=None, mode=None):

        #Coerce the content into a string
        self.data = str(content)

        #Check that the passed in error level is valid
        try:
            self.error = tables.error_level[str(error).upper()]
        except:
            raise ValueError('The error parameter is not one of '
                             '"L", "M", "Q", or "H."')

        #Guess the mode of the code, this will also be used for
        #error checking
        guessed_content_type = self._detect_content_type()

        #Force a passed in mode to be lowercase
        if mode:
            mode = mode.lower()

        #Check that the mode parameter is compatible with the contents
        if not mode:
            #Use the guessed mode
            self.mode = guessed_content_type
            self.mode_num = tables.modes[self.mode]
        elif guessed_content_type == 'binary' and \
             tables.modes[mode] != tables.modes['binary']:
            #Binary is only guessed as a last resort, if the
            #passed in mode is not binary the data won't encode
            raise ValueError('The content provided cannot be encoded with '
                             'the mode {}, it can only be encoded as '
                             'binary.'.format(mode))
        elif tables.modes[mode] == tables.modes['numeric'] and \
             guessed_content_type != 'numeric':
            #If numeric encoding is requested make sure the data can
            #be encoded in that format
            raise ValueError('The content cannot be encoded as numeric.')
        else:
            #The data should encode with the passed in mode
            self.mode = mode
            self.mode_num = tables.modes[self.mode]

        #Guess the "best" version
        self.version = self._pick_best_fit()

        #If the user supplied a version, then check that it has
        #sufficient data capacity for the contents passed in
        if version:
            if version >= self.version:
                self.version = version
            else:
                raise ValueError('The data will not fit inside a version {} '
                                 'code with the given encoding and error '
                                 'level (the code must be at least a '
                                 'version {}).'.format(version, self.version))

        #Build the QR code
        self.builder = builder.QRCodeBuilder(data=content,
                                             version=self.version,
                                             mode=self.mode,
                                             error=self.error)

        #Save the code for easier reference
        self.code = self.builder.code
示例#3
0
def test_illegal_version():
    try:
        builder.QRCodeBuilder('123', version=41, mode='numeric', error='M')
        raise Exception('Expected an error for illegal mode')
    except ValueError as ex:
        ok_('41' in str(ex))
示例#4
0
    def __init__(self, content, error='H', version=None, mode=None,
                 encoding='iso-8859-1'):
        #Guess the mode of the code, this will also be used for
        #error checking
        guessed_content_type, encoding = self._detect_content_type(content, encoding)
        
        if encoding is None:
            encoding = 'iso-8859-1'

        #Store the encoding for use later
        if guessed_content_type == 'kanji':
            self.encoding = 'shiftjis'
        else:
            self.encoding = encoding
        
        if version is not None:
            if 1 <= version <= 40:
                self.version = version
            else:
                raise ValueError("Illegal version {0}, version must be between "
                                 "1 and 40.".format(version))

        #Decode a 'byte array' contents into a string format
        if isinstance(content, bytes):
            self.data = content.decode(encoding)

        #Give a string an encoding
        elif hasattr(content, 'encode'):
            self.data = content.encode(self.encoding)

        #The contents are not a byte array or string, so
        #try naively converting to a string representation.
        else:
            self.data = str(content)  # str == unicode in Py 2.x, see file head

        #Force a passed in mode to be lowercase
        if hasattr(mode, 'lower'):
            mode = mode.lower()

        #Check that the mode parameter is compatible with the contents
        if mode is None:
            #Use the guessed mode
            self.mode = guessed_content_type
            self.mode_num = tables.modes[self.mode]
        elif mode not in tables.modes.keys():
            #Unknown mode
            raise ValueError('{0} is not a valid mode.'.format(mode))
        elif guessed_content_type == 'binary' and \
             tables.modes[mode] != tables.modes['binary']:
            #Binary is only guessed as a last resort, if the
            #passed in mode is not binary the data won't encode
            raise ValueError('The content provided cannot be encoded with '
                             'the mode {}, it can only be encoded as '
                             'binary.'.format(mode))
        elif tables.modes[mode] == tables.modes['numeric'] and \
             guessed_content_type != 'numeric':
            #If numeric encoding is requested make sure the data can
            #be encoded in that format
            raise ValueError('The content cannot be encoded as numeric.')
        elif tables.modes[mode] == tables.modes['kanji'] and \
             guessed_content_type != 'kanji':
            raise ValueError('The content cannot be encoded as kanji.')
        else:
            #The data should encode with the passed in mode
            self.mode = mode
            self.mode_num = tables.modes[self.mode]

        #Check that the user passed in a valid error level
        if error in tables.error_level.keys():
            self.error = tables.error_level[error]
        else:
            raise ValueError('{0} is not a valid error '
                             'level.'.format(error))

        #Guess the "best" version
        self.version = self._pick_best_fit(self.data)

        #If the user supplied a version, then check that it has
        #sufficient data capacity for the contents passed in
        if version:
            if version >= self.version:
                self.version = version
            else:
                raise ValueError('The data will not fit inside a version {} '
                                 'code with the given encoding and error '
                                 'level (the code must be at least a '
                                 'version {}).'.format(version, self.version))

        #Build the QR code
        self.builder = builder.QRCodeBuilder(data=self.data,
                                             version=self.version,
                                             mode=self.mode,
                                             error=self.error)

        #Save the code for easier reference
        self.code = self.builder.code