def from_string(cls, string, path=None, encoding=None, is_sass=None): """Read Sass source from the contents of a string.""" if isinstance(string, six.text_type): # Already decoded; we don't know what encoding to use for output, # though, so still check for a @charset. # TODO what if the given encoding conflicts with the one in the # file? do we care? if encoding is None: encoding = determine_encoding(string) byte_contents = string.encode(encoding) text_contents = string elif isinstance(string, six.binary_type): encoding = determine_encoding(string) byte_contents = string text_contents = string.decode(encoding) else: raise TypeError("Expected text or bytes, got {0!r}".format(string)) is_real_file = False if path is None: m = hashlib.sha256() m.update(byte_contents) path = 'string:' + m.hexdigest() elif os.path.exists(path): path = os.path.normpath(os.path.abspath(path)) is_real_file = True return cls( path, text_contents, encoding=encoding, is_real_file=is_real_file, is_sass=is_sass, )
def from_string(cls, string, relpath=None, encoding=None, is_sass=None): """Read Sass source from the contents of a string. The origin is always None. `relpath` defaults to "string:...". """ if isinstance(string, six.text_type): # Already decoded; we don't know what encoding to use for output, # though, so still check for a @charset. # TODO what if the given encoding conflicts with the one in the # file? do we care? if encoding is None: encoding = determine_encoding(string) byte_contents = string.encode(encoding) text_contents = string elif isinstance(string, six.binary_type): encoding = determine_encoding(string) byte_contents = string text_contents = string.decode(encoding) else: raise TypeError("Expected text or bytes, got {0!r}".format(string)) origin = None if relpath is None: m = hashlib.sha256() m.update(byte_contents) relpath = repr("string:{0}:{1}".format( m.hexdigest()[:16], text_contents[:100])) return cls( origin, relpath, text_contents, encoding=encoding, is_sass=is_sass, )
def from_string(cls, string, relpath=None, encoding=None, is_sass=None): """Read Sass source from the contents of a string. The origin is always None. `relpath` defaults to "string:...". """ if isinstance(string, six.text_type): # Already decoded; we don't know what encoding to use for output, # though, so still check for a @charset. # TODO what if the given encoding conflicts with the one in the # file? do we care? if encoding is None: encoding = determine_encoding(string) byte_contents = string.encode(encoding) text_contents = string elif isinstance(string, six.binary_type): encoding = determine_encoding(string) byte_contents = string text_contents = string.decode(encoding) else: raise TypeError("Expected text or bytes, got {0!r}".format(string)) origin = None if relpath is None: m = hashlib.sha256() m.update(byte_contents) relpath = repr("string:{0}:{1}".format(m.hexdigest()[:16], text_contents[:100])) return cls( origin, relpath, text_contents, encoding=encoding, is_sass=is_sass, )
def from_file(cls, f, path=None, **kwargs): """Read Sass source from a file or file-like object.""" contents = f.read() encoding = determine_encoding(contents) if isinstance(contents, six.binary_type): contents = contents.decode(encoding) is_real_file = False if path is None: path = getattr(f, 'name', repr(f)) elif os.path.exists(path): path = os.path.normpath(os.path.abspath(path)) is_real_file = True return cls( path, contents, encoding=encoding, is_real_file=is_real_file, **kwargs)
def from_file(cls, f, path=None, **kwargs): """Read Sass source from a file or file-like object.""" contents = f.read() encoding = determine_encoding(contents) if isinstance(contents, six.binary_type): contents = contents.decode(encoding) is_real_file = False if path is None: path = getattr(f, 'name', repr(f)) elif os.path.exists(path): path = os.path.normpath(os.path.abspath(path)) is_real_file = True return cls(path, contents, encoding=encoding, is_real_file=is_real_file, **kwargs)
def from_file(cls, f, origin=MISSING, relpath=MISSING, **kwargs): """Read Sass source from a file or file-like object. If `origin` or `relpath` are missing, they are derived from the file's ``.name`` attribute as with `from_path`. If it doesn't have one, the origin becomes None and the relpath becomes the file's repr. """ contents = f.read() encoding = determine_encoding(contents) if isinstance(contents, six.binary_type): contents = contents.decode(encoding) if origin is MISSING or relpath is MISSING: filename = getattr(f, 'name', None) if filename is None: origin = None relpath = repr(f) else: origin, relpath = cls._key_from_path(Path(filename), origin) return cls(origin, relpath, contents, encoding=encoding, **kwargs)
def input(self, **kwargs): source = SourceFile( self.origin, self.relpath, self.content, encoding=determine_encoding(self.content), ) return self.compiler.compile_sources(source)