"Fossies" - the Fresh Open Source Software Archive 
Member "TeXmacs-2.1.2-src/plugins/tmpy/graph/graph.py" (5 May 2022, 4409 Bytes) of package /linux/misc/TeXmacs-2.1.2-src.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "graph.py" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.1.1_vs_2.1.2.
1 #!/usr/bin/env python
2 ###############################################################################
3 ##
4 ## MODULE : graph.py
5 ## DESCRIPTION : An abstract Graph class
6 ## COPYRIGHT : (C) 2019 Darcy Shen
7 ##
8 ## This software falls under the GNU general public license version 3 or later.
9 ## It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
10 ## in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
11
12 import os
13 import platform
14 import shutil
15 import time
16 from ..protocol import *
17 from ..compat import *
18
19
20 class Graph(object):
21 name = ""
22 message = ""
23 pre_code = ""
24 post_code = ""
25 height = ""
26 width = ""
27 output = ""
28 default_width = "0px"
29 default_height = "0px"
30 default_output = "eps"
31
32 def greet(self):
33 for x in self.message.split("\n"):
34 if len(x) == 0:
35 pass
36 else:
37 flush_verbatim (x + "\n")
38 flush_prompt (self.name + "] ")
39
40 def available(self):
41 return which(self.name) is not None
42
43 def reset_options(self):
44 self.height = self.default_height
45 self.width = self.default_width
46 self.output = self.default_output
47
48 def apply_magic(self, magic_line):
49 args = list(filter(lambda x: len(x)!=0, magic_line.split(" ")[1:]))
50 while (len(args) > 1):
51 option = args[0]
52 value = args[1]
53 if (option == '-width'):
54 self.width = value
55 elif (option == '-height'):
56 self.height = value
57 elif (option == '-output'):
58 self.output = value
59 else:
60 pass
61 args = args[2:]
62
63 def before_evaluate(self):
64 self.reset_options()
65 if not os.path.exists(self.get_tmp_dir()):
66 os.mkdir(self.get_tmp_dir())
67
68 def evaluate(self, code):
69 pass
70
71 def after_evaluate(self):
72 self.clean_tmp_dir()
73 self.remove_tmp_dir()
74
75 def eval(self, code):
76 self.before_evaluate()
77 if (code.startswith("%")):
78 magic_lines = code.split("\n")
79 magic_line = magic_lines[0]
80 self.apply_magic(magic_line)
81 code = '\n'.join(magic_lines[1:])
82 self.evaluate(code)
83 time.sleep(1)
84 self.after_evaluate()
85
86 def main_loop(self):
87 # Main session loop.
88 while True:
89 line = tm_input()
90 if not line:
91 continue
92 if line[0] == DATA_COMMAND:
93 # TODO: Handle completions
94 continue
95 else:
96 lines = [line]
97 while line != "<EOF>":
98 line = tm_input()
99 lines.append(line)
100 text = '\n'.join(lines[:-1])
101 self.eval(text)
102
103 def get_tmp_dir(self):
104 dir = "graph_" + self.name + "_" + str(os.getpid())
105 if (platform.system() == "Windows"):
106 return os.getenv("TEXMACS_HOME_PATH") + "\\system\\tmp\\" + dir + "\\"
107 else:
108 return os.getenv("TEXMACS_HOME_PATH") + "/system/tmp/" + dir + "/"
109
110 def remove_tmp_dir(self):
111 if (platform.system() != "Windows"):
112 os.rmdir(self.get_tmp_dir())
113
114 def clean_tmp_dir(self):
115 folder = self.get_tmp_dir()
116 for the_file in os.listdir(folder):
117 file_path = os.path.join(folder, the_file)
118 try:
119 if os.path.isfile(file_path):
120 os.unlink(file_path)
121 elif os.path.isdir(file_path):
122 shutil.rmtree(file_path)
123 except Exception as e:
124 print(e)
125
126 def get_png_path(self):
127 return self.get_tmp_dir() + self.name + ".png"
128
129 def get_eps_path(self):
130 return self.get_tmp_dir() + self.name + ".eps"
131
132 def get_svg_path(self):
133 return self.get_tmp_dir() + self.name + ".svg"
134
135 def get_png(self):
136 return self.get_png_path() +\
137 "?" + "width=" + str(self.width) +\
138 "&" + "height=" + str(self.height)
139
140 def get_eps(self):
141 return self.get_eps_path() +\
142 "?" + "width=" + str(self.width) +\
143 "&" + "height=" + str(self.height)
144
145 def get_svg(self):
146 return self.get_svg_path() +\
147 "?" + "width=" + str(self.width) +\
148 "&" + "height=" + str(self.height)