21
21
####################################################################################################
22
22
23
23
from pathlib import Path
24
- from typing import Iterator
24
+ from typing import Iterable , Iterator
25
25
26
26
import logging
27
27
import os
28
28
import pickle
29
29
import re
30
30
31
- from .SpiceInclude import SpiceInclude
32
31
from PySpice .Spice .Parser import Subcircuit , Model
33
32
from PySpice .Tools import PathTools
33
+ from .SpiceInclude import SpiceInclude , is_yaml
34
34
35
35
####################################################################################################
36
36
@@ -70,25 +70,38 @@ class SpiceLibrary:
70
70
71
71
##############################################
72
72
73
- def __init__ (self , root_path : str | Path , scan : bool = True ) -> None :
73
+ def __init__ (self , root_path : str | Path , scan : bool = False ) -> None :
74
74
self ._path = PathTools .expand_path (root_path )
75
75
if not self ._path .exists ():
76
76
self ._path .mkdir (parents = True )
77
77
self ._logger .info (f"Created { self ._path } " )
78
78
self ._subcircuits = {}
79
79
self ._models = {}
80
+ if not scan :
81
+ if self .has_db_path :
82
+ self .load ()
83
+ else :
84
+ self ._logger .info ("Initialize library..." )
85
+ scan = True
80
86
if scan :
81
87
self .scan ()
82
88
self .save ()
83
- else :
84
- self .load ()
85
89
86
90
##############################################
87
91
88
92
@property
89
93
def db_path (self ) -> Path :
90
94
return self ._path .joinpath ('db.pickle' )
91
95
96
+ @property
97
+ def has_db_path (self ) -> bool :
98
+ return self .db_path .exists ()
99
+
100
+ ##############################################
101
+
102
+ def __bool__ (self ) -> bool :
103
+ return bool (self ._subcircuits or self ._models )
104
+
92
105
##############################################
93
106
94
107
def __getstate__ (self ):
@@ -114,13 +127,10 @@ def save(self) -> None:
114
127
pickle .dump (_ , fh )
115
128
116
129
def load (self ) -> None :
117
- if self .db_path .exists ():
118
- self ._logger .info (f"Load { self .db_path } " )
119
- with open (self .db_path , 'rb' ) as fh :
120
- _ = pickle .load (fh )
121
- self .__setstate__ (_ )
122
- else :
123
- self ._logger .warning ("uninitialised library" )
130
+ self ._logger .info (f"Load { self .db_path } " )
131
+ with open (self .db_path , 'rb' ) as fh :
132
+ _ = pickle .load (fh )
133
+ self .__setstate__ (_ )
124
134
125
135
##############################################
126
136
@@ -155,40 +165,43 @@ def list_categories(self) -> str:
155
165
##############################################
156
166
157
167
def scan (self ) -> None :
168
+ self ._logger .info (f"Scan { self ._path } ..." )
158
169
for path in PathTools .walk (self ._path ):
159
- extension = path .suffix .lower ()
160
- if extension in self .EXTENSIONS :
170
+ _ = path .suffix .lower ()
171
+ if _ in self .EXTENSIONS :
161
172
self ._handle_library (path )
162
173
163
174
##############################################
164
175
165
176
def _handle_library (self , path : Path ) -> None :
166
177
spice_include = SpiceInclude (path )
178
+ # Fixme: check overwrite
167
179
self ._models .update ({_ .name : path for _ in spice_include .models })
168
180
self ._subcircuits .update ({_ .name : path for _ in spice_include .subcircuits })
169
181
170
182
##############################################
171
183
172
184
def delete_yaml (self ) -> None :
173
185
for path in PathTools .walk (self ._path ):
174
- extension = path .suffix .lower ()
175
- if extension == '.yaml' :
186
+ if is_yaml (path ):
176
187
self ._logger .info (f"{ NEWLINE } Delete { path } " )
177
188
path .unlink ()
178
189
179
190
##############################################
180
191
181
192
def __getitem__ (self , name : str ) -> Subcircuit | Model :
182
- if not ( self . _subcircuits or self . _models ) :
193
+ if not self :
183
194
self ._logger .warning ("Empty library" )
184
195
if name in self ._subcircuits :
185
- return self ._subcircuits [name ]
196
+ path = self ._subcircuits [name ]
186
197
elif name in self ._models :
187
- return self ._models [name ]
198
+ path = self ._models [name ]
188
199
else :
189
200
# print('Library {} not found in {}'.format(name, self._path))
190
201
# self._logger.warn('Library {} not found in {}'.format(name, self._path))
191
202
raise KeyError (name )
203
+ # Fixme: lazy ???
204
+ return SpiceInclude (path )[name ]
192
205
193
206
##############################################
194
207
@@ -214,14 +227,12 @@ def models(self) -> Iterator[Model]:
214
227
215
228
# ##############################################
216
229
217
- def search (self , regexp : str ) -> dict [ str , Subcircuit | Model ]:
230
+ def search (self , regexp : str ) -> Iterable [ tuple [ str , SpiceInclude ] ]:
218
231
""" Return dict of all models/subcircuits with names matching regex. """
219
232
regexp = re .compile (regexp )
220
- matches = {}
221
233
models_subcircuits = {** self ._models , ** self ._subcircuits }
222
234
if not models_subcircuits :
223
235
self ._logger .warning ("Empty library" )
224
236
for name , _ in models_subcircuits .items ():
225
237
if regexp .search (name ):
226
- matches [name ] = _
227
- return matches
238
+ yield name , SpiceInclude (_ )
0 commit comments