Commit eea2076f authored by Nathan/Eilisha Shiraini's avatar Nathan/Eilisha Shiraini
Browse files

Implemented the main method and multiple corrections and improvements

parent 00e4f039
......@@ -8,3 +8,4 @@ on multiple color channels.
"""
from .loader import BUILDERS, listBuilders
from . import imgen
from .cli import main
main()
\ No newline at end of file
......@@ -31,23 +31,18 @@ class AztecBuilder(object):
self.__data = data
self.__prepared = None
self.__wordcount = None
if forceLayers and forceCompact:
self.__size = (forceLayers, forceCompact)
elif forceLayers:
self.__size = (forceLayers, None)
elif forceCompact:
self.__size = (None, forceCompact)
else:
self.__size = None
self.__size = (forceLayers, forceCompact)
@property
def size(self):
if self.__size is None:
if self.__size is None or None in self.__size:
self.prepare()
return self.__size
@size.setter
def size(self, value):
if value == self.__size:
return
self.__prepared = None
self.__size = value
......
......@@ -9,7 +9,8 @@ including argument parsing and main procedure
import argparse
from .loader import BUILDERS
from .imgen.interleave import INTERLEAVING_MODES
from .imgen.interleave import INTERLEAVING_MODES, BEST_PARSE_MODE
from .imgen.draw import ImGenerator
_parseBool = lambda i: i.lower()[0] in '1tyo' and i.lower()[1] != 'f'
......@@ -92,7 +93,6 @@ def _apInit():
action = 'store',
nargs = '+',
metavar = 'DATA',
dest = 'data',
help = 'The data of each code',
)
......@@ -110,6 +110,10 @@ def parseCLI():
-------
data : :py:class:`list`
The list of data elements to encode
codeType : :py:class:`str`
The id of the code generation module
outfile : :py:class:`str`
The name of the file to which the image will be saved
cgenKwargs : :py:class:`dict`
The dictionnary to be passed by keyword argument expansion
to the code builder constructor
......@@ -120,12 +124,17 @@ def parseCLI():
args = _apInit().parse_args()
codeType = args.type
interleave = INTERLEAVING_MODES[args.int]
if args.usefiles:
data = [open(f, 'rb') for f in args.data]
data = [open(f, 'rb').read() for f in args.data]
else:
data = [d.encode('utf8') for d in args.data]
if len(data) > interleave['maxcodes']:
if args.int is not None:
interleave = INTERLEAVING_MODES[args.int]
else:
interleave = INTERLEAVING_MODES[BEST_PARSE_MODE[len(data) - 1]]
if args.int is not None and len(data) > interleave['maxcodes']:
raise ArgumentError("Too many codes specified for this interleaving mode")
imgenKwargs = {
......@@ -141,4 +150,21 @@ def parseCLI():
cgenKwargs['forceLayers'] = args.aztec_layers
cgenKwargs['forceCompact'] = args.aztec_compact
return data, cgenKwargs, imgenKwargs
return data, codeType, args.out, cgenKwargs, imgenKwargs
def main():
"""
Main procedure of the CLI version of the program
This function is the main of the program for use in command line mode.
"""
data, codeType, outfile, cgenKwargs, imgenKwargs = parseCLI()
cgenClass = BUILDERS[codeType]['builder']
cgenInstances = [cgenClass(d, **cgenKwargs) for d in data]
size = max((g.size for g in cgenInstances), key = BUILDERS[codeType]['params']['sizeSortKey'])
for g in cgenInstances:
g.size = size
codes = [g.encode() for g in cgenInstances]
imgenerator = ImGenerator(codes, **imgenKwargs)
imgenerator.drawCode()
imgenerator.saveImage(outfile)
......@@ -275,4 +275,4 @@ INTERLEAVING_MODES = {
},
}
BEST_PARSE_MODE = ['single', random.choice(('redcyan', 'greenmagenta', 'blueyellow')), 'rgb'] + (['rgb2'] * 3) + (['fullgray'] * 2) + (['allcolors'] * 16)
\ No newline at end of file
BEST_PARSE_MODE = ['single', random.choice(('redcyan', 'greenmagenta', 'blueyellow')), 'rgb'] + (['rgb2'] * 3) + (['fullgray'] * 2) + (['allcolors'] * 16)
......@@ -34,7 +34,8 @@ import importlib
EXCLUDE_MODULES = [
"imgen",
"loader.py"
"loader.py",
"cli.py",
]
def _findModules(location = '.'):
......@@ -60,7 +61,7 @@ def _findBuilders(location = '.'):
if __name__ == '__main__':
module = importlib.import_module(modname)
else:
module = importlib.import_module('.' + modname, __name__)
module = importlib.import_module('.' + modname, '.'.join(__name__.split('.')[:-1]))
try:
builder = module.Builder
params = module.IMGEN_PARAMS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment