diff options
author | Pearu Peterson <pearu.peterson@gmail.com> | 2007-08-10 13:57:35 +0000 |
---|---|---|
committer | Pearu Peterson <pearu.peterson@gmail.com> | 2007-08-10 13:57:35 +0000 |
commit | d4375f2985a3e5f3e503960b925b4f0e3a307171 (patch) | |
tree | f943ee782ac2be0ccb073ef28b663a73d06cfea8 /numpy/f2py/lib/extgen/utils.py | |
parent | 907a90f4c28fe811d878abe66ae375a757c9ece3 (diff) | |
download | numpy-d4375f2985a3e5f3e503960b925b4f0e3a307171.tar.gz |
extgen: rewrite, clean up, update docs, simple example from Python reference manual.
Diffstat (limited to 'numpy/f2py/lib/extgen/utils.py')
-rw-r--r-- | numpy/f2py/lib/extgen/utils.py | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/numpy/f2py/lib/extgen/utils.py b/numpy/f2py/lib/extgen/utils.py new file mode 100644 index 000000000..b3666c8f6 --- /dev/null +++ b/numpy/f2py/lib/extgen/utils.py @@ -0,0 +1,126 @@ + +__all__ = ['Word', 'Line', 'Code', 'FileSource'] + +from base import Component + +class Word(Component): + template = '%(word)s' + + def initialize(self, word): + if not word: return None + self.word = word + return self + + def add(self, component, container_label=None): + raise ValueError('%s does not take components' % (self.__class__.__name__)) + + def __repr__(self): + return '%s(%s)' % (self.__class__.__name__, ', '.join(map(repr,[self.word]+[c for (c,l) in self.components]))) + + +class Line(Component): + + """ + >>> l = Line('hey') + >>> l += ' you ' + >>> l += 2 + >>> print l + Line('hey you 2') + >>> print l.generate() + hey you 2 + >>> l += l + >>> print l.generate() + hey you 2hey you 2 + """ + + template = '%(line)s' + + def initialize(self, *strings): + self.line = '' + map(self.add, strings) + return self + + def add(self, component, container_label=None): + if isinstance(component, Line): + self.line += component.line + elif isinstance(component, str): + self.line += component + elif component is None: + pass + else: + self.line += str(component) + + def __repr__(self): + return '%s(%s)' % (self.__class__.__name__, ', '.join(map(repr,[self.line]+[c for (c,l) in self.components]))) + + +class Code(Component): + + """ + >>> c = Code('start') + >>> c += 2 + >>> c += 'end' + >>> c + Code(Line('start'), Line('2'), Line('end')) + >>> print c.generate() + start + 2 + end + """ + + template = '%(Line)s' + + container_options = dict( + Line = dict(default = '<KILLLINE>', ignore_empty_content=True) + ) + component_container_map = dict( + Line = 'Line' + ) + default_component_class_name = 'Line' + + def initialize(self, *lines): + map(self.add, lines) + return self + + def add(self, component, label=None): + if isinstance(component, Code): + assert label is None,`label` + self.components += component.components + else: + Component.add(self, component, label) + + +class FileSource(Component): + + container_options = dict( + Content = dict(default='<KILLLINE>') + ) + + template = '%(Content)s' + + default_component_class_name = 'Code' + + component_container_map = dict( + Line = 'Content', + Code = 'Content', + ) + + def initialize(self, path, *components, **options): + self.path = path + map(self.add, components) + self._provides = options.pop('provides', path) + if options: self.warning('%s unused options: %s\n' % (self.__class__.__name__, options)) + return self + + def finalize(self): + self._provides = self.get_path() or self._provides + + def __repr__(self): + return '%s(%s)' % (self.__class__.__name__, ', '.join(map(repr,[self.path]+[c for (c,l) in self.components]))) + +def _test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": + _test() |