This is the final stage to the file system provided in previous recipes. A test of the seven other modules is shown here that demonstrates the abilities of this abstract design. Working disks is provided via a simple introductory menu, disk failure rate is fully customizable. A shell is provided with several commands that built in along with two programs that allow the creation and viewing of files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | from dal_6 import DAL6, Context
from sys import exit
################################################################################
def main():
welcome()
loop()
def welcome():
print '+---------------------+'
print '| DISK DEMO |'
print '| by Stephen Chappell |'
print '+---------------------+'
def loop():
valid_disk = False
while True:
print
print 'PROGRAM MENU'
print '============'
print '(1) Create Disk'
print '(2) Load Disk'
if valid_disk:
print '(3) Save Disk'
print '(4) Use Disk'
while True:
try:
select = raw_input('Select: ')
except:
exit(0)
try:
select = int(select)
if 0 < select < 3 or (2 < select < 5 and valid_disk):
break
else:
print 'Select should be between 1 and',
if valid_disk:
print '4.'
else:
print '2.'
except:
print 'Select should be a number.'
print
if select == 1:
disk = create_disk()
disk.seed(False)
valid_disk = True
elif select == 2:
disk = load_disk()
disk.seed(False)
valid_disk = True
elif select == 3:
save_disk(disk)
else:
use_disk(disk)
def create_disk():
while True:
try:
print
print 'CREATE A DISK'
blocks = int(raw_input('BLOCKS: '))
size = int(raw_input('SIZE: '))
return DAL6(blocks, size)
except:
print 'INCORRECT VALUE'
def load_disk():
while True:
try:
print
print 'LOAD A DISK'
name = raw_input('FILENAME: ')
disk = DAL6(10, 10)
disk.load(name, False)
return disk
except:
print 'INVALID FILENAME'
def save_disk(disk):
while True:
try:
print
print 'SAVE THE DISK'
name = raw_input('FILENAME: ')
disk.dump(name)
break
except:
print 'INVALID FILENAME'
################################################################################
def use_disk(disk):
set_fail_rate(disk)
context = disk.new_context()
print
print 'CREATING A SHELL ...'
Shell(context)
print 'EXITING THE SHELL ...'
def set_fail_rate(disk):
while True:
try:
print
print 'SET DISK FAIL RATE'
probability = float(raw_input('PROBABILITY: '))
disk.fail(probability)
break
except:
print '0 <= PROBABILITY <= 1'
################################################################################
class Shell:
# The Users Interface
def __init__(self, context):
assert context.__class__ is Context
self.__context = context
self.__commands = ['chdir', 'getcwd', 'listdir', \
'mkdir', 'rmdir', 'remove', 'exit']
self.__programs = ['type', 'edit']
print 'Welcome to DAL'
print '=============='
while True:
try:
prompt = raw_input('>>> ')
except:
continue
if prompt == 'help':
print 'COMMANDS:'
print ' chdir: change current working directory'
print ' getcwd: get current working directory'
print ' listdir: display directory contents'
print ' mkdir: make a new directory'
print ' rmdir: remove an old directory'
print ' remove: remove a file'
print ' exit: terminates this shell'
print 'PROGRAMS:'
print ' type: display the contents of a file'
print ' edit: create a new file'
else:
prompt = prompt.split(' ')
command = prompt[0]
path = ' '.join(prompt[1:])
if command in self.__commands:
if command == 'chdir':
self.__chdir(path)
elif command == 'getcwd':
self.__getcwd()
elif command == 'listdir':
self.__listdir(path)
elif command == 'mkdir':
self.__mkdir(path)
elif command == 'rmdir':
self.__rmdir(path)
elif command == 'remove':
self.__remove(path)
else:
break
elif command in self.__programs:
if command == 'type':
try:
Type(self.__context, path)
except:
print 'Type has crashed.'
else:
try:
Edit(self.__context, path)
except:
print 'Edit has crashed.'
else:
print repr(command), 'is an unrecognized command or program.'
# Changes Current Directory
def __chdir(self, path):
try:
if self.__context.exists(path):
if self.__context.isdir(path):
self.__context.chdir(path)
else:
print 'PATH IS NOT A DIRECTORY'
else:
print 'PATH DOES NOT EXIST'
except:
print 'COULD NOT CHANGE CURRENT WORKING DIRECTORY'
# Get Current Directory
def __getcwd(self):
print self.__context.getcwd()
# Lists Directory Contents
def __listdir(self, path):
try:
if self.__context.exists(path):
if self.__context.isdir(path):
names = self.__context.listdir(path)
for name in names:
if len(name) > 40:
temp = name[:37] + '...'
else:
temp = name
print temp + ' ' * (41 - len(temp)) + '<>',
if path:
temp = path + ' ' + name
else:
temp = name
if self.__context.isdir(temp):
print 'DIR'
else:
print 'FILE'
else:
print 'PATH IS NOT A DIRECTORY'
else:
print 'PATH DOES NOT EXIST'
except:
print 'COULD NOT DISPLAY DIRECTORY CONTENTS'
# Create New Directory
def __mkdir(self, path):
try:
if self.__context.exists(path):
if self.__context.isfile(path):
print 'NAME IS ALREADY IN USE'
else:
print 'THE DIRECTORY ALREADY EXISTS'
else:
self.__context.mkdir(path)
except:
print 'DIRECTORY COULD NOT BE CREATED'
# Remove A Directory
def __rmdir(self, path):
try:
if self.__context.exists(path):
if self.__context.isdir(path):
self.__context.rmdir(path)
else:
print 'PATH IS NOT A DIRECTORY'
else:
print 'PATH DOES NOT EXIST'
except:
print 'DIRECTORY COULD NOT BE REMOVED'
# Remove A File
def __remove(self, path):
try:
if self.__context.exists(path):
if self.__context.isfile(path):
self.__context.remove(path)
else:
print 'PATH IS NOT A FILE'
else:
print 'PATH DOES NOT EXIST'
except:
print 'FILE COULD NOT BE REMOVED'
################################################################################
class Type:
# Display A File
def __init__(self, context, path):
assert context.__class__ is Context
assert type(path) is str
try:
print context.file(path, 'r').read()
except:
print 'FILE COULD NOT BE FOUND'
################################################################################
class Edit:
# Create A File
def __init__(self, context, path):
assert context.__class__ is Context
assert type(path) is str
while True:
try:
option = raw_input('write OR append? ')
assert option == 'write' or option == 'append'
break
except:
pass
if option[0] == 'a':
if context.exists(path):
if context.isfile(path):
self.__menu(context, path, 'a')
else:
print 'PATH IN NOT A FILE'
else:
print 'FILE DOES NOT EXIST AND WILL NOT BE CREATED'
else:
try:
if context.exists(path):
if context.isfile(path):
self.__menu(context, path, 'w')
else:
print 'PATH IS NOT A FILE'
else:
self.__menu(context, path, 'w')
except:
print 'FILE COULD NOT BE CREATED'
# Display A Menu
def __menu(self, context, path, mode):
file_object = context.file(path, mode)
data = self.__edit()
file_object.write(data)
file_object.close(False)
# Get New Lines
def __edit(self):
lines = []
while True:
try:
lines.append(raw_input())
except:
break
return '\n'.join(lines)
################################################################################
if __name__ == '__main__':
main()
|
This is the last recipe is a series of recipes demonstrating an abstract file system. dal_6.py can be found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/492214
Note: The files presented in these recipes are not guaranteed to be without errors. Not all functions have been implemented (primarily those for checking errors). These files have had rather limited testing and were written for an OS class.