moti.utils package

Submodules

moti.utils.connection module

class moti.utils.connection.Connection(sock, addr=('127.0.0.1', 8000))

Bases: object

Connection is a simplification of socket for the purposes of the client-server connection

close()

Close the socket, hence the conection

connect(port)

Create a Connection to (host, port)

Parameters:
  • host (str) – ip
  • port (int) – port
receive(size)

Receive a message of size exactly size

Parameters:size (int) – size of expected message
receive_message()

Receive one message (length, message) and return message

send(data)
Parameters:data (bytes) – data to send
send_message(message)

Send a message and it’s length in the beggining

Parameters:message (str) – a message to send
send_message_to_addr(message)

Send a message and it’s length in the beggining to self.addr

Parameters:message (str) – a message to send
sendto(message, addr)
Parameters:
  • message (bytes) – a messsage to send
  • addr (tuple) – address to send the message to

moti.utils.consumer module

class moti.utils.consumer.Consumer(mq_url)

Bases: object

A consumer is basically a generalized message queue consumer One can choose which consumer they want to use for example: the rabbitmq_consumer that is in the consumers directory A consumer must have the following methods: consumer.add_queue(queue, callback): which tells the consumer how to consume the queue consumer.consume(): which makes the consumer to start consuming on all queues added A consumer must have a class variable ‘protocol’ which is basically it’s name

add_queue(queue, callback)

Adds the queue to the consumer with the callback function

Parameters:
  • queue (str) – name of queue
  • callback (callable) – callback function for queue consumption
consume()

Start consuming

get_consumers()

Import all consumers in ./utils/consumers Every consumer should have ‘consumer’ in the name of it’s file Only one consumer per file The name of file should be in snake case The name of consumer should be in camel case and the same as file’s name The consumer should have ‘protocol’ variable so it can be chosen by that variable

moti.utils.database_reader module

class moti.utils.database_reader.DatabaseReader(database_url)

Bases: object

DatabaseReader is a reader for a general database One can choose which reader they want to use for example: mongo_reader that is in dbreaders directory A reader must have the following methods: reader.find(collection, filters): finds all entries in the collection that pass the filters reader.find_one(collection, filters): finds one entry in the collection that pass the filters A reader must also have a ‘db’ class variable which is basically it’s name

find(collection, filters=None)

Find all entries in the collection that pass the filters

Parameters:
  • collection (str) – the collection to access
  • filters (dict(,optional)) – the filters to apply on the search (usally a dictionary), defaults to None
Returns:

a list of all results in the collection that match the filters

Return type:

list

find_one(collection, filters=None)

Find an entry in the colleciton that passes the filters

Parameters:
  • collection (str) – the collection to access
  • filters (dict(,optional)) – the filters to apply on the search (usally a dictionary), defaults to None
Returns:

a list of all results in the collection that match the filters

Return type:

list

get_readers()

Import all dbreaders in ./utils/dbreaders Every dbreader should have ‘reader’ in the name of it’s file Only one dbreader per file The name of file should be in snake case The name of dbreader should be in camel case and the same as file’s name The dbreader should have ‘db’ variable so it can be chosen by that variable

moti.utils.handle_config module

class moti.utils.handle_config.Config_handler(path, part=None)

Bases: object

moti.utils.listener module

class moti.utils.listener.Listener(port, host='0.0.0.0', backlog=1000, reuseaddr=True)

Bases: object

Listener is basically a socket that listens but when it accepts a connection it returns a Connection instead of socket

accept()

Accept a connection

Returns:a Connection object representing the connection
Return type:Conneciton
start()

Start listening fro connections

stop()

Close the listening socket

moti.utils.misc module

moti.utils.misc.camel_from_snake(name)
Parameters:name (str) – a snake case name
Returns:name in camel case
Return type:str
moti.utils.misc.read_string(message)

This is a coroutine that given a string message reads a the message x chars at a time when x has to be sent to it To initiate: g = read_string(message), next(message) Then to g.send(x) reads the next x bytes of message

Parameters:message (str) – a string

moti.utils.parser module

class moti.utils.parser.Parser(fields=None)

Bases: object

The parser class collects all parsers in the parsers directory under the conditions: 1. the module the parser is inside of has a variable include and it’s set to 1 2. the name of parser begins with parse_ 3. the parser has a variable called fields After collecting all the parsers the fields variable will contain all the fields there is a parsers to The mapping variable contains a mapping between fields and parsers that parse them

decode_queue(queue)

Given a queue name generated by generate_queues return the corresponding parser

Parameters:queue (str) – queue name
Returns:a parser corresponding to the queue
Return type:callable
generate_queues()

Create queue names based on availible parsers

Returns:a list of queues based on availible parsers
Return type:list
parse_field(field, data, context)

Run all parsers corresponding to field and return all of their results in a list :param field: field name :type field: str :param data: data to parse :tpye data: something that the parsers can parse :param context: context for parser to save it’s data at :type: str :return: a list containing the data parsed by every parser that parses this field :rtype: list

moti.utils.protocol module

class moti.utils.protocol.Config(fields)

Bases: object

deserialize()
Parameters:message (bytes) – a serialized config message
Returns:A Config class instance
Type:Config
serialize()
Returns:a serialized Config object
Return type:bytes
class moti.utils.protocol.Hello(user_id, user_name, birth_date, gender)

Bases: object

deserialize()
Parameters:message (bytes) – a serialized hello message
Returns:A Hello class instance
Type:Hello
serialize()
Returns:a serialized Hello object
Return type:bytes
class moti.utils.protocol.Snapshot(timestamp, translation, rotation, color_image, depth_image, feelings)

Bases: object

class Image(height, width, image, fmt)

Bases: object

compactify(path, timestamp)
Parameters:
  • path (Path) – a base_path to save the Image to
  • timestamp (int) – timestamp
Returns:

a dictionary with all of image’s fields but the image is saved to file

Return type:

dict

deserialize(height, width, fmt)
Parameters:message (bytes) – a serialized image message
Returns:A Image class instance
Type:Image
serialize()
Returns:a serialized Image object
Return type:bytes
compactify(path)
Parameters:path (Path) – base path to sabe the iamges
Returns:a dictironary with all the snapshot’s fields but images are saved to file
Return type:dict
deserialize()
Parameters:message (bytes) – a serialized snapshot message
Returns:A Snapshot class instance
Type:Snapshot
serialize(fields)
Returns:a serialized Image object
Return type:bytes

moti.utils.publisher module

class moti.utils.publisher.Publisher(queues, mq_url, path=None, name='Server', parsers=None)

Bases: object

Publisher is a general publisher of data One can choose which publisher they want to use for example: the rabbitmq_parser_publisher that is in the publishers directory A publisher must have one of the following methods: publisher.publish(snapshot): which publishes with snapshot publisher.publish_factory(snapshot, queue): which publishes the snapshot to a specific queue

the publish_factory method can also change the format of publishing depending on the queue

A publisher must have a class variable ‘protocol’ which is basically it’s name

get_publishers()

Import all publishers in ./utils/publishers Every publisher should have ‘publisher’ in the name of it’s file Only one publisher per file The name of file should be in snake case The name of publisher should be in camel case and the same as file’s name The publisher should have ‘protocol’ variable so it can be chosen by that variable

moti.utils.reader module

class moti.utils.reader.Reader(path, reader)

Bases: object

Reader is a general reader of data One can choose which reader they want to use for example: the proto_reader that is in the readers directory A reader must have the following methods: reader.get_user(): which reads the user details from the smaple file reader.get_snapshot(): which reads the next snapshot and returns a dict object A reader must have a class variable ‘reader’ which is basically it’s name

get_readers()

Import all readers in ./utils/readers Every reader should have ‘reader’ in the name of it’s file Only one reader per file The name of file should be in snake case The name of reader should be in camel case and the same as file’s name The reader should have ‘reader’ variable so it can be chosen by that variable

class moti.utils.reader.ReaderSnapshot(dic)

Bases: object

Trun the dictionary from get_snapshot into a class for convinience

moti.utils.transfer module

class moti.utils.transfer.Transfer(input_url, queues, publish_factory=None, publish=None)

Bases: object

Transfer is class that recives a publishing method and queue to consume from It sets up a Consumer and adds all the queues in queues with the appropriate callback functions The start method begins the consumers consumption The transfer should be initialized with either a publish factory or a publish publish factory takes precedence

callback_factory(queue)

Creates a callback function of the right format

Parameters:queue (str) – name of a queue
Returns:creates a callback functions fit for message queues
Return type:callable
start()

Module contents