Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def pdf_concatenate(*pargs, **kwargs):
"""Concatenates PDF files together and returns a DAFile representing
the new PDF.
"""
paths = list()
get_pdf_paths([x for x in pargs], paths)
if len(paths) == 0:
raise DAError("pdf_concatenate: no valid files to concatenate")
pdf_path = docassemble.base.pandoc.concatenate_files(paths, pdfa=kwargs.get('pdfa', False), password=kwargs.get('password', None))
pdf_file = DAFile()._set_instance_name_for_function()
pdf_file.initialize(filename=kwargs.get('filename', 'file.pdf'))
pdf_file.copy_into(pdf_path)
pdf_file.retrieve()
pdf_file.commit()
return pdf_file
raise DAError('Unknown data type ' + str(type(sub_data)) + ' in list in attachment metadata' + self.idebug(target))
newdata = list(map((lambda x: TextObject(x)), data))
metadata[key] = newdata
elif type(data) is str:
metadata[key] = TextObject(data)
elif type(data) is bool:
metadata[key] = data
else:
raise DAError('Unknown data type ' + str(type(data)) + ' in key in attachment metadata' + self.idebug(target))
if 'content file' in target:
if type(target['content file']) is not list:
target['content file'] = [target['content file']]
target['content'] = ''
for content_file in target['content file']:
if type(content_file) is not str:
raise DAError('A content file must be specified as text or a list of text filenames' + self.idebug(target))
file_to_read = docassemble.base.util.package_template_filename(content_file, package=self.package)
if file_to_read is not None and os.path.isfile(file_to_read) and os.access(file_to_read, os.R_OK):
with open(file_to_read, 'rU') as the_file:
target['content'] += the_file.read()
else:
raise DAError('Unable to read content file ' + str(content_file) + ' after trying to find it at ' + str(file_to_read) + self.idebug(target))
if 'fields' in target:
if 'pdf template file' not in target:
raise DAError('Fields supplied to attachment but no pdf template file supplied' + self.idebug(target))
if type(target['pdf template file']) is not str:
raise DAError('pdf template file supplied to attachment must be a string' + self.idebug(target))
if type(target['fields']) is not dict:
raise DAError('fields supplied to attachment must be a dictionary' + self.idebug(target))
target['content'] = ''
target['valid formats'] = ['pdf']
options['pdf_template_file'] = docassemble.base.util.package_template_filename(target['pdf template file'], package=self.package)
def __init__(self, **kwargs):
self.playground = None
if 'filepath' in kwargs:
if re.search(r'SavedFile', str(type(kwargs['filepath']))):
#logmessage("We have a saved file on our hands")
self.playground = kwargs['filepath']
if os.path.isfile(self.playground.path) and os.access(self.playground.path, os.R_OK):
self.set_filepath(self.playground.path)
else:
raise DAError("Reference to invalid playground path")
else:
self.set_filepath(kwargs['filepath'])
else:
self.filepath = None
if 'path' in kwargs:
self.set_path(kwargs['path'])
return super(InterviewSourceFile, self).__init__(**kwargs)
def set_path(self, path):
def finalize(self):
#sys.stderr.write("finalize: starting " + str(self.section) + '/' + str(self.file_number) + "\n")
if cloud is None:
return
if not self.fixed:
raise DAError("SavedFile: finalize called before fix")
for filename in listfiles(self.directory):
fullpath = os.path.join(self.directory, filename)
#logmessage("Found " + fullpath)
if os.path.isfile(fullpath):
save = True
if filename in self.keydict:
key = self.keydict[filename]
if self.modtimes[filename] == os.path.getmtime(fullpath):
save = False
else:
key = cloud.get_key(str(self.section) + '/' + str(self.file_number) + '/' + path_to_key(filename))
if save:
if self.extension is not None and filename == self.filename:
extension, mimetype = get_ext_and_mimetype(filename + '.' + self.extension)
else:
extension, mimetype = get_ext_and_mimetype(filename)
def text_of_table(table_info, orig_user_dict, temp_vars):
table_content = "\n"
user_dict = copy.copy(orig_user_dict)
user_dict.update(temp_vars)
#logmessage("i is " + unicode(user_dict['i']))
header_output = [table_safe(x.text(user_dict)) for x in table_info.header]
the_iterable = eval(table_info.row, user_dict)
if not hasattr(the_iterable, '__iter__'):
raise DAError("Error in processing table " + table_info.saveas + ": row value is not iterable")
if hasattr(the_iterable, 'instanceName') and hasattr(the_iterable, 'elements') and type(the_iterable.elements) in (list, dict) and docassemble.base.functions.get_gathering_mode(the_iterable.instanceName):
the_iterable = the_iterable.complete_elements()
contents = list()
if hasattr(the_iterable, 'iteritems') and callable(the_iterable.iteritems):
for key in sorted(the_iterable):
user_dict['row_item'] = the_iterable[key]
user_dict['row_index'] = key
contents.append([table_safe(eval(x, user_dict)) for x in table_info.column])
else:
indexno = 0
for item in the_iterable:
user_dict['row_item'] = item
user_dict['row_index'] = indexno
contents.append([table_safe(eval(x, user_dict)) for x in table_info.column])
indexno += 1
user_dict.pop('row_item', None)
def finalize(self):
if not S3_ENABLED:
return
if not self.fixed:
raise DAError("SavedFile: finalize called before fix")
existing_files = list()
for filename in os.listdir(self.directory):
existing_files.append(filename)
fullpath = os.path.join(self.directory, filename)
#logmessage("Found " + fullpath)
if os.path.isfile(fullpath):
save = True
if filename in self.keydict:
key = self.keydict[filename]
if self.modtimes[filename] == os.path.getmtime(fullpath):
save = False
else:
key = s3.new_key()
key.key = str(self.section) + '/' + str(self.file_number) + '/' + str(filename)
if filename == self.filename:
extension, mimetype = get_ext_and_mimetype(filename + '.' + self.extension)
def make_image_files(path):
#logmessage("make_image_files on " + str(path))
if PDFTOPPM_COMMAND is not None:
args = [PDFTOPPM_COMMAND, '-r', str(PNG_RESOLUTION), '-png', path, path + 'page']
result = call(args)
if result > 0:
raise DAError("Call to pdftoppm failed")
args = [PDFTOPPM_COMMAND, '-r', str(PNG_SCREEN_RESOLUTION), '-png', path, path + 'screen']
result = call(args)
if result > 0:
raise DAError("Call to pdftoppm failed")
return
if 'fields' in target:
if 'pdf template file' not in target:
raise DAError('Fields supplied to attachment but no pdf template file supplied' + self.idebug(target))
if type(target['pdf template file']) is not str:
raise DAError('pdf template file supplied to attachment must be a string' + self.idebug(target))
if type(target['fields']) is not dict:
raise DAError('fields supplied to attachment must be a dictionary' + self.idebug(target))
target['content'] = ''
target['valid formats'] = ['pdf']
options['pdf_template_file'] = docassemble.base.util.package_template_filename(target['pdf template file'], package=self.package)
options['fields'] = dict()
for key, val in target['fields'].iteritems():
logmessage("Set " + str(key) + " to " + str(val))
options['fields'][key] = TextObject(str(val))
if 'content' not in target:
raise DAError("No content provided in attachment")
#logmessage("The content is " + str(target['content']))
return({'name': TextObject(target['name']), 'filename': TextObject(target['filename']), 'description': TextObject(target['description']), 'content': TextObject("\n".join(defs) + "\n" + target['content']), 'valid_formats': target['valid formats'], 'metadata': metadata, 'variable_name': variable_name, 'options': options})
elif type(target) is str:
return({'name': TextObject('Document'), 'filename': TextObject('document'), 'content': TextObject(target), 'valid_formats': ['*'], 'metadata': metadata, 'variable_name': variable_name, 'options': options})
else:
raise DAError("Unknown data type in process_attachment")
if hasattr(field, 'datatype') and field.datatype in ['object', 'object_radio', 'object_checkboxes']:
if field.number not in selectcompute:
raise DAError("datatype was set to object but no code or selections was provided")
string = "_internal['objselections'][" + repr(from_safeid(field.saveas)) + "] = dict()"
logmessage("Doing " + string)
try:
exec(string, user_dict)
for selection in selectcompute[field.number]:
key = selection[0]
#logmessage("key is " + str(key))
real_key = codecs.decode(key, 'base64').decode('utf-8')
string = "_internal['objselections'][" + repr(from_safeid(field.saveas)) + "][" + repr(key) + "] = " + real_key
logmessage("Doing " + string)
exec(string, user_dict)
except:
raise DAError("Failure while processing field with datatype of object")
if hasattr(field, 'label'):
labels[field.number] = field.label.text(user_dict)
if hasattr(field, 'extras'):
for key in ['note', 'html', 'script', 'css', 'min', 'max', 'minlength', 'maxlength', 'show_if_val']:
if key in field.extras:
if key not in extras:
extras[key] = dict()
extras[key][field.number] = field.extras[key].text(user_dict)
if hasattr(field, 'saveas'):
try:
defaults[field.number] = eval(from_safeid(field.saveas), user_dict)
except:
if hasattr(field, 'default'):
defaults[field.number] = field.default.text(user_dict)
if hasattr(field, 'helptext'):
helptexts[field.number] = field.helptext.text(user_dict)