2020import re
2121import time
2222from dataclasses import asdict , dataclass
23- from typing import Any , Callable , Dict , List , Optional , Union
23+ from typing import Any , Callable , Dict , List , Optional , Set , Union
2424
2525import conclib
2626import corplib
27+ from bgcalc .task import AsyncTaskStatus
28+ import bgcalc
2729import mailing
2830import plugins
2931import settings
@@ -1237,6 +1239,7 @@ class SaveConcArgs:
12371239 align_kwic : int = 0
12381240 from_line : int = 0
12391241 to_line : IntOpt = - 1
1242+ task_id : str = ''
12401243
12411244
12421245def _get_ipm_base_set_desc (corp : AbstractKCorpus , contains_within , translate : Callable [[str ], str ]):
@@ -1259,7 +1262,7 @@ def _get_ipm_base_set_desc(corp: AbstractKCorpus, contains_within, translate: Ca
12591262@bp .route ('/saveconc' )
12601263@http_action (
12611264 access_level = 2 , action_model = ConcActionModel , mapped_args = SaveConcArgs , return_type = 'plain' )
1262- async def saveconc (amodel : ConcActionModel , req : KRequest [SaveConcArgs ], resp : KResponse ):
1265+ async def saveconc (amodel : ConcActionModel , req : KRequest [SaveConcArgs ], resp : KResponse ):
12631266 def mkfilename (suffix ):
12641267 return f'{ amodel .args .corpname } -concordance.{ suffix } '
12651268
@@ -1292,6 +1295,7 @@ def mkfilename(suffix):
12921295 'Content-Disposition' ,
12931296 f'attachment; filename="{ mkfilename (req .mapped_args .saveformat )} "' )
12941297
1298+ long_lines : Set [int ] = set ()
12951299 for from_line , to_line in line_range_chunks :
12961300 if from_line > 0 :
12971301 req .mapped_args .heading = 0
@@ -1316,12 +1320,18 @@ def mkfilename(suffix):
13161320
13171321 maxcontext = int (amodel .corp .get_conf ('MAXCONTEXT' ))
13181322 if maxcontext :
1319- for line in data .Lines :
1323+ for i , line in enumerate ( data .Lines , from_line + 1 ) :
13201324 if len (line ["Kwic" ]) > maxcontext :
1321- raise UnavailableForLegalReasons ('KWIC too large' )
1325+ line ["Kwic" ] = line ["Kwic" ][:maxcontext ] + [{'str' : '...' }]
1326+ long_lines .add (i )
13221327 if len (data .Lines ) > 0 :
13231328 await writer .write_conc (amodel , data , req .mapped_args )
1329+
13241330 output = writer .raw_content ()
1331+
1332+ worker = bgcalc .calc_backend_client (settings )
1333+ notification = f'KWIC exceeds max allowed length; Shortened lines: { ", " .join (map (str , sorted (long_lines )))} ' if long_lines else None
1334+ await worker .send_task ('notification' , object .__class__ , (notification ,), task_id = req .mapped_args .task_id )
13251335 return bytes_stream (output )
13261336
13271337 except Exception as e :
0 commit comments