CSS codec

Python codec for CSS.

class cssutils.codec.IncrementalDecoder(errors='strict', encoding=None, force=True)
decode(input, final=False)

Decode input and returns the resulting object.

getstate()

Return the current state of the decoder.

This must be a (buffered_input, additional_state_info) tuple. buffered_input must be a bytes object containing bytes that were passed to decode() that have not yet been converted. additional_state_info must be a non-negative integer representing the state of the decoder WITHOUT yet having processed the contents of buffered_input. In the initial state and after reset(), getstate() must return (b””, 0).

reset()

Reset the decoder to the initial state.

setstate(state)

Set the current state of the decoder.

state must have been returned by getstate(). The effect of setstate((b””, 0)) must be equivalent to reset().

class cssutils.codec.IncrementalEncoder(errors='strict', encoding=None)
encode(input, final=False)

Encodes input and returns the resulting object.

getstate()

Return the current state of the encoder.

reset()

Resets the encoder to the initial state.

setstate(state)

Set the current state of the encoder. state must have been returned by getstate().

class cssutils.codec.StreamReader(stream, errors='strict', encoding=None, force=True)
charbuffertype

alias of str

decode(input, errors='strict')

Decodes the object input and returns a tuple (output object, length consumed).

input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot.

errors defines the error handling to apply. It defaults to ‘strict’ handling.

The method may not store state in the Codec instance. Use StreamReader for codecs which have to keep state in order to make decoding efficient.

The decoder must be able to handle zero length input and return an empty object of the output object type in this situation.

encode(input, errors='strict')

Encodes the object input and returns a tuple (output object, length consumed).

errors defines the error handling to apply. It defaults to ‘strict’ handling.

The method may not store state in the Codec instance. Use StreamWriter for codecs which have to keep state in order to make encoding efficient.

The encoder must be able to handle zero length input and return an empty object of the output object type in this situation.

read(size=- 1, chars=- 1, firstline=False)

Decodes data from the stream self.stream and returns the resulting object.

chars indicates the number of decoded code points or bytes to return. read() will never return more data than requested, but it might return less, if there is not enough available.

size indicates the approximate maximum number of decoded bytes or code points to read for decoding. The decoder can modify this setting as appropriate. The default value -1 indicates to read and decode as much as possible. size is intended to prevent having to decode huge files in one step.

If firstline is true, and a UnicodeDecodeError happens after the first line terminator in the input only the first line will be returned, the rest of the input will be kept until the next call to read().

The method should use a greedy read strategy, meaning that it should read as much data as is allowed within the definition of the encoding and the given size, e.g. if optional encoding endings or state markers are available on the stream, these should be read too.

readline(size=None, keepends=True)

Read one line from the input stream and return the decoded data.

size, if given, is passed as size argument to the read() method.

readlines(sizehint=None, keepends=True)

Read all lines available on the input stream and return them as a list.

Line breaks are implemented using the codec’s decoder method and are included in the list entries.

sizehint, if given, is ignored since there is no efficient way to finding the true end-of-line.

reset()

Resets the codec buffers used for keeping state.

Note that no stream repositioning should take place. This method is primarily intended to be able to recover from decoding errors.

seek(offset, whence=0)

Set the input stream’s current position.

Resets the codec buffers used for keeping state.

class cssutils.codec.StreamWriter(stream, errors='strict', encoding=None, header=False)
decode(input, errors='strict')

Decodes the object input and returns a tuple (output object, length consumed).

input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot.

errors defines the error handling to apply. It defaults to ‘strict’ handling.

The method may not store state in the Codec instance. Use StreamReader for codecs which have to keep state in order to make decoding efficient.

The decoder must be able to handle zero length input and return an empty object of the output object type in this situation.

encode(input, errors='strict')

Encodes the object input and returns a tuple (output object, length consumed).

errors defines the error handling to apply. It defaults to ‘strict’ handling.

The method may not store state in the Codec instance. Use StreamWriter for codecs which have to keep state in order to make encoding efficient.

The encoder must be able to handle zero length input and return an empty object of the output object type in this situation.

reset()

Flushes and resets the codec buffers used for keeping state.

Calling this method should ensure that the data on the output is put into a clean state, that allows appending of new fresh data without having to rescan the whole stream to recover state.

write(object)

Writes the object’s contents encoded to self.stream.

writelines(list)

Writes the concatenated list of strings to the stream using .write().

cssutils.codec.detectencoding_str(input, final=False)

Detect the encoding of the byte string input, which contains the beginning of a CSS file. This function returns the detected encoding (or None if it hasn’t got enough data), and a flag that indicates whether that encoding has been detected explicitely or implicitely. To detect the encoding the first few bytes are used (or if input is ASCII compatible and starts with a charset rule the encoding name from the rule). “Explicit” detection means that the bytes start with a BOM or a charset rule.

If the encoding can’t be detected yet, None is returned as the encoding. final specifies whether more data will be available in later calls or not. If final is true, detectencoding_str() will never return None as the encoding.

cssutils.codec.detectencoding_unicode(input, final=False)

Detect the encoding of the unicode string input, which contains the beginning of a CSS file. The encoding is detected from the charset rule at the beginning of input. If there is no charset rule, "utf-8" will be returned.

If the encoding can’t be detected yet, None is returned. final specifies whether more data will be available in later calls or not. If final is true, detectencoding_unicode() will never return None.