Skip to content

Commit

Permalink
Now Python 3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
smartt committed Aug 23, 2011
1 parent 7ba9f36 commit ea21d8f
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Why bother?


Why Python? Why Python?
----------- -----------
Besides being fun to use, Python is available on all of the development and deployment environments that I've used in the past who-knows-how-many years. This is important, since it means not installing something new on servers (which might not be mine.) While it might be fun to write this tool in Lisp, Haskell, or even JavaScript on v8, those aren't practical for a wider audience. Perl, Python, and to some degree, Java, are a pretty safe bet. Besides being fun to use, Python is available on all of the development and deployment environments that I've used in the past who-knows-how-many years. This is important, since it means not installing something new on servers (which might not be mine.) While it might be fun to write this tool in another language, the one's I'm interested in aren't practical for a wider audience. Perl, Python, and to some degree, Java, are a pretty safe bet for general use.


Note though, that the tool intentionally isn't called "pyjsmacro". I'm perfectly happy having implementations in other languages, provided they all pass the same test cases. Note though, that the tool intentionally isn't called "pyjsmacro". I'm perfectly happy having implementations in other languages, provided they all pass the same test cases.


Expand All @@ -93,6 +93,10 @@ Future Ideas


Changes Changes
------- -------
v0.2.14

- jsmacro.py now runs on Python 3. Testing environments include: Python 2.6, Python 2.7, PyPy 1.6 (which is Python 2.7.1), and Python 3.2.

v0.2.10 v0.2.10


- Fixed a bug where a DEFINE on the command-line wasn't overriding a DEFINE in the input file. - Fixed a bug where a DEFINE on the command-line wasn't overriding a DEFINE in the input file.
Expand Down
30 changes: 13 additions & 17 deletions jsmacro.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@


from datetime import datetime from datetime import datetime
import getopt import getopt
import hashlib
import os import os
import re import re
import sys import sys


__author__ = "Erik Smartt" __author__ = "Erik Smartt"
__copyright__ = "Copyright 2010, Erik Smartt" __copyright__ = "Copyright 2010, Erik Smartt"
__license__ = "MIT" __license__ = "MIT"
__version__ = "0.2.12" __version__ = "0.2.14"


__usage__ = """Normal usage: __usage__ = """Normal usage:
jsmacro.py -f [INPUT_FILE_NAME] > [OUTPUT_FILE] jsmacro.py -f [INPUT_FILE_NAME] > [OUTPUT_FILE]
Expand Down Expand Up @@ -61,7 +60,7 @@ def reset(self):
self.env = {} self.env = {}


def handle_define(self, key, value='1'): def handle_define(self, key, value='1'):
if self.env.has_key(key): if key in self.env:
return return


self.env[key] = eval(value) self.env[key] = eval(value)
Expand Down Expand Up @@ -101,7 +100,7 @@ def handle_ifdef(self, arg, text):
""" """
parts = re.split(self.re_else_pattern, text) parts = re.split(self.re_else_pattern, text)


if self.env.has_key(arg): if arg in self.env:
return "\n{s}".format(s=parts[0]) return "\n{s}".format(s=parts[0])


else: else:
Expand All @@ -120,7 +119,7 @@ def handle_ifndef(self, arg, text):
""" """
parts = re.split(self.re_else_pattern, text) parts = re.split(self.re_else_pattern, text)


if self.env.has_key(arg): if arg in self.env:
try: try:
return "{s}".format(s=parts[1]) return "{s}".format(s=parts[1])


Expand Down Expand Up @@ -190,7 +189,7 @@ def scan_and_parse_dir(srcdir, destdir, parser):
in_file_path = "{p}/{f}".format(p=in_path, f=filename) in_file_path = "{p}/{f}".format(p=in_path, f=filename)
out_file_path = "{p}/{f}".format(p=out_path, f=filename) out_file_path = "{p}/{f}".format(p=out_path, f=filename)


print("{i} -> {o}".format(i=in_file_path, o=out_file_path)) print(("{i} -> {o}".format(i=in_file_path, o=out_file_path)))


if not(os.path.exists(out_path)): if not(os.path.exists(out_path)):
os.mkdir(out_path) os.mkdir(out_path)
Expand All @@ -217,13 +216,10 @@ def scan_for_test_files(dirname, parser):
out_target_output = out_file.read() out_target_output = out_file.read()
out_file.close() out_file.close()


# Hopefully this doesn't come back to bite me, but I'm using a hash of the if out_target_output == in_parsed:
# output to compare it with the known TEST PASS state. The odds of a false print(("PASS [{s}]".format(s=in_file_path)))
# positive are pretty slim...
if hashlib.sha224(out_target_output).hexdigest() == hashlib.sha224(in_parsed).hexdigest():
print("PASS [{s}]".format(s=in_file_path))
else: else:
print("FAIL [{s}]".format(s=in_file_path)) print(("FAIL [{s}]".format(s=in_file_path)))


if parser.save_expected_failures: if parser.save_expected_failures:
# Write the expected output file for local diffing # Write the expected output file for local diffing
Expand All @@ -232,8 +228,8 @@ def scan_for_test_files(dirname, parser):
fout.close() fout.close()


else: else:
print("\n-- EXPECTED --\n{s}".format(s=out_target_output)) print(("\n-- EXPECTED --\n{s}".format(s=out_target_output)))
print("\n-- GOT --\n{s}".format(s=in_parsed)) print(("\n-- GOT --\n{s}".format(s=in_parsed)))


parser.reset() parser.reset()


Expand All @@ -248,8 +244,8 @@ def scan_for_test_files(dirname, parser):
opts, args = getopt.getopt(sys.argv[1:], opts, args = getopt.getopt(sys.argv[1:],
"hf:s:d:", "hf:s:d:",
["help", "file=", "srcdir=","dstdir=", "test", "def=", "savefail", "version"]) ["help", "file=", "srcdir=","dstdir=", "test", "def=", "savefail", "version"])
except getopt.GetoptError, err: except getopt.GetoptError as err:
print(str(err)) print((str(err)))
print(__usage__) print(__usage__)


sys.exit(2) sys.exit(2)
Expand Down Expand Up @@ -298,7 +294,7 @@ def scan_for_test_files(dirname, parser):
break break


if o in ["-f", "--file"]: if o in ["-f", "--file"]:
print(p.parse(a)) print((p.parse(a)))


break break


Expand Down
Loading

0 comments on commit ea21d8f

Please sign in to comment.