def _load_dir(self, cf_dir, namespace): """ Create a list of compile units for the given directory @param cf_dir: The directory to get all files from @param namespace: The namespace the files need to be added to """ for cf_file in glob.glob(os.path.join(cf_dir, "model", '*')): file_name = os.path.basename(cf_file) if os.path.isdir(cf_file) and self._is_cf_module(cf_file): # create a new namespace new_ns = Namespace(file_name) new_ns.parent = namespace self.graph.add_namespace(new_ns, namespace) self._units.append(new_ns) self._load_dir(cf_file, new_ns) elif file_name[-3:] == ".cf": if file_name == self.__init_cf: namespace.unit = FileCompileUnit(self, cf_file, namespace) else: new_ns = Namespace(file_name[:-3]) new_ns.parent = namespace self.graph.add_namespace(new_ns, namespace) self._units.append(new_ns) new_ns.unit = FileCompileUnit(self, cf_file, new_ns)
def load(self): """ Compile the configuration model """ root_ns = Namespace("__root__") main_ns = Namespace("__config__") main_ns.parent = root_ns # add namespaces to the graph self.graph.add_namespace(root_ns) self._units.append(root_ns) self.graph.add_namespace(main_ns, root_ns) self._units.append(main_ns) self._add_other_ns(root_ns) # load main file cf_file = FileCompileUnit(self, self.__cf_file, main_ns) main_ns.unit = cf_file # load libraries for library in self.__lib_dirs: # TODO: take into account the precedence self.load_libdir(library, root_ns) self.__root_ns = root_ns return root_ns
def _add_other_ns(self, root_ns): """ Add the namespace of builtin types and plugins. """ type_ns = Namespace("__types__") plugin_ns = Namespace("__plugins__") type_ns.parent = root_ns plugin_ns.parent = root_ns type_ns.unit = BuiltinCompileUnit(self, type_ns) plugin_ns.unit = PluginCompileUnit(self, plugin_ns) self.graph.add_namespace(type_ns, root_ns) self._units.append(type_ns) self.graph.add_namespace(plugin_ns, root_ns) self._units.append(plugin_ns)