#! /usr/bin/env python
# -*- coding: utf-8 -*-

#==========================================================
# tdir.py - calcula el tamaño de un directorio
#----------------------------------------------------------
# FJA - fja@neocipres.com           Mayo de 2015 (17/06/15)
#==========================================================

import os
import os.path
import sys


def raya(n, ca='='):
    rep = lambda p: p[0] * p[1]
    return rep((n, ca))


def GBy(numBy):
    return numBy // (1024 * 1024 * 1024), numBy % (1024 * 1024 * 1024)


def MBy(numBy):
    return numBy // (1024 * 1024), numBy % (1024 * 1024)


def KBy(numBy):
    return numBy // (1024), numBy % 1024


def engine(path='.'):
#Código en: https://code.activestate.com/recipes/578197-directory-file-counter/
    directories = 0
    files = 0
    for information in os.walk(path):
        directories += len(information[1])
        files += len(information[2])
    print raya(60)
    print '\t Directorios =', directories
    print '\t Archivos =', files


def tamdir(directorio='.'):
    numbytes = 0
    KBytes = 0
    MBytes = 0
    GBytes = 0
    # Código de la suma de bytes mejorado a partir de una idea de:
    # http://joedicastro.com/conocer-el-tamano-de-un-directorio-con-python.html
    try:
        for root, dirs, files in os.walk(directorio):
            for name in files:
                path = os.path.join(root, name)
                numbytes += os.lstat(path).st_size
            for d in dirs:
                path = os.path.join(root, d)
                numbytes += os.lstat(path).st_size
        numbytes += os.path.getsize(directorio)

    except OSError, e:
        print '\n\t'
        print 'Error: ', e, sys.exc_info()[0], '\n'
        print '¡¡El tamaño obtenido puede no ser exacto!!\n'

    print raya(60, '-')
    print "\t %s  -  %s bytes" % (directorio, numbytes)
    if numbytes < 1024:
        print "%s bytes" % (numbytes)
    elif numbytes >= 1024 and numbytes < 1024 * 1024:
        KBytes, numbytes = KBy(numbytes)
        print "\n\t %s KiB y %s bytes" % (KBytes, numbytes)
    elif numbytes >= 1024 * 1024 and numbytes < 1024 * 1024 * 1024:
        MBytes, numbytes = MBy(numbytes)
        if numbytes >= 1024 and numbytes < 1024 * 1024:
                    KBytes, numbytes = KBy(numbytes)
        print "\n\t %s MiB, %s KiB y %s bytes" \
        % (MBytes, KBytes, numbytes)
    elif numbytes >= 1024 * 1024 * 1024:
        GBytes, numbytes = GBy(numbytes)
        if numbytes >= 1024 * 1024 and numbytes < 1024 * 1024 * 1024:
                MBytes, numbytes = MBy(numbytes)
                if numbytes >= 1024 and numbytes < 1024 * 1024:
                    KBytes, numbytes = KBy(numbytes)
        print "\n\t %s GiB, %s MiB, %s KiB y %s bytes" \
        % (GBytes, MBytes, KBytes, numbytes)
    print raya(60)

if __name__ == '__main__':
    if len(sys.argv) == 1:
        engine()
        tamdir()
    elif len(sys.argv) == 2:
        if sys.argv[1] == '-h':
            print '\n\t Uso: tdir [directorio] \n'
            sys.exit(0)
        engine(sys.argv[1])
        tamdir(sys.argv[1])