1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
import sys
import re
import os.path
import time
from traceback import format_exc as error_stack
from types import CodeType, FunctionType
try:
from types import TypeType
except ImportError:
TypeType = type
from renardo_lib.Utils import modi
from renardo_lib.Settings import *
"""
Live Object
===========
Base for any self-scheduling objects
"""
# Player RegEx
re_player = re.compile(r"(\s*?)(\w+)\s*?>>\s*?\w+")
class LiveObject(object):
foxdot_object = True
isAlive = True
metro = None
step = None
n = 0
def kill(self):
self.isAlive = False
return self
def __call__(self):
self.metro.schedule(self, self.metro.now() + float(modi(self.step, self.n)))
self.n += 1
return self
"""
FoxCode
=======
Handles the execution of FoxDot code
"""
class CodeString:
def __init__(self, raw):
self.raw = raw
self.iter = -1
self.lines = [s + "\n" for s in self.raw.split("\n")] + ['']
def readline(self):
self.iter += 1
return self.lines[self.iter]
def __str__(self):
return self.raw
if sys.version_info[0] > 2:
def clean(string):
string = string.replace("\u03BB", "lambda")
return string
else:
def clean(string):
""" Removes non-ascii characters from a string """
string = string.replace(u"\u03BB", "lambda")
return string.encode("ascii", "replace")
class _StartupFile:
def __init__(self, path):
self.set_path(path)
def set_path(self, path):
self.path = path if path is None else os.path.realpath(path)
def load(self):
if self.path is not None:
try:
file = open(self.path)
code = file.read()
file.close()
return code
except (IOError, OSError):
WarningMsg("'{}' startup file not found.".format(self.path))
return ""
FOXDOT_STARTUP = _StartupFile(FOXDOT_STARTUP_PATH)
class FoxDotCode:
namespace={}
player_line_numbers={}
@staticmethod
def _compile(string):
''' Returns the bytecode for '''
return compile(str(CodeString(string)), "FoxDot", "exec")
@classmethod
def use_sample_directory(cls, directory):
''' Forces FoxDot to look in `directory` instead of the default
directory when using audio samples. '''
return cls.namespace['symbolToDir'].set_root( directory )
@classmethod
def use_startup_file(cls, path):
return cls.namespace['FOXDOT_STARTUP'].set_path(path)
@classmethod
def no_startup(cls):
return cls.namespace["FOXDOT_STARTUP"].set_path(None)
def load_startup_file(self):
""" Must be initialised first """
code = self.namespace["FOXDOT_STARTUP"].load()
return self.__call__(code, verbose=False)
def __call__(self, code, verbose=True, verbose_error=None):
""" Takes a string of FoxDot code and executes as Python """
if self.namespace['_Clock'].waiting_for_sync:
time.sleep(0.25)
return self.__call__(code, verbose, verbose_error)
if verbose_error is None:
verbose_error = verbose
if not code:
return
try:
if type(code) != CodeType:
code = clean(code)
response = stdout(code)
if verbose is True:
print(response)
exec(self._compile(code), self.namespace)
except Exception as e:
response = error_stack()
if verbose_error is True:
print(response)
return response
def update_line_numbers(self, text_widget, start="1.0", end="end", remove=0):
lines = text_widget.get(start, end).split("\n")[remove:]
update = []
offset = int(start.split(".")[0])
for i, line in enumerate(lines):
# Check line for a player and assign it a line number
match = re_player.match(line)
line_changed = False
if match is not None:
whitespace = len(match.group(1))
player = match.group(2)
line = i + offset
if player in self.player_line_numbers:
if (line, whitespace) != self.player_line_numbers[player]:
line_changed = True
if line_changed or player not in self.player_line_numbers:
self.player_line_numbers[player] = (line, whitespace)
update.append("{}.id = '{}'".format(player, player))
update.append("{}.line_number = {}".format(player, line))
update.append("{}.whitespace = {}".format(player, whitespace))
# Execute updates if necessary
if len(update) > 0:
self.__call__("\n".join(update), verbose = False)
return
execute = FoxDotCode() # this is not well named
def get_now(obj):
""" Returns the value of objects if they are time-varying """
return getattr(obj, 'now', lambda: obj).__call__()
def get_input():
""" Similar to `input` but can handle multi-line input. Terminates on a final "\n" """
line = " "; text = []
while len(line) > 0:
line = input("")
text.append(line)
return "\n".join(text)
def handle_stdin():
""" When FoxDot is run with the --pipe added, this function
is called and continuosly """
load_startup_file()
while True:
try:
text = get_input()
execute(text, verbose=False, verbose_error=True)
except(EOFError, KeyboardInterrupt):
sys.exit("Quitting")
def stdout(code):
""" Shell-based output """
console_text = code.strip().split("\n")
return ">>> {}".format("\n... ".join(console_text))
def debug_stdout(*args):
""" Forces prints to server-side """
sys.__stdout__.write(" ".join([str(s) for s in args]) + "\n")
def load_startup_file():
return execute.load_startup_file()
def WarningMsg(*text):
print("Warning: {}".format( " ".join(str(s) for s in text) ))
def write_to_file(fn, text):
try:
with open(fn, "w") as f:
f.write(clean(text))
except IOError:
print("Unable to write to {}".format(fn))
return
# These functions return information about an imported module
# Should use insepct module
def classes(module):
""" Returns a list of class names defined in module """
return [name for name, data in vars(module).items() if type(data) == TypeType]
def instances(module, cls):
""" Returns a list of instances of cls from module """
return [name for name, data in vars(module).items() if isinstance(data, cls)]
def functions(module):
""" Returns a list of function names defined in module """
return [name for name, data in vars(module).items() if type(data) == FunctionType]
|