~ubuntu-branches/ubuntu/utopic/qr-tools/utopic

« back to all changes in this revision

Viewing changes to qrtools.py

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2014-01-25 16:53:37 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140125165337-osiau6glrb1b4upm
Tags: 1.4~bzr21-1
* New upstream revision
* debian/patches/101_fix_import_image.patch
 - imported to upstream
* debian/patches/01_setup_script.patch
 - update for version 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import shutil
27
27
import hashlib
28
28
import zbar
 
29
try:
 
30
    from PIL import Image
 
31
except ImportError:
 
32
    import Image
29
33
import re
30
34
from codecs import BOM_UTF8
31
35
 
32
 
try:
33
 
        from PIL import Image
34
 
except ImportError:
35
 
        import Image
36
 
 
37
36
class QR(object):
38
37
 
39
38
    def encode_url(data):
113
112
        self.directory = os.path.join('/tmp', 'qr-%f' % time.time())
114
113
        self.filename = filename
115
114
        os.makedirs(self.directory)
 
115
        self.qrencode_version = self.get_qrencode_version()
 
116
        self.qrencode_types = self.get_qrencode_types()
116
117
 
117
118
    def data_to_string(self):
118
119
        """Returns a UTF8 string with the QR Code's data"""
134
135
 
135
136
    def encode(self, filename=None):
136
137
        self.filename = filename or self.get_tmp_file()
137
 
        if not self.filename.endswith('.png'):
 
138
        ext = os.path.splitext(self.filename)[1].replace('.', '').upper()
 
139
        if  ext != 'PNG' \
 
140
        and ext != 'EPS' \
 
141
        and ext != 'SVG' \
 
142
        and ext != 'ANSI' \
 
143
        and ext != 'ANSI256' \
 
144
        and ext != 'ASCII' \
 
145
        and ext != 'ASCIII' \
 
146
        and ext != 'UTF8' \
 
147
        and ext != 'ANSIUTF8':
138
148
            self.filename += '.png'
139
 
        return subprocess.Popen([
140
 
            'qrencode',
141
 
            '-o', self.filename,
142
 
            '-s', unicode(self.pixel_size),
143
 
            '-m', unicode(self.margin_size),
144
 
            '-l', self.level,
145
 
            self.data_to_string()
146
 
        ]).wait()
 
149
            ext = 'PNG'
 
150
        if self.qrencode_version > '3.1.1':
 
151
            command = [
 
152
                'qrencode',
 
153
                '-o', self.filename,
 
154
                '-s', unicode(self.pixel_size),
 
155
                '-m', unicode(self.margin_size),
 
156
                '-l', self.level,
 
157
                '-t', ext,
 
158
                self.data_to_string()
 
159
            ]
 
160
        else:
 
161
            command = [
 
162
                'qrencode',
 
163
                '-o', self.filename,
 
164
                '-s', unicode(self.pixel_size),
 
165
                '-m', unicode(self.margin_size),
 
166
                '-l', self.level,
 
167
                #'-t', ext,
 
168
                self.data_to_string()
 
169
            ]
 
170
        return subprocess.Popen(command).wait()
147
171
 
148
172
    def decode(self, filename=None):
149
173
        self.filename = filename or self.filename
207
231
 
208
232
    def destroy(self):
209
233
        shutil.rmtree(self.directory)
 
234
 
 
235
    def get_qrencode_version(self):
 
236
        #Somehow qerencode writes this to stderr instead of stdout :-/
 
237
        #FIXME: Probably a future bug in newer versions.
 
238
        p = subprocess.Popen(['qrencode','-V'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
239
        version_text = p.communicate()[1]
 
240
        version = re.search('version\s([\d.]*)',version_text) 
 
241
        if version:
 
242
            version_number = version.group(1)
 
243
        else:
 
244
            version_number = -1
 
245
        #print "Using qrencode version:", version_number
 
246
        return version_number
 
247
 
 
248
    def get_qrencode_types(self):
 
249
        p = subprocess.Popen(['qrencode','-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
250
        help_text = p.communicate()[1]
 
251
        types_text = re.search('-t {([\w,]*)}', help_text) 
 
252
        if types_text:
 
253
            types = types_text.group(1).split(',')
 
254
            #print "The following format types have been found!:", types
 
255
        else:
 
256
            types = ['png']
 
257
            #print "Help text for format types not found. Using:", types
 
258
        return types
 
 
b'\\ No newline at end of file'