Skip to content

Commit 3ed1f46

Browse files
committed
handle ... at the end of the scilab script
exec does not handle ...
1 parent 6e2cd44 commit 3ed1f46

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

blocks/simulationAPI/helpers/scilab_manager.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,62 @@ def uploaddatafile(session, task):
692692
return JsonResponse(rv)
693693

694694

695+
def handle_line(current_line, line):
696+
line = line.rstrip()
697+
if line.endswith('...'):
698+
# Remove continuation and add to buffer
699+
current_line += line.rstrip('.').rstrip() + ' '
700+
logger.info('Current line: %s#', current_line.rstrip())
701+
complete_line = False
702+
else:
703+
if current_line:
704+
current_line += line
705+
logger.info('Line: %s$', current_line.rstrip())
706+
else:
707+
current_line = line
708+
complete_line = True
709+
return current_line, complete_line
710+
711+
712+
def handle_uploaded_sce_file(file, fname):
713+
with open(fname, 'w') as f:
714+
current_line = ''
715+
partial_line = ''
716+
717+
for chunk in file.chunks():
718+
text = chunk.decode('ascii')
719+
lines = text.splitlines()
720+
721+
if not lines:
722+
continue
723+
724+
# Add partial_line to the first line
725+
if partial_line:
726+
lines[0] = partial_line + lines[0]
727+
partial_line = ''
728+
logger.info('Complete line: %s$', lines[0].rstrip())
729+
730+
# If the last line doesn't end with a newline, it is partial
731+
if not lines[-1].endswith('\n'):
732+
partial_line = lines.pop() # Save the partial line
733+
logger.info('Partial line: %s#', partial_line)
734+
735+
for line in lines:
736+
current_line, complete_line = handle_line(current_line, line)
737+
if complete_line:
738+
f.write(current_line.rstrip() + '\n')
739+
current_line = ''
740+
741+
# Add partial_line to the remaining line
742+
if partial_line:
743+
current_line, complete_line = handle_line(current_line, partial_line)
744+
745+
# Write remaining line if any
746+
if current_line:
747+
logger.info('Last line: %s$', current_line.rstrip())
748+
f.write(current_line.rstrip() + '\n')
749+
750+
695751
def uploadscript(session, task):
696752
'''
697753
Below route is called for uploading script file.
@@ -706,10 +762,7 @@ def uploadscript(session, task):
706762

707763
fname = join(sessiondir, SCRIPT_FILES_FOLDER,
708764
f"{script.script_id}_script.sce")
709-
# file.save(fname)
710-
with open(fname, 'wb+') as destination:
711-
for chunk in file.chunks():
712-
destination.write(chunk)
765+
handle_uploaded_sce_file(file, fname)
713766
script.filename = fname
714767

715768
if is_unsafe_script(fname):

0 commit comments

Comments
 (0)