Page MenuHomec4science

Report_Number_Generation.py
No OneTemporary

File Metadata

Created
Wed, Nov 13, 11:35

Report_Number_Generation.py

## $Id$
## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002, 2003, 2004, 2005, 2006 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
## Description: function Report_Number_Generation
## This function creates a reference for the submitted
## document and saves it in the specified file.
## Author: T.Baron
##
## PARAMETERS: edsrn: name of the file in which the reference is saved
## autorngen: one of "A", "N", "Y"
## "A": The reference is the submission number
## "N": The reference is taken from a file [edsrn]
## "Y": The reference is generated
## rnin: name of the file containing the category
## counterpath: path to the counter file
## rnformat: format for the generated reference
## yeargen: if "AUTO", current year, else the year is
## extracted from the file [yeargen]
def Report_Number_Generation(parameters,curdir,form):
global doctype,access,act,dir,rn
# The program must automatically generate the report number
# Generate Year
if parameters['autorngen'] == "Y":
if parameters['yeargen'] == "AUTO":
# Current year is used
yy = time.strftime("%Y")
else :
# If yeargen != auto then the contents of the file named 'yeargen' is used
# Assumes file uses DD-MM-YYYY format
fp = open("%s/%s" % (curdir,parameters['yeargen']),"r")
mydate = fp.read()
fp.close()
yy = re.sub("^......","",mydate)
# Evaluate category - Category is read from the file named 'rnin
if os.path.exists("%s/%s" % (curdir,parameters['rnin'])):
fp = open("%s/%s" % (curdir,parameters['rnin']),"r")
category = fp.read()
category = category.replace("\n","")
else:
category = ""
# The counter and report number formats are evaluated.
# All data is considered as a string literal except that enclosed
# in the <PA></PA> brackets. The category replaces <PA>categ</PA>
# and the 4 digits year replaces <PA>yy</PA>
counter_path = parameters['counterpath'].replace("<PA>categ</PA>",category)
counter_path = counter_path.replace ("<PA>yy</PA>",yy)
counter_path = counter_path.replace(" ","")
counter_path = counter_path.replace("\n","")
rn_format = parameters['rnformat'].replace ("<PA>categ</PA>",category)
rn_format = rn_format.replace ("<PA>yy</PA>",yy)
# Check if the report number does not already exists
if os.path.exists("%s/%s" % (curdir,parameters['edsrn'])):
fp = open("%s/%s" % (curdir,parameters['edsrn']),"r")
oldrn = fp.read()
fp.close()
if oldrn != "" and not re.search("\?\?\?",oldrn):
rn = oldrn
return ""
# create it
rn = Create_Reference(counter_path,rn_format)
rn = rn.replace("\n","")
rn = rn.replace("\r","")
rn = rn.replace("\015","")
rn = rn.replace("\013","")
rn = rn.replace("\012","")
# The file edsrn is created in the submission directory, and it stores the report number
fp = open("%s/%s" % (curdir,parameters['edsrn']),"w")
fp.write(rn)
fp.close()
# The report number is just read from a specified file
elif parameters['autorngen'] == "N":
fp = open("%s/%s" % (curdir,parameters['edsrn']),"r")
rn = fp.read()
fp.close()
# Some documents are really annoying and insist on a totally different way of doing things
# This code is for documents which have the access number in the report
# number (instead of using a counter)
elif parameters['autorngen'] == "A":
rn = parameters['rnformat'].replace ("<PA>access</PA>",access)
# The file accessno/edsrn is created, and it stores the report number
fp = open("%s/%s" % (curdir,parameters['edsrn']),"w")
fp.write(rn)
fp.close()
return ""
def Create_Reference(counter_path,ref_format):
# test if the counters directory exists, if not attempts to create it
if not os.path.exists(counters):
try:
os.mkdir(counters)
except:
raise functionError("File System: Cannot create counters directory %s" % counters)
if not os.path.exists("%s/%s" % (counters,counter_path)):
try:
fp = open("%s/%s" % (counters,counter_path),"w")
except:
raise functionError("File System: no permission to write in counters directory %s" % counters)
fp.write ("0")
fp.close()
# retrieve current counter value
fp = open("%s/%s" % (counters,counter_path),"r")
id = fp.read()
fp.close()
if id == "":
id=0
# increment the counter
id = int(id)+1
# store new value
fp = open("%s/%s" % (counters,counter_path),"w")
fp.write (str(id))
fp.close()
# create final value
reference = "%s-%03d" % (ref_format,id)
# Return the report number prelude with the id concatenated on at the end
return reference

Event Timeline