import logging
import abstracthandler
import os

class FileHandler(abstracthandler.AbstractHandler):

    def __init__(self, conf, bot):
        abstracthandler.AbstractHandler.__init__(self, 'file', conf, bot)
        self.log = logging.getLogger(__name__)
        self.commands={}
        self.commands['list'] = self.get_file_list

    def handle_message(self,cid, command, args):
        try:
            self.commands[command](cid,args)
        except Exception as e:
            self.send_formatted_message(cid,self.get_sorry_message())
            self.log.error(e)

    def get_file_list(self, cid, args):
        if len(args) >= 1:
            for folder in args:
                self.send_formatted_message(cid,self.get_folder_content(folder))
        else:
            self.send_formatted_message(cid,'*file list* usage: file list _[DIRECTORY]_...')

    def get_folder_content(self, folder):
        message = 'Lista dei files in *%s*:\n_%s_'
        files = '\n'.join(os.listdir(folder))
        return message % (folder,files);


[((8, 8, 8, 73), 'abstracthandler.AbstractHandler.__init__', 'abstracthandler.AbstractHandler.__init__', ({(8, 49, 8, 53): 'self', (8, 55, 8, 61): '"""file"""', (8, 63, 8, 67): 'conf', (8, 69, 8, 72): 'bot'}, {}), "(self, 'file', conf, bot)", False, 'import abstracthandler\n'), ((9, 19, 9, 46), 'logging.getLogger', 'logging.getLogger', ({(9, 37, 9, 45): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((29, 26, 29, 44), 'os.listdir', 'os.listdir', ({(29, 37, 29, 43): 'folder'}, {}), '(folder)', False, 'import os\n')]