# Path: mcm/librouteros/datastructures.py
# def mksnt( data ):
#     '''
#     Convert dictionary to attribute words.
# 
#     :param args: Dictionary with key value attributes.
#     Those will be converted to attribute words.
# 
#     :returns: Tuple with attribute words.
#     '''
# 
#     converted = map( mkattrwrd, data.items() )
#     return tuple( converted )
# 
# def parsresp(sentences):
#     '''
#     Parse given response to set of dictionaries containing parsed sentences.
# 
#     :praam sentences: Iterable (tuple,list) with each element as a sentence
#     '''
# 
#     response = map( parsnt, sentences )
#     # filter out empty sentences
#     filtered = filter( None, response )
#     return tuple( filtered )
# 
# def trapCheck( snts ):
#     '''
#     Check for existance of !trap word. This word indicates that an error occured.
#     At least one !trap word in any sentence triggers an error.
#     Unfortunatelly mikrotik api may throw one or more errors as response.
# 
#     :param snts: Iterable with each sentence as tuple
#     '''
# 
#     errmsgs = []
# 
#     for snt in snts:
#         if '!trap' in snt:
#             emsg = ' '.join( word for word in snt )
#             errmsgs.append( emsg )
# 
#     if errmsgs:
#         e = ', '.join( errmsgs )
#         raise CmdError( e )
# 
# def raiseIfFatal( sentence ):
#     '''
#     Check if a given sentence contains error message. If it does then raise an exception.
#     !fatal means that connection have been closed and no further transmission will work.
#     '''
# 
#     if '!fatal' in sentence:
#         error = ', '.join( word for word in sentence if word != '!fatal' )
#         raise ConnError( error )
# 
# Path: mcm/librouteros/exceptions.py
# class LibError( Exception ):
#     '''
#     This is a base exception for all other.
#     '''


from mcm.librouteros.datastructures import mksnt, parsresp, trapCheck, raiseIfFatal
from mcm.librouteros.exceptions import LibError
# -*- coding: UTF-8 -*-




class Api:


    def __init__( self, rwo ):
        self.rwo = rwo


    def run( self, cmd, args = dict() ):
        '''
        Run any 'non interactive' command. Returns parsed response.

        cmd Command word eg. /ip/address/print.
        args Dictionary with key, value pairs.
        '''

        snt = (cmd,) + mksnt( args )
        self.rwo.writeSnt( snt )
        response = self._readResponse()
        trapCheck( response )

# Path: mcm/librouteros/datastructures.py
# def mksnt( data ):
#     '''
#     Convert dictionary to attribute words.
# 
#     :param args: Dictionary with key value attributes.
#     Those will be converted to attribute words.
# 
#     :returns: Tuple with attribute words.
#     '''
# 
#     converted = map( mkattrwrd, data.items() )
#     return tuple( converted )
# 
# def parsresp(sentences):
#     '''
#     Parse given response to set of dictionaries containing parsed sentences.
# 
#     :praam sentences: Iterable (tuple,list) with each element as a sentence
#     '''
# 
#     response = map( parsnt, sentences )
#     # filter out empty sentences
#     filtered = filter( None, response )
#     return tuple( filtered )
# 
# def trapCheck( snts ):
#     '''
#     Check for existance of !trap word. This word indicates that an error occured.
#     At least one !trap word in any sentence triggers an error.
#     Unfortunatelly mikrotik api may throw one or more errors as response.
# 
#     :param snts: Iterable with each sentence as tuple
#     '''
# 
#     errmsgs = []
# 
#     for snt in snts:
#         if '!trap' in snt:
#             emsg = ' '.join( word for word in snt )
#             errmsgs.append( emsg )
# 
#     if errmsgs:
#         e = ', '.join( errmsgs )
#         raise CmdError( e )
# 
# def raiseIfFatal( sentence ):
#     '''
#     Check if a given sentence contains error message. If it does then raise an exception.
#     !fatal means that connection have been closed and no further transmission will work.
#     '''
# 
#     if '!fatal' in sentence:
#         error = ', '.join( word for word in sentence if word != '!fatal' )
#         raise ConnError( error )
# 
# Path: mcm/librouteros/exceptions.py
# class LibError( Exception ):
#     '''
#     This is a base exception for all other.
#     '''


# Path: mcm/librouteros/api.py
from mcm.librouteros.datastructures import mksnt, parsresp, trapCheck, raiseIfFatal
from mcm.librouteros.exceptions import LibError

# -*- coding: UTF-8 -*-




class Api:


    def __init__( self, rwo ):
        self.rwo = rwo


    def run( self, cmd, args = dict() ):
        '''
        Run any 'non interactive' command. Returns parsed response.

        cmd Command word eg. /ip/address/print.
        args Dictionary with key, value pairs.
        '''

        snt = (cmd,) + mksnt( args )
        self.rwo.writeSnt( snt )
        response = self._readResponse()
        trapCheck( response )

        raiseIfFatal( response )