Skip to content

Commit

Permalink
futzing with a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Tagliamonte committed Mar 21, 2013
1 parent c8eec0c commit 980cd49
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 14 deletions.
6 changes: 6 additions & 0 deletions bin/hyc
@@ -0,0 +1,6 @@
#!/usr/bin/env hy

(import sys)
(import-from hy.importer write-hy-as-pyc)

(write-hy-as-pyc (get sys.argv 1))
7 changes: 6 additions & 1 deletion hy/__main__.py
@@ -1,7 +1,12 @@
import hy
import hy # NOQA
import imp
import sys

# This just mocks the normalish behavior of the Python interp. Helpful to aid
# with shiming existing apps that don't really "work" with Hy.
#
# You could say this script helps Hyjack a file.
#

if len(sys.argv) > 1:
sys.argv.pop(0)
Expand Down
5 changes: 3 additions & 2 deletions hy/compiler.py
Expand Up @@ -227,12 +227,13 @@ def compile_import_expression(self, expr):
@builds("import_as")
def compile_import_as_expression(self, expr):
expr.pop(0) # index
modlist = [expr[i:i+2] for i in range(0, len(expr), 2)]
modlist = [expr[i:i + 2] for i in range(0, len(expr), 2)]
return ast.Import(
lineno=expr.start_line,
col_offset=expr.start_column,
module=str(expr.pop(0)),
names=[ast.alias(name=str(x[0]), asname=str(x[1])) for x in modlist])
names=[ast.alias(name=str(x[0]),
asname=str(x[1])) for x in modlist])

@builds("import_from")
def compile_import_from_expression(self, expr):
Expand Down
24 changes: 23 additions & 1 deletion hy/importer.py
Expand Up @@ -21,8 +21,10 @@
from hy.compiler import hy_compile
from hy.lex import tokenize
from hy.core import process
from py_compile import wr_long, MAGIC


import marshal
import imp
import sys
import os
Expand All @@ -31,7 +33,7 @@
if sys.version_info[0] >= 3:
from io import StringIO
else:
from StringIO import StringIO
from StringIO import StringIO # NOQA


def import_buffer_to_hst(fd):
Expand Down Expand Up @@ -64,6 +66,26 @@ def import_file_to_module(name, fpath):
return mod


def write_hy_as_pyc(fname):
with open(fname, 'U') as f:
try:
timestamp = long(os.fstat(f.fileno()).st_mtime)
except AttributeError:
timestamp = long(os.stat(fname).st_mtime)

_ast = import_file_to_ast(fname)
code = compile(_ast, fname, "exec")
cfile = "%s.pyc" % fname[:-len(".hy")]

with open(cfile, 'wb') as fc:
fc.write('\0\0\0\0')
wr_long(fc, timestamp)
marshal.dump(code, fc)
fc.flush()
fc.seek(0, 0)
fc.write(MAGIC)


class HyFinder(object):
def is_package(self, fullname):
dirpath = "/".join(fullname.split("."))
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -34,6 +34,7 @@
version=__version__,
scripts=[
"bin/hy",
"bin/hyc",
],
packages=[
'hy',
Expand Down
9 changes: 5 additions & 4 deletions site/coffee/main.coffee
Expand Up @@ -37,20 +37,21 @@ HyCodeMirror.setSize("100%", "100%")

reload = ->
input = HyCodeMirror.getValue()
format = "h:mm:ss"
$.ajax({
url: "/hy2py",
type: "POST",
data: {'code': input},
success: (result) ->
PyCodeMirror.setValue(result)
now = new Date().getTime()
$("#build-msgs").text(now + " Updated.")
now = Date.parse("now").toString(format)
$("#build-msgs").prepend(now + " updated.<br />")
$("#repl-root").removeClass("error")
$("#repl-root").addClass("ok")
statusCode: {
500: (response) ->
now = new Date().getTime()
$("#build-msgs").text(now + " " + response.responseText)
now = Date.parse("now").toString(format)
$("#build-msgs").prepend(now + " " + response.responseText + "<br />")
$("#repl-root").removeClass("ok")
$("#repl-root").addClass("error")
}
Expand Down
104 changes: 104 additions & 0 deletions site/js/date.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion site/less/layout.less
Expand Up @@ -78,7 +78,8 @@ h1 {

#build-msgs {
border-top: 1px solid rgba(82, 168, 236, 0.2);
height: 10%;
height: 8%;
padding: 5px;
color: #777777;
overflow: hidden;
}
1 change: 1 addition & 0 deletions site/templates/base.html
Expand Up @@ -5,6 +5,7 @@
<link rel="stylesheet" href="{{ url_for("static", filename="css/pygments.css") }}" ></link>
<link rel="stylesheet" href="{{ url_for("static", filename="css/hy.css") }}" ></link>
<script src="{{ url_for("static", filename="js/jquery-1.9.1.min.js") }}" type="text/javascript" ></script>
<script src="{{ url_for("static", filename="js/date.js") }}" type="text/javascript" ></script>
<script src="{{ url_for("static", filename="js/main.js") }}" type="text/javascript" ></script>
{% block head %}{% endblock %}
</head>
Expand Down
10 changes: 5 additions & 5 deletions site/templates/repl.html
Expand Up @@ -16,7 +16,7 @@

{% block content %}
<div id='repl-root' class='repl-root' >
<div class='repl' id='hython-repl'>
<div class='repl' id='hython-repl'>
<textarea id="hython-target">
;;;; This is Hy. Hy is a Lisp variant that
;;;; "compiles" to Python ASTs.
Expand All @@ -43,9 +43,9 @@

(print (add-two-numbers 1 2))
</textarea>
</div>
<div class='repl' id='python-repl'></div>
<div class='clear'></div>
<div class='msgs' id='build-msgs'></div>
</div>
<div class='repl' id='python-repl'></div>
<div class='clear'></div>
<div class='msgs' id='build-msgs'></div>
</div>
{% endblock %}

0 comments on commit 980cd49

Please sign in to comment.