def create_template(argv=None): """cli for creating a template""" parser = ArgumentParser( description='Create a tempalte image', epilog='Copyright 2019 AxJu | blurring v{}'.format(__version__), ) parser.add_argument('filename', help='The templates filename, without the extension!') args = parser.parse_args(argv) filename = os.path.abspath(args.filename) folder = os.path.dirname(filename) name = os.path.splitext(filename)[0] data = { 'text': 'PASSWORD', 'height': 18, 'width': 70, 'scale': 0.4, 'font': 0, 'pos_x': 0, 'pos_y': 12, } for key, default in data.items(): data[key] = input('{} [{}]: '.format(key, default)) or default data['kind'] = 'cv2' data['name'] = name data['scale'] = float(data['scale']) data['font'] = int(data['font']) data['pos'] = (int(data['pos_x']), int(data['pos_y'])) data['size'] = (int(data['height']), int(data['width'])) tempgen = TempGen(folder=folder, data=data) tempgen.run()
def test_create_gen(self): tempgen = TempGen(folder='.', data={'test': True}) self.assertTrue(len(tempgen.folder) > 2) self.assertIsInstance(tempgen.data, list) with self.assertRaises(TypeError): TempGen(folder='.', data='') with self.assertRaises(TypeError): TempGen(folder='.', data=['']) tempgen = TempGen(folder='.', data=[{'test': True}]) self.assertIsInstance(tempgen.data, list)
def test_run_logging(self): data = {} with self.assertLogs('TempGen', level='INFO') as cm: TempGen(data=data).run() self.assertEqual( cm.output, ['INFO:TempGen:Wrong data. Missing kind definition!'])
def test_create_temp_cv2(self): folder = self.mkdtemp() data = { 'kind': 'cv2', 'name': 'test', 'size': [18, 70], 'text': 'PASSWORD', 'scale': 0.4, 'font': 0, 'pos': [0, 12], } self.assertFalse(os.path.exists(os.path.join(folder, 'test.png'))) TempGen(folder=folder, data=data).run() self.assertTrue(os.path.exists(os.path.join(folder, 'test.png')))
def add_template(self, file=None, folder=None, data=None): """ Add a templat to the work folder. file: Add a image file. folder: All images inside the folder. data: A file with data to create different templates. """ if file: self.logger.debug('Add template file "%s"', file) copyfile(file, os.path.join(self.work['templates'], os.path.basename(file))) elif folder: self.logger.debug('Add template folder "%s"', folder) for name in os.listdir(folder): filename = os.path.join(folder, name) if os.path.isfile(filename): copy(filename, os.path.join(self.work['templates'], name)) elif data: self.logger.debug('Add template data file "%s"', data) TempGen(folder=self.work['templates'], data=data).run()
import os from blurring.utils import TempGen import logging log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' logging.basicConfig(level=logging.DEBUG, format=log_format) folder = os.path.dirname(__file__) data = [{ 'kind': 'cv2', 'name': 'test', 'size': [18, 70], 'text': 'PASSWORD', 'scale': 0.4, 'font': 0, 'pos': [0, 12], }] tempgen = TempGen(folder=folder, data=data) tempgen.run()