iab_tcf package

Submodules

iab_tcf.bits module

class iab_tcf.bits.Reader(consent: bytes)[source]

Bases: object

Represents a bit reader that can extract bits sequentially from a bytes consent and return a representation in different formats.

Parameters:consent – The consent to process in bytes.
read_bitfield(n: int) → Dict[int, bool][source]

Reads certain number of bits from the pointer position and returns the value as a dictionary. The key is the bit position (starting by 1) and the value is True if the bit is 1 and False if the bit is 0.

Parameters:n – Number of bits to retrieve and transform into the dictionary. The dictionary will have as many keys as n.
read_bits(n: int) → bitarray.bitarray[source]

Reads the number of bits requested from the pointer position and automatically advances the pointer of the reader for the subsequent read_bits calls.

Parameters:n – Number of bits to retrieve.
read_bool() → bool[source]

Reads a bit and returns the value as boolean.

read_character() → bytes[source]

Reads 6 bits and returns the value as a character starting with A

read_int(n: int) → int[source]

Reads certain number of bits from the pointer position and returns the value as integer.

Parameters:n – Number of bits to retrieve and transform into int.
read_range(n: int) → List[Tuple[int, int]][source]

Reads a complex “ranged” type from the reader given the number of ranges we are expecting to find.

For each range: - The first bit indicates if it’s a range or just a value. - If it’s a range, the next 16 bits are the start int and the

next 16 bits are the end int.
  • If it’s not a range, the next 16 bits are the start and the
    end is the same as start.
Parameters:n – Number of ranges we are going to process.
read_string(n: int) → bytes[source]

Reads certain number of bits from the pointer position and returns the value as string character by character.

Parameters:n – Number of bits to retrieve and transform into string.
read_time() → datetime.datetime[source]

Reads 36 bits (the length TCF uses for timestamps) and transforms the value into a utc datetime object with seconds granularity.

iab_tcf.iab_tcf module

iab_tcf.iab_tcf.base64_decode(segment: str) → bytes[source]

Helper to decode the IAB TCF segments encoded.

iab_tcf.iab_tcf.segments(consent: str) → List[str][source]

Helper to split the core and non core consents.

iab_tcf.iab_tcf.version(consent: bytes) → int[source]

Helper to extract the version from a consent without having to wait for the full decoding.

iab_tcf.iab_tcf_v1 module

class iab_tcf.iab_tcf_v1.ConsentV1(consent: bytes)[source]

Bases: object

Represents a v1.1 consent with all the information extracted.

Parameters:consent – The consent to process in bytes.
is_purpose_allowed(id: int) → bool[source]

Checks if a purpose is allowed or not.

Parameters:id – Purpose id to check if it’s allowed or not.
is_vendor_allowed(id: int) → bool[source]

Checks if a vendor is allowed or not.

Parameters:id – Vendor id to check if it’s allowed or not.
iab_tcf.iab_tcf_v1.decode_v1(consent: str)[source]

Decodes a v1.1 consent string that it’s encoded in base64.

Parameters:consent – base64 encoded consent string.

iab_tcf.iab_tcf_v2 module

class iab_tcf.iab_tcf_v2.ConsentV2(consent: bytes)[source]

Bases: object

Represents a v2 consent with all the information extracted.

Parameters:consent – The consent to process in bytes.
get_restriction(publisher: int, purpose: int) → iab_tcf.v2.publisher_restriction.PubRestrictionEntry[source]
has_purpose_legitimate_interest(id: int) → bool[source]
is_interest_allowed(id: int) → bool[source]
is_purpose_allowed(id: int) → bool[source]

Checks if a purpose is allowed or not.

Parameters:id – Purpose id to check if it’s allowed or not.
is_vendor_allowed(id: int) → bool[source]

Checks if a vendor is allowed or not.

Parameters:id – Vendor id to check if it’s allowed or not.

Reads the consent vendors. It must be called with the reader already in the position where the consent vendors start.

read_interest_vendors()[source]

Reads the interest vendors. It must be called with the reader already in the position where the interest vendors start.

read_non_core_segments(segments: List[str])[source]

Receives list of non core segments and tries to parse the information inside them.

read_pub_restriction_entries()[source]

Reads the publisher restriction entries. It must be called with the reader already in the position where the publisher restrictions start.

iab_tcf.iab_tcf_v2.decode_v2(consent: str)[source]

Decodes a v2 consent string that it’s encoded in base64 but split in segments.

Parameters:consent – base64 encoded consent string.

Module contents

iab_tcf.decode(consent: str)[source]

Generic implementation of a IAB TCF decoder.

It detects if the consent received is v1.1 or v2 and returns the appropriate ConsentV1 or ConsentV2 instance.