# __init__.py - command package
#
# Copyright 2007 Nokia
# Author: Daniel d'Andrada T. de Carvalho <daniel.carvalho@indt.org.br>
#
# This file is part of mediahandler-skeleton.
#
# mediahandler-skeleton is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# mediahandler-skeleton is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with mediahandler-proxy. If not, see <http://www.gnu.org/licenses/>.

from fetchinfo import FetchInfo
from transcode import Transcode
from quit import Quit
from util import parse_command

__all__ = ['from_string', 'FetchInfo', 'Transcode', 'Quit']

def from_string(string, sock):
    '''Returns a Command object given its string representation.
    
       Returns None if the string is invalid.'''

    cmd_itens = parse_command(string)
    if cmd_itens == None:
        return None
    
    if cmd_itens['name'] == 'transcode':
        command = Transcode(sock)
    elif cmd_itens['name'] == 'fetch-info':
        command = FetchInfo(sock)
    elif cmd_itens['name'] == 'quit':
        command = Quit()
    else:
        # Unknown command
        return None

    ok = command.parse(cmd_itens['params'])
    if not ok:
        return None

    return command
