16
16
limitations under the License.
17
17
"""
18
18
19
- try :
20
- from urllib2 import urlopen , URLError
21
- except ImportError :
22
- from urllib .request import urlopen , URLError
23
- from bs4 import BeautifulSoup
24
- from os .path import join , dirname , basename
25
- from os import makedirs
26
- from errno import EEXIST
27
- from threading import Thread
28
- try :
29
- from Queue import Queue
30
- except ImportError :
31
- from queue import Queue
32
- from re import compile , sub
33
- from sys import stderr , stdout
34
- from itertools import takewhile
35
- import argparse
36
- from json import dump , load
37
- from zipfile import ZipFile
38
- from tempfile import gettempdir
19
+ from os .path import join , dirname
20
+ from json import load , dump
39
21
import warnings
40
22
from cmsis_pack_manager import Cache as _Cache
41
- from distutils .version import LooseVersion
42
23
43
24
from tools .flash_algo import PackFlashAlgo
44
25
49
30
LocalPackDir = dirname (__file__ )
50
31
LocalPackIndex = join (LocalPackDir , "index.json" )
51
32
LocalPackAliases = join (LocalPackDir , "aliases.json" )
33
+ LocalPackLegacyNames = join (LocalPackDir , "legacy-names.json" )
52
34
53
35
36
+ class _CacheLookup (object ):
37
+ def __init__ (self , index , legacy_names ):
38
+ self .index = index
39
+ self .legacy_names = legacy_names
54
40
55
- class Cache (_Cache ):
41
+ def __getitem__ (self , name ):
42
+ try :
43
+ return self .index [name ]
44
+ except KeyError :
45
+ return self .index [self .legacy_names [name ]]
46
+
47
+ def __contains__ (self , name ):
48
+ return name in self .index or name in self .legacy_names
49
+
50
+
51
+ class Cache (object ):
56
52
""" The Cache object is the only relevant API object at the moment
57
53
58
54
Constructing the Cache object does not imply any caching.
59
55
A user of the API must explicitly call caching functions.
60
56
61
- :param silent: A boolean that, when True, significantly reduces the printing of this Object
57
+ :param silent: Not used
62
58
:type silent: bool
63
- :param no_timeouts: A boolean that, when True, disables the default connection timeout and low speed timeout for downloading things.
59
+ :param no_timeouts: Not used
64
60
:type no_timeouts: bool
65
61
"""
66
- def __init__ (self , silent , no_timeouts ):
67
- super ( Cache , self ). __init__ (
62
+ def __init__ (self , silent , no_timeouts ):
63
+ self . _cache = _Cache (
68
64
silent , no_timeouts ,
69
65
json_path = LocalPackDir , data_path = LocalPackDir
70
66
)
67
+ try :
68
+ self ._legacy_names = load (open (LocalPackLegacyNames ))
69
+ except IOError :
70
+ self ._legacy_names = {}
71
71
72
72
def _get_sectors (self , device ):
73
73
"""Extract sector sizes from device FLM algorithm
74
74
75
- Will return None if there is no algorithm, pdsc URL formatted in correctly
75
+ Will return None if there is no algorithm, pdsc URL formatted in
76
+ correctly
76
77
77
78
:return: A list tuples of sector start and size
78
79
:rtype: [list]
79
80
"""
80
81
try :
81
- pack = self .pack_from_cache (device )
82
+ pack = self ._cache . pack_from_cache (device )
82
83
ret = []
83
84
for algo in device ['algorithms' ]:
84
85
try :
85
- flm = pack .open (algo ["file_name" ].replace ("\\ \\ " , "/" ).replace ("\\ " , "/" ))
86
+ flm = pack .open (
87
+ algo ["file_name" ]
88
+ .replace ("\\ \\ " , "/" )
89
+ .replace ("\\ " , "/" )
90
+ )
86
91
flash_alg = PackFlashAlgo (flm .read ())
87
92
sectors = [(flash_alg .flash_start + offset , size )
88
93
for offset , size in flash_alg .sector_sizes ]
@@ -97,14 +102,21 @@ def _get_sectors(self, device):
97
102
print (e )
98
103
return None
99
104
105
+ @property
106
+ def index (self ):
107
+ return _CacheLookup (self ._cache .index , self ._legacy_names )
108
+
109
+ def cache_descriptors (self ):
110
+ self ._cache .cache_descriptors
111
+
100
112
def cache_everything (self ):
101
- super ( Cache , self ) .cache_everything ()
102
- for name , device in self .index .items ():
113
+ self . _cache .cache_everything ()
114
+ for name , device in self ._cache . index .items ():
103
115
if name != "version" :
104
116
device ["sectors" ] = self ._get_sectors (device )
105
117
self .generate_index ()
106
118
107
- def get_svd_file (self , device_name ) :
119
+ def get_svd_file (self , device_name ):
108
120
"""Retrieve the flash algorithm file for a particular part.
109
121
110
122
Assumes that both the PDSC and the PACK file associated with that part are in the cache.
@@ -120,5 +132,6 @@ def get_svd_file(self, device_name) :
120
132
121
133
def generate_index (self ):
122
134
with open (LocalPackIndex , "wb+" ) as out :
123
- self ._index ["version" ] = "0.2.0"
124
- dump (self ._index , out , indent = 4 , sort_keys = True )
135
+ self ._cache .index ["version" ] = "0.2.0"
136
+ dump (self ._cache .index , out , indent = 4 , sort_keys = True )
137
+
0 commit comments