diff --git a/class_dumper.py b/class_dumper.py index e4db84a..62bf7f6 100644 --- a/class_dumper.py +++ b/class_dumper.py @@ -1,55 +1,64 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- " Mother class for all class dumpers " ################################################################ from abc import ABCMeta, abstractmethod from class_reader import ClassReader ################################################################ class ClassDumper(object): " Mother class for all class dumpers " __metaclass__ = ABCMeta - def __init__(self): + def __init__(self, output_file): self.base_types = ['int', 'double', 'float', 'unsigned int'] self.base_types = self.base_types + \ [e + ' *' for e in self.base_types] + \ [e + ' &' for e in self.base_types] - def dump(self, class_file, classes=None, **dummy_kwargs): + self.output_file = output_file + + def dump(self, class_file=None, classes=None, **dummy_kwargs): " perform the dump " - cls_reader = ClassReader() - _classes = cls_reader.read(class_file) + + fout = open(self.output_file, 'w') + + classes = [] + + if class_file is not None: + cls_reader = ClassReader() + classes = cls_reader.read(class_file) + sstr = "" - for _class in _classes: + for _class in classes: if classes is None: condition = True else: condition = (_class.name in classes) if condition: - sstr += self.dump_file(_class) + sstr += self.dump_class(_class, fout) else: print "ignore class '{0}'".format(_class.name) return sstr @abstractmethod - def dump_file(self, _class): + def dump_class(self, _class, _file): " formats the provided class as a string: abstract method " raise Exception('pure virtual function') @classmethod def base_type(cls, _type): " todo:: " temp_type = _type.replace('&', '') temp_type = temp_type.replace('*', '') temp_type = temp_type.strip() return temp_type diff --git a/class_dumper_classes.py b/class_dumper_classes.py index 973ecab..1c95b34 100644 --- a/class_dumper_classes.py +++ b/class_dumper_classes.py @@ -1,157 +1,160 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- ################################################################ +import argparse from class_dumper import ClassDumper -from class_reader import ClassReader ################################################################ class ClassDumperClasses(ClassDumper): " Class emplyed to output to classes text format " def __init__(self, output_file): - ClassDumper.__init__(self) + ClassDumper.__init__(self, output_file) self.output_file = output_file self.nb_tabulation = 0 - def dump(self, class_file=None, classes=None): - fout = open(self.output_file, 'w') - - if class_file is not None: - cls_reader = ClassReader() - classes = cls_reader.read(class_file) - for _class in classes: - self.dump_class(_class, fout) - def dump_class(self, _class, _file): " dumps a class into the provided file " sstr = self.format_class_declaration(_class) - self.incTabulation() + self._inc_tabulation() sstr += self.format_constructors(_class) sstr += self.format_methods(_class) sstr += self.format_members(_class) sstr += "\n" - self.decTabulation() + self._dec_tabulation() _file.write(sstr) - def format_class_declaration(self, _class): + @classmethod + def format_class_declaration(cls, _class): " forge a str representing the class " sstr = "class " + _class.name if _class.inheritance is not None: sstr += "(" + ", ".join(_class.inheritance) + ")" sstr += "\n" return sstr def format_constructors(self, _class): " forge a str representing the constructor " sstr = "" for encaps in ['public', 'private', 'protected']: meths = _class.getMethods(encaps) if _class.name in meths: for meth in meths[_class.name]: - sstr += self.formatMethod(_class, meth) + sstr += self._format_method(_class, meth) for encaps in ['public', 'private', 'protected']: meths = _class.getMethods(encaps) if '~' + _class.name in meths: for meth in meths['~' + _class.name]: - sstr += self.formatMethod(_class, meth) + sstr += self._format_method(_class, meth) return sstr def format_methods(self, _class): " forge a str representing methods " sstr = "" for encaps in ['public', 'private', 'protected']: meths = _class.getMethods(encaps) meths_names = set(meths.keys()) - set([_class.name, '~' + _class.name]) meths_names = list(meths_names) if len(meths_names) is not 0: for _name in meths_names: for meth in meths[_name]: - sstr += self.formatMethod(_class, meth) + sstr += self._format_method(_class, meth) return sstr def format_members(self, _class): " forge a str representing members " sstr = "" for encaps in ['public', 'private', 'protected']: membs = _class.getMembers(encaps) if len(membs) is not 0: for dummy_n, memb in membs.iteritems(): - sstr += self.formatMember(_class, memb) + sstr += self._format_member(_class, memb) sstr += "\n" return sstr - def formatMethod(self, _class, meth): + def _format_method(self, dummy_class, meth): - sstr = self.get_tabulation() - sstr += m.encapsulation + " " - if m.static: sstr += m.static + " " - if m.virtual: sstr += m.virtual + " " - if m.ret: sstr += m.ret + " " + sstr = self._get_tabulation() + sstr += meth.encapsulation + " " + if meth.static: + sstr += meth.static + " " + if meth.virtual: + sstr += meth.virtual + " " + if meth.ret: + sstr += meth.ret + " " - sstr += m.name + "(" - sstr += ", ".join([a + " " + b for b,a in list(m.args.iteritems())]) - sstr += ")" - if m.const: sstr += " const" - sstr+= ";\n" + sstr += meth.name + "(" + sstr += ", ".join([a + " " + b + for b, a in list(meth.args.iteritems())]) + sstr += ")" + if meth.const: + sstr += " const" + sstr += ";\n" - self.incTabulation() - - self.decTabulation() + self._inc_tabulation() + self._dec_tabulation() return sstr - def formatMember(self,c,m): - sstr = self.getTabulation() - sstr += m.encapsulation + " " - if m.static == 'static': sstr += m.static - sstr += m.type + " " + m.name + ';' + def _format_member(self, dummy_class, memb): + + sstr = self._get_tabulation() + sstr += memb.encapsulation + " " + if memb.static == 'static': + sstr += memb.static + sstr += memb.type + " " + memb.name + ';' return sstr - def getTabulation(self): + def _get_tabulation(self): return " " * self.nb_tabulation - def incTabulation(self): + def _inc_tabulation(self): self.nb_tabulation += 1 - def decTabulation(self): + def _dec_tabulation(self): self.nb_tabulation -= 1 +################################################################ -import argparse - -if __name__ == '__main__': +def main(): + parser = argparse.ArgumentParser( + description='Classes descriptor format producer for class representation') - parser = argparse.ArgumentParser(description='Classes descriptor format producer for class representation') - parser.add_argument('--class_file','-c', help='The class file to process',required=True) - parser.add_argument('--output_file','-o' , help='The file where to put the classes description',required=True) + parser.add_argument('--class_file', '-c', help='The class file to process', required=True) + parser.add_argument('--output_file', '-o', + help='The file where to put the classes description', required=True) args = parser.parse_args() args = vars(args) dumper_class = ClassDumperClasses(args['output_file']) dumper_class.dump(class_file=args['class_file']) + + +if __name__ == '__main__': + main()