kopia lustrzana https://gitlab.com/sane-project/backends
pixma: auto generate options files with python[23]
see issue sane-project/backends#327merge-requests/244/head
rodzic
7525fda505
commit
7481254f1b
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys,os,re
|
||||
import functools
|
||||
|
||||
class Error(Exception):
|
||||
pass
|
||||
|
@ -181,37 +183,35 @@ def parseFile(f):
|
|||
|
||||
|
||||
def genHeader(options):
|
||||
print """
|
||||
typedef union {
|
||||
SANE_Word w;
|
||||
SANE_Int i;
|
||||
SANE_Bool b;
|
||||
SANE_Fixed f;
|
||||
SANE_String s;
|
||||
void *ptr;
|
||||
} option_value_t;
|
||||
"""
|
||||
print 'typedef enum {'
|
||||
print ("\ntypedef union {")
|
||||
print (" SANE_Word w;")
|
||||
print (" SANE_Int i;")
|
||||
print (" SANE_Bool b;")
|
||||
print (" SANE_Fixed f;")
|
||||
print (" SANE_String s;")
|
||||
print (" void *ptr;")
|
||||
print ("} option_value_t;")
|
||||
print ("\ntypedef enum {")
|
||||
for o in options:
|
||||
print ' %(cname_opt)s,' % o
|
||||
print ' ' + opt_prefix + 'last'
|
||||
print '} option_t;'
|
||||
print """
|
||||
typedef struct {
|
||||
SANE_Option_Descriptor sod;
|
||||
option_value_t val,def;
|
||||
SANE_Word info;
|
||||
} option_descriptor_t;
|
||||
print (" %(cname_opt)s," % o)
|
||||
print (" " + opt_prefix + "last")
|
||||
print ("} option_t;")
|
||||
|
||||
struct pixma_sane_t;
|
||||
static int build_option_descriptors(struct pixma_sane_t *ss);"""
|
||||
print ("\ntypedef struct {")
|
||||
print (" SANE_Option_Descriptor sod;")
|
||||
print (" option_value_t val,def;")
|
||||
print (" SANE_Word info;")
|
||||
print ("} option_descriptor_t;")
|
||||
|
||||
print ("\nstruct pixma_sane_t;")
|
||||
print ("static int build_option_descriptors(struct pixma_sane_t *ss);\n")
|
||||
|
||||
|
||||
def genMinMaxRange(n, t, r):
|
||||
if t == 'SANE_TYPE_FIXED':
|
||||
r = ['SANE_FIX(%s)' % x for x in r]
|
||||
print 'static const SANE_Range ' + n + ' ='
|
||||
print ' { ' + r[0] + ',' + r[1] + ',' + r[2] + ' };'
|
||||
print ("static const SANE_Range " + n + " =")
|
||||
print (" { " + r[0] + "," + r[1] + "," + r[2] + " };")
|
||||
|
||||
|
||||
def genList(n, t, l):
|
||||
|
@ -224,13 +224,14 @@ def genList(n, t, l):
|
|||
elif t == 'SANE_TYPE_STRING':
|
||||
etype = 'SANE_String_Const'
|
||||
l = ['SANE_I18N("%s")' % x for x in l] + ['NULL']
|
||||
print 'static const %s %s[%d] = {' % (etype, n, len(l))
|
||||
print ("static const %s %s[%d] = {" % (etype, n, len(l)))
|
||||
for x in l[0:-1]:
|
||||
print '\t' + x + ','
|
||||
print '\t' + l[-1] + ' };'
|
||||
print ("\t" + x + ",")
|
||||
print ("\t" + l[-1] + " };")
|
||||
|
||||
|
||||
def genConstraints(options):
|
||||
print ("")
|
||||
for o in options:
|
||||
if 'constraint' not in o: continue
|
||||
c = o['constraint']
|
||||
|
@ -240,7 +241,6 @@ def genConstraints(options):
|
|||
genMinMaxRange(oname, otype, c)
|
||||
elif isinstance(c, list):
|
||||
genList(oname, otype, c)
|
||||
print
|
||||
|
||||
def buildCodeVerbatim(o):
|
||||
for f in ('name', 'title', 'desc', 'type', 'unit', 'size', 'cap',
|
||||
|
@ -280,12 +280,12 @@ def ccode(o):
|
|||
o['code_size'] = code
|
||||
|
||||
if ('code_cap' not in o) and ('cap' in o):
|
||||
o['code_cap'] = reduce(lambda a,b: a+'|'+b, o['cap'])
|
||||
o['code_cap'] = functools.reduce(lambda a,b: a+'|'+b, o['cap'])
|
||||
else:
|
||||
o['code_cap'] = '0'
|
||||
|
||||
if ('code_info' not in o) and ('info' in o):
|
||||
o['code_info'] = reduce(lambda a,b: a+'|'+b, o['info'])
|
||||
o['code_info'] = functools.reduce(lambda a,b: a+'|'+b, o['info'])
|
||||
else:
|
||||
o['code_info'] = '0'
|
||||
|
||||
|
@ -332,22 +332,21 @@ def ccode(o):
|
|||
return o
|
||||
|
||||
def genBuildOptions(options):
|
||||
print """
|
||||
static
|
||||
int find_string_in_list(SANE_String_Const str, const SANE_String_Const *list)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; list[i] && strcmp(str, list[i]) != 0; i++) {}
|
||||
return i;
|
||||
}
|
||||
|
||||
static
|
||||
int build_option_descriptors(struct pixma_sane_t *ss)
|
||||
{
|
||||
SANE_Option_Descriptor *sod;
|
||||
option_descriptor_t *opt;
|
||||
|
||||
memset(OPT_IN_CTX, 0, sizeof(OPT_IN_CTX));"""
|
||||
print ("\nstatic")
|
||||
print ("int find_string_in_list(SANE_String_Const str, const SANE_String_Const *list)")
|
||||
print ("{")
|
||||
print (" int i;")
|
||||
print (" for (i = 0; list[i] && strcmp(str, list[i]) != 0; i++) {}")
|
||||
print (" return i;")
|
||||
print ("}")
|
||||
print ("")
|
||||
print ("static")
|
||||
print ("int build_option_descriptors(struct pixma_sane_t *ss)")
|
||||
print ("{")
|
||||
print (" SANE_Option_Descriptor *sod;")
|
||||
print (" option_descriptor_t *opt;")
|
||||
print ("")
|
||||
print (" memset(OPT_IN_CTX, 0, sizeof(OPT_IN_CTX));")
|
||||
|
||||
for o in options:
|
||||
o = ccode(o)
|
||||
|
@ -367,9 +366,9 @@ int build_option_descriptors(struct pixma_sane_t *ss)
|
|||
' OPT_IN_CTX[%(cname_opt)s].info = %(code_info)s;\n' \
|
||||
'%(full_code_default)s'
|
||||
sys.stdout.write(code % o)
|
||||
print
|
||||
print ' return 0;'
|
||||
print '}'
|
||||
print ("")
|
||||
print (" return 0;")
|
||||
print ("}\n")
|
||||
|
||||
g = Struct()
|
||||
g.ngroups = 0
|
||||
|
@ -377,8 +376,8 @@ opt_prefix = 'opt_'
|
|||
con_prefix = 'constraint_'
|
||||
cnameMap = createCNameMap()
|
||||
options = parseFile(sys.stdin)
|
||||
print "/* DO NOT EDIT THIS FILE! */"
|
||||
print "/* Automatically generated from pixma.c */"
|
||||
print ("/* DO NOT EDIT THIS FILE! */")
|
||||
print ("/* Automatically generated from pixma.c */")
|
||||
if (len(sys.argv) == 2) and (sys.argv[1] == 'h'):
|
||||
genHeader(options)
|
||||
else:
|
||||
|
|
Ładowanie…
Reference in New Issue