1
+ import sys , re
2
+
3
+ from urllib2 import urlopen
4
+ from BeautifulSoup import BeautifulSoup
5
+
6
+ def getSoup (url ):
7
+ print 'Opening' , url , '...'
8
+ page = urlopen (url )
9
+ soup = BeautifulSoup (page )
10
+ return soup
11
+
12
+ def parseAll ():
13
+ soup = getSoup ("https://developer.android.com/reference/android/Manifest.permission.html" )
14
+ print ' parsing...'
15
+ table = soup .find ('table' , { 'id' : 'constants' , 'class' : 'responsive constants' })
16
+ entries = table .findAll ('tr' )
17
+ strList = ''
18
+ for entry in entries :
19
+ if not entry or not entry .attrs : continue
20
+ if 'absent' in entry .attrs [0 ][1 ]: continue
21
+ info = entry .find ('td' , {'width' :'100%' })
22
+ if info :
23
+ name = info .find ('code' ).find ('a' ).contents [0 ]
24
+ pieces = []
25
+ for piece in info .find ('p' ).contents :
26
+ piece_str = re .sub ('\s+' , ' ' , str (piece )).strip ()
27
+ if '<code>' in piece_str :
28
+ piece_str = piece .find ('a' ).contents [0 ].strip ();
29
+ pieces += [piece_str ]
30
+ if name and pieces :
31
+ desc = ' ' .join (pieces ).strip ().replace ('"' , '\\ "' )
32
+ strList += (',' if strList else '' ) + '\n "' + name + '", "' + desc + '"'
33
+ strList = 'static final String[] listing = {' + strList + '\n };\n '
34
+ return strList
35
+
36
+ def replaceAll (source , strList ):
37
+ print ' replacing...'
38
+ idx0 = source .find ('static final String[] listing = {' )
39
+ idx1 = source [idx0 :].find (' };' )
40
+ return source [:idx0 ] + strList + source [idx0 + idx1 + 5 :]
41
+
42
+ def parseDanger ():
43
+ soup = getSoup ("https://developer.android.com/guide/topics/security/permissions.html" )
44
+ print ' parsing...'
45
+ table = soup .find ('table' )
46
+ entries = table .findAll ('tr' )
47
+ strList = ''
48
+ for entry in entries :
49
+ items = entry .findAll ('li' )
50
+ for item in items :
51
+ name = item .find ('code' ).find ('a' ).contents [0 ]
52
+ strList += (',' if strList else '' ) + '\n "' + name + '"'
53
+ strList = 'static final String[] dangerous = {' + strList + '\n };\n '
54
+ return strList
55
+
56
+ def replaceDanger (source , strList ):
57
+ print ' replacing...'
58
+ idx0 = source .find ('static final String[] dangerous = {' )
59
+ idx1 = source [idx0 :].find (' };' )
60
+ return source [:idx0 ] + strList + source [idx0 + idx1 + 5 :]
61
+
62
+ print 'Reading Permissions.java...'
63
+ with open ('src/processing/mode/android/Permissions.java' , 'r' ) as f :
64
+ source = f .read ()
65
+
66
+ allList = parseAll ()
67
+ source = replaceAll (source , allList )
68
+
69
+ dangerList = parseDanger ()
70
+ source = replaceDanger (source , dangerList )
71
+
72
+ print 'Writing Permissions.java...'
73
+ with open ('src/processing/mode/android/Permissions.java' , 'w' ) as f :
74
+ f .write (source )
75
+ print 'Done.'
0 commit comments