# -*- coding: iso-8859-7 -*-
import sys
from p_ggen import path,Struct,prg
import filmed, ffff
openFilePar = lambda iun, f: None
ppro = 'egn', 'ydr'
lang = -1
files1 = []
if 1:
DLERRLIN = 'Error at line'
DLWARLIN = 'Warning at line'
DLOFFIL = 'Of file'
DLERRSTO = 'Errors logged. Program stops.'
DLMAXNUM = 'Max number of'
DLERRSYN = 'Syntax error.'
DLFILACC = 'File can not be accessed'
DLLIBUNI = 'Library filDat.lib accepts file units between'
DLFILEND = 'Unexpected end of file.'
DLFILREA = 'File can not be read.'
DLDSKFUL = 'The disk is full.'
DLFILWRI = 'The file can not be written to.'
DLTRYAGA = 'Try again.'
DLFILSUF = 'Do not specify suffix in the filename!'
DLMAXPRE = 'Up to 2 prefixes are allowed.'
DLPREFIX = 'PREFIX : '
DLPREFI1 = 'PREFIX 1 : '
DLPREFI2 = 'PREFIX 2 : '
DLMES01 = 'The data files are composed by a prefix and a suffix.'
DLMES02 = 'The suffix is predefined, and the suffix is given by the user.'
DLMES03 = 'For example given the prefix "' + ppro[0] + '"'
DLMES04 = 'the program opens the following FILES:'
DLMES05 = 'For example given the prefix 1 "' + ppro[0] + " and the prefix2 " + ppro[1] + '"'
DLFILDAT = '(data)'
DLFILRES = '(results)'
DLFILOPT = '(optional)'
DLNOSUF = 'Do not append a suffix to the prefix!'
DLNODESC = '(No description available for previous program)'
DLGETPRE = 'Getting prefix from file'
DLERRPRV = 'Error in the execution of the previous program:'
DLLOGOST = '!$B3)"Y%U$B%V"U%T$!X7"G3'
def setPar(fun):
"Sets the user defined function for reading extended information from mediat.tmp."
global openFilePar
openFilePar = fun
def openFile1(un, ext, stat, iPro, desc):
"""Requests a file to be opened.
c-----un = 0 : .
c , desc .
c iPro=-1, . ..
c un = 999: .
c .
c un = 888: .
c . FILNAMMED
c un > 0 : File unit. uArx<un<uTel. un
c 0, 888, 999 .
c
c ext :
c
c stat = 'old' :
c .
c stat = ' ' : .
c stat = 'opt' : .
c (optional).
c
c optional,
c (
c ).
c
c iPro = 1 2 : ( : 1 2)
c iPro = -1 -2 : .
c
c .
c .
c
c (batch file).
c
c desc :
"""
# hhhh()
global descp, dispLogo, files1, fylPro, lang
lang = -1
if un == 0:
descp = desc
dispLogo = iPro != -1
files1 = []
if lang == 0: optFile1(0, -1) # If language is not set, then set language to Greek
elif un == 888:
fylPro = True
return openFiles1()
elif un == 999:
fylPro = False
return openFiles1()
else:
if not (1 <= abs(iPro) <= 2):
prg(' Sr openFile1: %d %s' % (iPro, DLMAXPRE))
sys.exit(1)
file1 = Struct()
file1.ext = ext; file1.stat = stat; file1.desc = desc; file1.iPro = iPro
file1.linesf = 0 # It signals that the file unit is used
files1.append(file1)
def opFile1e(un, ext, stat, pro, desc):
"Opens the file and fails if it can not open it."
#-------It is assumed that stat1 is at least 3 characters long
di, why = opFile1(un, ext, stat, pro, desc)
if di != None: return di
stat = stat.lower().strip()
if stat[:3] == 'opt': return {ext:None} # Optional data
raise IOError, why
def opFile1(un, ext, stat1, pro, desc):
"Opens file immediatly."
stat = stat1[:3].strip().lower()
fr, why = _opFile1(pro+"."+ext, stat) # Try to open file
if fr == None: return None, why
import openfile
file1 = Struct()
file1.ext = ext; file1.stat = stat; file1.desc = desc; file1.iPro = 0
file1.linesf = 0 # It signals that the file unit is used
file1.fun = fr
openfile.files1.append(file1)
return {ext:fr}, None
#===========================================================================
def openFiles1():
"Tries to open the requested files."
nPro = prothem1(); pro = [0,0]
nProm = filmed.openFileMed(pro, nPro)
if nProm == None:
openfileMhn(nPro)
if nPro == 1:
pro[0] = openfilepro(DLPREFIX, 0)
else:
pro[0] = openfilepro(DLPREFI1, 0)
pro[1] = openfilepro(DLPREFI2, prothem1Opt(1))
filmed.openFileWrmed(nPro, pro) # Gets ready for errors
openFileOpen(pro)
openFilePar(0, 0) #
filmed.openFileWrmed(nPro, pro) # Include
else:
filmed.openFileWrmed(nProm, pro) # Gets ready for errors
openFileOpen(pro)
filmed.openFileWrmed(nProm, pro) # Include
return dict([(file1.ext, file1.fun) for file1 in files1])
#===========================================================================
def openfileMhn(nPro):
"Prints message for the user."
if dispLogo: print "%s\n========================" % ffff.gggg(DLLOGOST) #
prg("\n\n %s\n%s" % (descp, "-"*(len(descp)+2))) #
openFilePar(lang, 0) #
prg("\n".join(("", DLMES01, DLMES02))) # Files composed by prefix, suffix
if nPro == 1: prg("\n".join((DLMES03, DLMES04, ""))) # Example for 1 prefix
else: prg("\n".join((DLMES05, DLMES04, ""))) # Example for 2 prefixes
#-----
n = max([len(file1.desc) for file1 in files1]) #
form = "%s.%s: FILE %-" + str(n) + "s %s"
for file1 in files1:
ded = DLFILDAT # data
if file1.stat != 'old': ded = DLFILRES # results
if file1.stat == 'opt': ded = DLFILOPT # optional
i = abs(file1.iPro)
if file1.iPro > 0:
prg(form % (ppro[i-1], file1.ext, file1.desc, ded))
print "\n"
#===========================================================================
def openfilepro(mes, icod):
""" .
icod=0 .
icod=1 .
"""
while True:
pro = path(raw_input(mes).strip())
if pro == '' and icod == 0: continue
if pro == '': return pro
if pro.ext in ("", "."): return pro.parent / pro.namebase
prg(DLNOSUF) # Do not append a suffix to the prefix!
#===========================================================================
def openFileOpen(pro):
"Open all the requested files."
#-----
for file1 in files1:
i = abs(file1.iPro)
if len(pro[i-1]) > 0: # Check if prefix is optional
filnam = pro[i-1] + "." + file1.ext
file1.fun = _opFile1e(filnam, file1.stat)
else:
file1.fun = None
#----- files
# batch file.
for file1 in files1:
if file1.iPro < 0 and file1.fun != None: file1.fun.close(); file1.fun = None
#===========================================================================
def prothem1():
" "
nPro = 1
for file1 in files1:
i1 = abs(file1.iPro)
if i1 > nPro: nPro = i1
return nPro
#===========================================================================
def prothem1Opt(iPro):
" optional."
for file1 in files1:
if abs(file1.iPro)-1 == iPro:
if file1.stat != 'opt': return 0 # not optional
return 1 # optional
def closeFiles1():
for file1 in files1:
if file1.fun != None: file1.fun.close()
filmed.okMedFile1()
def stopErr1():
for file1 in files1: file1.fun.close()
filmed.errMedFile1()
def _opFile1(filnam1, stat1):
stat = stat1.lower().strip()
if stat == '': stat = 'w' # unknown -> write
elif stat[:3] == 'old': stat = 'r' # read
elif stat[:3] == 'app': stat = 'a' # append
elif stat[:3] == 'opt': stat = 'r' # Optional data
try: return open(filnam1, stat), None # Try to open file
except IOError, why: return None, why
def _opFile1e(filnam1, stat1):
"Opens a file with status stat1; fail if error."
f, why = _opFile1(filnam1, stat1)
if f != None: return f
stat1 = stat1.lower().strip()
if stat1[:3] == 'opt': return f # Optional data
prg("%s: %s" % (filnam1, DLFILACC))
sys.exit(1)
|