From e82ca278c4d2c532ca85bf7c3a1760986254ca4c Mon Sep 17 00:00:00 2001 From: Adi Roiban Date: Tue, 18 Feb 2014 11:24:00 +0200 Subject: [PATCH] Update after review. --- .gitignore | 1 - lesscpy/lessc/parser.py | 27 ++++++++++++++++++--------- test/test_parser.py | 4 +++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 6194089..46e49c8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,3 @@ dist MANIFEST .tox .venv* -*.egg-info/ diff --git a/lesscpy/lessc/parser.py b/lesscpy/lessc/parser.py index 84e65c2..32014c3 100644 --- a/lesscpy/lessc/parser.py +++ b/lesscpy/lessc/parser.py @@ -77,23 +77,32 @@ class LessParser(object): self.result = None self.target = None - def parse(self, filename='', filestream=None, debuglevel=0): + def parse(self, filename=None, file=None, debuglevel=0): """ Parse file. kwargs: filename (str): File to parse debuglevel (int): Parser debuglevel """ + self.scope.push() + + if not file: + # We use a path. + file = filename + else: + # We use a stream and try to extract the name from the stream. + if hasattr(file, 'name'): + if filename is not None: + raise AssertionError( + 'names of file and filename are in conflict') + filename = file.name() + else: + filename = '(stream)' + + self.target = filename if self.verbose: 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( - content, lexer=self.lex, debug=debuglevel) + file, lexer=self.lex, debug=debuglevel) self.post_parse() diff --git a/test/test_parser.py b/test/test_parser.py index 6653c3c..7a23605 100644 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -24,10 +24,12 @@ class TestLessParser(unittest.TestCase): @nice-blue: #5B83AD; """) - self.parser.parse(filestream=stream) + self.parser.parse(file=stream) # A single object is parser which is the expected variable. self.assertEqual(1, len(self.parser.result)) + # This is a stream without a name so it sets default name. + self.assertEqual('(stream)', self.parser.target) variable = self.parser.result[0] self.assertEqual('@nice-blue', variable.name) self.assertEqual(['#5b83ad'], variable.value)