5"""Module to manage IDL files."""
15from generator
import IsDartCollectionType, IsPureInterface
17_logger = logging.getLogger(
'database')
21 """The Database class manages a collection of IDL files stored
24 Each IDL is describing a single interface. The IDL files are written
in the
25 FremontCut syntax, which
is derived
from the Web IDL syntax
and includes
28 Database operations include adding, updating
and removing IDL files.
32 """Initializes a Database over a given directory.
35 root_dir -- a directory. If directory does not exist, it will
39 if not os.path.exists(root_dir):
40 _logger.debug(
'creating root directory %s' % root_dir)
52 new_database._interfaces_to_delete = copy.deepcopy(
54 new_database._enums = copy.deepcopy(self.
_enums)
61 """Deletes the database by deleting its directory"""
67 def _ScanForInterfaces(self):
68 """Iterates over the database files and lists all interface names.
71 A list of interface names.
77 if os.path.isfile(os.path.join(dirname, name)):
78 root, ext = os.path.splitext(name)
82 os.path.walk(self.
_root_dir, Visitor,
None)
85 def _FilePath(self, interface_name):
86 """Calculates the file path that a given interface should
90 interface_name -- the name of the interface.
92 return os.path.join(self.
_root_dir,
'%s.idl' % interface_name)
94 def _LoadInterfaceFile(self, interface_name):
95 """Loads an interface from the database.
98 An IDLInterface instance or None if the interface
is not found.
100 interface_name -- the name of the interface.
102 file_name = self._FilePath(interface_name)
103 _logger.info('loading %s' % file_name)
104 if not os.path.exists(file_name):
107 f = open(file_name,
'r')
114 if not idl_file.interfaces:
115 raise RuntimeError(
'No interface found in %s' % file_name)
116 elif len(idl_file.interfaces) > 1:
117 raise RuntimeError(
'Expected one interface in %s' % file_name)
119 interface = idl_file.interfaces[0]
124 """Loads all interfaces into memory.
132 """Serialize the database using pickle for faster startup in the future
134 output_file = open(os.path.join(self._root_dir, 'cache.pickle'),
'wb')
139 """Deserialize the database using pickle for fast startup
141 input_file_name = os.path.join(self._root_dir, 'cache.pickle')
142 if not os.path.isfile(input_file_name):
145 input_file = open(input_file_name,
'rb')
151 """Saves all in-memory interfaces into files."""
157 def _SaveInterfaceFile(self, interface):
158 """Saves an interface into the database.
161 interface -- an IDLInterface instance.
164 interface_name = interface.id
167 file_path = self.
_FilePath(interface_name)
168 _logger.debug(
'writing %s' % file_path)
170 dir_name = os.path.dirname(file_path)
171 if not os.path.exists(dir_name):
172 _logger.debug(
'creating directory %s' % dir_name)
176 text = idlrenderer.render(interface)
178 f = open(file_path,
'w')
183 """Returns True if the interface is in memory"""
187 """Returns an IDLInterface corresponding to the interface_name
191 interface_name -- the name of the interface.
194 raise RuntimeError(
'Interface %s is not loaded' % interface_name)
198 """Returns an IDLInterface corresponding to the interface_name
202 interface -- the name of the interface.
204 interface_name = interface.id
206 raise RuntimeError(
'Interface %s already exists' % interface_name)
210 """Returns a list of all loaded interfaces."""
213 res.append(interface)
217 """Deletes an interface from the database. File is deleted when
221 interface_name -- the name of the interface.
224 raise RuntimeError(
'Interface %s not found' % interface_name)
228 def _DeleteInterfaceFile(self, interface_name):
229 """Actual file deletion"""
230 file_path = self.
_FilePath(interface_name)
231 if os.path.exists(file_path):
232 _logger.debug(
'deleting %s' % file_path)
237 for parent
in interface.parents:
238 parent_name = parent.type.id
243 yield parent_interface
246 return enum_name
in self.
_enums
249 return self.
_enums[enum_name]
252 self.
_enums[enum.id] = enum
255 """Returns True if the dictionary is in memory"""
259 """Returns an IDLDictionary corresponding to the dictionary_name
263 dictionary_name -- the name of the dictionary.
266 raise RuntimeError(
'Dictionary %s is not loaded' % dictionary_name)
270 """Returns an IDLDictionary corresponding to the dictionary_name
274 dictionary -- the name of the dictionary.
276 dictionary_name = dictionary.id
278 raise RuntimeError(
'Dictionary %s already exists' % dictionary_name)
282 """Returns a list of all loaded dictionaries."""
285 res.append(dictionary)
289 """Returns True if the typedef is in memory"""
293 """Returns an IDLTypeDef corresponding to the type_def_name
297 type_def_name -- the name of the typedef.
300 raise RuntimeError(
'Typedef %s is not loaded' % type_def_name)
304 """Add only a typedef that a unions they map to any (no type)."""
305 type_def_name = type_def.id
307 raise RuntimeError(
'Typedef %s already exists' % type_def_name)
309 print(
' Added typedef %s' % type_def_name)
312 """Returns a list of all non-primary parents.
314 The list contains the interface objects for interfaces defined
in the
315 database,
and the name
for undefined interfaces.
318 def walk(parents, walk_result):
319 for parent
in parents:
320 parent_name = parent.type.id
321 if IsDartCollectionType(parent_name):
322 if not (parent_name
in walk_result):
323 walk_result.append(parent_name)
327 if not (parent_interface
in walk_result):
330 walk_result.append(parent_interface)
331 walk(parent_interface.parents, walk_result)
335 if interface.parents:
336 parent = interface.parents[0]
337 if (IsPureInterface(parent.type.id, self)
or
338 (propagate_event_target
and parent.type.id ==
'EventTarget')):
339 result = walk(interface.parents, [])
341 result = walk(interface.parents[1:], [])
def GetDictionaries(self)
def GetTypeDef(self, type_def_name)
def AddTypeDef(self, type_def)
def DeleteInterface(self, interface_name)
def TransitiveSecondaryParents(self, interface, propagate_event_target)
def GetEnum(self, enum_name)
def _FilePath(self, interface_name)
def GetInterface(self, interface_name)
def __init__(self, root_dir)
def AddDictionary(self, dictionary)
def HasEnum(self, enum_name)
def _LoadInterfaceFile(self, interface_name)
def _DeleteInterfaceFile(self, interface_name)
def HasDictionary(self, dictionary_name)
def AddInterface(self, interface)
def _ScanForInterfaces(self)
def _SaveInterfaceFile(self, interface)
def HasInterface(self, interface_name)
def GetDictionary(self, dictionary_name)
def Hierarchy(self, interface)
def HasTypeDef(self, type_def_name)
static void append(char **dst, size_t *count, const char *src, size_t n)
Visitor(Ts...) -> Visitor< Ts... >
def parse(repo_root, recipes_cfg_path)
def print(*args, **kwargs)