Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.stsci.edu/spst/UnixTransition/doc/restore_attach.py
Дата изменения: Fri Feb 28 14:46:11 2014
Дата индексирования: Sat Mar 1 17:13:42 2014
Кодировка:

Поисковые слова: annular solar eclipse
#MODULE restore_attach
#
#*******************************************************************
"""

**PURPOSE** --

Restores attachments for crafted (attached) parallels to the PMDB
Data is taken from the standard .attachments file which is assumed
to be in the local directory.

**DEVELOPER** --

Alan Patterson

**MODIFICATION HISTORY** --

Initial implementation 05/07/2001 APP.
Python review changes 05/30/2001 APP.
Make test for each existing record more
explicit (i.e include primary) 08/28/2002 APP.

"""
#*******************************************************************
import spss_sys_util,sys,time_util,string,spst_getopt,stpydb,su_util

def run(weekid = None, *args):
"""
do restore_attach [-parsus=suid1,suid2,suid3,...]
[-r=oldmain,newmain,...]
[-f=]

where weekid is the standard calendar week id (yyddd)
-parsus=suid1,suid2,suid3,.... specifies the
crafted parallel SUs that are to be included
in the re-attachments
-r=oldmain,newmain,... specifies which main SUS are
being replaced by new ones. All attachments
using these mains will be restored. The mains
appear as pairs (old,new,old,new,...). This
arguement should only be used if the alignment
and obset structure of the new mains is the
same as the old.
-f= gives the name of the file
containing the attachment records. The default
is yyddd.attachments in the working directory
where yyddd is the weekid (first arg).
If -parsus is missing the user will be queried to
confirm that ALL attachments should be restored.
"""

if (not weekid):
print run.__doc__
return (not spss_sys_util.SUCCESS)

week = time_util.weekid(weekid)
attach_file = '%s.attachments' % weekid
parlist = su_util.su_list()
oldmains =[]
newmains = []
allowed_options = ['parsus=','r=','f=']
print 'Length of args'
print len(args)
options, parms = spst_getopt.spst_getopt(args, allowed_options)

if options.has_key("-parsus"):
parsus = string.split(string.upper(options['-parsus']),',')
parlist = su_util.su_list(parsus)
if options.has_key("-r"):
mains = string.split(string.upper(options['-r']),',')
if (len(mains) == 0 or len(mains) % 2 == 1):
print "Incorrect main specification"
raise ValueError
for i in range(0,len(mains),2):
oldmains.append(su_util.su(mains[i]))
newmains.append(su_util.su(mains[i+1]))
if options.has_key("-f"):
attach_file=options['-f']

doall = 0
if (len(parlist) == 0 and len(oldmains) == 0):
print "No attached parallels are specified"
yn = ''
while len(yn) == 0 or string.upper(yn[0]) not in ['Y','N']:
yn = raw_input("Do you wish to restore ALL attachments? (Y/N):")
if (string.upper(yn[0]) != 'Y'):
print "Exiting ..."
return (not spss_sys_util.SUCCESS)
else:
doall = 1

print 'Confirming arguements...'
if (doall == 1):
print 'ALL attachments will be restored'
if (len(parlist) > 0):
print ' The parallel attachments for these SUs will be restored'
for par in parlist:
print par
if (len(oldmains) > 0):
print ' The following conversion of primaries will be applied'
print ' and all attachments to these SUs will be restored'
for main in oldmains:
print '%s --> %s' % (main, newmains[oldmains.index(main)])

print 'Attachment file %s' % attach_file
# get all lines in original attach file
f = open(attach_file,'r')
attlines = f.readlines()
f.close()

db = stpydb.stpydb(dbmsName=\
spss_sys_util.get_environ_variable('SPSS_DB')[0])

restored = 0
# process each data line in the attachment file
for line in attlines:
# data line should have >80 chars with : and * at particular places
if (len(line) > 80):
if (line[7] == ':' and line[11] == '*'):
# setup data record from line data
parrec = get_pardata(line,db)
# only certain restore options are allowed
# - ALL lines or when the parallel SU was specified
# - and when the primary SU has been changed
if (doall == 1 or \
parrec['sunit_id'] in parlist or \
parrec['prim_su'] in oldmains):
if (parrec['prim_su'] in oldmains):
newsu = newmains[oldmains.index(parrec['prim_su'])]
newobs = newsu.get_obsets()
if len(newobs) == 0:
print 'Ignoring su %s - does not exist' % newsu
else:
newobs0 = '%s' % newobs[0]
primid = '%s:01:01' % (newobs0[:8])
parrec['prim_su_id'] = primid
primsustring = '%s' % newsu
parrec['prim_su'] = primsustring[:7]
# check for existence of qparallels record with parsu
db.query('select sunit_id from qparallels')
db.query('where sunit_id = @su')
#db.query(' and week_id = @week')
db.query(' and prim_prop_id = @primprop')
db.query(' and prim_obs_id = @primobs')
db.setParam('su', parrec['sunit_id'])
db.setParam('primprop', parrec['prim_su_id'][:5])
db.setParam('primobs', parrec['prim_su_id'][6:8])
db.setParam('week', weekid)
result = [[]]
db.executeAll(result)

# insert (restore) a record if one does not exist for
# the parallel SU.
if (not result):
num_rows = insert_par_rec(parrec,db,weekid)
if num_rows == 1:
restored = restored+1
else:
print 'Attachment for %s on %s found - no restore' % \
(parrec['sunit_id'], parrec['prim_su'])

print '%i attachments restored' % restored
return spss_sys_util.SUCCESS

def insert_par_rec(parrec,db,weekid):
db.beginTransaction()
db.query('insert into qparallels')
db.query(' (sunit_id, version_num, prim_prop_id,')
db.query(' prim_obs_id,prim_ali_id,prim_version,')
db.query(' prim_su_id,rest_window,rest_start,')
db.query(' rest_end,rest_align,rest_v3_flg,')
db.query(' rest_min_v3,rest_max_v3,par_target,')
db.query(' priority,week_id)')
db.query(' values (@su,@ver,@prop,@obs,@ali,@vn,')
db.query(' @primsu,@restw,@reststart,@restend,')
db.query(' @restali,@restflg,@restmin,@restmax,')
db.query(' @partarg,@pri,@week)')
db.setParam([['su', parrec['sunit_id']],
['ver', parrec['version_num']],
['prop', parrec['prim_su_id'][0:5]],
['obs', parrec['prim_su_id'][6:8]],
['ali', parrec['prim_su_id'][9:11]],
['vn', parrec['prim_su_id'][12:14]],
['primsu', parrec['prim_su']],
['restw', parrec['rest_window']],
['reststart', parrec['rest_start']],
['restend', parrec['rest_end']],
['restali', parrec['rest_align']],
['restflg', parrec['rest_v3_flg']],
['restmin', parrec['rest_min_v3']],
['restmax', parrec['rest_max_v3']],
['partarg', parrec['par_target']],
['pri', parrec['priority']],
['week', weekid]])
db.executeUpdate()
num_rows = db.getRowcount()
db.commitTransaction()
return num_rows


def get_pardata(line,db):
parsu = line[0:7]
parver = line[8:10]
primsu = line[28:35]
primid = line[13:27]
# if primsu is blank get SU id from PMDB
if (primsu == ' '):
db.query('select sunit_id from qsbranching')
db.query('where proposal_id = @prop')
db.query(' and obset_id = @obs')
db.setParam([['prop', primid[:5]], ['obs', primid[6:8]]])
result = [[]]
db.executeAll(result)
primsu = result[0][0]
# if primid is blank construct it from primsu
if (primid == ' '):
primsu = '%s%s' % (primid[:5],primid[6:8])
windflg = line[37]
if (windflg != 'N'):
windstart = int(time_util.spss_time(line[42:59]).get_sogsseconds())
windend = int(time_util.spss_time(line[60:77]).get_sogsseconds())
else:
windstart = 0
windend = 0
algflg = line[78]
v3flg = line[82]
if (v3flg != 'N'):
v3min = float(line[86:93])
v3max = float(line[94:101])
else:
v3min = 0.0
v3max = 0.0
targ = line[102:110]
prior = int(line[111:119])
return ({'sunit_id': parsu, 'version_num': parver, 'prim_su': primsu, \
'prim_su_id': primid, 'rest_window': windflg, 'rest_start': windstart, \
'rest_end': windend, 'rest_align': algflg, 'rest_v3_flg': v3flg, \
'rest_min_v3': v3min, 'rest_max_v3': v3max, 'par_target': targ, \
'priority': prior})


# to run on commandline
if __name__ == '__main__':
if len(sys.argv) > 1:
apply(run, tuple(sys.argv[1:]))
else:
run()