From eb0e8a438ae4c2cf98051ec820d299bd95d151af Mon Sep 17 00:00:00 2001 From: "OCHIAI, Gouji" Date: Sat, 1 Mar 2014 16:15:05 +0900 Subject: [PATCH 1/2] Add command-line option for strip base names. --- codekitlang/command.py | 8 +++++--- codekitlang/compiler.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/codekitlang/command.py b/codekitlang/command.py index f596152..5aa3a02 100644 --- a/codekitlang/command.py +++ b/codekitlang/command.py @@ -8,14 +8,16 @@ def main(): parser = argparse.ArgumentParser(description='CodeKit Language Compiler.') parser.add_argument('src', nargs=1, metavar='SOURCE') parser.add_argument('dest', nargs=1, metavar='DEST') + parser.add_argument('--strip-basenames', action='store_true', + default=False, help="apply CodeKit's buggy behavior.") parser.add_argument('--framework-paths', '-f', action='append', metavar='DIR') namespace = parser.parse_args() options = vars(namespace) - src = options.pop('src')[0] - dest = options.pop('dest')[0] + src = options.pop('src') + dest = options.pop('dest') compiler_ = compiler.Compiler(**options) - compiler_.generate_to_file(dest, src) + compiler_.generate_to_file(dest[0], src[0]) if __name__ == '__main__': # pragma:nocover main() diff --git a/codekitlang/compiler.py b/codekitlang/compiler.py index 15b27de..233c3e9 100644 --- a/codekitlang/compiler.py +++ b/codekitlang/compiler.py @@ -47,10 +47,11 @@ class Compiler(object): re.DOTALL | re.LOCALE | re.MULTILINE | re.UNICODE ) - def __init__(self, framework_paths=None): + def __init__(self, framework_paths=None, strip_basenames=None): """ @param framework_paths: [str, ...] """ + self.strip_basenames = bool(strip_basenames) if framework_paths is None: self.framework_paths = tuple() elif isinstance(framework_paths, tuple): From 23f4dc3249c79cd70c7e01dd8f93dd7d9805c0b2 Mon Sep 17 00:00:00 2001 From: "OCHIAI, Gouji" Date: Sat, 1 Mar 2014 18:50:29 +0900 Subject: [PATCH 2/2] First implementation with minimum tests. --- codekitlang/compiler.py | 15 +++++++++++++++ codekitlang/tests/test_compiler.py | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/codekitlang/compiler.py b/codekitlang/compiler.py index 233c3e9..cdd3020 100644 --- a/codekitlang/compiler.py +++ b/codekitlang/compiler.py @@ -20,6 +20,16 @@ def get_file_content(filepath, encoding_hints=None): return 'utf-8', unicode(b, encoding='utf-8', errors='replace') +def strip_basenames(filepath): + """ + @type filepath: str + """ + # TODO: normalize directory separators. + has_leading = filepath.startswith('/') + has_trailing = filepath.endswith('/') + return '/'.join([b.strip() for b in filepath.split('/')]) + + class CompileError(Exception): pass @@ -90,6 +100,11 @@ def resolve_path(self, filename, base_path): if os.path.exists(filepath): logger.debug('Using %s for %s', filepath, filename) return filepath + if self.strip_basenames: + filepath = strip_basenames(filepath) + if os.path.exists(filepath): + logger.debug('Using %s for %s', filepath, filename) + return filepath return None def normalize_path(self, filepath=None, filename=None, basepath=None): diff --git a/codekitlang/tests/test_compiler.py b/codekitlang/tests/test_compiler.py index e4b5a5b..e379d2d 100644 --- a/codekitlang/tests/test_compiler.py +++ b/codekitlang/tests/test_compiler.py @@ -103,6 +103,11 @@ def test(self): self.assertFound('_file15', ('f2', '_file15.kit')) self.assertFound('_file15.kit', ('f2', '_file15.kit')) + def test_strip_basenames(self): + self.obj.strip_basenames = True + self.assertFound(' file1.html ', ('b', 'file1.html')) + self.assertFound(' sub / file4.html ', ('b', 'sub', 'file4.html')) + class NormalizePathTestCase(unittest.TestCase):