def as_event_class(obj): """ Convert obj into a subclass of AbinitEvent. obj can be either a class or a string with the class name or the YAML tag """ if is_string(obj): for c in all_subclasses(AbinitEvent): if c.__name__ == obj or c.yaml_tag == obj: return c raise ValueError("Cannot find event class associated to %s" % obj) # Assume class. assert obj in all_subclasses(AbinitEvent) return obj
def as_event_class(obj): """ Convert obj into a subclass of AbinitEvent. obj can be either a class or a string with the class name or the YAML tag """ if is_string(obj): for c in all_subclasses(AbinitEvent): if c.__name__ == obj or c.yaml_tag == obj: return c raise ValueError("Cannot find event class associated to %s" % obj) # Assume class. assert obj in all_subclasses(AbinitEvent) return obj
def get_event_handler_classes(categories=None): """Return the list of handler classes.""" classes = [ c for c in all_subclasses(EventHandler) if c not in _ABC_EVHANDLER_CLASSES ] return classes
def test_if_all_scripts_are_tested(): """Testing if all scripts are tested""" tested_scripts = set(os.path.basename(c.script) for c in all_subclasses(ScriptTest)) all_scripts = set(f for f in os.listdir(script_dir) if f.endswith(".py")) not_tested = all_scripts.difference(tested_scripts) if not_tested: print("The following scripts are not tested") for i, s in enumerate(not_tested): print("[%d] %s" % (i, s))
def test_if_all_scripts_are_tested(): """Testing if all scripts are tested""" tested_scripts = set(os.path.basename(c.script) for c in all_subclasses(ScriptTest)) all_scripts = set(f for f in os.listdir(script_dir) if f.endswith(".py")) not_tested = all_scripts.difference(tested_scripts) if not_tested: print("The following scripts are not tested") for i, s in enumerate(not_tested): print("[%d] %s" % (i, s))
def read_field(self): """ Read and return the first field variable found in the netcdf_ file. Raise: `ValueError` if cannot find a field or multiple fields are found. """ found = [field_cls for field_cls in all_subclasses(_Field) if field_cls.netcdf_name in self.rootgrp.variables] if not found or len(found) > 1: raise ValueError("Found `%s` fields in file: %s" % (str(found), self.path)) field_cls = found[0] return self.read_denpot(varname=field_cls.netcdf_name, field_cls=field_cls)
def class_from_netcdf_name(cls, netcdf_name): """Return the subclass associated to the given netcdf name.""" nfound = 0 for subclass in all_subclasses(cls): if subclass.netcdf_name == netcdf_name: nfound += 1 out_cls = subclass if nfound == 0: raise ValueError("Cannot find subclass associated to `%s`" % str(netcdf_name)) if nfound > 1: raise ValueError("Find multiple subclasses associated to `%s`" % str(netcdf_name)) return out_cls
def class_from_netcdf_name(cls, netcdf_name): """Return the subclass associated to the given netcdf name.""" nfound = 0 for subclass in all_subclasses(cls): if subclass.netcdf_name == netcdf_name: nfound += 1 out_cls = subclass if nfound == 0: raise ValueError("Cannot find subclass associated to `%s`" % str(netcdf_name)) if nfound > 1: raise ValueError("Find multiple subclasses associated to `%s`" % str(netcdf_name)) return out_cls
def from_qtype_and_id(qtype, queue_id, qname=None): """ Return a new istance of the appropriate subclass. Args: qtype: String specifying the Resource manager type. queue_id: Job identifier. qname: Name of the queue (optional). """ for cls in all_subclasses(QueueJob): if cls.QTYPE == qtype: break else: logger.critical("Cannot find QueueJob subclass registered for qtype %s" % qtype) cls = QueueJob return cls(queue_id, qname=qname)
def autodoc_event_handlers(stream=sys.stdout): """ Print to the given string, the documentation for the events and the associated handlers. """ lines = [] for cls in all_subclasses(EventHandler): if cls in _ABC_EVHANDLER_CLASSES: continue event_class = cls.event_class lines.extend(cls.cls2str().split("\n")) # Here we enforce the abstract protocol of the class # The unit test in tests_events will detect the problem. if not hasattr(cls, "can_change_physics"): raise RuntimeError("%s: can_change_physics must be defined" % cls) stream.write("\n".join(lines) + "\n")
def autodoc_event_handlers(stream=sys.stdout): """ Print to the given string, the documentation for the events and the associated handlers. """ lines = [] for cls in all_subclasses(EventHandler): if cls in _ABC_EVHANDLER_CLASSES: continue event_class = cls.event_class lines.extend(cls.cls2str().split("\n")) # Here we enforce the abstract protocol of the class # The unit test in tests_events will detect the problem. if not hasattr(cls, "can_change_physics"): raise RuntimeError("%s: can_change_physics must be defined" % cls) stream.write("\n".join(lines) + "\n")
def test_if_all_scripts_are_tested(): """Testing if all scripts are tested""" tested_scripts = set(os.path.basename(c.script) for c in all_subclasses(ScriptTest)) all_scripts = set(f for f in os.listdir(script_dir) if f.endswith(".py")) not_tested = all_scripts.difference(tested_scripts) if not_tested: print("The following scripts are not tested") for i, s in enumerate(not_tested): print("[%d] %s" % (i, s)) assert not_tested == set([ "abibatch.py", "mrgddb.py", "abiGWprint.py", "abiGWstore.py", "abiGWoutput.py", "abiphonons.py", "abiGWsetup.py", ])
def test_if_all_scripts_are_tested(): """Testing if all scripts are tested""" tested_scripts = set( os.path.basename(c.script) for c in all_subclasses(ScriptTest)) all_scripts = set(f for f in os.listdir(script_dir) if f.endswith(".py")) not_tested = all_scripts.difference(tested_scripts) if not_tested: print("The following scripts are not tested") for i, s in enumerate(not_tested): print("[%d] %s" % (i, s)) assert not_tested == set([ "abibatch.py", "mrgddb.py", "abiGWprint.py", "abiGWstore.py", "abiGWoutput.py", "abiphonons.py", "abiGWsetup.py", ])
def get_event_handler_classes(categories=None): """Return the list of handler classes.""" classes = [c for c in all_subclasses(EventHandler) if c not in _ABC_EVHANDLER_CLASSES] return classes