Skip to content

Commit

Permalink
Added Python 3 support using six.(for #91)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-hirokawa committed Apr 9, 2013
1 parent 8e69e0e commit 9ddf474
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 68 deletions.
9 changes: 5 additions & 4 deletions pyjade/__init__.py
@@ -1,4 +1,5 @@
from parser import Parser
from compiler import Compiler
from utils import process
from filters import register_filter
from __future__ import absolute_import
from .parser import Parser
from .compiler import Compiler
from .utils import process
from .filters import register_filter
12 changes: 8 additions & 4 deletions pyjade/compiler.py
@@ -1,5 +1,6 @@
import re
import os
import six

class Compiler(object):
RE_INTERPOLATE = re.compile(r'(\\)?([#!]){(.*?)}')
Expand Down Expand Up @@ -80,7 +81,10 @@ def compile(self):
self.buf = [self.compile_top()]
self.lastBufferedIdx = -1
self.visit(self.node)
return unicode(u''.join(self.buf))
compiled = u''.join(self.buf)
if isinstance(compiled, six.binary_type):
compiled = six.text_type(compiled, 'utf8')
return compiled

def setDoctype(self,name):
self.doctype = self.doctypes.get(name or 'default','<!DOCTYPE %s>'%name)
Expand Down Expand Up @@ -290,7 +294,7 @@ def visitDynamicAttributes(self,attrs):
buf = ', '.join(buf)
if self.terse: params['terse'] = 'True'
if buf: params['attrs'] = '[%s]'%buf
param_string = ', '.join(['%s=%s'%(n,v) for n,v in params.iteritems()])
param_string = ', '.join(['%s=%s'%(n,v) for n,v in six.iteritems(params)])
if buf or terse:
self.buf.append(self.attributes(param_string))

Expand All @@ -302,7 +306,7 @@ def visitAttributes(self,attrs):
self.visitDynamicAttributes(temp_attrs)
temp_attrs = []
n,v = attr['name'], attr['val']
if isinstance(v,basestring):
if isinstance(v,six.string_types):
if self.useRuntime or attr['static']:
self.buf.append(' %s=%s'%(n,v))
else:
Expand All @@ -326,4 +330,4 @@ def register_autoclosecode(cls,name):
cls.autocloseCode.append(name)


#1-
#1-
11 changes: 6 additions & 5 deletions pyjade/convert.py
@@ -1,3 +1,4 @@
from __future__ import print_function
import logging
import codecs
from optparse import OptionParser
Expand All @@ -10,7 +11,7 @@ def convert_file():
for i in support_compilers_list:
try:
compiler_class = __import__('pyjade.ext.%s' % i, fromlist=['pyjade']).Compiler
except ImportError, e:
except ImportError as e:
logging.warning(e)
else:
available_compilers[i] = compiler_class
Expand All @@ -23,17 +24,17 @@ def convert_file():
# compiler absolutely necessary (ex. django)
default_compiler = sorted(available_compilers.keys())[0]
parser.add_option("-c", "--compiler", dest="compiler",
choices=available_compilers.keys(),
choices=list(available_compilers.keys()),
default=default_compiler,
type="choice",
help=("COMPILER must be one of %s, default is %s" %
(','.join(available_compilers.keys()), default_compiler)))
(','.join(list(available_compilers.keys())), default_compiler)))
parser.add_option("-e", "--ext", dest="extension",
help="Set import/extends default file extension", metavar="FILE")

options, args = parser.parse_args()
if len(args) < 1:
print "Specify the input file as the first argument."
print("Specify the input file as the first argument.")
exit()
file_output = options.output or (args[1] if len(args) > 1 else None)
compiler = options.compiler
Expand All @@ -52,7 +53,7 @@ def convert_file():
outfile = codecs.open(file_output, 'w', encoding='utf-8')
outfile.write(output)
else:
print output
print(output)
else:
raise Exception('You must have %s installed!' % compiler)

Expand Down
5 changes: 3 additions & 2 deletions pyjade/ext/django/__init__.py
@@ -1,2 +1,3 @@
from compiler import Compiler
from loader import Loader
from __future__ import absolute_import
from .compiler import Compiler
from .loader import Loader
3 changes: 2 additions & 1 deletion pyjade/ext/django/loader.py
@@ -1,11 +1,12 @@
from __future__ import absolute_import
import hashlib

from django.template.base import TemplateDoesNotExist
from django.template.loader import BaseLoader, get_template_from_string, find_template_loader, make_origin
import os

from django.conf import settings
from compiler import Compiler
from .compiler import Compiler
from pyjade import Parser

from pyjade.utils import process
Expand Down
11 changes: 6 additions & 5 deletions pyjade/ext/django/templatetags.py
Expand Up @@ -11,6 +11,7 @@
from django import template
from django.template import FilterExpression
from django.template.loader import get_template
import six

from pyjade.runtime import iteration

Expand Down Expand Up @@ -109,7 +110,7 @@ def do_macro(parser, token):
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
raise template.TemplateSyntaxError(m)
# TODO: could do some validations here,
# for now, "blow your head clean off"
nodelist = parser.parse(('end__pyjade_kwacro', ))
Expand All @@ -136,7 +137,7 @@ def do_loadmacros(parser, token):
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
raise template.TemplateSyntaxError(m)
if filename[0] in ('"', "'") and filename[-1] == filename[0]:
filename = filename[1:-1]
t = get_template(filename)
Expand Down Expand Up @@ -166,7 +167,7 @@ def render(self, context):
except IndexError:
context[arg] = ""

for name, default in self.macro.kwargs.iteritems():
for name, default in six.iteritems(self.macro.kwargs):
if name in self.fe_kwargs:
context[name] = self.fe_kwargs[name].resolve(context)
else:
Expand All @@ -185,12 +186,12 @@ def do_usemacro(parser, token):
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
raise template.TemplateSyntaxError(m)
try:
macro = parser._macros[macro_name]
except (AttributeError, KeyError):
m = "Macro '%s' is not defined" % macro_name
raise template.TemplateSyntaxError, m
raise template.TemplateSyntaxError(m)

fe_kwargs = {}
fe_args = []
Expand Down
12 changes: 6 additions & 6 deletions pyjade/ext/html.py
Expand Up @@ -4,13 +4,13 @@

import pyjade
from pyjade.runtime import is_mapping, iteration

import six

def process_param(key, value, terse=False):
if terse:
if (key == value) or (value is True):
return key
if isinstance(value, basestring):
if isinstance(value, six.binary_type):
value = value.decode('utf8')
return '''%s="%s"''' % (key, value)

Expand Down Expand Up @@ -38,7 +38,7 @@ class HTMLCompiler(pyjade.compiler.Compiler):
mixins = dict()
useRuntime = True
def _do_eval(self, value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
value = value.encode('utf-8')
try:
value = eval(value, self.global_context, self.local_context)
Expand All @@ -50,7 +50,7 @@ def _get_value(self, attr):
value = attr['val']
if attr['static']:
return attr['val']
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return self._do_eval(value)
else:
return attr['name']
Expand Down Expand Up @@ -106,7 +106,7 @@ def visitCode(self, code):
if code.block:
self.visit(code.block)
if not code.buffer and not code.block:
exec code.val.lstrip() in self.global_context, self.local_context
six.exec_(code.val.lstrip(), self.global_context, self.local_context)

def visitEach(self, each):
obj = iteration(self._do_eval(each.obj), len(each.keys))
Expand Down Expand Up @@ -139,7 +139,7 @@ def visitDynamicAttributes(self, attrs):
if value not in (None,False):
params.append((attr['name'], value))
if classes:
classes = [unicode(c) for c in classes]
classes = [six.text_type(c) for c in classes]
params.append(('class', " ".join(classes)))
if params:
self.buf.append(" "+" ".join([process_param(k, v, self.terse) for (k,v) in params]))
Expand Down
10 changes: 5 additions & 5 deletions pyjade/ext/underscore.py
Expand Up @@ -3,13 +3,13 @@
from pyjade import Parser, Compiler as _Compiler
from pyjade.runtime import attrs
from pyjade.utils import process

import six

def process_param(key, value, terse=False):
if terse:
if (key == value) or (value is True):
return key
if isinstance(value, basestring):
if isinstance(value, six.binary_type):
value = value.decode('utf8')
return '''%s="%s"''' % (key, value)

Expand Down Expand Up @@ -57,7 +57,7 @@ def visitEach(self,each):
self.buf.append('<% } %>')

def _do_eval(self, value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
value = value.encode('utf-8')
try:
value = eval(value, {}, {})
Expand All @@ -69,7 +69,7 @@ def _get_value(self, attr):
value = attr['val']
if attr['static']:
return attr['val']
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return self._do_eval(value)
else:
return attr['name']
Expand All @@ -89,7 +89,7 @@ def visitAttributes(self,attrs):
if (value is not None) and (value is not False):
params.append((attr['name'], value))
if classes:
classes = [unicode(c) for c in classes]
classes = [six.text_type(c) for c in classes]
params.append(('class', " ".join(classes)))
if params:
self.buf.append(" "+" ".join([process_param(k, v, self.terse) for (k,v) in params]))
Expand Down
5 changes: 3 additions & 2 deletions pyjade/filters.py
@@ -1,4 +1,5 @@
from compiler import Compiler
from __future__ import absolute_import
from .compiler import Compiler

def register_filter(name=None):
def decorator(f):
Expand Down Expand Up @@ -26,4 +27,4 @@ def markdown_filter(x,y):
return markdown.markdown(x, output_format='html5')

except ImportError:
pass
pass
14 changes: 9 additions & 5 deletions pyjade/lexer.py
@@ -1,5 +1,7 @@
from __future__ import absolute_import
import re
from collections import deque
import six

class Token:
def __init__(self, **kwds):
Expand Down Expand Up @@ -42,9 +44,11 @@ class Lexer(object):
RE_INDENT_SPACES = re.compile(r'^\n( *)')
RE_COLON = re.compile(r'^: *')
# RE_ = re.compile(r'')
def __init__(self,str,**options):
def __init__(self,string,**options):
if isinstance(string, six.binary_type):
string = six.text_type(string, 'utf8')
self.options = options
self.input = self.RE_INPUT.sub('\n',str)
self.input = self.RE_INPUT.sub('\n',string)
self.colons = self.options.get('colons',False)
self.deferredTokens = deque()
self.lastIndents = 0
Expand Down Expand Up @@ -238,7 +242,7 @@ def each(self):
if captures:
self.consume(len(captures[0]))
tok = self.tok('each',None)
tok.keys = map(lambda x:x.strip(),captures[1].split(','))
tok.keys = [x.strip() for x in captures[1].split(',')]
tok.code = captures[2]
return tok

Expand Down Expand Up @@ -283,10 +287,10 @@ def interpolate(attr):
return attr, (num>0)

self.consume(index+1)
from utils import odict
from .utils import odict
tok.attrs = odict()
tok.static_attrs = set()
str_nums = map(str,range(10))
str_nums = list(map(str, range(10)))
def parse(c):
real = c
if colons and ':'==c: c = '='
Expand Down
9 changes: 5 additions & 4 deletions pyjade/nodes.py
@@ -1,4 +1,5 @@
from collections import deque
import six

class Node(object):
debug = False
Expand Down Expand Up @@ -115,7 +116,7 @@ def __init__(self,name, block=None, inline=False):

@classmethod
def static(self, string, only_remove=False):
if not isinstance(string,basestring) or not string: return string
if not isinstance(string,six.string_types) or not string: return string
if string[0] in ('"',"'"):
if string[0]==string[-1]: string = string[1:-1]
else: return string
Expand All @@ -142,7 +143,7 @@ def attrs(self):
for attr in self._attrs:
name = attr['name']
val = attr['val']
static = attr['static'] # and isinstance(val,basestring)
static = attr['static'] # and isinstance(val,six.string_types)
if static:
val = self.static(val)
if val in ("True","False","None"):
Expand All @@ -164,7 +165,7 @@ def attrs(self):
class Text(Node):
def __init__(self, line=None):
self.nodes = []
if isinstance(line,basestring): self.append(line)
if isinstance(line,six.string_types): self.append(line)

def append(self,node):
return self.nodes.append(node)
return self.nodes.append(node)
8 changes: 5 additions & 3 deletions pyjade/parser.py
@@ -1,5 +1,7 @@
from lexer import Lexer
import nodes
from __future__ import absolute_import
from .lexer import Lexer
from . import nodes
import six

textOnly = ('script','style')

Expand Down Expand Up @@ -272,7 +274,7 @@ def parseTag(self):
# continue
elif 'attrs'==t:
tok = self.advance()
for n,v in tok.attrs.iteritems():
for n,v in six.iteritems(tok.attrs):
tag.setAttribute(n,v,n in tok.static_attrs)
continue
else:
Expand Down

0 comments on commit 9ddf474

Please sign in to comment.