pep8 standarization

This commit is contained in:
Fanani M. Ihsan 2016-06-24 19:00:55 +07:00
parent 5abb3b5134
commit c6b8cf1200
8 changed files with 67 additions and 75 deletions

View File

@ -1,6 +1,6 @@
# pyzk # pyzk
pyzk is unofficial library of zksoftware the fingerprint attendance machine. pyzk is an unofficial library of zksoftware the fingerprint attendance machine.
# Installation # Installation
@ -14,25 +14,24 @@ Complete documentation can be found at [Readthedocs](http://pyzk.readthedocs.io/
# Api Usage # Api Usage
Create ZK object and you will ready to call api. Create an ZK instnace and you will ready to call api.
* Basic Usage * Basic Usage
``` ```
import zk from zk import ZK, const
from zk import const
conn = False sys.path.append("zk")
zk = zk.ZK(ip='192.168.1.201', port=4370, timeout=5)
conn = None
zk = ZK('192.168.1.10', port=4370, timeout=5)
try: try:
print 'Connecting to device ...'
conn = zk.connect() conn = zk.connect()
print 'Disabling device ...'
# disable (lock) the device, make sure no activity when process run conn.disable_device()
zk.disable_device() print 'Firmware Version: : {}'.format(conn.get_firmware_version())
# print '--- Get User ---'
# Do another task here users = conn.get_users()
firmware_version = zk.get_firmware_version()
print 'Firmware Version: : {}'.format(firmware_version)
users = zk.get_users()
for user in users: for user in users:
privilege = 'User' privilege = 'User'
if user.privilege == const.USER_ADMIN: if user.privilege == const.USER_ADMIN:
@ -45,67 +44,69 @@ try:
print ' Group ID : {}'.format(user.group_id) print ' Group ID : {}'.format(user.group_id)
print ' User ID : {}'.format(user.user_id) print ' User ID : {}'.format(user.user_id)
zk.enable_device() print "Voice Test ..."
conn.test_voice()
print 'Enabling device ...'
conn.enable_device()
except Exception, e: except Exception, e:
print "Process terminate : {}".format(e) print "Process terminate : {}".format(e)
finally: finally:
if conn: if conn:
zk.disconnect() conn.disconnect()
``` ```
* Connect/Disconnect * Connect/Disconnect
``` ```
zk.connect() conn = zk.connect()
zk.disconnect() conn.disconnect()
``` ```
* Disable/Enable Connected Device * Disable/Enable Connected Device
``` ```
zk.disable_device() conn.disable_device()
zk.enable_device() conn.enable_device()
``` ```
* Ger Firmware Version * Ger Firmware Version
``` ```
zk.get_firmware_version() conn.get_firmware_version()
``` ```
* User Operation * User Operation
``` ```
# Create user # Create user
zk.set_user(uid=1, name='Fanani M. Ihsan', privilege=const.USER_ADMIN, password='12345678', group_id='', user_id='123') conn.set_user(uid=1, name='Fanani M. Ihsan', privilege=const.USER_ADMIN, password='12345678', group_id='', user_id='123')
# Get all users (will return list of User object) # Get all users (will return list of User object)
users = zk.get_users() users = conn.get_users()
# Delete User # Delete User
zk.delete_user(uid=1) conn.delete_user(uid=1)
``` ```
* Attendance Record * Attendance Record
``` ```
# Get attendances (will return list of Attendance object) # Get attendances (will return list of Attendance object)
attendances = zk.get_attendance() attendances = conn.get_attendance()
# Clear attendances record # Clear attendances record
zk.clear_attendance() conn.clear_attendance()
``` ```
* Test voice * Test voice
``` ```
zk.test_voice() conn.test_voice()
``` ```
* Device Maintenance * Device Maintenance
``` ```
# shutdown connected device # shutdown connected device
zk.power_off() conn.power_off()
# restart connected device # restart connected device
zk.restart() conn.restart()
``` ```
# Related Project # Related Project

View File

@ -1,18 +1,19 @@
# -*- coding: utf-8 -*-
from setuptools import setup from setuptools import setup
setup( setup(
name='pyzk', name='pyzk',
version='0.4', version='0.4',
description='an unofficial library of zksoftware the fingerprint attendance machine.', description='an unofficial library of zksoftware fingerprint device',
url='https://github.com/fananimi/pyzk', url='https://github.com/fananimi/pyzk',
author='Fanani M. Ihsan', author='Fanani M. Ihsan',
author_email='fanani.mi@gmail.com', author_email='fanani.mi@gmail.com',
license='LICENSE.txt', license='LICENSE.txt',
packages=['zk'], packages=['zk'],
keywords = [ keywords=[
'zksoftware',
'pyzk',
'zk', 'zk',
'pyzk',
'zksoftware',
'attendance machine', 'attendance machine',
'fingerprint', 'fingerprint',
'biometrics', 'biometrics',

35
test.py
View File

@ -1,26 +1,20 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys import sys
from zk import ZK, const
sys.path.append("zk") sys.path.append("zk")
import zk conn = None
from zk import const zk = ZK('192.168.1.10', port=4370, timeout=5)
conn = False
zk = zk.ZK('192.168.1.201', port=4370, timeout=5)
try: try:
print 'Connecting to device ...' print 'Connecting to device ...'
conn = zk.connect() conn = zk.connect()
print 'Disabling device ...' print 'Disabling device ...'
zk.disable_device() conn.disable_device()
print 'Firmware Version: : {}'.format(zk.get_firmware_version()) print 'Firmware Version: : {}'.format(conn.get_firmware_version())
# Load test create 2000 users
# for i in range(1, 2000+1):
# privilege = const.USER_DEFAULT
# if i == 1:
# privilege = const.USER_ADMIN
# print zk.set_user(uid=i, name='user #{}'.format(i), privilege=privilege, password='12345678', group_id='', user_id='{}'.format(i))
# print '--- Get User ---' # print '--- Get User ---'
users = zk.get_users() users = conn.get_users()
for user in users: for user in users:
privilege = 'User' privilege = 'User'
if user.privilege == const.USER_ADMIN: if user.privilege == const.USER_ADMIN:
@ -34,18 +28,11 @@ try:
print ' User ID : {}'.format(user.user_id) print ' User ID : {}'.format(user.user_id)
print "Voice Test ..." print "Voice Test ..."
zk.test_voice() conn.test_voice()
# print 'Restarting device ...'
# zk.restart()
# print 'Turning off device ...'
# zk.power_off()
print 'Enabling device ...' print 'Enabling device ...'
zk.enable_device() conn.enable_device()
print 'Disconnecting to device ...'
zk.disconnect()
except Exception, e: except Exception, e:
print "Process terminate : {}".format(e) print "Process terminate : {}".format(e)
finally: finally:
if conn: if conn:
zk.disconnect() conn.disconnect()

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from base import ZK from base import ZK
VERSION = (0,3) VERSION = (0, 3)
__all__ = ['ZK'] __all__ = ['ZK']

View File

@ -6,7 +6,7 @@ class Attendance(object):
self.status = status self.status = status
def __str__(self): def __str__(self):
return self.user_id return '<Attendance>: {}'.format(self.user_id)
def __repr__(self): def __repr__(self):
return '<Attendance>: {}'.format(self.user_id) return '<Attendance>: {}'.format(self.user_id)

View File

@ -1,13 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from datetime import datetime from datetime import datetime
from socket import AF_INET, SOCK_DGRAM, socket
from struct import pack, unpack from struct import pack, unpack
from socket import socket, AF_INET, SOCK_DGRAM
from zk import const from zk import const
from zk.exception import ZKErrorResponse, ZKNetworkError
from zk.attendance import Attendance from zk.attendance import Attendance
from zk.exception import ZKErrorResponse, ZKNetworkError
from zk.user import User from zk.user import User
class ZK(object): class ZK(object):
is_connect = False is_connect = False
@ -26,7 +27,7 @@ class ZK(object):
Puts a the parts that make up a packet together and packs them into a byte string Puts a the parts that make up a packet together and packs them into a byte string
''' '''
buf = pack('HHHH', command, checksum, session_id, reply_id) + command_string buf = pack('HHHH', command, checksum, session_id, reply_id) + command_string
buf = unpack('8B'+'%sB' % len(command_string), buf) buf = unpack('8B' + '%sB' % len(command_string), buf)
checksum = unpack('H', self.__create_checksum(buf))[0] checksum = unpack('H', self.__create_checksum(buf))[0]
reply_id += 1 reply_id += 1
if reply_id >= const.USHRT_MAX: if reply_id >= const.USHRT_MAX:
@ -100,8 +101,8 @@ class ZK(object):
def __reverse_hex(self, hex): def __reverse_hex(self, hex):
data = '' data = ''
for i in reversed( xrange( len(hex)/2 ) ): for i in reversed(xrange(len(hex) / 2)):
data += hex[i*2:(i*2)+2] data += hex[i * 2:(i * 2) + 2]
return data return data
def __decode_time(self, t): def __decode_time(self, t):
@ -120,10 +121,10 @@ class ZK(object):
hour = t % 24 hour = t % 24
t = t / 24 t = t / 24
day = t % 31+1 day = t % 31 + 1
t = t / 31 t = t / 31
month = t % 12+1 month = t % 12 + 1
t = t / 12 t = t / 12
year = t + 2000 year = t + 2000
@ -149,7 +150,7 @@ class ZK(object):
self.is_connect = True self.is_connect = True
# set the session id # set the session id
self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2] self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2]
return True return self
else: else:
raise ZKErrorResponse("Invalid response") raise ZKErrorResponse("Invalid response")
@ -377,11 +378,11 @@ class ZK(object):
userdata = ''.join(userdata) userdata = ''.join(userdata)
userdata = userdata[11:] userdata = userdata[11:]
while len(userdata) >= 72: while len(userdata) >= 72:
uid, privilege, password, name, sparator, group_id, user_id = unpack( '2s2s8s28sc7sx23s', userdata.ljust(72)[:72]) uid, privilege, password, name, sparator, group_id, user_id = unpack('2s2s8s28sc7sx23s', userdata.ljust(72)[:72])
u1 = int(uid[0].encode("hex"), 16) u1 = int(uid[0].encode("hex"), 16)
u2 = int(uid[1].encode("hex"), 16) u2 = int(uid[1].encode("hex"), 16)
uid = u2 + (u1*256) uid = u2 + (u1 * 256)
name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore') name = unicode(name.strip('\x00|\x01\x10x'), errors='ignore')
privilege = int(privilege.encode("hex"), 16) privilege = int(privilege.encode("hex"), 16)
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore') password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
@ -478,7 +479,7 @@ class ZK(object):
attendance_data = ''.join(attendance_data) attendance_data = ''.join(attendance_data)
attendance_data = attendance_data[14:] attendance_data = attendance_data[14:]
while len(attendance_data) >= 38: while len(attendance_data) >= 38:
user_id, sparator, timestamp, status, space = unpack( '24sc4sc10s', attendance_data.ljust(40)[:40]) user_id, sparator, timestamp, status, space = unpack('24sc4sc10s', attendance_data.ljust(40)[:40])
user_id = user_id.strip('\x00|\x01\x10x') user_id = user_id.strip('\x00|\x01\x10x')
timestamp = self.__decode_time(timestamp) timestamp = self.__decode_time(timestamp)
@ -493,7 +494,6 @@ class ZK(object):
return attendances return attendances
def clear_attendance(self): def clear_attendance(self):
''' '''
clear all attendance record clear all attendance record

View File

@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
class ZKError(Exception): class ZKError(Exception):
pass pass
class ZKErrorResponse(ZKError): class ZKErrorResponse(ZKError):
pass pass
class ZKNetworkError(ZKError): class ZKNetworkError(ZKError):
pass pass

View File

@ -9,8 +9,8 @@ class User(object):
self.group_id = group_id self.group_id = group_id
self.user_id = user_id self.user_id = user_id
def __repr__(self): def __str__(self):
return self.name return '<User>: {}'.format(self.name)
def __repr__(self): def __repr__(self):
return '<User>: {}'.format(self.name) return '<User>: {}'.format(self.name)