Initial code for parsing from stream.
This commit is contained in:
parent
923f1b089a
commit
032cfa1a8c
1
.gitignore
vendored
1
.gitignore
vendored
@ -9,3 +9,4 @@ dist
|
||||
MANIFEST
|
||||
.tox
|
||||
.venv*
|
||||
*.egg-info/
|
||||
|
@ -460,12 +460,16 @@ class LessLexer:
|
||||
self.lexer.input(f.read())
|
||||
return self
|
||||
|
||||
def input(self, filename):
|
||||
def input(self, file):
|
||||
"""
|
||||
Wrapper for file
|
||||
Load lexer with content from `file` which can be a path or a file
|
||||
like object.
|
||||
"""
|
||||
with open(filename) as f:
|
||||
self.lexer.input(f.read())
|
||||
if isinstance(file, basestring):
|
||||
with open(file) as f:
|
||||
self.lexer.input(f.read())
|
||||
else:
|
||||
self.lexer.input(file.read())
|
||||
|
||||
def token(self):
|
||||
"""
|
||||
|
@ -77,7 +77,7 @@ class LessParser(object):
|
||||
self.result = None
|
||||
self.target = None
|
||||
|
||||
def parse(self, filename='', debuglevel=0):
|
||||
def parse(self, filename='', filestream=None, debuglevel=0):
|
||||
""" Parse file.
|
||||
kwargs:
|
||||
filename (str): File to parse
|
||||
@ -87,8 +87,14 @@ class LessParser(object):
|
||||
print('Compiling target: %s' % filename, file=sys.stderr)
|
||||
self.scope.push()
|
||||
self.target = filename
|
||||
if filestream:
|
||||
content = filestream
|
||||
else:
|
||||
content = filename
|
||||
|
||||
self.result = self.parser.parse(
|
||||
filename, lexer=self.lex, debug=debuglevel)
|
||||
content, lexer=self.lex, debug=debuglevel)
|
||||
|
||||
self.post_parse()
|
||||
|
||||
def post_parse(self):
|
||||
|
47
test/test_lexer.py
Normal file
47
test/test_lexer.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""
|
||||
Unit tests for the lexer.
|
||||
"""
|
||||
from StringIO import StringIO
|
||||
from tempfile import NamedTemporaryFile
|
||||
import unittest
|
||||
|
||||
|
||||
from lesscpy.lessc.lexer import LessLexer
|
||||
|
||||
|
||||
class TestLessLexer(unittest.TestCase):
|
||||
"""
|
||||
Unit tests for LessLexer
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.lexer = LessLexer()
|
||||
|
||||
|
||||
def test_input_stream(self):
|
||||
"""
|
||||
It can load content from a string.
|
||||
"""
|
||||
file = StringIO("""
|
||||
@simple-var: 1;
|
||||
""")
|
||||
|
||||
self.lexer.input(file)
|
||||
|
||||
token = self.lexer.token()
|
||||
self.assertEqual('@simple-var', token.value)
|
||||
|
||||
def test_input_path(self):
|
||||
"""
|
||||
It can load content from a path.
|
||||
"""
|
||||
file = NamedTemporaryFile()
|
||||
file.write("""
|
||||
@simple-var: 1;
|
||||
""")
|
||||
file.seek(0)
|
||||
|
||||
self.lexer.input(file.name)
|
||||
|
||||
token = self.lexer.token()
|
||||
self.assertEqual('@simple-var', token.value)
|
31
test/test_parser.py
Normal file
31
test/test_parser.py
Normal file
@ -0,0 +1,31 @@
|
||||
"""
|
||||
Unit test for the parser.
|
||||
"""
|
||||
from StringIO import StringIO
|
||||
import unittest
|
||||
|
||||
from lesscpy.lessc.parser import LessParser
|
||||
|
||||
class TestLessParser(unittest.TestCase):
|
||||
"""
|
||||
Unit tests for LessParser.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.parser = LessParser()
|
||||
|
||||
def test_parse_stream(self):
|
||||
"""
|
||||
It can parse input from a file stream.
|
||||
"""
|
||||
stream = StringIO("""
|
||||
@nice-blue: #5B83AD;
|
||||
""")
|
||||
|
||||
self.parser.parse(filestream=stream)
|
||||
|
||||
# A single object is parser which is the expected variable.
|
||||
self.assertEqual(1, len(self.parser.result))
|
||||
variable = self.parser.result[0]
|
||||
self.assertEqual('@nice-blue', variable.name)
|
||||
self.assertEqual(['#5b83ad'], variable.value)
|
Loading…
Reference in New Issue
Block a user