def iterate_progressbar(self, function, filter=lambda: True, *args, **kwargs): '''call function once for each sheet and display a progressbar ''' with self._db as con: c = con.cursor() c.execute('SELECT count(*) FROM sheets WHERE survey_rowid=?', (self._survey_rowid,)) count = c.fetchone()[0] # The old code used to first filter, and then run; but that is # a bit ineffective in a way print(ungettext('%i sheet', '%i sheets', count) % count) if count == 0: return log.progressbar.start(count) processed = 0 c.execute('SELECT rowid FROM sheets WHERE survey_rowid=? ORDER BY sort,rowid', (self._survey_rowid,)) for index, (sheetid,) in enumerate(c.fetchall()): self.goto_sheet(self._db_get_sheet(sheetid)) if filter(): function(*args, **kwargs) processed += 1 log.progressbar.update(index + 1) print(_('Processed %i of %i sheets, took %f seconds') % (processed, count, log.progressbar.elapsed_time))
def iterate_progressbar(self, function, filter=lambda: True): '''call function once for each sheet and display a progressbar ''' print ungettext('%i sheet', '%i sheets', len(self.sheets)) % len(self.sheets) if len(self.sheets) == 0: return log.progressbar.start(len(self.sheets)) for self.index in range(len(self.sheets)): if filter(): function() log.progressbar.update(self.index + 1) print _('%f seconds per sheet') % ( float(log.progressbar.elapsed_time) / float(log.progressbar.max_value) )
def iterate_progressbar(self, function, filter=lambda: True): '''call function once for each sheet and display a progressbar ''' count = 0 for self.index in range(len(self.sheets)): if filter(): count += 1 print ungettext('%i sheet', '%i sheets', count) % count if count == 0: return log.progressbar.start(len(self.sheets)) for self.index in range(len(self.sheets)): if filter(): function() log.progressbar.update(self.index + 1) print _('%f seconds per sheet') % (float(log.progressbar.elapsed_time) / float(log.progressbar.max_value))
def __init__(self, provider): self.about_dialog = None self.close_dialog = None self.ask_open_dialog = None self.provider = provider self._load_image = 0 self._builder = Gtk.Builder() if paths.local_run: self._builder.add_from_file( os.path.join(os.path.dirname(__file__), 'main_window.ui')) else: self._builder.add_from_file( os.path.join(paths.prefix, 'share', 'sdaps', 'ui', 'main_window.ui')) self._window = self._builder.get_object("main_window") self._builder.connect_signals(self) self._window.maximize() scrolled_window = self._builder.get_object("sheet_scrolled_window") self.sheet = SheetWidget(self.provider) self.sheet.show() scrolled_window.add(self.sheet) self.data_viewport = self._builder.get_object("data_view") widgets = provider.survey.questionnaire.widget.create_widget() widgets.show_all() self.data_viewport.add(widgets) provider.survey.questionnaire.widget.connect_ensure_visible( self.data_view_ensure_visible) self.sheet.connect('key-press-event', self.sheet_view_key_press) combo = self._builder.get_object("page_number_combo") cell = Gtk.CellRendererText() combo.pack_start(cell, True) combo.add_attribute(cell, 'text', 0) store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_INT) store.append(row=(_("Page|Invalid"), -1)) for i in range(self.provider.survey.questionnaire.page_count): store.append(row=(ungettext("Page %i", "Page %i", i + 1) % (i + 1), i + 1)) combo.set_model(store) self.sheet.props.zoom = 0.2 # So the buttons are insensitive self.update_ui()
def __init__(self, provider): self.about_dialog = None self.close_dialog = None self.ask_open_dialog = None self.provider = provider self._load_image = 0 self._builder = Gtk.Builder() if paths.local_run: self._builder.add_from_file( os.path.join(os.path.dirname(__file__), 'main_window.ui')) else: self._builder.add_from_file( os.path.join( paths.prefix, 'share', 'sdaps', 'ui', 'main_window.ui')) self._window = self._builder.get_object("main_window") self._builder.connect_signals(self) self._window.maximize() scrolled_window = self._builder.get_object("sheet_scrolled_window") self.sheet = SheetWidget(self.provider) self.sheet.show() scrolled_window.add(self.sheet) self.data_viewport = self._builder.get_object("data_view") widgets = provider.survey.questionnaire.widget.create_widget() widgets.show_all() self.data_viewport.add(widgets) provider.survey.questionnaire.widget.connect_ensure_visible(self.data_view_ensure_visible) self.sheet.connect('key-press-event', self.sheet_view_key_press) combo = self._builder.get_object("page_number_combo") cell = Gtk.CellRendererText() combo.pack_start(cell, True) combo.add_attribute(cell, 'text', 0) store = Gtk.ListStore(GObject.TYPE_STRING, GObject.TYPE_INT) store.append(row=(_("Page|Invalid"), -1)) for i in range(self.provider.survey.questionnaire.page_count): store.append(row=( ungettext("Page %i", "Page %i", i + 1) % (i + 1), i + 1)) combo.set_model(store) self.sheet.props.zoom = 0.2 # So the buttons are insensitive self.update_ui()
def create_stamp_pdf(survey, output_filename, questionnaire_ids): sheets = 1 if questionnaire_ids is None else len(questionnaire_ids) questionnaire_length = survey.questionnaire.page_count have_pdftk = False # Test if pdftk is present, if it is we can use it to be faster try: result = subprocess.Popen(['pdftk', '--version'], stdout=subprocess.PIPE) # Just assume pdftk is there, if it was executed sucessfully if result is not None: have_pdftk = True except OSError: pass if not have_pdftk: try: import pyPdf except: log.error( _(u'You need to have either pdftk or pyPdf installed. pdftk is the faster method.' )) sys.exit(1) # Write the "stamp" out to tmp.pdf if are using pdftk. if have_pdftk: stampsfile = file(survey.path('tmp.pdf'), 'wb') else: stampsfile = StringIO.StringIO() canvas = \ reportlab.pdfgen.canvas.Canvas(stampsfile, bottomup=False, pagesize=(survey.defs.paper_width * mm, survey.defs.paper_height * mm)) # bottomup = False =>(0, 0) is the upper left corner print ungettext(u'Creating stamp PDF for %i sheet', u'Creating stamp PDF for %i sheets', sheets) % sheets log.progressbar.start(sheets) for i in range(sheets): if questionnaire_ids is not None: id = questionnaire_ids.pop(0) for j in range(questionnaire_length): if survey.defs.style == "classic": draw_corner_marks(survey, canvas) draw_corner_boxes(survey, canvas, j) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_questionnaire_id(canvas, survey, id) if survey.defs.print_survey_id: draw_survey_id(canvas, survey) elif survey.defs.style == "code128": draw_corner_marks(survey, canvas) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_code128_questionnaire_id(canvas, survey, id) # Survey ID has to be printed in CODE128 mode, because it # contains the page number and rotation. draw_code128_sdaps_info(canvas, survey, j + 1) if survey.global_id is not None: draw_code128_global_id(canvas, survey) elif survey.defs.style == "custom": # Only draw corner marker draw_corner_marks(survey, canvas) pass else: raise AssertionError() canvas.showPage() log.progressbar.update(i + 1) canvas.save() print ungettext( u'%i sheet; %f seconds per sheet', u'%i sheet; %f seconds per sheet', log.progressbar.max_value) % (log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value)) if have_pdftk: stampsfile.close() # Merge using pdftk print _("Stamping using pdftk") tmp_dir = tempfile.mkdtemp() for page in xrange(1, questionnaire_length + 1): print ungettext(u"pdftk: Splitting out page %d of each sheet.", u"pdftk: Splitting out page %d of each sheet.", page) % page args = [] args.append('pdftk') args.append(survey.path('tmp.pdf')) args.append('cat') cur = page for i in range(sheets): args.append('%d' % cur) cur += questionnaire_length args.append('output') args.append(os.path.join(tmp_dir, 'stamp-%d.pdf' % page)) subprocess.call(args) print _(u"pdftk: Splitting the questionnaire for watermarking.") subprocess.call([ 'pdftk', survey.path('questionnaire.pdf'), 'dump_data', 'output', os.path.join(tmp_dir, 'doc_data.txt') ]) for page in xrange(1, questionnaire_length + 1): subprocess.call([ 'pdftk', survey.path('questionnaire.pdf'), 'cat', '%d' % page, 'output', os.path.join(tmp_dir, 'watermark-%d.pdf' % page) ]) for page in xrange(1, questionnaire_length + 1): print ungettext(u"pdftk: Watermarking page %d of all sheets.", u"pdftk: Watermarking page %d of all sheets.", page) % page subprocess.call([ 'pdftk', os.path.join(tmp_dir, 'stamp-%d.pdf' % page), 'background', os.path.join(tmp_dir, 'watermark-%d.pdf' % page), 'output', os.path.join(tmp_dir, 'watermarked-%d.pdf' % page) ]) args = [] args.append('pdftk') for page in xrange(1, questionnaire_length + 1): char = chr(ord('A') + page - 1) args.append('%s=' % char + os.path.join(tmp_dir, 'watermarked-%d.pdf' % page)) args.append('cat') for i in range(sheets): for page in xrange(1, questionnaire_length + 1): char = chr(ord('A') + page - 1) args.append('%s%d' % (char, i + 1)) args.append('output') args.append(os.path.join(tmp_dir, 'final.pdf')) print _(u"pdftk: Assembling everything into the final PDF.") subprocess.call(args) subprocess.call([ 'pdftk', os.path.join(tmp_dir, 'final.pdf'), 'update_info', os.path.join(tmp_dir, 'doc_data.txt'), 'output', output_filename ]) # Remove tmp.pdf os.unlink(survey.path('tmp.pdf')) # Remove all the temporary files shutil.rmtree(tmp_dir) else: # Merge using pyPdf stamped = pyPdf.PdfFileWriter() stamped._info.getObject().update({ pyPdf.generic.NameObject('/Producer'): pyPdf.generic.createStringObject(u'sdaps'), pyPdf.generic.NameObject('/Title'): pyPdf.generic.createStringObject(survey.title), }) subject = [] for key, value in survey.info.iteritems(): subject.append(u'%(key)s: %(value)s' % { 'key': key, 'value': value }) subject = u'\n'.join(subject) stamped._info.getObject().update({ pyPdf.generic.NameObject('/Subject'): pyPdf.generic.createStringObject(subject), }) stamps = pyPdf.PdfFileReader(stampsfile) del stampsfile questionnaire = pyPdf.PdfFileReader( file(survey.path('questionnaire.pdf'), 'rb')) print _(u'Stamping using pyPdf. For faster stamping, install pdftk.') log.progressbar.start(sheets) for i in range(sheets): for j in range(questionnaire_length): s = stamps.getPage(i * questionnaire_length + j) if not have_pdftk: q = questionnaire.getPage(j) s.mergePage(q) stamped.addPage(s) log.progressbar.update(i + 1) stamped.write(open(output_filename, 'wb')) print ungettext( u'%i sheet; %f seconds per sheet', u'%i sheet; %f seconds per sheet', log.progressbar.max_value) % (log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value))
def create_stamp_pdf(survey, output_filename, questionnaire_ids): sheets = 1 if questionnaire_ids is None else len(questionnaire_ids) questionnaire_length = survey.questionnaire.page_count have_pdftk = False # Test if pdftk is present, if it is we can use it to be faster try: result = subprocess.Popen(['pdftk', '--version'], stdout=subprocess.PIPE) # Just assume pdftk is there, if it was executed sucessfully if result is not None: have_pdftk = True except OSError: pass if not have_pdftk: try: import pyPdf except: log.error(_(u'You need to have either pdftk or pyPdf installed. pdftk is the faster method.')) sys.exit(1) # Write the "stamp" out to tmp.pdf if are using pdftk. if have_pdftk: stampsfile = file(survey.path('tmp.pdf'), 'wb') else: stampsfile = StringIO.StringIO() canvas = \ reportlab.pdfgen.canvas.Canvas(stampsfile, bottomup=False, pagesize=(survey.defs.paper_width * mm, survey.defs.paper_height * mm)) # bottomup = False =>(0, 0) is the upper left corner print ungettext(u'Creating stamp PDF for %i sheet', u'Creating stamp PDF for %i sheets', sheets) % sheets log.progressbar.start(sheets) for i in range(sheets): id = questionnaire_ids.pop(0) for j in range(questionnaire_length): if survey.defs.style == "classic": draw_corner_marks(survey, canvas) draw_corner_boxes(survey, canvas, j) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_questionnaire_id(canvas, survey, id) if survey.defs.print_survey_id: draw_survey_id(canvas, survey) elif survey.defs.style == "code128": draw_corner_marks(survey, canvas) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_code128_questionnaire_id(canvas, survey, id) # Survey ID has to be printed in CODE128 mode, because it # contains the page number and rotation. draw_code128_sdaps_info(canvas, survey, j + 1) if survey.global_id is not None: draw_code128_global_id(canvas, survey) else: raise AssertionError() canvas.showPage() log.progressbar.update(i + 1) canvas.save() print ungettext(u'%i sheet; %f seconds per sheet', u'%i sheet; %f seconds per sheet', log.progressbar.max_value) % ( log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value) ) if have_pdftk: stampsfile.close() # Merge using pdftk print _("Stamping using pdftk") tmp_dir = tempfile.mkdtemp() for page in xrange(1, questionnaire_length + 1): print ungettext(u"pdftk: Splitting out page %d of each sheet.", u"pdftk: Splitting out page %d of each sheet.", page) % page args = [] args.append('pdftk') args.append(survey.path('tmp.pdf')) args.append('cat') cur = page for i in range(sheets): args.append('%d' % cur) cur += questionnaire_length args.append('output') args.append(os.path.join(tmp_dir, 'stamp-%d.pdf' % page)) subprocess.call(args) print _(u"pdftk: Splitting the questionnaire for watermarking.") subprocess.call(['pdftk', survey.path('questionnaire.pdf'), 'dump_data', 'output', os.path.join(tmp_dir, 'doc_data.txt')]) for page in xrange(1, questionnaire_length + 1): subprocess.call(['pdftk', survey.path('questionnaire.pdf'), 'cat', '%d' % page, 'output', os.path.join(tmp_dir, 'watermark-%d.pdf' % page)]) for page in xrange(1, questionnaire_length + 1): print ungettext(u"pdftk: Watermarking page %d of all sheets.", u"pdftk: Watermarking page %d of all sheets.", page) % page subprocess.call(['pdftk', os.path.join(tmp_dir, 'stamp-%d.pdf' % page), 'background', os.path.join(tmp_dir, 'watermark-%d.pdf' % page), 'output', os.path.join(tmp_dir, 'watermarked-%d.pdf' % page)]) args = [] args.append('pdftk') for page in xrange(1, questionnaire_length + 1): char = chr(ord('A') + page - 1) args.append('%s=' % char + os.path.join(tmp_dir, 'watermarked-%d.pdf' % page)) args.append('cat') for i in range(sheets): for page in xrange(1, questionnaire_length + 1): char = chr(ord('A') + page - 1) args.append('%s%d' % (char, i + 1)) args.append('output') args.append(os.path.join(tmp_dir, 'final.pdf')) print _(u"pdftk: Assembling everything into the final PDF.") subprocess.call(args) subprocess.call(['pdftk', os.path.join(tmp_dir, 'final.pdf'), 'update_info', os.path.join(tmp_dir, 'doc_data.txt'), 'output', output_filename]) # Remove tmp.pdf os.unlink(survey.path('tmp.pdf')) # Remove all the temporary files shutil.rmtree(tmp_dir) else: # Merge using pyPdf stamped = pyPdf.PdfFileWriter() stamped._info.getObject().update({ pyPdf.generic.NameObject('/Producer'): pyPdf.generic.createStringObject(u'sdaps'), pyPdf.generic.NameObject('/Title'): pyPdf.generic.createStringObject(survey.title), }) subject = [] for key, value in survey.info.iteritems(): subject.append(u'%(key)s: %(value)s' % {'key': key, 'value': value}) subject = u'\n'.join(subject) stamped._info.getObject().update({ pyPdf.generic.NameObject('/Subject'): pyPdf.generic.createStringObject(subject), }) stamps = pyPdf.PdfFileReader(stampsfile) del stampsfile questionnaire = pyPdf.PdfFileReader( file(survey.path('questionnaire.pdf'), 'rb') ) print _(u'Stamping using pyPdf. For faster stamping, install pdftk.') log.progressbar.start(sheets) for i in range(sheets): for j in range(questionnaire_length): s = stamps.getPage(i * questionnaire_length + j) if not have_pdftk: q = questionnaire.getPage(j) s.mergePage(q) stamped.addPage(s) log.progressbar.update(i + 1) stamped.write(open(output_filename, 'wb')) print ungettext(u'%i sheet; %f seconds per sheet', u'%i sheet; %f seconds per sheet', log.progressbar.max_value) % ( log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value))
def create_stamp_pdf(survey, output_filename, questionnaire_ids): sheets = 1 if questionnaire_ids is None else len(questionnaire_ids) questionnaire_length = survey.questionnaire.page_count have_pdftk = False # Test if pdftk is present, if it is we can use it to be faster try: result = subprocess.Popen(["pdftk", "--version"], stdout=subprocess.PIPE) # Just assume pdftk is there, if it was executed sucessfully if result is not None: have_pdftk = True except OSError: pass if not have_pdftk: try: import pyPdf except: log.error(_(u"You need to have either pdftk or pyPdf installed. pdftk is the faster method.")) sys.exit(1) # Write the "stamp" out to tmp.pdf if are using pdftk. if have_pdftk: stampsfile = file(survey.path("tmp.pdf"), "wb") else: stampsfile = StringIO.StringIO() canvas = reportlab.pdfgen.canvas.Canvas( stampsfile, bottomup=False, pagesize=(survey.defs.paper_width * mm, survey.defs.paper_height * mm) ) # bottomup = False =>(0, 0) is the upper left corner print ungettext(u"Creating stamp PDF for %i sheet", u"Creating stamp PDF for %i sheets", sheets) % sheets log.progressbar.start(sheets) for i in range(sheets): if questionnaire_ids is not None: id = questionnaire_ids.pop(0) for j in range(questionnaire_length): if survey.defs.style == "classic": draw_corner_marks(survey, canvas) draw_corner_boxes(survey, canvas, j) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_questionnaire_id(canvas, survey, id) if survey.defs.print_survey_id: draw_survey_id(canvas, survey) elif survey.defs.style == "code128": draw_corner_marks(survey, canvas) if not survey.defs.duplex or j % 2: if questionnaire_ids is not None: draw_code128_questionnaire_id(canvas, survey, id) # Survey ID has to be printed in CODE128 mode, because it # contains the page number and rotation. draw_code128_sdaps_info(canvas, survey, j + 1) if survey.global_id is not None: draw_code128_global_id(canvas, survey) elif survey.defs.style == "custom": # Only draw corner marker draw_corner_marks(survey, canvas) pass else: raise AssertionError() canvas.showPage() log.progressbar.update(i + 1) canvas.save() print ungettext(u"%i sheet; %f seconds per sheet", u"%i sheet; %f seconds per sheet", log.progressbar.max_value) % ( log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value), ) if have_pdftk: stampsfile.close() # Merge using pdftk print _("Stamping using pdftk") tmp_dir = tempfile.mkdtemp() if sheets == 1: # Shortcut if we only have one sheet. # In this case form data in the PDF will *not* break, in # the other code path it *will* break. print _(u"pdftk: Overlaying the original PDF with the markings.") subprocess.call( ["pdftk", survey.path("questionnaire.pdf"), "stamp", survey.path("tmp.pdf"), "output", output_filename] ) else: for page in xrange(1, questionnaire_length + 1): print ungettext( u"pdftk: Splitting out page %d of each sheet.", u"pdftk: Splitting out page %d of each sheet.", page ) % page args = [] args.append("pdftk") args.append(survey.path("tmp.pdf")) args.append("cat") cur = page for i in range(sheets): args.append("%d" % cur) cur += questionnaire_length args.append("output") args.append(os.path.join(tmp_dir, "stamp-%d.pdf" % page)) subprocess.call(args) print _(u"pdftk: Splitting the questionnaire for watermarking.") subprocess.call( [ "pdftk", survey.path("questionnaire.pdf"), "dump_data", "output", os.path.join(tmp_dir, "doc_data.txt"), ] ) for page in xrange(1, questionnaire_length + 1): subprocess.call( [ "pdftk", survey.path("questionnaire.pdf"), "cat", "%d" % page, "output", os.path.join(tmp_dir, "watermark-%d.pdf" % page), ] ) if sheets == 1: for page in xrange(1, questionnaire_length + 1): print ungettext( u"pdftk: Watermarking page %d of all sheets.", u"pdftk: Watermarking page %d of all sheets.", page, ) % page subprocess.call( [ "pdftk", os.path.join(tmp_dir, "stamp-%d.pdf" % page), "background", os.path.join(tmp_dir, "watermark-%d.pdf" % page), "output", os.path.join(tmp_dir, "watermarked-%d.pdf" % page), ] ) else: for page in xrange(1, questionnaire_length + 1): print ungettext( u"pdftk: Watermarking page %d of all sheets.", u"pdftk: Watermarking page %d of all sheets.", page, ) % page subprocess.call( [ "pdftk", os.path.join(tmp_dir, "stamp-%d.pdf" % page), "background", os.path.join(tmp_dir, "watermark-%d.pdf" % page), "output", os.path.join(tmp_dir, "watermarked-%d.pdf" % page), ] ) args = [] args.append("pdftk") for page in xrange(1, questionnaire_length + 1): char = chr(ord("A") + page - 1) args.append("%s=" % char + os.path.join(tmp_dir, "watermarked-%d.pdf" % page)) args.append("cat") for i in range(sheets): for page in xrange(1, questionnaire_length + 1): char = chr(ord("A") + page - 1) args.append("%s%d" % (char, i + 1)) args.append("output") args.append(os.path.join(tmp_dir, "final.pdf")) print _(u"pdftk: Assembling everything into the final PDF.") subprocess.call(args) subprocess.call( [ "pdftk", os.path.join(tmp_dir, "final.pdf"), "update_info", os.path.join(tmp_dir, "doc_data.txt"), "output", output_filename, ] ) # Remove tmp.pdf os.unlink(survey.path("tmp.pdf")) # Remove all the temporary files shutil.rmtree(tmp_dir) else: # Merge using pyPdf stamped = pyPdf.PdfFileWriter() stamped._info.getObject().update( { pyPdf.generic.NameObject("/Producer"): pyPdf.generic.createStringObject(u"sdaps"), pyPdf.generic.NameObject("/Title"): pyPdf.generic.createStringObject(survey.title), } ) subject = [] for key, value in survey.info.iteritems(): subject.append(u"%(key)s: %(value)s" % {"key": key, "value": value}) subject = u"\n".join(subject) stamped._info.getObject().update( {pyPdf.generic.NameObject("/Subject"): pyPdf.generic.createStringObject(subject)} ) stamps = pyPdf.PdfFileReader(stampsfile) del stampsfile questionnaire = pyPdf.PdfFileReader(file(survey.path("questionnaire.pdf"), "rb")) print _(u"Stamping using pyPdf. For faster stamping, install pdftk.") log.progressbar.start(sheets) for i in range(sheets): for j in range(questionnaire_length): s = stamps.getPage(i * questionnaire_length + j) if not have_pdftk: q = questionnaire.getPage(j) s.mergePage(q) stamped.addPage(s) log.progressbar.update(i + 1) stamped.write(open(output_filename, "wb")) print ungettext( u"%i sheet; %f seconds per sheet", u"%i sheet; %f seconds per sheet", log.progressbar.max_value ) % (log.progressbar.max_value, float(log.progressbar.elapsed_time) / float(log.progressbar.max_value))