Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging #1

Open
wants to merge 1 commit into
base: logx-squid
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cl4py/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

'''
import reprlib
import logging

logging.basicConfig(level=logging.DEBUG)

class LispObject:
pass
Expand Down Expand Up @@ -96,10 +99,13 @@ def __eq__(self, other):
def python_name(self):
name = self.name
if name in python_name_translations:
logging.info("Translated lisp {} to python {}".format(name, python_name_translations[name]))
return python_name_translations[name]
else:
old_name = name
for (old, new) in python_name_substitutions.items():
name = name.replace(old, new)
logging.info("Translated lisp {} to python {}".format(old_name, name))
return name.lower()


Expand All @@ -118,6 +124,7 @@ def __getitem__(self, name):

class Cons (LispObject):
def __init__(self, car, cdr):
logging.info("Created new cons {} {}".format(car, cdr))
self.car = car
self.cdr = cdr

Expand Down Expand Up @@ -241,6 +248,7 @@ def __del__(self):

def __call__(self, *args, **kwargs):
restAndKeys = [ Quote(arg) for arg in args ]
logging.info("restAndKeys: {}".format(restAndKeys))
for key, value in kwargs.items():
restAndKeys.append(Keyword(key.upper()))
restAndKeys.append(Quote(value))
Expand Down
39 changes: 33 additions & 6 deletions cl4py/lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
from .data import *
from .reader import Readtable
from .writer import lispify
import logging
import sys


logging.basicConfig(level=logging.DEBUG)
class Lisp:
def __init__(self, cmd=['sbcl', '--script'], quicklisp=False):
def __init__(self, cmd=['sbcl', '--script'], quicklisp=False, debug=False):
p = subprocess.Popen(cmd + [resource_filename(__name__, 'py.lisp')],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
Expand All @@ -31,17 +33,24 @@ def __init__(self, cmd=['sbcl', '--script'], quicklisp=False):
# This allows us to patch these instances later.
self.unpatched_instances = {}
# If debug is true, cl4py will print plenty of debug information.
self.debug = False
self.debug = debug
# Finally, check whether the user wants quicklisp to be available.
logging.info("Created new Lisp instance:\ncmd: {}\nquicklisp: {}\ndebug: {}".format(cmd, quicklisp, debug))
if quicklisp:
logging.info("Installing QuickLisp...")
install_and_load_quicklisp(self)
logging.info("QuickLisp installed.")


def __del__(self):
try:
logging.info("Deleting instance...")
self.stdin.write('(cl-user:quit)\n')
logging.info("Instance deleted")
except:
pass
logging.debug("Instance deletion failed.")
logging.debug(sys.exc_info()[0])
# pass


def eval(self, expr):
Expand All @@ -60,6 +69,7 @@ def eval(self, expr):
if isinstance(err, Cons):
condition = err.car
msg = err.cdr.car if err.cdr else ""
logging.debug("Error being raised in lisp.py/eval.\nerr = {}\ncondition = {}\nmsg = {}".format(err, condition, msg))
def init(self):
RuntimeError.__init__(self, msg)
raise type(str(condition), (RuntimeError,),
Expand All @@ -72,38 +82,54 @@ def init(self):
cls = type(cls_name.python_name, (LispWrapper,), {})
self.classes[cls_name] = cls
alist = self.function('cl4py:class-information')(cls_name)
logging.info("Adding member functions for {}".format(cls_name))
for cons in alist:
add_member_function(cls, cons.car, cons.cdr)
for instance in instances:
logging.info("Patching {} with class {}".format(instance, cls_name))
instance.__class__ = cls
# Finally, return the resulting values.
if val == ():
logging.info("{} returns None".format(expr))
return None
elif val.cdr == ():
logging.info("{} returns {}".format(expr, val.car))
return val.car
else:
return tuple(val)
ret = tuple(val)
logging.info("{} returns {}".format(expr, ret))
return ret


def find_package(self, name):
logging.info("Finding package {}".format(name))
return self.function('CL:FIND-PACKAGE')(name)
# logging.info("Package {} Found: {}".format(name, p))
# return p


def function(self, name):
return self.eval( ('CL:FUNCTION', name) )
logging.info("Evaluating {}".format(name))
e = self.eval( ('CL:FUNCTION', name) )
logging.info("Evaluated {} Found {}".format(name, e))
return e


def add_member_function(cls, name, gf):
class_name = cls.__name__
method_name = name.python_name
logging.info("Adding member function. Method {}, Class {}".format(method_name, class_name))
setattr(cls, method_name, lambda self, *args: gf(self, *args))


def install_and_load_quicklisp(lisp):
logging.info("Installing QuickLisp")
quicklisp_setup = os.path.expanduser('~/quicklisp/setup.lisp')
if os.path.isfile(quicklisp_setup):
lisp.function('cl:load')(quicklisp_setup)
logging.info("QuickLisp found already and loaded.")
else:
logging.info("Installing from outside.")
install_quicklisp(lisp)


Expand All @@ -116,3 +142,4 @@ def install_quicklisp(lisp):
lisp.function('cl:load')(tmp.name)
print('Installing Quicklisp...')
lisp.eval( ('quicklisp-quickstart:install',) )
logging.info("QuickLisp installed from outside.")
15 changes: 14 additions & 1 deletion cl4py/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from .data import *
from .circularity import *

logging.basicConfig(level=logging.DEBUG)

# An implementation of the Common Lisp reader algorithm, with the following
# simplifications and changes:
#
Expand Down Expand Up @@ -160,17 +162,21 @@ def read_aux(self, stream):


def parse(self, token):
logging.info("Parsing: {}".format(token))
# integer
m = re.fullmatch(integer_regex, token)
if m:
logging.info("Parsed {} as integer".format(token))
return int(m.group(0))
# ratio
m = re.fullmatch(ratio_regex, token)
if m:
logging.info("Parsed {} as ratio".format(token))
return Fraction(int(m.group(1)), int(m.group(2)))
# float
m = re.fullmatch(float_regex, token)
if m:
logging.info("Parsed {} as float".format(token))
base = m.group(1)
exponent_marker = m.group(2)
exponent = m.group(3)
Expand All @@ -187,6 +193,7 @@ def parse(self, token):
# symbol
m = re.fullmatch(symbol_regex, token)
if m:
logging.info("Parsed {} as symbol".format(token))
package = m.group(1)
delimiter = m.group(2)
name = m.group(3)
Expand Down Expand Up @@ -229,14 +236,18 @@ def skip_whitespace():


def left_parenthesis(r, s, c):
return r.read_delimited_list(')', s, True)
logging.info("Left parenthesis found, reading delimited list")
l = r.read_delimited_list(')', s, True)
logging.info("List read: {}".format(r))
return l


def right_parenthesis(r, s, c):
raise RuntimeError('Unmatched closing parenthesis.')


def left_curly_bracket(r, s, c):
logging.info("Left curly bracket found, reading delimited list")
table = {}
data = r.read_delimited_list('}', s, True)
while data:
Expand All @@ -247,6 +258,7 @@ def left_curly_bracket(r, s, c):
value = car(rest)
table[key] = value
data = cdr(rest)
logging.info("Table read: {}".format(table))
return table


Expand Down Expand Up @@ -314,6 +326,7 @@ def sharpsign_backslash(r, s, c, n):
else:
key = ''.join(token).upper()
if key in character_names:
logging.info("Reading special character {}".format(key))
return character_names[key]
else:
raise RuntimeError('Not a valid character name: {}'.format('key'))
Expand Down
4 changes: 4 additions & 0 deletions cl4py/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
from fractions import Fraction
from .data import *
from .circularity import *
import logging

logging.basicConfig(level=logging.DEBUG)

def lispify(lisp, obj):
return lispify_datum(decircularize(obj, lisp.readtable))


def lispify_datum(obj):
lispifier = lispifiers.get(type(obj))
logging.info("obj {} is type {}".format(obj, lispifier))
if lispifier:
return lispifier(obj)
elif isinstance(obj, LispWrapper):
Expand Down