|
9 | 9 | import subprocess |
10 | 10 | import logging |
11 | 11 | import re |
| 12 | +import base64 |
12 | 13 |
|
13 | 14 | from netpyne import specs, sim, analysis |
14 | 15 | from netpyne.specs.utils import validateFunction |
@@ -593,42 +594,51 @@ def header(title, spacer='-'): |
593 | 594 | params = ['popParams', 'cellParams', 'synMechParams'] |
594 | 595 | params += ['connParams', 'stimSourceParams', 'stimTargetParams'] |
595 | 596 |
|
596 | | - fname = args['fileName'] if args['fileName'][-3:] == '.py' else args['fileName'] + '.py' |
| 597 | + fname = args['fileName'] |
| 598 | + if not fname: |
| 599 | + # default option |
| 600 | + fname = 'output.py' |
| 601 | + |
| 602 | + if not fname[-3:] == '.py': |
| 603 | + fname = f"{fname}.py" |
597 | 604 |
|
| 605 | + # TODO: use methods offered by netpyne to create this script! |
598 | 606 | with open(fname, 'w') as script: |
599 | | - script.write('from netpyne import specs, sim\n') |
600 | | - script.write(header('documentation')) |
601 | | - script.write("''' Script generated with NetPyNE-UI. Please visit:\n") |
602 | | - script.write(" - https://www.netpyne.org\n - https://github.com/MetaCell/NetPyNE-UI\n'''\n") |
603 | | - script.write(header('script', spacer='=')) |
604 | | - script.write('netParams = specs.NetParams()\n') |
605 | | - script.write('simConfig = specs.SimConfig()\n') |
606 | | - script.write(header('single value attributes')) |
| 607 | + script.write("from netpyne import specs, sim\n") |
| 608 | + script.write(header("documentation")) |
| 609 | + script.write("Script generated with NetPyNE-UI. Please visit:\n") |
| 610 | + script.write(" - https://www.netpyne.org\n - https://github.com/MetaCell/NetPyNE-UI\n\n") |
| 611 | + script.write(header("script", spacer="=")) |
| 612 | + script.write("netParams = specs.NetParams()\n") |
| 613 | + script.write("simConfig = specs.SimConfig()\n") |
| 614 | + script.write(header("single value attributes")) |
607 | 615 | for attr, value in list(self.netParams.__dict__.items()): |
608 | 616 | if attr not in params: |
609 | 617 | if value != getattr(specs.NetParams(), attr): |
610 | | - script.write('netParams.' + attr + ' = ') |
611 | | - script.write(convert2bool(json.dumps(value, indent=4)) + '\n') |
| 618 | + script.write("netParams." + attr + " = ") |
| 619 | + script.write(convert2bool(json.dumps(value, indent=4)) + "\n") |
612 | 620 |
|
613 | | - script.write(header('network attributes')) |
| 621 | + script.write(header("network attributes")) |
614 | 622 | for param in params: |
615 | 623 | for key, value in list(getattr(self.netParams, param).items()): |
616 | | - script.write("netParams." + param + "['" + key + "'] = ") |
617 | | - script.write(convert2bool(json.dumps(value, indent=4)) + '\n') |
| 624 | + script.write("netParams." + param + "[" + key + "] = ") |
| 625 | + script.write(convert2bool(json.dumps(value, indent=4)) + "\n") |
618 | 626 |
|
619 | | - script.write(header('network configuration')) |
| 627 | + script.write(header("network configuration")) |
620 | 628 | for attr, value in list(self.simConfig.__dict__.items()): |
621 | 629 | if value != getattr(specs.SimConfig(), attr): |
622 | | - script.write('simConfig.' + attr + ' = ') |
623 | | - script.write(convert2bool(json.dumps(value, indent=4)) + '\n') |
| 630 | + script.write("simConfig." + attr + " = ") |
| 631 | + script.write(convert2bool(json.dumps(value, indent=4)) + "\n") |
624 | 632 |
|
625 | | - script.write(header('create simulate analyze network')) |
626 | | - script.write('# sim.createSimulateAnalyze(netParams=netParams, simConfig=simConfig)\n') |
| 633 | + script.write(header("create simulate analyze network")) |
| 634 | + script.write("# sim.createSimulateAnalyze(netParams=netParams, simConfig=simConfig)\n") |
627 | 635 |
|
628 | | - script.write(header('end script', spacer='=')) |
| 636 | + script.write(header("end script", spacer="=")) |
629 | 637 |
|
630 | 638 | with open(fname) as f: |
631 | | - return f.read() |
| 639 | + file_b64 = base64.b64encode(bytes(f.read(), 'utf-8')).decode() |
| 640 | + export_info = {"fileContent": file_b64, "fileName": fname} |
| 641 | + return export_info |
632 | 642 |
|
633 | 643 | except: |
634 | 644 | return utils.getJSONError("Error while importing the NetPyNE model", sys.exc_info()) |
|
0 commit comments