Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/web_bluetooth/compact_blacklist.py

Issue 2285663002: bluetooth: Added script for converting the Web Bluetooth blacklist. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved to folder web_bluetooth and fixed typo. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/web_bluetooth/compact_blacklist_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2016 The Chromium Authors. All rights reserved.
scottmg 2016/08/29 15:34:02 no (c)
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Script for converting the Web Bluetooth GATT blacklist into the format
7 expected by ContentBrowserClient#GetWebBluetoothBlacklist.
8
9 See:
10 https://github.com/WebBluetoothCG/registries/blob/master/gatt_blacklist.txt
11 content/public/browser/content_browser_client.h
12
13 Usage:
14 compact_blacklist.py <gatt_blacklist.txt>
15 """
16
17 import collections
18 import string
19 import sys
20
21 blacklist = collections.OrderedDict()
scottmg 2016/08/29 15:34:03 I would make this g_blacklist, or BLACKLIST, or be
22
23 UUID_LENGTH = 36
24 UUID_BASE_POSTFIX = '-0000-1000-8000-00805f9b34fb'
25
26
27 class BadLineException(Exception):
28 pass
scottmg 2016/08/29 15:34:02 2-space indent in Chromium still afaik: https://ww
29
30
31 class InvalidUUIDException(Exception):
32 pass
33
34
35 class DuplicateUUIDException(Exception):
36 pass
37
38
39 class InvalidExclusionException(Exception):
40 pass
41
42
43 def valid_uuid(uuid):
scottmg 2016/08/29 15:34:03 Functions should be UpperCase-style.
44 if len(uuid) != UUID_LENGTH:
45 return False
46 for i in range(UUID_LENGTH):
47 if i in [8, 13, 18, 23]:
48 if uuid[i] != '-':
49 return False
50 else:
51 if uuid[i] not in string.hexdigits:
52 return False
53 return True
54
55
56 # Note: this function shortens all UUIDs that use the Bluetooth base
scottmg 2016/08/29 15:34:02 Make this a """ docstring instead of a block comme
57 # UUID even thought the specification states that only an assigned
scottmg 2016/08/29 15:34:02 "thought" -> "though"
58 # UUID can be shortened. In this case it works fine, since the constructor in
59 # bluetooth_uuid.cc also works the same way.
60 def shorten_uuid(uuid):
61 if uuid[8:] == UUID_BASE_POSTFIX:
62 new_uuid = '%x' % int(uuid[:8], 16)
63 if len(new_uuid) in [4, 8]:
64 uuid = new_uuid
65 return uuid
66
67
68 def process(line):
69 line = line.strip().lower()
70 if not line or line.startswith('#'):
71 return
72 fields = line.split()
73 if len(fields) not in [1, 2]:
74 raise BadLineException('Badly formatted line: %s' % line)
75 uuid = fields[0]
76 if not valid_uuid(uuid):
77 raise InvalidUUIDException('Invalid UUID: %s' % line)
78 uuid = shorten_uuid(uuid)
79 if uuid in blacklist:
80 raise DuplicateUUIDException('Duplicate UUID: %s' % line)
81 if len(fields) == 1:
82 blacklist[uuid] = 'e'
83 elif fields[1] == 'exclude-writes':
84 blacklist[uuid] = 'w'
85 elif fields[1] == 'exclude-reads':
86 blacklist[uuid] = 'r'
87 else:
88 raise InvalidExclusionException('Invalid exclusion value: %s' % line)
89
90
91 def main():
92 if len(sys.argv) != 2:
93 sys.exit('Usage: %s <gatt_blacklist.txt>' % sys.argv[0])
scottmg 2016/08/29 15:34:03 print and return 1 here
94
95 try:
96 with open(sys.argv[1]) as f:
97 for line in f:
98 process(line)
99 print(','.join('%s:%s' % (uuid, blacklist[uuid]) for uuid in blacklist))
scottmg 2016/08/29 15:34:03 return 0 after here
100 except Exception as e:
101 sys.exit('Failed to compact blacklist. %s' % e)
scottmg 2016/08/29 15:34:02 and here
102
103
104 if __name__ == '__main__':
105 main()
scottmg 2016/08/29 15:34:02 sys.exit(main()) here
OLDNEW
« no previous file with comments | « no previous file | tools/web_bluetooth/compact_blacklist_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698