Utility API

Describes various utility and configuration options. Mostly should be used by client code to set configuration properties.

class tgdb.utils.Class

The class-based utilities required by this API.

classmethod forName(clsname: str)

Find the object by name or dotted path, importing as necessary.

Parameters

clsname (str) – name of class to discover.

Returns

The module, class, or object needed.

class tgdb.utils.ConfigName(value)

Stores Configuration Related Keys, Default Values, and Descriptions

property aliasname

The aliasname. This should be a shortened version of the propertyname attribute and should be unique across all aliasname.

Returns

The shortened, unique name for this configuration property.

Return type

str

property defaultvalue

The default value for this configuration property.

Returns

The default value.

Return type

typing.Optional[str]

property description

A description of this configuration property.

Returns

The description.

Return type

str

classmethod fromName(name: str)

Converts a name into a ConfigName object.

Parameters

name (str) – The name to use as a lookup.

Returns

The ConfigName object that matches the name.

Return type

tgdb.utils.ConfigName

property propertyname

The full property name. Should be reverse-domain-notation style.

Returns

The full property name.

Return type

str

class tgdb.utils.HexUtils

A class for all of the hex utilities used by this API.

classmethod formatHex(buf: Union[bytes, bytearray], buflen: int = 0, ll: int = 48, cl: int = 2, prettyprint: bool = True)str

Prints bytes as a hex string

Parameters
  • buf (typing.Union[bytes, bytearray]) – bytes to print as hex string

  • buflen (int) – how many bytes of the buffer to print, if less than 1, then print the whole thing (default: 0).

  • ll (int) – how many bytes to print on a line (default: 48). Only used when pretty printing.

  • cl (int) – how many bytes to print before inserting a space (default: 2). Only used when pretty printing.

  • prettyprint (bool) – whether to print the format in a more human-readable format (if True), or something machines can more easily decipher (if False) (default: True).

Returns

The formatted hexadecimal of the bytes-like.

Return type

str

class tgdb.utils.TGProperties(*args, **kwds)

Intended for accessing configuration data.

get(k: Union[str, tgdb.utils.ConfigName], v: Any = None)

Gets a particular property from this TGProperties instance, with a fallback if it doesn’t exist

Parameters
  • k (typing.Union[str, ConfigName]) – The key for this property

  • v (typing.Any) – A value for this property if one doesn’t already exist

Returns

the value for that property, or None if that property doesn’t exist.

Return type

typing.Any

getDef(k: tgdb.utils.ConfigName)

Gets the default value of k if not set in the properties.

Parameters

k – The property configuration file.

Type

ConfigName

Returns

The configuration value for that property, or that configuration name’s default value.

Return type

typing.Optional[str]

class tgdb.utils.TGProtocolVersion

TGProtocolVersion keeps track of protocol constants.

classmethod getProtocolVersion()int

Gets the protocol version as a single integer

Returns

The protocol major version and minor version.

Return type

int

classmethod isCompatible(protver: int)bool

Checks if this version is compatible with the server’s version.

Returns

Whether this API’s protocol version is compatible with the server’s protocol version.

Return type

bool

classmethod magic()

Gets the magic number

Returns

The magic number.

Return type

int

tgdb.utils.tg_checkSum64(data: Union[bytes, bytearray], seed: int = 0)

This function creates a TGDB standard checksum.

This checksum is based on the MurMur64 hashing algorithm.

Parameters
  • data (bytes-like) – The data bytes to find the hash of.

  • seed (int) – a 64-bit integer that represents a fixed seed to base the checksum off of (default is 0).

Returns

a 64-bit integer representing the MurMur64 hash of the input data and the seed.

Return type

int