Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39558
1
#!/usr/bin/env python
2
# -*- coding: utf8 -*-
3
4
# CoCalc: Collaborative Calculation in the Cloud
5
#
6
# Copyright (C) 2017, SageMath, Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
#
22
23
"""
24
This script converts a .m GNU Octave file to an SMC sagews file.
25
It relies on the sagews built-in mode `%octave` to instantiate a communication bridge
26
to the octave Jupyter kernel.
27
28
Authors:
29
30
* Hal Snyder <[email protected]>, started January 2017
31
* Harald Schilly <[email protected]>, started June 2016
32
"""
33
34
from __future__ import print_function
35
import sys
36
import os
37
import codecs
38
import textwrap
39
40
from smc_pyutil.lib import SagewsCell
41
42
43
class M2SageWS(object):
44
45
def __init__(self, filename, overwrite=True):
46
"""
47
Convert a GNU Octave .m file to a CoCalc .sagews file.
48
49
INPUT:
50
- ``filename`` -- the name of an m file, say foo.m
51
52
OUTPUT:
53
- creates a file foo.sagews if it doesn't already exist
54
"""
55
self.infile = filename
56
base = os.path.splitext(filename)[0]
57
self.outfile = base + '.sagews'
58
if not overwrite and os.path.exists(self.outfile):
59
raise Exception(
60
"%s: Warning --CoCalc worksheet '%s' already exists. Not overwriting.\n" % (sys.argv[0], self.outfile))
61
62
self.m = None # holds the notebook data
63
self.output = None # use self.write([line]) to write to output
64
65
def convert(self):
66
"""
67
Main routine
68
"""
69
self.read()
70
self.open()
71
self.kernel()
72
self.body()
73
74
def read(self):
75
"""
76
Reads the m file
77
"""
78
with open(self.infile, 'r') as inf:
79
self.m = inf.read()
80
81
def write(self, line):
82
if line is not None:
83
self.output.send(line)
84
85
def open(self):
86
sys.stdout.write("%s: Creating CoCalc worksheet '%s'\n" %
87
(sys.argv[0], self.outfile))
88
sys.stdout.flush()
89
90
def output():
91
with codecs.open(self.outfile, 'w', 'utf8') as fout:
92
while True:
93
cell = yield
94
if cell is None:
95
break
96
fout.write(cell)
97
self.output = output()
98
self.output.next()
99
100
def kernel(self):
101
"""
102
The first cell contains a small info text and sets the global octave mode.
103
"""
104
cell = '''\
105
%auto
106
# This cell automatically evaluates on startup -- or run it manually if it didn't evaluate.
107
# Here, it starts the Jupyter octave kernel and sets it as the default mode for this worksheet.
108
%default_mode octave'''
109
self.write(SagewsCell(input=textwrap.dedent(cell)).convert())
110
111
def body(self):
112
"""
113
Convert input to body of the sagews document.
114
"""
115
fhead = "# {}\n".format(self.infile)
116
self.write(SagewsCell(input=fhead+self.m).convert())
117
118
119
def main():
120
if len(sys.argv) == 1:
121
sys.stderr.write("""
122
Convert a GNU Octave .m file to a CoCalc .sagews file.
123
124
Usage: %s path/to/filename.m [path/to/filename2.m ...]
125
126
Creates corresponding file path/to/filename.sagews, if it doesn't exist. Sets
127
default_mode to %octave. Places the .m file in a single sagews cell with
128
file name in a comment at the start.
129
""" % sys.argv[0])
130
sys.exit(1)
131
132
for path in sys.argv[1:]:
133
M2SageWS(path).convert()
134
135
if __name__ == "__main__":
136
main()
137
138