This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, generate has been updated via 6c56dcd01573c443d0cd512a43a5966aa6edfde8 (commit) from 27ea5ba0493ced3a9af7c3d6ee912fe87e78f0e3 (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/6c56dcd01573c443d0cd512a43a5966aa6edf...
commit 6c56dcd01573c443d0cd512a43a5966aa6edfde8 Author: Tomas Gavenciak gavento@ucw.cz Date: Thu May 26 22:44:05 2011 +0200
Mid-way restructuring the generating system
Make rules now work for few targets (GP_Pixel.gen.[ch])
diff --git a/gen.mk b/gen.mk index 28b1bc6..f15aae6 100644 --- a/gen.mk +++ b/gen.mk @@ -35,3 +35,10 @@ GENERATORS=$(PYTHON_FILES) # Generated files depend on python generators and libs # %.gen.c %.gen.h: $(GENERATORS) +ifdef VERBOSE + ${PYTHON} ${TOPDIR}/pylib/bin/generate_file.py "$@" +else + @echo "GEN $@" + @${PYTHON} ${TOPDIR}/pylib/bin/generate_file.py "$@" +endif + diff --git a/libs/core/Makefile b/libs/core/Makefile index 989d786..8126e00 100644 --- a/libs/core/Makefile +++ b/libs/core/Makefile @@ -8,15 +8,3 @@ include $(TOPDIR)/gen.mk include $(TOPDIR)/include.mk include $(TOPDIR)/lib.mk
-$(INCLUDE_PREFIX)GP_Pixel.gen.h GP_Pixel.gen.c: ${GENERATORS} - ${PYTHON} "${PYLIBSDIR}/gfxprim/core/make_GP_Pixel.py" - "$(INCLUDE_PREFIX)GP_Pixel.gen.h" "GP_Pixel.gen.c" - -$(INCLUDE_PREFIX)GP_Blit.gen.h GP_Blit.gen.c: ${GENERATORS} - ${PYTHON} "${PYLIBSDIR}/gfxprim/core/make_GP_Blit.py" - "$(INCLUDE_PREFIX)GP_Blit.gen.h" "GP_Blit.gen.c" - -$(INCLUDE_PREFIX)GP_Pixel_Scale.gen.h: ${GENERATORS} - ${PYTHON} "${PYLIBSDIR}/gfxprim/core/make_GP_Pixel_Scale.py" - "$(INCLUDE_PREFIX)GP_Pixel_Scale.gen.h" - diff --git a/pylib/bin/generate_file.py b/pylib/bin/generate_file.py index ee56e8f..086f801 100644 --- a/pylib/bin/generate_file.py +++ b/pylib/bin/generate_file.py @@ -7,16 +7,19 @@ #
import sys -from gfxprim import generators.utils, die +from gfxprim.generators import utils +from gfxprim import die
usage = """Usage: %s [files_to_generate...] The files are matched only based on path suffixes, but written to given paths. """
-def __main__(): - generators.utils.load_generators() +def main(): + utils.load_generators() if len(sys.argv) <= 1: die(usage) for f in sys.argv[1:]: - generators.utils.generate_file(f) + utils.generate_file(f)
+if __name__ == '__main__': + main() diff --git a/pylib/gfxprim/__init__.py b/pylib/gfxprim/__init__.py index cacbf91..7b3b919 100644 --- a/pylib/gfxprim/__init__.py +++ b/pylib/gfxprim/__init__.py @@ -4,8 +4,12 @@ # 2011 - Tomas Gavenciak gavento@ucw.cz #
+import sys + def die(msg): "Exit(1) with a message." + if not msg.endswith('n'): + msg += 'n' sys.stderr.write(msg) sys.exit(1)
diff --git a/pylib/gfxprim/generators/core/make_GP_Pixel_Scale.py b/pylib/gfxprim/generators/core/make_GP_Pixel_Scale.py index f864489..77619fa 100644 --- a/pylib/gfxprim/generators/core/make_GP_Pixel_Scale.py +++ b/pylib/gfxprim/generators/core/make_GP_Pixel_Scale.py @@ -5,38 +5,38 @@ # 2011 - Tomas Gavenciak gavento@ucw.cz #
-from gfxprim.genutils import * -from gfxprim.pixeltype import * -from gfxprim.genutils import j2render as r -from gfxprim.core.gen_pixeltype import * - -h = [] -c = [] - -## Headers - -gen_headers(h, c, - descr = "fast value scaling macros", - authors = ["2011 - Tomas Gavenciak gavento@ucw.cz"], - generator = __file__, - hdef = "GP_PIXEL_SCALEVAL") - -h.append(r( - "/* helper macros to transfer s1-bit value to s2-bit valuen" - " * NOTE: efficient and accurate for both up- and downscaling,n" - " * WARNING: GP_SCALE_VAL requires constants numebrs as first two parametersn" - " */n" - "#define GP_SCALE_VAL(s1, s2, val) ( GP_SCALE_VAL_##s1##_##s2(val) )nn" - - "{% for s1 in [1, 2, 4, 8] %}{% for s2 in [1, 2, 4, 8] %}" - "{% if s2>s1 %}" - "#define GP_SCALE_VAL_{{s1}}_{{s2}}(val) ((val) * {{ multcoef(s1, s2) }})n" - "{% else %}" - "#define GP_SCALE_VAL_{{s1}}_{{s2}}(val) ((val) >> {{ s1 - s2 }})n" - "{% endif %}" - "{% endfor %}{% endfor %}", multcoef = lambda s1,s2: hex(sum([1<<i*s1 for i in range(s2/s1)])) - )) - -if __name__ == '__main__': - main_write(h, None) +from gfxprim.generators.generator import * +#from gfxprim.generators.core.gen_pixeltype import * + +@generator(CHeaderGenerator(name = 'GP_Pixel_Scale.gen.h'), + descr = 'fast value scaling macros', + authors = ["2011 - Tomas Gavenciak gavento@ucw.cz"]) +def core_GP_Pixel_Scale_gen(h): + h.rhead( + "/* helper macros to transfer s1-bit value to s2-bit valuen" + " * NOTE: efficient and accurate for both up- and downscaling,n" + " * WARNING: GP_SCALE_VAL requires constants numebrs as first two parametersn" + " */n" + "#define GP_SCALE_VAL(s1, s2, val) ( GP_SCALE_VAL_##s1##_##s2(val) )nn" + + "{% for s1 in [1, 2, 4, 8] %}{% for s2 in [1, 2, 4, 8] %}" + "{% if s2>s1 %}" + "#define GP_SCALE_VAL_{{s1}}_{{s2}}(val) ((val) * {{ multcoef(s1, s2) }})n" + "{% else %}" + "#define GP_SCALE_VAL_{{s1}}_{{s2}}(val) ((val) >> {{ s1 - s2 }})n" + "{% endif %}" + "{% endfor %}{% endfor %}", multcoef = lambda s1,s2: hex(sum([1<<i*s1 for i in range(s2/s1)])) + ) + +@generator(CSourceGenerator(name='GP_Pixel.gen.c'), + CHeaderGenerator(name='GP_Pixel.gen.h'), + descr = 'pixel type definitions and functions', + authors = ["2011 - Tomas Gavenciak gavento@ucw.cz"]) +def core_GP_Pixel_gen(c, h): + c.rhead("CCCCCC") + h.rhead("HHHHHH") + + + +
diff --git a/pylib/gfxprim/generators/generator.py b/pylib/gfxprim/generators/generator.py index e0b4192..df2e8c9 100644 --- a/pylib/gfxprim/generators/generator.py +++ b/pylib/gfxprim/generators/generator.py @@ -1,3 +1,4 @@ +# # Module with templates and tools for writing generated code, # especially C source and headers # @@ -5,9 +6,10 @@ #
import sys, os, time -import jinja2 +from gfxprim import die +from gfxprim.generators.utils import j2render
-# List of known CodeGenerator classes +# List of known CodeGenerator instances known_generators = []
class CodeGenerator(object): @@ -16,19 +18,23 @@ class CodeGenerator(object): "fdir ('') - directory prefix to matchn" "generating_f (None) - user function called in generate(), should generate contentn" "register (False) - if true, register in global generator listn" + generator_attributes__ = ['generating_f', 'register'] def __init__(self, **kwargs): - self.fname = kwargs.pop('fname') - self.fdir = kwargs.pop('fdir', "") - self.generating_f = kwargs.pop('generating_f', None) - self.register = kwargs.pop('register', False) - self.fname_suffix = os.path.join(self.fdir, self.fname) + self.name = kwargs.pop('name') + self.fdir, self.fname = os.path.split(self.name) + self.generating_f = None + self.register = False self.head = [] self.body = [] self.foot = [] - if self.register: - known_generators.append(self) + self.setargs(**kwargs) + if kwargs: + die('Unknown arguments to CodeGenerator: %s' & str(kwargs.keys())) + def setargs(self, **kwargs): + for i in self.generator_attributes__: + if i in kwargs: self.__setattr__(i, kwargs.pop(i)) def matches_path(self, path): - return path.endswith(self.fname_suffix) + return path.endswith(self.name) def r(self, s, *args, **kwargs): return j2render(s, g=self, *args, **kwargs) def rhead(self, *args, **kwargs): @@ -43,29 +49,41 @@ class CodeGenerator(object): self.foot = [] if run_gen_base: self.gen_base() - # Run user-specified generation + # Run user-specified generation f if self.generating_f: - self.generating_f(self) + f, pos, params = self.generating_f + args = [self if i == pos else global_null_generator for i in range(params)] + f(*args) return ''.join(self.head + self.body + self.foot) def gen_base(self): "Fill basic subclass-dependent content into head/body/foot" pass
+class NullGenerator(CodeGenerator): + def rhead(self, *args, **kwargs): pass + def rbody(self, *args, **kwargs): pass + def rfoot(self, *args, **kwargs): pass + def generate(self, *args, **kwargs): return '' + def r(self, *args, **kwargs): return '' + +global_null_generator = NullGenerator(name='null_generator') + class CCodeGenerator(CodeGenerator): "Args:n" "authors ([]) - list of author creditsn" "descr ("") - (multiline) file descriptionn" + generator_attributes__ = ['authors', 'descr'] + CodeGenerator.generator_attributes__ def __init__(self, **kwargs): + self.authors = [] + self.descr = "" super(CCodeGenerator, self).__init__(**kwargs) - self.authors = kwargs.pop('authors', []) - self.descr = kwargs.pop('descr', "") def gen_base(self): super(CCodeGenerator, self).gen_base() self.head.append(self.r( "/*n" " * {{ g.fname }}n" " *n" - " * GENERATED on {{ date }} for suffix "{{ g.fname_suffix }}"n" + " * GENERATED on {{ date }} by generator "{{ g.name }}"n" " * DO NOT MODIFY THIS FILE DIRECTLY!n" " *n" "{% if g.descr %}" @@ -78,22 +96,12 @@ class CCodeGenerator(CodeGenerator):
class CSourceGenerator(CCodeGenerator): def __init__(self, **kwargs): - print kwargs super(CSourceGenerator, self).__init__(**kwargs)
-def c_source_generator(*args, **kwargs): - "Decorator to create C source generators." - def decorate(f): - kwargs['generating_f'] = f - kwargs['register'] = True - g = CSourceGenerator(*args, **kwargs) - return f - return decorate - class CHeaderGenerator(CCodeGenerator): def __init__(self, **kwargs): super(CHeaderGenerator, self).__init__(**kwargs) - self.hdef = 'GP_HEADER_' + self.fname_suffix.replace('.', '_').replace('/', '_').upper() + self.hdef = 'GP_HEADER_' + self.name.replace('.', '_').replace('/', '_').upper() def gen_base(self): super(CHeaderGenerator, self).gen_base() self.head.append(self.r( @@ -102,11 +110,15 @@ class CHeaderGenerator(CCodeGenerator): self.foot.append(self.r( "#endif /* {{ g.hdef }} */n"))
-def c_header_generator(*args, **kwargs): - "Decorator to create C header generators." +def generator(*args, **kwargs): + "Decorate functions to be content-creator for given generators.n" + "By default also registers the generator to pool of known generators." + register = kwargs.pop('register', True) def decorate(f): - kwargs['generating_f'] = f - kwargs['register'] = True - g = CHeaderGenerator(*args, **kwargs) + for i in range(len(args)): + kwargs['generating_f'] = (f, i, len(args)) + args[i].setargs(**kwargs) + if register: + known_generators.append(args[i]) return f return decorate diff --git a/pylib/gfxprim/generators/utils.py b/pylib/gfxprim/generators/utils.py index ee388f9..cd12e5e 100644 --- a/pylib/gfxprim/generators/utils.py +++ b/pylib/gfxprim/generators/utils.py @@ -6,14 +6,21 @@
import sys, os, time import jinja2 -from gfxprim.generators.generator import known_generators +from gfxprim import die + +def j2render(tmpl, **kw): + "Internal helper to render jinja2 templates (with StrictUndefined)" + t2 = tmpl.rstrip('n') # Jinja strips the last 'n', so add these later + return jinja2.Template(t2, undefined=jinja2.StrictUndefined).render(**kw) + tmpl[len(t2):]
def load_generators(): "Load all modules containig generators to allow them to register" - # TODO: write + # TODO: write proper discovery + import gfxprim.generators.core.make_GP_Pixel_Scale pass
def generate_file(fname): + from gfxprim.generators.generator import known_generators "Function trying to generate file `fname` using matching known generator." matches = [] for k in known_generators: @@ -27,8 +34,4 @@ def generate_file(fname): with open(fname, "wt") as f: f.write(s)
-def j2render(tmpl, **kw): - "Internal helper to render jinja2 templates (with StrictUndefined)" - t2 = tmpl.rstrip('n') # Jinja strips the last 'n', so add these later - return jinja2.Template(t2, undefined=jinja2.StrictUndefined).render(**kw) + tmpl[len(t2):]
-----------------------------------------------------------------------
Summary of changes: gen.mk | 7 ++ libs/core/Makefile | 12 --- pylib/bin/generate_file.py | 11 ++- pylib/gfxprim/__init__.py | 4 + .../gfxprim/generators/core/make_GP_Pixel_Scale.py | 68 +++++++++--------- pylib/gfxprim/generators/generator.py | 74 +++++++++++-------- pylib/gfxprim/generators/utils.py | 15 +++-- 7 files changed, 104 insertions(+), 87 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.