-
Notifications
You must be signed in to change notification settings - Fork 11
/
pid_loader.py
152 lines (124 loc) · 4.77 KB
/
pid_loader.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# pid_loader.py
# Copyright (C) 2011 Simon Newton
# Load PID data.
import common
import logging
from model import Command, Pid
class UnknownManufacturerException(Exception):
"""Raised when the manufacturer ID doesn't exist."""
class PidLoader():
"""Load pids."""
def LookupPid(self, pid_id, manufacturer_id):
"""
Lookup a PID
Returns:
A tuple of (manufacturer, pid) data objects.
"""
manufacturer = common.GetManufacturer(manufacturer_id)
if manufacturer is None:
raise UnknownManufacturerException(manufacturer_id)
pid_query = Pid.all()
pid_query.filter('pid_id = ', pid_id)
pid_query.filter('manufacturer = ', manufacturer.key())
result_set = pid_query.fetch(1)
if result_set:
return manufacturer, result_set[0]
else:
return manufacturer, None
def UpdateCommand(self, pid, new_pid_data, command_type):
"""Update a command if required.
Returns:
True if the command was updated, False otherwise.
"""
command_attr = '%s_command' % command_type
request_attr = '%s_request' % command_type
response_attr = '%s_response' % command_type
sub_device_attr = '%s_sub_device_range' % command_type
# We assume the pull request validator has run and checked the consistency
# of the PID data.
has_command = (request_attr in new_pid_data and
response_attr in new_pid_data and
sub_device_attr in new_pid_data)
existing_command = getattr(pid, command_attr)
if existing_command and has_command:
save = False
if existing_command.sub_device_range != new_pid_data[sub_device_attr]:
existing_command.sub_device_range = new_pid_data[sub_device_attr]
save = True
request = new_pid_data.get(request_attr)
if eval(existing_command.request) != request:
existing_command.request = str(request)
save = True
response = new_pid_data.get(response_attr)
if eval(existing_command.response) != response:
existing_command.response = str(response)
save = True
if save:
existing_command.put()
logging.info('Updated existing %s:%s' %
(new_pid_data['name'], command_type))
return save
elif existing_command and not has_command:
# remove the existing command
existing_command.delete()
setattr(pid, command_attr, None)
logging.info('Removed existing %s:%s' %
(command_type, new_pid_data['name']))
return True
elif has_command:
command = Command(sub_device_range=new_pid_data[sub_device_attr],
request=str(new_pid_data.get(request_attr)),
response=str(new_pid_data.get(response_attr)))
command.put()
setattr(pid, command_attr, command)
logging.info('Set %s:%s' %
(command_type, new_pid_data['name']))
return True
else:
return False
def UpdateIfRequired(self, new_pid_data, manufacturer_id=0):
"""
Check if we need to update the data for this PID.
Args:
pid: The PID data
manufacturer_id: The manufacturer_id for the PID data.
Returns:
True if the PID was updated, false otherwise.
"""
manufacturer, pid = self.LookupPid(new_pid_data['value'], manufacturer_id)
save = False
if not pid:
pid = Pid(manufacturer=manufacturer,
pid_id=new_pid_data['value'],
name=new_pid_data['name'])
if pid.link != new_pid_data.get('link'):
pid.link = new_pid_data.get('link')
save = True
if pid.notes != new_pid_data.get('notes'):
pid.notes = new_pid_data.get('notes')
save = True
if pid.draft != new_pid_data.get('draft', False):
pid.draft = new_pid_data.get('draft', False)
save = True
save |= self.UpdateCommand(pid, new_pid_data, 'discovery')
save |= self.UpdateCommand(pid, new_pid_data, 'get')
save |= self.UpdateCommand(pid, new_pid_data, 'set')
if save:
logging.info('Updated %s' % new_pid_data['name'])
pid.put()
return save