#
# Copyright 2009
# Greg Swift <gregswift@gmail.com>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import func_module
from func.minion import sub_process

class Disk(func_module.FuncModule):
    version = "0.0.1"
    api_version = "0.0.1"
    description = "Gathering disk related information"

    def usage(self, partition=None):
        """
        This process takes the full results of df -P and returns them as a 
        dictionary of {mount: {"device":X, "total":X, "used":X, "avail", "percentage":X)}
        """
        results = {}
        command = '/bin/df -P'
        if (partition):
            command += " %s" % (partition)
        cmdref = sub_process.Popen(command, stdout=sub_process.PIPE,
                                   stderr=sub_process.PIPE, shell=True,
                                   close_fds=True)
        (stdout, stderr) = cmdref.communicate()
        for disk in stdout.split('\n'):
            if (disk.startswith('Filesystem') or not disk):
                continue
            (device, total, used, available, percentage, mount) = disk.split()
            results[mount] = {'device':device,
                              'total':int(total),
                              'used':int(used),
                              'available':int(available),
                              'percentage':int(percentage[:-1])}
        return results

    def register_method_args(self):
        """
        The argument export method
        """
        return {
                'usage':{
                    'args':{
                        'partition': {
                            'type':'string',
                            'optional':True,
                            'description':'A specific partition to get usage data for',
                            }
                        },
                    'description':'Gather disk usage information'
                    }
                }

