reorx
10/28/2011 - 4:17 AM

os_storage

os_storage

#!/usr/bin/python
#coding=utf8

import sys
from utils import col_decorate, BaseError

class InputError(BaseError):
    pass


if '__main__' == __name__:
    # size :: 64
    d = [
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
        ([0, 0, 0, 0, ], [0, 0, 0, 0, ]),
    ]
    d_size = 64

    def show():
        global d
        global d_size
        op = '\n  Disk 1  |  Disk 2 ' +\
             '\n ---------+---------'
        for sp in d:
            op += '\n'
            for i in sp[0]:
                op += ' %s' % i
            op += '  | '
            for i in sp[1]:
                op += ' %s' % i
        op += '\nUsed space: %s' % get_used()
        op += '\nFree space: %s' % (d_size-get_used())
        print op

    def get_used():
        global d
        used = 0
        for sp in d:
            for i in sp[0]:
                if i == 1: used +=1
            for i in sp[1]:
                if i == 1: used +=1
        return used

    def insert():
        global d_size
        if get_used() >= d_size:
            raise InputError('Data storage overflows !')
        global d
        inserted = False
        for sp in d:
            if inserted: break
            for ph in range(len(sp[0])):
                if inserted: break
                if sp[0][ph] == 0:
                    sp[0][ph] = 1
                    inserted = True
            for ph in range(len(sp[1])):
                if inserted: break
                if sp[1][ph] == 0:
                    sp[1][ph] = 1
                    inserted = True

    def release(di, ph, sp):
        global d
        try:
            d[sp][di][ph] = 0
        except IndexError:
            raise InputError('Release index out of range')

    def args_input():
        print '磁盘/物理/柱面: ',
        args = raw_input().split('/')
        if len(args) != 3:
            raise InputError('Input arguments must be of 3')
        try:
            args = [int(i) for i in args]
        except ValueError:
            raise InputError('Input arguments must be integer')
        return args

    while True:
        try:
            print ''
            print '1 to insert and 0 to release: ',
            mode = raw_input()
            if '1' == mode:
                insert()
                show()
            elif '0' == mode:
                release(*args_input())
                show()
            else:
                print col_decorate('\nUneffected', 'red')

        except BaseError as e:
            print e
        except KeyboardInterrupt:
            print col_decorate('\nexit', 'blue')
            sys.exit()