-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqr.py
32 lines (27 loc) · 1.12 KB
/
qr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from pyqrcode import create
from io import BytesIO
from base64 import b64encode
class QrCode:
def __init__(self, ssid, authentication_method, password, **kwargs):
self._qr = create('WIFI:S:{ssid};T:{authentication_method};P:{password};;'.format(
ssid = self._mecard_escape(ssid),
authentication_method = self._authentication_method(authentication_method),
password = self._mecard_escape(password)
))
def _authentication_method(self, authentication_method):
if authentication_method.lower() in ['wep']:
return 'WEP'
if authentication_method.lower() in ['wpa', 'wpa2', 'wpawpa2', 'psk', 'psk2', 'pskpsk2']:
return 'WPA'
return 'nopass'
def _mecard_escape(self, value):
value = value.replace('\\', '\\\\')
value = value.replace(';', '\\;')
value = value.replace(':', '\\:')
value = value.replace(',', '\\,')
return value
@property
def base64svg(self):
stream = BytesIO()
self._qr.svg(stream, scale=4)
return b64encode(stream.getvalue()).decode('utf-8')