def test_stamp_allpages(self): """websubmit - stamping all pages of a PDF (APIs)""" file_stamper_options = { "latex-template": "demo-stamp-left.tex", "latex-template-var": {"REPORTNUMBER": "TEST-2010", "DATE": "10/10/2000"}, "input-file": CFG_PREFIX + "/lib/webtest/invenio/test.pdf", "output-file": "test-stamp-allpages.pdf", "stamp": "all", "layer": "foreground", "verbosity": 0, } try: (stamped_file_path_only, stamped_file_name) = websubmit_file_stamper.stamp_file(file_stamper_options) except: self.fail("Stamping failed") # Test that file is now bigger... assert os.path.getsize(os.path.join(stamped_file_path_only, stamped_file_name)) > 12695
def test_stamp_allpages(self): """websubmit - stamping all pages of a PDF (APIs)""" file_stamper_options = { 'latex-template' : "demo-stamp-left.tex", 'latex-template-var' : {'REPORTNUMBER':'TEST-2010','DATE':'10/10/2000'}, 'input-file' : CFG_PREFIX + "/lib/webtest/invenio/test.pdf", 'output-file' : "test-stamp-allpages.pdf", 'stamp' : "all", 'layer' : "foreground", 'verbosity' : 0, } try: (stamped_file_path_only, stamped_file_name) = \ websubmit_file_stamper.stamp_file(file_stamper_options) except: self.fail("Stamping failed") # Test that file is now bigger... assert os.path.getsize(os.path.join(stamped_file_path_only, stamped_file_name)) > 12695
def Stamp_Replace_Single_File_Approval(parameters, \ curdir, \ form, \ user_info=None): """ This function is intended to be called when a document has been approved and needs to be stamped. The function should be used when there is ONLY ONE file to be stamped after approval (for example, the "main file"). The name of the file to be stamped should be known and should be stored in a file in the submission's working directory (without the extension). Generally, this will work our fine as the main file is named after the report number of the document, this will be stored in the report number file. @param parameters: (dictionary) - must contain: + latex_template: (string) - the name of the LaTeX template that should be used for the creation of the stamp. + latex_template_vars: (string) - a string-ified dictionary of variables to be replaced in the LaTeX template and the values (or names of files in curdir containing the values) with which to replace them. Use prefix 'FILE:' to specify that the stamped value must be read from a file in submission directory instead of being a fixed value to stamp. E.G.: { 'TITLE' : 'FILE:DEMOTHESIS_TITLE', 'DATE' : 'FILE:DEMOTHESIS_DATE' } + file_to_be_stamped: (string) - this is the name of a file in the submission's working directory that contains the name of the bibdocfile that is to be stamped. + new_file_name: (string) - this is the name of a file in the submission's working directory that contains the name that is to be given to the file after it has been stamped. If empty, or if that file doesn't exist, the file will not be renamed after stamping. + switch_file: (string) - when this value is set, specifies the name of a file that will swith on/off the stamping. The stamp will be applied if the file exists in the submission directory and is not empty. If the file cannot be found or is empty, the stamp is not applied. Useful for eg. if you want to let your users control the stamping with a checkbox on your submission page. Leave this parameter empty to always stamp by default. + stamp: (string) - the type of stamp to be applied to the file. should be one of: + first (only the first page is stamped); + all (all pages are stamped); + coverpage (a separate cover-page is added to the file as a first page); + layer: (string) - the position of the stamp. Should be one of: + background (invisible if original file has a white -not transparent- background layer) + foreground (on top of the stamped file. If the stamp does not have a transparent background, will hide all of the document layers) The default value is 'background'. """ ############ ## Definition of important variables: ############ ## The file stamper needs to be called with a dictionary of options of ## the following format: ## { 'latex-template' : "", ## TEMPLATE_NAME ## 'latex-template-var' : {}, ## TEMPLATE VARIABLES ## 'input-file' : "", ## INPUT FILE ## 'output-file' : "", ## OUTPUT FILE ## 'stamp' : "", ## STAMP TYPE ## 'layer' : "", ## LAYER TO STAMP ## 'verbosity' : 0, ## VERBOSITY (we don't care about it) ## } file_stamper_options = { 'latex-template': "", 'latex-template-var': {}, 'input-file': "", 'output-file': "", 'stamp': "", 'layer': "", 'verbosity': 0, } ## Check if stamping is enabled switch_file = parameters.get('switch_file', '') if switch_file: # Good, a "switch file" was specified. Check if it exists, and # it its value is not empty. if not _read_in_file(os.path.join(curdir, switch_file)): # File does not exist, or is emtpy. Silently abort # stamping. return "" ## Submission access number: access = _read_in_file("%s/access" % curdir) ## record ID for the current submission. It is found in the special file ## "SN" (sysno) in curdir: recid = _read_in_file("%s/SN" % curdir) try: recid = int(recid) except ValueError: ## No record ID. Cannot continue. err_msg = "Error in Stamp_Replace_Single_File_Approval: " \ "Cannot recover record ID from the submission's working " \ "directory. Stamping cannot be carried out. The " \ "submission ID is [%s]." % cgi.escape(access) register_exception(prefix=err_msg) raise InvenioWebSubmitFunctionError(err_msg) ############ ## Resolution of function parameters: ############ ## The name of the LaTeX template to be used for stamp creation: latex_template = "%s" % ((type(parameters['latex_template']) is str \ and parameters['latex_template']) or "") ## A string containing the variables/values that should be substituted ## in the final (working) LaTeX template: latex_template_vars_string = "%s" % \ ((type(parameters['latex_template_vars']) is str \ and parameters['latex_template_vars']) or "") ## The type of stamp to be applied to the file(s): stamp = "%s" % ((type(parameters['stamp']) is str and \ parameters['stamp'].lower()) or "") ## The layer to use for stamping: try: layer = parameters['layer'] except KeyError: layer = "background" if not layer in ('background', 'foreground'): layer = "background" ## Get the name of the file to be stamped from the file indicated in ## the file_to_be_stamped parameter: try: file_to_stamp_file = parameters['file_to_be_stamped'] except KeyError: file_to_stamp_file = "" else: if file_to_stamp_file is None: file_to_stamp_file = "" ## Get the "basename" for the file to be stamped (it's mandatory that it ## be in curdir): file_to_stamp_file = os.path.basename(file_to_stamp_file).strip() name_file_to_stamp = _read_in_file("%s/%s" % (curdir, file_to_stamp_file)) name_file_to_stamp.replace("\n", "").replace("\r", "") ## ## Get the name to be given to the file after it has been stamped (if there ## is one.) Once more, it will be found in a file in curdir: try: new_file_name_file = parameters['new_file_name'] except KeyError: new_file_name_file = "" else: if new_file_name_file is None: new_file_name_file = "" ## Get the "basename" for the file containing the new file name. (It's ## mandatory that it be in curdir): new_file_name_file = os.path.basename(new_file_name_file).strip() new_file_name = _read_in_file("%s/%s" % (curdir, new_file_name_file)) ############ ## Begin: ############ ## ## If no name for the file to stamp, warning. if name_file_to_stamp == "": wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It was not possible to recover a valid name for the " \ "file to be stamped. Stamping could not, therefore, be " \ "carried out. The submission ID is [%s]." \ % access raise InvenioWebSubmitFunctionWarning(wrn_msg) ## ## The file to be stamped is a bibdoc. We will only stamp it (a) if it ## exists; and (b) if it is a PDF file. So, get the path (in the bibdocs ## tree) to the file to be stamped: ## ## First get the object representing the bibdocs belonging to this record: bibrecdocs = BibRecDocs(recid) try: bibdoc_file_to_stamp = bibrecdocs.get_bibdoc("%s" % name_file_to_stamp) except InvenioBibDocFileError: ## Couldn't get a bibdoc object for this filename. Probably the file ## that we wanted to stamp wasn't attached to this record. wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It was not possible to recover a bibdoc object for the " \ "filename [%s] when trying to stamp the main file. " \ "Stamping could not be carried out. The submission ID is " \ "[%s] and the record ID is [%s]." \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) ## Get the BibDocFile object for the PDF version of the bibdoc to be ## stamped: try: bibdocfile_file_to_stamp = bibdoc_file_to_stamp.get_file("pdf") except InvenioBibDocFileError: ## This bibdoc doesn't have a physical file with the extension ".pdf" ## (take note of the lower-case extension - the bibdocfile library ## is case-sensitive with respect to filenames. Log that there was ## no "pdf" and check for a file with extension "PDF": wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It wasn't possible to recover a PDF BibDocFile object " \ "for the file with the name [%s], using the extension " \ "[pdf] - note the lower case - the bibdocfile library " \ "relies upon the case of an extension. The submission ID " \ "is [%s] and the record ID is [%s]. Going to try " \ "looking for a file with a [PDF] extension before giving " \ "up . . . " \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) try: bibdocfile_file_to_stamp = bibdoc_file_to_stamp.get_file("PDF") except InvenioBibDocFileError: wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It wasn't possible to recover a PDF " \ "BibDocFile object for the file with the name [%s], " \ "using the extension [PDF] - note the upper case. " \ "Had previously tried searching for [pdf] - now " \ "giving up. Stamping could not be carried out. " \ "The submission ID is [%s] and the record ID is [%s]." \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) ############ ## Go ahead and prepare the details for the LaTeX stamp template and its ## variables: ############ ## Strip the LaTeX filename into the basename (All templates should be ## in the template repository): latex_template = os.path.basename(latex_template) ## Convert the string of latex template variables into a dictionary ## of search-term/replacement-term pairs: latex_template_vars = get_dictionary_from_string( latex_template_vars_string) ## For each of the latex variables, check in `CURDIR' for a file with that ## name. If found, use it's contents as the template-variable's value. ## If not, just use the raw value string already held by the template ## variable: latex_template_varnames = latex_template_vars.keys() for varname in latex_template_varnames: ## Get this variable's value: varvalue = latex_template_vars[varname].strip() if not ((varvalue.find("date(") == 0 and varvalue[-1] == ")") or \ (varvalue.find("include(") == 0 and varvalue[-1] == ")")) \ and varvalue != "": ## We don't want to interfere with date() or include() directives, ## so we only do this if the variable value didn't contain them: ## ## Is this variable value the name of a file in the current ## submission's working directory, from which a literal value for ## use in the template should be extracted? If yes, it will ## begin with "FILE:". If no, we leave the value exactly as it is. if varvalue.upper().find("FILE:") == 0: ## The value to be used is to be taken from a file. Clean the ## file name and if it's OK, extract that value from the file. ## seekvalue_fname = varvalue[5:].strip() seekvalue_fname = os.path.basename(seekvalue_fname).strip() if seekvalue_fname != "": ## Attempt to extract the value from the file: if os.access("%s/%s" % (curdir, seekvalue_fname), \ os.R_OK|os.F_OK): ## The file exists. Extract its value: try: repl_file_val = \ open("%s/%s" \ % (curdir, seekvalue_fname), "r").readlines() except IOError: ## The file was unreadable. err_msg = "Error in Stamp_Replace_Single_File_" \ "Approval: The function attempted to " \ "read a LaTex template variable " \ "value from the following file in the " \ "current submission's working " \ "directory: [%s]. However, an " \ "unexpected error was encountered " \ "when doing so. Please inform the " \ "administrator." \ % seekvalue_fname register_exception(req=user_info['req']) raise InvenioWebSubmitFunctionError(err_msg) else: final_varval = "" for line in repl_file_val: final_varval += line final_varval = final_varval.rstrip() ## Replace the variable value with that which has ## been read from the file: latex_template_vars[varname] = final_varval else: ## The file didn't actually exist in the current ## submission's working directory. Use an empty ## value: latex_template_vars[varname] = "" else: ## The filename was not valid. err_msg = "Error in Stamp_Replace_Single_File_Approval: " \ "The function was configured to read a LaTeX " \ "template variable from a file with the " \ "following instruction: [%s --> %s]. The " \ "filename, however, was not considered valid. " \ "Please report this to the administrator." \ % (varname, varvalue) raise InvenioWebSubmitFunctionError(err_msg) ## Put the 'fixed' values into the file_stamper_options dictionary: file_stamper_options['latex-template'] = latex_template file_stamper_options['latex-template-var'] = latex_template_vars file_stamper_options['stamp'] = stamp file_stamper_options['layer'] = layer ## Put the input file and output file into the file_stamper_options ## dictionary: file_stamper_options['input-file'] = bibdocfile_file_to_stamp.fullpath file_stamper_options[ 'output-file'] = bibdocfile_file_to_stamp.get_full_name() ## ## Before attempting to stamp the file, log the dictionary of arguments ## that will be passed to websubmit_file_stamper: try: fh_log = open("%s/websubmit_file_stamper-calls-options.log" \ % curdir, "a+") fh_log.write("%s\n" % file_stamper_options) fh_log.flush() fh_log.close() except IOError: ## Unable to log the file stamper options. exception_prefix = "Unable to write websubmit_file_stamper " \ "options to log file " \ "%s/websubmit_file_stamper-calls-options.log" \ % curdir register_exception(prefix=exception_prefix) try: ## Try to stamp the file: (stamped_file_path_only, stamped_file_name) = \ websubmit_file_stamper.stamp_file(file_stamper_options) except InvenioWebSubmitFileStamperError: ## It wasn't possible to stamp this file. ## Register the exception along with an informational message: wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "There was a problem stamping the file with the name [%s] " \ "and the fullpath [%s]. The file has not been stamped. " \ "The submission ID is [%s] and the record ID is [%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ access, \ recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) else: ## Stamping was successful. The BibDocFile must now be revised with ## the latest (stamped) version of the file: file_comment = "Stamped by WebSubmit: %s" \ % time.strftime("%d/%m/%Y", time.localtime()) try: dummy = \ bibrecdocs.add_new_version("%s/%s" \ % (stamped_file_path_only, \ stamped_file_name), \ name_file_to_stamp, \ comment=file_comment, \ flags=('STAMPED', )) except InvenioBibDocFileError: ## Unable to revise the file with the newly stamped version. wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "After having stamped the file with the name [%s] " \ "and the fullpath [%s], it wasn't possible to revise " \ "that file with the newly stamped version. Stamping " \ "was unsuccessful. The submission ID is [%s] and the " \ "record ID is [%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ access, \ recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) else: ## File revised. If the file should be renamed after stamping, ## do so. if new_file_name != "": try: bibrecdocs.change_name(newname=new_file_name, docid=bibdoc_file_to_stamp.id) except (IOError, InvenioBibDocFileError): ## Unable to change the name wrn_msg = "Warning in Stamp_Replace_Single_File_Approval" \ ": After having stamped and revised the file " \ "with the name [%s] and the fullpath [%s], it " \ "wasn't possible to rename it to [%s]. The " \ "submission ID is [%s] and the record ID is " \ "[%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ new_file_name, \ access, \ recid) ## Finished. return ""
% visit_for_stamping_arguments['curdir'], "a+") fh_log.write("%s\n" % file_stamper_options) fh_log.flush() fh_log.close() except IOError: ## Unable to log the file stamper options. exception_prefix = "Unable to write websubmit_file_stamper " \ "options to log file " \ "%s/websubmit_file_stamper-calls-options.log" \ % visit_for_stamping_arguments['curdir'] register_exception(prefix=exception_prefix) try: ## Try to stamp the file: (stamped_file_path_only, stamped_file_name) = \ websubmit_file_stamper.stamp_file(file_stamper_options) except InvenioWebSubmitFileStamperError: ## It wasn't possible to stamp this file. ## Register the exception along with an informational message: exception_prefix = "A problem occurred when stamping [%s]. The " \ "stamping of this file was unsuccessful." \ % path_to_subject_file register_exception(prefix=exception_prefix) ## Skip this file, moving on to the next: continue else: ## Stamping was successful. path_to_stamped_file = "%s/%s" % (stamped_file_path_only, \ stamped_file_name) ## Move the unstamped file from the "files" directory into the ## "files_before_stamping" directory:
def Stamp_Replace_Single_File_Approval(parameters, \ curdir, \ form, \ user_info=None): """ This function is intended to be called when a document has been approved and needs to be stamped. The function should be used when there is ONLY ONE file to be stamped after approval (for example, the "main file"). The name of the file to be stamped should be known and should be stored in a file in the submission's working directory (without the extension). Generally, this will work our fine as the main file is named after the report number of the document, this will be stored in the report number file. @param parameters: (dictionary) - must contain: + latex_template: (string) - the name of the LaTeX template that should be used for the creation of the stamp. + latex_template_vars: (string) - a string-ified dictionary of variables to be replaced in the LaTeX template and the values (or names of files in curdir containing the values) with which to replace them. Use prefix 'FILE:' to specify that the stamped value must be read from a file in submission directory instead of being a fixed value to stamp. E.G.: { 'TITLE' : 'FILE:DEMOTHESIS_TITLE', 'DATE' : 'FILE:DEMOTHESIS_DATE' } + file_to_be_stamped: (string) - this is the name of a file in the submission's working directory that contains the name of the bibdocfile that is to be stamped. + new_file_name: (string) - this is the name of a file in the submission's working directory that contains the name that is to be given to the file after it has been stamped. If empty, or if that file doesn't exist, the file will not be renamed after stamping. + switch_file: (string) - when this value is set, specifies the name of a file that will swith on/off the stamping. The stamp will be applied if the file exists in the submission directory and is not empty. If the file cannot be found or is empty, the stamp is not applied. Useful for eg. if you want to let your users control the stamping with a checkbox on your submission page. Leave this parameter empty to always stamp by default. + stamp: (string) - the type of stamp to be applied to the file. should be one of: + first (only the first page is stamped); + all (all pages are stamped); + coverpage (a separate cover-page is added to the file as a first page); + layer: (string) - the position of the stamp. Should be one of: + background (invisible if original file has a white -not transparent- background layer) + foreground (on top of the stamped file. If the stamp does not have a transparent background, will hide all of the document layers) The default value is 'background'. """ ############ ## Definition of important variables: ############ ## The file stamper needs to be called with a dictionary of options of ## the following format: ## { 'latex-template' : "", ## TEMPLATE_NAME ## 'latex-template-var' : {}, ## TEMPLATE VARIABLES ## 'input-file' : "", ## INPUT FILE ## 'output-file' : "", ## OUTPUT FILE ## 'stamp' : "", ## STAMP TYPE ## 'layer' : "", ## LAYER TO STAMP ## 'verbosity' : 0, ## VERBOSITY (we don't care about it) ## } file_stamper_options = { 'latex-template' : "", 'latex-template-var' : { }, 'input-file' : "", 'output-file' : "", 'stamp' : "", 'layer' : "", 'verbosity' : 0, } ## Check if stamping is enabled switch_file = parameters.get('switch_file', '') if switch_file: # Good, a "switch file" was specified. Check if it exists, and # it its value is not empty. if not _read_in_file(os.path.join(curdir, switch_file)): # File does not exist, or is emtpy. Silently abort # stamping. return "" ## Submission access number: access = _read_in_file("%s/access" % curdir) ## record ID for the current submission. It is found in the special file ## "SN" (sysno) in curdir: recid = _read_in_file("%s/SN" % curdir) try: recid = int(recid) except ValueError: ## No record ID. Cannot continue. err_msg = "Error in Stamp_Replace_Single_File_Approval: " \ "Cannot recover record ID from the submission's working " \ "directory. Stamping cannot be carried out. The " \ "submission ID is [%s]." % cgi.escape(access) register_exception(prefix=err_msg) raise InvenioWebSubmitFunctionError(err_msg) ############ ## Resolution of function parameters: ############ ## The name of the LaTeX template to be used for stamp creation: latex_template = "%s" % ((type(parameters['latex_template']) is str \ and parameters['latex_template']) or "") ## A string containing the variables/values that should be substituted ## in the final (working) LaTeX template: latex_template_vars_string = "%s" % \ ((type(parameters['latex_template_vars']) is str \ and parameters['latex_template_vars']) or "") ## The type of stamp to be applied to the file(s): stamp = "%s" % ((type(parameters['stamp']) is str and \ parameters['stamp'].lower()) or "") ## The layer to use for stamping: try: layer = parameters['layer'] except KeyError: layer = "background" if not layer in ('background', 'foreground'): layer = "background" ## Get the name of the file to be stamped from the file indicated in ## the file_to_be_stamped parameter: try: file_to_stamp_file = parameters['file_to_be_stamped'] except KeyError: file_to_stamp_file = "" else: if file_to_stamp_file is None: file_to_stamp_file = "" ## Get the "basename" for the file to be stamped (it's mandatory that it ## be in curdir): file_to_stamp_file = os.path.basename(file_to_stamp_file).strip() name_file_to_stamp = _read_in_file("%s/%s" % (curdir, file_to_stamp_file)) name_file_to_stamp.replace("\n", "").replace("\r", "") ## ## Get the name to be given to the file after it has been stamped (if there ## is one.) Once more, it will be found in a file in curdir: try: new_file_name_file = parameters['new_file_name'] except KeyError: new_file_name_file = "" else: if new_file_name_file is None: new_file_name_file = "" ## Get the "basename" for the file containing the new file name. (It's ## mandatory that it be in curdir): new_file_name_file = os.path.basename(new_file_name_file).strip() new_file_name = _read_in_file("%s/%s" % (curdir, new_file_name_file)) ############ ## Begin: ############ ## ## If no name for the file to stamp, warning. if name_file_to_stamp == "": wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It was not possible to recover a valid name for the " \ "file to be stamped. Stamping could not, therefore, be " \ "carried out. The submission ID is [%s]." \ % access raise InvenioWebSubmitFunctionWarning(wrn_msg) ## ## The file to be stamped is a bibdoc. We will only stamp it (a) if it ## exists; and (b) if it is a PDF file. So, get the path (in the bibdocs ## tree) to the file to be stamped: ## ## First get the object representing the bibdocs belonging to this record: bibrecdocs = BibRecDocs(recid) try: bibdoc_file_to_stamp = bibrecdocs.get_bibdoc("%s" % name_file_to_stamp) except InvenioWebSubmitFileError: ## Couldn't get a bibdoc object for this filename. Probably the file ## that we wanted to stamp wasn't attached to this record. wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It was not possible to recover a bibdoc object for the " \ "filename [%s] when trying to stamp the main file. " \ "Stamping could not be carried out. The submission ID is " \ "[%s] and the record ID is [%s]." \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) ## Get the BibDocFile object for the PDF version of the bibdoc to be ## stamped: try: bibdocfile_file_to_stamp = bibdoc_file_to_stamp.get_file("pdf") except InvenioWebSubmitFileError: ## This bibdoc doesn't have a physical file with the extension ".pdf" ## (take note of the lower-case extension - the bibdocfile library ## is case-sensitive with respect to filenames. Log that there was ## no "pdf" and check for a file with extension "PDF": wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It wasn't possible to recover a PDF BibDocFile object " \ "for the file with the name [%s], using the extension " \ "[pdf] - note the lower case - the bibdocfile library " \ "relies upon the case of an extension. The submission ID " \ "is [%s] and the record ID is [%s]. Going to try " \ "looking for a file with a [PDF] extension before giving " \ "up . . . " \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) try: bibdocfile_file_to_stamp = bibdoc_file_to_stamp.get_file("PDF") except InvenioWebSubmitFileError: wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "It wasn't possible to recover a PDF " \ "BibDocFile object for the file with the name [%s], " \ "using the extension [PDF] - note the upper case. " \ "Had previously tried searching for [pdf] - now " \ "giving up. Stamping could not be carried out. " \ "The submission ID is [%s] and the record ID is [%s]." \ % (name_file_to_stamp, access, recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) ############ ## Go ahead and prepare the details for the LaTeX stamp template and its ## variables: ############ ## Strip the LaTeX filename into the basename (All templates should be ## in the template repository): latex_template = os.path.basename(latex_template) ## Convert the string of latex template variables into a dictionary ## of search-term/replacement-term pairs: latex_template_vars = get_dictionary_from_string(latex_template_vars_string) ## For each of the latex variables, check in `CURDIR' for a file with that ## name. If found, use it's contents as the template-variable's value. ## If not, just use the raw value string already held by the template ## variable: latex_template_varnames = latex_template_vars.keys() for varname in latex_template_varnames: ## Get this variable's value: varvalue = latex_template_vars[varname].strip() if not ((varvalue.find("date(") == 0 and varvalue[-1] == ")") or \ (varvalue.find("include(") == 0 and varvalue[-1] == ")")) \ and varvalue != "": ## We don't want to interfere with date() or include() directives, ## so we only do this if the variable value didn't contain them: ## ## Is this variable value the name of a file in the current ## submission's working directory, from which a literal value for ## use in the template should be extracted? If yes, it will ## begin with "FILE:". If no, we leave the value exactly as it is. if varvalue.upper().find("FILE:") == 0: ## The value to be used is to be taken from a file. Clean the ## file name and if it's OK, extract that value from the file. ## seekvalue_fname = varvalue[5:].strip() seekvalue_fname = os.path.basename(seekvalue_fname).strip() if seekvalue_fname != "": ## Attempt to extract the value from the file: if os.access("%s/%s" % (curdir, seekvalue_fname), \ os.R_OK|os.F_OK): ## The file exists. Extract its value: try: repl_file_val = \ open("%s/%s" \ % (curdir, seekvalue_fname), "r").readlines() except IOError: ## The file was unreadable. err_msg = "Error in Stamp_Replace_Single_File_" \ "Approval: The function attempted to " \ "read a LaTex template variable " \ "value from the following file in the " \ "current submission's working " \ "directory: [%s]. However, an " \ "unexpected error was encountered " \ "when doing so. Please inform the " \ "administrator." \ % seekvalue_fname register_exception(req=user_info['req']) raise InvenioWebSubmitFunctionError(err_msg) else: final_varval = "" for line in repl_file_val: final_varval += line final_varval = final_varval.rstrip() ## Replace the variable value with that which has ## been read from the file: latex_template_vars[varname] = final_varval else: ## The file didn't actually exist in the current ## submission's working directory. Use an empty ## value: latex_template_vars[varname] = "" else: ## The filename was not valid. err_msg = "Error in Stamp_Replace_Single_File_Approval: " \ "The function was configured to read a LaTeX " \ "template variable from a file with the " \ "following instruction: [%s --> %s]. The " \ "filename, however, was not considered valid. " \ "Please report this to the administrator." \ % (varname, varvalue) raise InvenioWebSubmitFunctionError(err_msg) ## Put the 'fixed' values into the file_stamper_options dictionary: file_stamper_options['latex-template'] = latex_template file_stamper_options['latex-template-var'] = latex_template_vars file_stamper_options['stamp'] = stamp file_stamper_options['layer'] = layer ## Put the input file and output file into the file_stamper_options ## dictionary: file_stamper_options['input-file'] = bibdocfile_file_to_stamp.fullpath file_stamper_options['output-file'] = bibdocfile_file_to_stamp.fullname ## ## Before attempting to stamp the file, log the dictionary of arguments ## that will be passed to websubmit_file_stamper: try: fh_log = open("%s/websubmit_file_stamper-calls-options.log" \ % curdir, "a+") fh_log.write("%s\n" % file_stamper_options) fh_log.flush() fh_log.close() except IOError: ## Unable to log the file stamper options. exception_prefix = "Unable to write websubmit_file_stamper " \ "options to log file " \ "%s/websubmit_file_stamper-calls-options.log" \ % curdir register_exception(prefix=exception_prefix) try: ## Try to stamp the file: (stamped_file_path_only, stamped_file_name) = \ websubmit_file_stamper.stamp_file(file_stamper_options) except InvenioWebSubmitFileStamperError: ## It wasn't possible to stamp this file. ## Register the exception along with an informational message: wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "There was a problem stamping the file with the name [%s] " \ "and the fullpath [%s]. The file has not been stamped. " \ "The submission ID is [%s] and the record ID is [%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ access, \ recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) else: ## Stamping was successful. The BibDocFile must now be revised with ## the latest (stamped) version of the file: file_comment = "Stamped by WebSubmit: %s" \ % time.strftime("%d/%m/%Y", time.localtime()) try: dummy = \ bibrecdocs.add_new_version("%s/%s" \ % (stamped_file_path_only, \ stamped_file_name), \ name_file_to_stamp, \ comment=file_comment, \ flags=('STAMPED', )) except InvenioWebSubmitFileError: ## Unable to revise the file with the newly stamped version. wrn_msg = "Warning in Stamp_Replace_Single_File_Approval: " \ "After having stamped the file with the name [%s] " \ "and the fullpath [%s], it wasn't possible to revise " \ "that file with the newly stamped version. Stamping " \ "was unsuccessful. The submission ID is [%s] and the " \ "record ID is [%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ access, \ recid) register_exception(prefix=wrn_msg) raise InvenioWebSubmitFunctionWarning(wrn_msg) else: ## File revised. If the file should be renamed after stamping, ## do so. if new_file_name != "": try: bibdoc_file_to_stamp.change_name(new_file_name) except (IOError, InvenioWebSubmitFileError): ## Unable to change the name wrn_msg = "Warning in Stamp_Replace_Single_File_Approval" \ ": After having stamped and revised the file " \ "with the name [%s] and the fullpath [%s], it " \ "wasn't possible to rename it to [%s]. The " \ "submission ID is [%s] and the record ID is " \ "[%s]." \ % (name_file_to_stamp, \ file_stamper_options['input-file'], \ new_file_name, \ access, \ recid) ## Finished. return ""
def visit_for_stamping(visit_for_stamping_arguments, dirname, filenames): """Visitor function called by os.path.walk. This function takes a directory and a list of files in that directory and for each file, calls the websubmit_file_stamper on it. When a file is stamped, the original is moved away into a directory of unstamped files and the new, stamped version is moved into its place. @param visit_for_stamping_arguments: (dictionary) of arguments needed by this function. Must contain 'curdir', 'user_info' and 'file_stamper_options' members. @param dirname: (string) - the path to the directory in which the files are to be stamped. @param filenames: (list) - the names of each file in dirname. An attempt will be made to stamp each of these files. @Exceptions Raised: + InvenioWebSubmitFunctionWarning; + InvenioWebSubmitFunctionError; """ ## Get the dictionary of options to pass to the stamper: file_stamper_options = visit_for_stamping_arguments['file_stamper_options'] ## Create a directory to store original files before stamping: dirname_files_pre_stamping = dirname.replace("/files/", \ "/files_before_stamping/", 1) if not os.path.exists(dirname_files_pre_stamping): try: os.makedirs(dirname_files_pre_stamping) except OSError as err: ## Unable to make a directory in which to store the unstamped ## files. ## Register the exception: exception_prefix = "Unable to stamp files in [%s]. Couldn't " \ "create directory in which to store the " \ "original, unstamped files." \ % dirname register_exception(prefix=exception_prefix) ## Since we can't make a directory for the unstamped files, ## we can't continue to stamp them. ## Skip the stamping of the contents of this directory by raising ## a warning: msg = "Warning: A problem occurred when stamping files in [%s]. " \ "Unable to create directory to store the original, " \ "unstamped files. Got this error: [%s]. This means the " \ "files in this directory were not stamped." \ % (dirname, str(err)) raise InvenioWebSubmitFunctionWarning(msg) ## Loop through each file in the directory and attempt to stamp it: for file_to_stamp in filenames: ## Get the path to the file to be stamped and put it into the ## dictionary of options that will be passed to stamp_file: path_to_subject_file = "%s/%s" % (dirname, file_to_stamp) file_stamper_options['input-file'] = path_to_subject_file ## Just before attempting to stamp the file, log the dictionary of ## options (file_stamper_options) that will be passed to websubmit- ## file-stamper: try: fh_log = open("%s/websubmit_file_stamper-calls-options.log" \ % visit_for_stamping_arguments['curdir'], "a+") fh_log.write("%s\n" % file_stamper_options) fh_log.flush() fh_log.close() except IOError: ## Unable to log the file stamper options. exception_prefix = "Unable to write websubmit_file_stamper " \ "options to log file " \ "%s/websubmit_file_stamper-calls-options.log" \ % visit_for_stamping_arguments['curdir'] register_exception(prefix=exception_prefix) try: ## Try to stamp the file: (stamped_file_path_only, stamped_file_name) = \ websubmit_file_stamper.stamp_file(file_stamper_options) except InvenioWebSubmitFileStamperError: ## It wasn't possible to stamp this file. ## Register the exception along with an informational message: exception_prefix = "A problem occurred when stamping [%s]. The " \ "stamping of this file was unsuccessful." \ % path_to_subject_file register_exception(prefix=exception_prefix) ## Skip this file, moving on to the next: continue else: ## Stamping was successful. path_to_stamped_file = "%s/%s" % (stamped_file_path_only, \ stamped_file_name) ## Move the unstamped file from the "files" directory into the ## "files_before_stamping" directory: try: shutil.move(path_to_subject_file, "%s/%s" \ % (dirname_files_pre_stamping, file_to_stamp)) except IOError: ## Couldn't move the original file away from the "files" ## directory. Log the problem and continue on to the next ## file: exception_prefix = "A problem occurred when stamping [%s]. " \ "The file was sucessfully stamped, and " \ "can be found here: [%s]. Unfortunately " \ "though, it could not be copied back to " \ "the current submission's working " \ "directory because the unstamped version " \ "could not be moved out of the way (tried " \ "to move it from here [%s] to here: " \ "[%s/%s]). The stamping of this file was " \ "unsuccessful." \ % (path_to_subject_file, \ path_to_stamped_file, \ path_to_subject_file, \ dirname_files_pre_stamping, \ file_to_stamp) register_exception(prefix=exception_prefix) continue else: ## The original file has been moved into the files before ## stamping directory. Now try to copy the stamped file into ## the files directory: try: shutil.copy(path_to_stamped_file, "%s/%s" \ % (dirname, file_to_stamp)) except IOError: ## Even though the original, unstamped file was moved away ## from the files directory, the stamped-version couldn't ## be moved into its place. Register the exception: exception_prefix = "A problem occurred when stamping " \ "[%s]. The file was sucessfully " \ "stamped, and can be found here: " \ "[%s]. Unfortunately though, it " \ "could not be copied back to the " \ "current submission's working " \ "directory." % (path_to_subject_file, \ path_to_stamped_file) register_exception(prefix=exception_prefix) ## Because it wasn't possible to move the stamped file ## into the files directory, attempt to move the original, ## unstamped file back into the files directory: try: shutil.move("%s/%s" % (dirname_files_pre_stamping, \ file_to_stamp), \ path_to_stamped_file) except IOError as err: ## It wasn't possible even to move the original file ## back to the files directory. Register the ## exception and stop the stamping process - it isn't ## safe to continue: exeption_prefix = "A problem occurred when stamping " \ "[%s]. The file was sucessfully " \ "stamped, and can be found here: " \ "[%s]. Unfortunately though, it " \ "could not be copied back to the " \ "current submission's working " \ "directory. Additionionally, the " \ "original, unstamped file " \ "could not be moved back to the " \ "files directory, from the files-" \ "before-stamping directory. It " \ "can now be found here: [%s/%s]. " \ "Stamping cannot continue and " \ "manual intervention is necessary " \ "because the file cannot be " \ "attached to the record." \ % (path_to_subject_file, \ path_to_stamped_file, \ dirname_files_pre_stamping, \ file_to_stamp) register_exception(prefix=exeption_prefix) ## Raise an InvenioWebSubmitFunctionError, stopping ## further stamping, etc: raise InvenioWebSubmitFunctionError(exception_prefix)