Skip to content

Commit

Permalink
Fixing Marc export
Browse files Browse the repository at this point in the history
  • Loading branch information
filak committed Mar 31, 2020
1 parent feb4315 commit 3a2d929
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
15 changes: 8 additions & 7 deletions flask-app/mtw/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __call__(self, environ, start_response):

pp = pprint.PrettyPrinter(indent=2)


import logging
from logging import FileHandler
from logging import Formatter
Expand All @@ -50,7 +49,7 @@ def __call__(self, environ, start_response):

app.config.update(dict(
APP_NAME = 'MTW',
APP_VER = '1.3.5',
APP_VER = '1.3.6',
API_VER = '1.0.0',
APP_URL = '/mtw',
TEMP_DIR = mtu.get_instance_dir(app, 'temp'),
Expand All @@ -63,6 +62,7 @@ def __call__(self, environ, start_response):
CACHE_IGNORE_ERRORS = False,
CACHE_DIR = mtu.get_instance_dir(app, 'cache'),
SESSION_COOKIE_HTTPONLY = True,
SESSION_COOKIE_SAMESITE = 'Strict',
SESSION_PERMANENT = True,
SESSION_USE_SIGNER = True,
SESSION_FILE_DIR = mtu.get_instance_dir(app, 'sessions')
Expand Down Expand Up @@ -1374,7 +1374,7 @@ def manage(action):
msg['text'] = msg_text
msg['head'] = msg_head

mpath = mtu.getTempFpath('admin-msg')
mpath = mtu.getTempFpath('admin-msg', year=False)
mtu.writeJsonFile(mpath, msg)

return redirect(ref_redirect())
Expand Down Expand Up @@ -1697,7 +1697,6 @@ def login():


@app.route(getPath('/logout/'))
@login_required
def logout():

if session.get('logged_user') and session['ugroup']:
Expand Down Expand Up @@ -1737,14 +1736,16 @@ def checkWorker(worker):
except requests.RequestException as err:
app.logger.error('Worker: %s \n\n %s', worker, str(err) )
return 'ERROR'



### not used with Flask-Session
'''
@app.before_request
def before_request():
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(minutes=app.config['LOGOUT_AFTER'])
session.modified = True

'''

@app.context_processor
def utility_processor():
Expand Down Expand Up @@ -1818,7 +1819,7 @@ def get_uparams_skeleton():
@app.context_processor
def utility_processor():
def get_adminMsg():
mpath = mtu.getTempFpath('admin-msg')
mpath = mtu.getTempFpath('admin-msg', year=False)
if mpath.is_file():
return mtu.loadJsonFile(mpath)
else:
Expand Down
64 changes: 43 additions & 21 deletions flask-app/mtw_utils/mtw_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def exportData(export):
ext = 'json'
if export in ('umls'):
ext = 'tsv'

fpath = getTempFpath(export, ext=ext)

if not lpath.is_file():
Expand Down Expand Up @@ -572,12 +573,12 @@ def getLookupXml(lookups, export):
for k in sorted(items):
eng = html.escape(items[k].get('eng','') )
trx = html.escape(items[k].get('trx','') )
dclass = items[k].get('dc',-1)
dclass = items[k].get('dc','-1')

if items[k].get('active') == False:
line = '<%s id="%s" eng="%s" trx="%s" dclass="%d" active="0" />\n' % (tag, k, eng, trx, dclass)
line = '<%s id="%s" eng="%s" trx="%s" dclass="%s" active="0" />\n' % (tag, k, eng, trx, dclass)
else:
line = '<%s id="%s" eng="%s" trx="%s" dclass="%d" />\n' % (tag, k, eng, trx, dclass)
line = '<%s id="%s" eng="%s" trx="%s" dclass="%s" />\n' % (tag, k, eng, trx, dclass)
xml.write(line)

xml.write('</'+root+'>\n')
Expand Down Expand Up @@ -695,7 +696,24 @@ def getMarcFields(dui, item, descriptors, qualifiers, qualifs, xnote, lp='=', cp
crt = crt.replace('-','')
crt = crt[2:8]

rows.append(lp + '008' + cp + crt + '#n#ancnnbaba###########a#ana#####d')

htag = '50'
dt = 'a'
if item.get('dc') == '1': ## 150 - topical
htag = '50'
elif item.get('dc') == '2': ## 155 - publ
htag = '55'
dt = 'b'
elif item.get('dc') == '3': ## 150 - checktag
htag = '50'
elif item.get('dc') == '4': ## 151 - geo
htag = '51'
dt = 'd'
elif item.get('dc') == '0': ## 150 - Qualifiers not exported as Marc records
htag = '50'


rows.append(lp + '008' + cp + crt + '#n#ancnnbab'+ dt + '###########a#ana#####d')

rows.append(lp + '035 $a' + fw + '(DNLM)' + dui )
rows.append(lp + '040 $a' + fw + app.config['MARC_CATCODE'] + fw + '$b' + fw + getLangCodeUmls(app.config['TARGET_LANG'], lower=True) )
Expand All @@ -705,11 +723,6 @@ def getMarcFields(dui, item, descriptors, qualifiers, qualifs, xnote, lp='=', cp
trx = '$a' + fw + trn.replace('.', '.' + fw + '$x' + fw)
rows.append(lp + '072 ' + trx)

htag = '50'
if item.get('dc') == 2:
htag = '55'
elif item.get('dc') == 4:
htag = '51'

heading = item.get('trx', '')
if heading == '':
Expand Down Expand Up @@ -821,7 +834,7 @@ def getMarcFields(dui, item, descriptors, qualifiers, qualifs, xnote, lp='=', cp
#=750 /2$aCalcimycin$7D000001
# 750 /2 $a Calcimycin $7 D000001

rows.append(lp + '750' + cp + ' 2' + fw + '$a' + fw + item.get('eng', '') + fw + '$7' + fw + dui)
rows.append(lp + '7' + htag + cp + ' 2' + fw + '$a' + fw + item.get('eng', '') + fw + '$7' + fw + dui)

return '\n'.join(rows)

Expand Down Expand Up @@ -853,21 +866,30 @@ def getFpathDate(fpath):
return datetime.datetime.fromtimestamp(fpath.stat().st_mtime).strftime('%Y-%m-%d %H:%M:%S')


def getStatsFpath(stat, ext='json', params=None):
def getStatsFpath(stat, ext='json', params=None, target_year=None):
if ext == 'marc' and params:
p = params.split('~')
ext = p[1] + '.txt'
stat += '_' + p[0]
stat += '_' + p[0]

if not target_year:
target_year = app.config['TARGET_YEAR']

return Path( app.config['EXP_DIR'], app.config['TARGET_YEAR'] + '_' + stat + '.' + ext )
return Path( app.config['EXP_DIR'], target_year + '_' + stat + '.' + ext )


def getLockFpath(stat):
return Path( app.config['TEMP_DIR'], '_' + stat + '.lock' )


def getTempFpath(fname, ext='json', subdir=''):
return Path( app.config['TEMP_DIR'], subdir, fname + '.' + ext )
def getTempFpath(fname, ext='json', subdir='', year=True):

if year:
target_year = app.config['TARGET_YEAR'] + '_'
else:
target_year = ''

return Path( app.config['TEMP_DIR'], subdir, target_year + fname + '.' + ext )


def backStatsProcess(fpath, lpath, stat):
Expand Down Expand Up @@ -1321,13 +1343,13 @@ def getLangCodeUmls(lang_tag, lower=False):
### meshv:TopicalDescriptor,meshv:GeographicalDescriptor,meshv:PublicationType,meshv:CheckTag,meshv:Qualifier
def getDescClass(dtype):
dtype_dict = {
'Qualifier' : 0,
'TopicalDescriptor' : 1,
'GeographicalDescriptor' : 2,
'PublicationType' : 3,
'CheckTag' : 4
'Qualifier': '0',
'TopicalDescriptor': '1',
'PublicationType': '2',
'CheckTag': '3',
'GeographicalDescriptor': '4'
}
return dtype_dict.get(dtype, -1)
return dtype_dict.get(dtype, '-1')


def exportTsv(export, inputFile, outputFile):
Expand Down

0 comments on commit 3a2d929

Please sign in to comment.