added pyaccuwage-genfieldfill
This commit is contained in:
parent
ef9f012bd2
commit
9bbe100929
3 changed files with 59 additions and 83 deletions
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from pyaccuwage import model as pyaccuwagemodel
|
from pyaccuwage import model as pyaccuwagemodel
|
||||||
|
from pyaccuwage.modeldef import ModelDefParser, ClassEntryCommentSequence
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
@ -15,86 +16,5 @@ parser.add_argument("-i", "--input",
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
class ClassEntry(object):
|
parser = ModelDefParser(args.input[0], ClassEntryCommentSequence)
|
||||||
re_rangecomment = re.compile('#\s+(\d+)\-?(\d*)$')
|
|
||||||
|
|
||||||
def __init__(self, classname, line):
|
|
||||||
self.classname = classname,
|
|
||||||
self.line = line
|
|
||||||
self.lines = []
|
|
||||||
|
|
||||||
def add_line(self, line):
|
|
||||||
self.lines.append(line)
|
|
||||||
|
|
||||||
def validate(self):
|
|
||||||
i = 0
|
|
||||||
for (line_no, line) in enumerate(self.lines):
|
|
||||||
match = self.re_rangecomment.search(line)
|
|
||||||
if match:
|
|
||||||
(a, b) = match.groups()
|
|
||||||
a = int(a)
|
|
||||||
|
|
||||||
if (i + 1) != a:
|
|
||||||
line_number = self.line + line_no
|
|
||||||
print("ERROR\tline:%d\tnear:%s\texpected:%d\tsaw:%d" % (line_number, line.split(' ')[0].strip(), i+1, a))
|
|
||||||
|
|
||||||
i = int(b) if b else a
|
|
||||||
|
|
||||||
class ModelDefParser(object):
|
|
||||||
re_triplequote = re.compile('"""')
|
|
||||||
re_whitespace = re.compile("^(\s*)[^\s]+")
|
|
||||||
re_classdef = re.compile(r"^\s*class\s(.*)\((.*)\):\s*$")
|
|
||||||
|
|
||||||
def __init__(self, infile):
|
|
||||||
self.infile = infile
|
|
||||||
self.line = 0
|
|
||||||
|
|
||||||
def endclass(self):
|
|
||||||
if self.current_class:
|
|
||||||
self.current_class.validate()
|
|
||||||
self.current_class = None
|
|
||||||
|
|
||||||
def beginclass(self, classname, line):
|
|
||||||
self.current_class = ClassEntry(classname, line)
|
|
||||||
|
|
||||||
def parse(self):
|
|
||||||
infile = self.infile
|
|
||||||
whitespace = 0
|
|
||||||
in_block_comment = False
|
|
||||||
self.current_class = None
|
|
||||||
|
|
||||||
for line in infile:
|
|
||||||
self.line += 1
|
|
||||||
|
|
||||||
if line.startswith('#'):
|
|
||||||
continue
|
|
||||||
|
|
||||||
if self.re_triplequote.search(line):
|
|
||||||
in_block_comment = not in_block_comment
|
|
||||||
|
|
||||||
if in_block_comment:
|
|
||||||
continue
|
|
||||||
|
|
||||||
match_whitespace = self.re_whitespace.match(line)
|
|
||||||
if match_whitespace:
|
|
||||||
match_whitespace = len(match_whitespace.groups()[0])
|
|
||||||
else:
|
|
||||||
match_whitespace = 0
|
|
||||||
|
|
||||||
classmatch = self.re_classdef.match(line)
|
|
||||||
if classmatch:
|
|
||||||
classname, subclass = classmatch.groups()
|
|
||||||
self.beginclass(classname, self.line)
|
|
||||||
continue
|
|
||||||
|
|
||||||
if match_whitespace < whitespace:
|
|
||||||
whitespace = match_whitespace
|
|
||||||
self.endclass()
|
|
||||||
continue
|
|
||||||
|
|
||||||
if self.current_class:
|
|
||||||
whitespace = match_whitespace
|
|
||||||
self.current_class.add_line(line)
|
|
||||||
|
|
||||||
parser = ModelDefParser(args.input[0])
|
|
||||||
parser.parse()
|
parser.parse()
|
||||||
|
|
51
scripts/pyaccuwage-genfieldfill
Executable file
51
scripts/pyaccuwage-genfieldfill
Executable file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
from pyaccuwage import model as pyaccuwagemodel
|
||||||
|
from pyaccuwage.modeldef import ModelDefParser, ClassEntryCommentSequence
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class ModelDefFFParser(ModelDefParser):
|
||||||
|
def __init__(self, infile, entryclass, wantedclass = None):
|
||||||
|
super(ModelDefFFParser, self).__init__(infile, entryclass)
|
||||||
|
self.wantedclass = wantedclass
|
||||||
|
|
||||||
|
def endclass(self):
|
||||||
|
if self.current_class:
|
||||||
|
if not self.wantedclass or self.wantedclass in self.current_class.classname:
|
||||||
|
self.current_class.process()
|
||||||
|
self.current_class = None
|
||||||
|
|
||||||
|
class ClassEntryFieldFill(ClassEntryCommentSequence):
|
||||||
|
def process(self):
|
||||||
|
print("def s(k,v):\n\tgetattr(record, k).value = v\n")
|
||||||
|
for line in self.lines:
|
||||||
|
parts = line.strip().split()
|
||||||
|
if parts and not parts[0].startswith('blank'):
|
||||||
|
print( ("s('%s'," % parts[0]).ljust(50) + ')')
|
||||||
|
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Generate prototype field filling commands from pyaccuwage model definition."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument("-i", "--input",
|
||||||
|
nargs=1,
|
||||||
|
required=True,
|
||||||
|
metavar="file",
|
||||||
|
type=argparse.FileType('r'),
|
||||||
|
help="Source file to generate")
|
||||||
|
|
||||||
|
parser.add_argument("-c", "--classname",
|
||||||
|
nargs=1,
|
||||||
|
required=False,
|
||||||
|
metavar="string",
|
||||||
|
default="",
|
||||||
|
help="Specific model to generate field fills from")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
parser = ModelDefFFParser(args.input[0], ClassEntryFieldFill, args.classname[0])
|
||||||
|
parser.parse()
|
7
setup.py
7
setup.py
|
@ -2,6 +2,11 @@ from distutils.core import setup
|
||||||
setup(name='pyaccuwage',
|
setup(name='pyaccuwage',
|
||||||
version='0.2012.1',
|
version='0.2012.1',
|
||||||
packages=['pyaccuwage'],
|
packages=['pyaccuwage'],
|
||||||
scripts=['scripts/pyaccuwage-parse', 'scripts/pyaccuwage-pdfparse', 'scripts/pyaccuwage-checkseq'],
|
scripts=[
|
||||||
|
'scripts/pyaccuwage-parse',
|
||||||
|
'scripts/pyaccuwage-pdfparse',
|
||||||
|
'scripts/pyaccuwage-checkseq',
|
||||||
|
'scripts/pyaccuwage-genfieldfill'
|
||||||
|
],
|
||||||
zip_safe=True,
|
zip_safe=True,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue