def get_full_source_path(self, source_path): """ Return the full path to the given source file. Check if the source file exists. The returned path is OS-dependent. :param source_path: relative path to a source file :type source_path: str :returns: str :raises: ValueError """ norm_source_path = normalize_path(source_path.lstrip("/")) if STATIC_ROOT: full_path = os.path.join(STATIC_ROOT, norm_source_path) if os.path.exists(full_path): return full_path try: full_path = finders.find(norm_source_path) except SuspiciousOperation: full_path = None if full_path is None: raise ValueError("Can't find staticfile named: {0}".format(source_path)) return full_path
def get_full_source_path(self, source_path): """ Return the full path to the given source file. Check if the source file exists. The returned path is OS-dependent. :param source_path: relative path to a source file :type source_path: str :returns: str :raises: ValueError """ norm_source_path = utils.normalize_path(source_path.lstrip("/")) if settings.STATIC_ROOT: full_path = os.path.join(settings.STATIC_ROOT, norm_source_path) if os.path.exists(full_path): return full_path try: full_path = finders.find(norm_source_path) except django.core.exceptions.SuspiciousOperation: full_path = None if full_path is None: raise ValueError( "Can't find staticfile named: {0}".format(source_path)) return full_path
def test_find_dependencies(self): compiler = SCSS() files = { "A.scss": "@import 'B/C.scss';", "B/C.scss": "@import '../E';", "_E.scss": "p {color: red;}", "compass-import.scss": '@import "compass"', } compiler.get_source = MagicMock(side_effect=lambda x: files[x]) root = os.path.dirname(__file__) existing_files = set() for f in files: existing_files.add(os.path.join(root, "static", normalize_path(f))) with patch("os.path.exists") as mocked_os_path_exist: mocked_os_path_exist.side_effect = lambda x: x in existing_files self.assertEqual( compiler.find_dependencies("A.scss"), ["B/C.scss", "_E.scss"] ) self.assertEqual( compiler.find_dependencies("B/C.scss"), ["_E.scss"] ) self.assertEqual( compiler.find_dependencies("_E.scss"), [] )
def test_locate_imported_file(self): compiler = LESS() with patch("os.path.exists") as mocked_os_path_exist: root = os.path.dirname(__file__) existing_files = set() for f in ("A/B.less", "D.less"): existing_files.add(os.path.join(root, "static", normalize_path(f))) mocked_os_path_exist.side_effect = lambda x: x in existing_files self.assertEqual( compiler.locate_imported_file("A", "B.less"), "A/B.less" ) self.assertEqual( compiler.locate_imported_file("E", "../D"), "D.less" ) self.assertEqual( compiler.locate_imported_file("E", "../A/B.less"), "A/B.less" ) self.assertEqual( compiler.locate_imported_file("", "D.less"), "D.less" ) self.assertRaises( StaticCompilationError, lambda: compiler.locate_imported_file("", "Z.less") )
def test_locate_imported_file(self): compiler = SCSS() with patch("os.path.exists") as mocked_os_path_exist: root = os.path.dirname(__file__) existing_files = set() for f in ("A/B.scss", "A/_C.scss", "D.scss"): existing_files.add( os.path.join(root, "static", normalize_path(f))) mocked_os_path_exist.side_effect = lambda x: x in existing_files self.assertEqual(compiler.locate_imported_file("A", "B.scss"), "A/B.scss") self.assertEqual(compiler.locate_imported_file("A", "C"), "A/_C.scss") self.assertEqual(compiler.locate_imported_file("E", "../D"), "D.scss") self.assertEqual(compiler.locate_imported_file("E", "../A/B.scss"), "A/B.scss") self.assertEqual(compiler.locate_imported_file("", "D.scss"), "D.scss") self.assertRaises( StaticCompilationError, lambda: compiler.locate_imported_file("", "Z.scss"))
def test_locate_imported_file(compiler_module, monkeypatch): root = os.path.dirname(__file__) existing_files = set() for f in ("A/B.scss", "A/_C.scss", "A/S.sass", "D.scss"): existing_files.add(os.path.join(root, "static", utils.normalize_path(f))) additional_path = os.path.join(root, "static", "additional-path") existing_files.add( os.path.join(additional_path, "foo.scss") ) monkeypatch.setattr("os.path.exists", lambda x: x in existing_files) compiler = compiler_module.SCSS(load_paths=( additional_path, )) assert compiler.locate_imported_file("A", "B.scss") == "A/B.scss" assert compiler.locate_imported_file("A", "C") == "A/_C.scss" assert compiler.locate_imported_file("E", "../D") == "D.scss" assert compiler.locate_imported_file("E", "../A/B.scss") == "A/B.scss" assert compiler.locate_imported_file("", "D.scss") == "D.scss" assert compiler.locate_imported_file("A", "S.sass") == "A/S.sass" assert compiler.locate_imported_file("A", "S") == "A/S.sass" assert compiler.locate_imported_file("A", "foo") == "foo.scss" assert compiler.locate_imported_file("bar", "foo") == "foo.scss" with pytest.raises(exceptions.StaticCompilationError): compiler.locate_imported_file("", "Z.scss")
def test_find_dependencies(self): compiler = LESS() files = { "A.less": "@import 'B/C.less';", "B/C.less": "@import '../E';", "E.less": "p {color: red;}", } compiler.get_source = MagicMock(side_effect=lambda x: files[x]) root = os.path.dirname(__file__) existing_files = set() for f in files: existing_files.add(os.path.join(root, "static", normalize_path(f))) with patch("os.path.exists") as mocked_os_path_exist: mocked_os_path_exist.side_effect = lambda x: x in existing_files self.assertEqual( compiler.find_dependencies("A.less"), ["B/C.less", "E.less"] ) self.assertEqual( compiler.find_dependencies("B/C.less"), ["E.less"] ) self.assertEqual( compiler.find_dependencies("E.less"), [] )
def get_full_output_path(self, source_path): """ Get full path to compiled file based for the given source file. The returned path is OS-dependent. :param source_path: relative path to a source file :type source_path: str :returns: str """ return os.path.join(settings.ROOT, utils.normalize_path(self.get_output_path(source_path)))
def get_full_output_path(self, source_path): """ Get full path to compiled file based for the given source file. The returned path is OS-dependent. :param source_path: relative path to a source file :type source_path: str :returns: str """ return os.path.join(ROOT, normalize_path(self.get_output_path(source_path.lstrip("/"))))
def get_full_source_path(self, source_path): try: return super(SCSS, self).get_full_source_path(source_path) except ValueError: # Try to locate the source file in directories specified in `load_paths` norm_source_path = utils.normalize_path(source_path.lstrip("/")) for dirname in self.load_paths: full_path = os.path.join(dirname, norm_source_path) if os.path.exists(full_path): return full_path raise
def locate_imported_file(self, source_dir, import_path): """ Locate the imported file in the source directory. Return the path to the imported file relative to STATIC_ROOT :param source_dir: source directory :type source_dir: str :param import_path: path to the imported file :type import_path: str :returns: str """ import_filename = posixpath.basename(import_path) import_dirname = posixpath.dirname(import_path) import_filename_root, import_filename_extension = posixpath.splitext( import_filename) if import_filename_extension: filenames_to_try = [import_filename] else: # No extension is specified for the imported file, try all supported extensions filenames_to_try = [ import_filename_root + "." + extension for extension in self.import_extensions ] if not import_filename.startswith("_"): # Try the files with "_" prefix filenames_to_try += [ "_" + filename for filename in filenames_to_try ] # Try to locate the file in the directory relative to `source_dir` for filename in filenames_to_try: source_path = posixpath.normpath( posixpath.join(source_dir, import_dirname, filename)) try: self.get_full_source_path(source_path) return source_path except ValueError: pass # Try to locate the file in the directories listed in `load_paths` for dirname in self.load_paths: for filename in filenames_to_try: source_path = posixpath.join(import_dirname, filename) if os.path.exists( os.path.join(dirname, utils.normalize_path(source_path))): return source_path raise exceptions.StaticCompilationError( "Can't locate the imported file: {0}".format(import_path))
def test_locate_imported_file(monkeypatch): compiler = compilers.Stylus() root = os.path.dirname(__file__) existing_files = set() for f in ("A/B.styl", "C.styl"): existing_files.add(os.path.join(root, "static", utils.normalize_path(f))) monkeypatch.setattr("os.path.exists", lambda x: x in existing_files) assert compiler.locate_imported_file("A", "B.styl") == "A/B.styl" assert compiler.locate_imported_file("", "C.styl") == "C.styl" with pytest.raises(exceptions.StaticCompilationError): compiler.locate_imported_file("", "Z.styl")
def test_locate_imported_file(monkeypatch): compiler = LESS() root = os.path.dirname(__file__) existing_files = set() for f in ("A/B.less", "D.less"): existing_files.add(os.path.join(root, "static", normalize_path(f))) monkeypatch.setattr("os.path.exists", lambda path: path in existing_files) assert compiler.locate_imported_file("A", "B.less") == "A/B.less" assert compiler.locate_imported_file("E", "../D") == "D.less" assert compiler.locate_imported_file("E", "../A/B.less") == "A/B.less" assert compiler.locate_imported_file("", "D.less") == "D.less" with pytest.raises(StaticCompilationError): compiler.locate_imported_file("", "Z.less")
def locate_imported_file(self, source_dir, import_path): """ Locate the imported file in the source directory. Return the path to the imported file relative to STATIC_ROOT :param source_dir: source directory :type source_dir: str :param import_path: path to the imported file :type import_path: str :returns: str """ import_filename = posixpath.basename(import_path) import_dirname = posixpath.dirname(import_path) import_filename_root, import_filename_extension = posixpath.splitext(import_filename) if import_filename_extension: filenames_to_try = [import_filename] else: # No extension is specified for the imported file, try all supported extensions filenames_to_try = [import_filename_root + "." + extension for extension in self.import_extensions] if not import_filename.startswith("_"): # Try the files with "_" prefix filenames_to_try += ["_" + filename for filename in filenames_to_try] # Try to locate the file in the directory relative to `source_dir` for filename in filenames_to_try: source_path = posixpath.normpath(posixpath.join(source_dir, import_dirname, filename)) try: self.get_full_source_path(source_path) return source_path except ValueError: pass # Try to locate the file in the directories listed in `load_paths` for dirname in self.load_paths: for filename in filenames_to_try: source_path = posixpath.join(import_dirname, filename) if os.path.exists(os.path.join(dirname, utils.normalize_path(source_path))): return source_path raise exceptions.StaticCompilationError("Can't locate the imported file: {0}".format(import_path))
def test_find_dependencies(monkeypatch): compiler = compilers.LESS() files = { "A.less": "@import 'B/C.less';", "B/C.less": "@import '../E';", "E.less": "p {color: red;}", } monkeypatch.setattr(compiler, "get_source", lambda x: files[x]) root = os.path.dirname(__file__) existing_files = set() for f in files: existing_files.add(os.path.join(root, "static", utils.normalize_path(f))) monkeypatch.setattr("os.path.exists", lambda path: path in existing_files) assert compiler.find_dependencies("A.less") == ["B/C.less", "E.less"] assert compiler.find_dependencies("B/C.less") == ["E.less"] assert compiler.find_dependencies("E.less") == []
def test_find_dependencies(compiler_module, monkeypatch): compiler = compiler_module.SCSS() files = { "A.scss": "@import 'B/C.scss';", "B/C.scss": "@import '../E';", "_E.scss": "p {color: red;}", "compass-import.scss": '@import "compass"', } monkeypatch.setattr(compiler, "get_source", lambda x: files[x]) root = os.path.dirname(__file__) existing_files = set() for f in files: existing_files.add(os.path.join(root, "static", utils.normalize_path(f))) monkeypatch.setattr("os.path.exists", lambda x: x in existing_files) assert compiler.find_dependencies("A.scss") == ["B/C.scss", "_E.scss"] assert compiler.find_dependencies("B/C.scss") == ["_E.scss"] assert compiler.find_dependencies("_E.scss") == []