|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import logging |
|
18 import __init__ |
|
19 |
|
20 from cone.public import exceptions,plugin,utils,api |
|
21 |
|
22 class HcrRepository(object): |
|
23 FLAG_READ_ONLY = 1 |
|
24 FLAG_NON_VOLATILE = 2 |
|
25 FLAG_BOOT_ONLY = 4 |
|
26 |
|
27 def __init__(self, records, version=1, flags=FLAG_READ_ONLY): |
|
28 self.records = records |
|
29 self.version = version |
|
30 self.flags = flags |
|
31 |
|
32 def count_records(self, category_id, element_id): |
|
33 """ |
|
34 Return the number of records in the repository with the given |
|
35 setting ID (category ID - element ID pair). |
|
36 """ |
|
37 count = 0 |
|
38 for r in self.records: |
|
39 if r.category_id == category_id and r.element_id == element_id: |
|
40 count += 1 |
|
41 return count |
|
42 |
|
43 def get_duplicate_record_ids(self): |
|
44 """ |
|
45 Return a list of duplicate record IDs in the repository. |
|
46 The list contains tuples of the form (category_id, element_id). |
|
47 """ |
|
48 result = [] |
|
49 for r in self.records: |
|
50 count = self.count_records(r.category_id, r.element_id) |
|
51 record_id = (r.category_id, r.element_id) |
|
52 if count > 1 and record_id not in result: |
|
53 result.append(record_id) |
|
54 return result |
|
55 |
|
56 def __repr__(self): |
|
57 buf = ["HcrRepository(version=%r, flags=%r, records=[" % (self.version, self.flags)] |
|
58 if len(self.records) > 0: |
|
59 buf.append('\n') |
|
60 for record in sorted(self.records): |
|
61 buf.append(" %r,\n" % record) |
|
62 buf.append('])') |
|
63 return ''.join(buf) |
|
64 |
|
65 def __eq__(self, other): |
|
66 return sorted(self.records) == sorted(other.records) \ |
|
67 and self.version == other.version \ |
|
68 and self.flags == other.flags |
|
69 |
|
70 def __ne__(self, other): |
|
71 return not self.__eq__(other) |
|
72 |
|
73 class HcrRecord(object): |
|
74 # Record value types used in HCRML |
|
75 VALTYPE_INT32 = 'int32' |
|
76 VALTYPE_INT16 = 'int16' |
|
77 VALTYPE_INT8 = 'int8' |
|
78 VALTYPE_BOOL = 'bool' |
|
79 VALTYPE_UINT32 = 'uint32' |
|
80 VALTYPE_UINT16 = 'uint16' |
|
81 VALTYPE_UINT8 = 'uint8' |
|
82 VALTYPE_LIN_ADDR = 'linaddr' |
|
83 VALTYPE_BIN_DATA = 'bindata' |
|
84 VALTYPE_TEXT8 = 'text8' |
|
85 VALTYPE_ARRAY_INT32 = 'arrayint32' |
|
86 VALTYPE_ARRAY_UINT32 = 'arrayuint32' |
|
87 VALTYPE_INT64 = 'int64' |
|
88 VALTYPE_UINT64 = 'uint64' |
|
89 |
|
90 FLAG_UNINITIALIZED = 1 |
|
91 FLAG_MODIFIABLE = 2 |
|
92 FLAG_PERSISTENT = 4 |
|
93 |
|
94 |
|
95 def __init__(self, type, value, category_id, element_id, flags=0): |
|
96 # Check the value type |
|
97 val_types = [] |
|
98 for name, val in self.__class__.__dict__.iteritems(): |
|
99 if name.startswith('VALTYPE_'): |
|
100 val_types.append(val) |
|
101 if type not in val_types: |
|
102 raise ValueError("Invalid HCRML record type '%s'" % type) |
|
103 |
|
104 self.type = type |
|
105 self.value = value |
|
106 self.category_id = category_id |
|
107 self.element_id = element_id |
|
108 self.flags = flags |
|
109 |
|
110 def __repr__(self): |
|
111 return 'HcrRecord(type=%r, value=%r, category_id=%r, element_id=%r, flags=%r)' \ |
|
112 % (self.type, self.value, self.category_id, self.element_id, self.flags) |
|
113 |
|
114 def __lt__(self, other): |
|
115 # Note: |
|
116 # The < operator is implemented purely for the sake of sorting records |
|
117 # in unit tests for comparison, the sorting is NOT the one used when |
|
118 # actually writing the records to file |
|
119 attrs_to_check = ['type', 'value', 'category_id', 'element_id', 'flags'] |
|
120 for attr_name in attrs_to_check: |
|
121 x = getattr(self, attr_name) |
|
122 y = getattr(other, attr_name) |
|
123 if x < y: return True |
|
124 elif x == y: continue # Equal, so need to check the next one |
|
125 else: return False |
|
126 return False |
|
127 |
|
128 def __eq__(self, other): |
|
129 attrs_to_check = ['type', 'value', 'category_id', 'element_id', 'flags'] |
|
130 for attr_name in attrs_to_check: |
|
131 x = getattr(self, attr_name) |
|
132 y = getattr(other, attr_name) |
|
133 if x != y: return False |
|
134 return True |
|
135 |
|
136 def __ne__(self, other): |
|
137 return not self.__eq__(other) |