Код: Выделить всё
#
# archive management
#
# For this project you must implement the functionality to manage the space allocation of files.
#
# The storage of the file content itself will not be part of the project, only the allocation of its storage
#
# Each file can have up to 4 datablocks per entry.
# Each file can have up to 9 entries. The sequence (1...9) indicates in which order they should be processed.
# The archive information is stored in ascii (no binary).
# The filesize, sequence and blockid will be store as ascii string, with each fileentry.
#
# the name of the archive, is hardcoded to archive.dat and must be in the current directory
#
# Command Line
#
# just must create and initialize the archive file
# python project3.py create
#
#
# add a file to the archive.
# python project3.py add a.txt
#
#
# remove a file from the archive.
# python project3.py remove a.txt
#
#
# update a file in the archive.
# python project3.py update a.txt
#
#
# list the files in the archives and display information on them.
# python project3.py list
#
from array import array
import sys
import time
#maximum number of entry for files in the archive and also for data blocks for the archive
MAX_ENTRY = 32
#maximum number of block for a single file archive entry
MAX_BLOCK_PER_FILE = 4
# maximum number of byte per datablock
MAX_BYTE_PER_DATABLOCK = 10
# maximum length for a file name
MAX_FILENAME = 8
# maximum number of characters used to store datablocks uses by a file
MAX_DIGIT_FOR_BLOCK = 2
# filesize will use a maximum of 3 digits
MAX_DIGIT_FOR_FILESIZE = 3
MAX_DIGIT_FOR_SEQUENCE = 1
ARCHIVE_FILENAME = "archive.dat"
# an empty archive entry has a filename = ""
# the block entries is equal to 0, when it is not in used
# a file can be stored using multiple FileEntry, if it requires more than MAX_BLOCK_PER_FILE*MAX_BYTE_PER_DATABLOCK
# for its storage. FileEntry.sequence allows to store up to 9(1..9) FileEntry for a single file.
# Their FileEntry.sequence allows the order the block they are currently using
# The system has up to 99 blocks to store data(only 2 digits for datablock #).
class FileEntry:
def __init__(
self,
filename="",
size=0,
):
self.filename = filename
self.size = size
self.sequence = 0
self.datablocks = [0] * MAX_BLOCK_PER_FILE
def readFromArchive(self, line):
self.size = int(line[:MAX_DIGIT_FOR_FILESIZE])
self.sequence = int(
line[MAX_DIGIT_FOR_FILESIZE:MAX_DIGIT_FOR_FILESIZE +
MAX_DIGIT_FOR_SEQUENCE])
self.filename = line[MAX_DIGIT_FOR_FILESIZE +
MAX_DIGIT_FOR_SEQUENCE:MAX_DIGIT_FOR_FILESIZE +
MAX_DIGIT_FOR_SEQUENCE + MAX_FILENAME]
for idx in range(MAX_BLOCK_PER_FILE):
block = line[MAX_DIGIT_FOR_FILESIZE + MAX_DIGIT_FOR_SEQUENCE +
MAX_FILENAME + (idx * 2):MAX_DIGIT_FOR_FILESIZE +
MAX_DIGIT_FOR_SEQUENCE + MAX_FILENAME +
((idx + 1) * 2)]
self.datablocks[idx] = int(block)
return self
def writeToArchive(self, file):
file.write(str(self.size).zfill(MAX_DIGIT_FOR_FILESIZE))
file.write(str(self.sequence).zfill(MAX_DIGIT_FOR_SEQUENCE))
file.write(self.filename.rjust(MAX_FILENAME))
for idx in range(0, len(self.datablocks)):
file.write(str(self.datablocks[idx]).zfill(MAX_DIGIT_FOR_BLOCK))
file.write("\n")
def list(self):
print(" filename:" + self.filename.rjust(MAX_FILENAME) + " sequence:" +
str(self.sequence) + " size:" + str(self.size))
for idx in range(0, len(self.datablocks)):
print(str(self.datablocks[idx]).zfill(MAX_DIGIT_FOR_BLOCK))
def isEmpty(self):
return len(self.filename) == 0
class Archive:
def __init__(self):
self.fileEntries = []
self.fileEntries = [FileEntry()] * MAX_ENTRY
def writeToArchive(self):
archive = open(ARCHIVE_FILENAME, "w")
for idx in range(0, MAX_ENTRY):
self.fileEntries[idx].writeToArchive(archive)
archive.close()
def readFromArchive(self):
archive = open(ARCHIVE_FILENAME, "r")
count = 0
datablockid = 1
for line in archive:
line = line.rstrip('\n')
fileEntry = FileEntry()
self.fileEntries[count] = fileEntry.readFromArchive(line)
count = count + 1
return self
def list(self):
for idx in range(0, MAX_ENTRY):
print("entry :" + str(idx))
if self.fileEntries[idx].isEmpty():
print(" empty")
else:
self.fileEntries[idx].list()
print("\n")
def create(self):
self.writeToArchive()
def addToArchive(self, filename):
if len(filename) > MAX_FILENAME:
print("Filename is too long\n")
return
for entry in self.fileEntries:
if entry.filename == filename:
print("File already exists\n")
return
with open(filename, 'r') as file:
file_content = file.read()
file_size = len(file_content)
for entry in self.fileEntries:
if entry.isEmpty():
entry.filename = filename
entry.size = file_size
needed_blocks = (file_size + MAX_BYTE_PER_DATABLOCK - 1) // MAX_BYTE_PER_DATABLOCK
block_count = 0
for idx in range(len(entry.datablocks)):
if block_count == needed_blocks:
break
if entry.datablocks[idx] == 0:
entry.datablocks[idx] = idx + 1
block_count += 1
if block_count == needed_blocks:
break # Break out of the outer loop after assigning all required blocks
else:
print("No more room\n")
def removeFromArchive(self, filename):
for entry in self.fileEntries:
if entry.filename == filename:
entry.filename = ""
entry.size = 0
for idx in range(MAX_BLOCK_PER_FILE):
entry.datablocks[idx] = 0
print("File has been removed")
return
else:
print("File not found")
def updateInArchive(self, filename):
# must do validation on
# file is in archive
# see addToArchive for other validations
print("not implemented, this is your assignment\n")
def createArchive():
print("Creating Archive")
Archive().create()
def addToArchive():
filename = sys.argv[2]
print("Adding to Archive:" + filename)
archive = Archive()
archive = archive.readFromArchive()
archive.list()
archive.addToArchive(filename)
archive.writeToArchive()
def removeFromArchive():
filename = sys.argv[2]
print("Removing from Archive:" + filename)
archive = Archive()
archive.readFromArchive()
archive.list()
archive.removeFromArchive(filename)
archive.writeToArchive()
def listArchive():
print("Listing from Archive")
archive = Archive()
archive = archive.readFromArchive()
archive.list()
def updateInArchive():
filename = sys.argv[2]
print("Updating in Archive:" + filename)
archive = Archive()
archive = archive.readFromArchive()
archive.list()
archive.updateInArchive(filename)
archive.writeToArchive()
#
# processing command
#
#
command = sys.argv[1]
print('Processing command:' + command)
if command == 'create':
createArchive()
elif command == 'add':
addToArchive()
elif command == 'remove':
removeFromArchive()
elif command == 'list':
listArchive()
elif command == 'update':
updateInArchive()
else:
print("Invalid command")
Код: Выделить всё
Processing command:add
Adding to Archive:a.txt
entry :0
filename: sequence:0 size:0
00
00
00
00
entry :1
filename: sequence:0 size:0
00
00
00
00
entry :2
filename: sequence:0 size:0
00
00
00
00
entry :3
filename: sequence:0 size:0
00
00
00
00
entry :4
filename: sequence:0 size:0
00
00
00
00
entry :5
filename: sequence:0 size:0
00
00
00
00
entry :6
filename: sequence:0 size:0
00
00
00
00
entry :7
filename: sequence:0 size:0
00
00
00
00
entry :8
filename: sequence:0 size:0
00
00
00
00
entry :9
filename: sequence:0 size:0
00
00
00
00
entry :10
filename: sequence:0 size:0
00
00
00
00
entry :11
filename: sequence:0 size:0
00
00
00
00
entry :12
filename: sequence:0 size:0
00
00
00
00
entry :13
filename: sequence:0 size:0
00
00
00
00
entry :14
filename: sequence:0 size:0
00
00
00
00
entry :15
filename: sequence:0 size:0
00
00
00
00
entry :16
filename: sequence:0 size:0
00
00
00
00
entry :17
filename: sequence:0 size:0
00
00
00
00
entry :18
filename: sequence:0 size:0
00
00
00
00
entry :19
filename: sequence:0 size:0
00
00
00
00
entry :20
filename: sequence:0 size:0
00
00
00
00
entry :21
filename: sequence:0 size:0
00
00
00
00
entry :22
filename: sequence:0 size:0
00
00
00
00
entry :23
filename: sequence:0 size:0
00
00
00
00
entry :24
filename: sequence:0 size:0
00
00
00
00
entry :25
filename: sequence:0 size:0
00
00
00
00
entry :26
filename: sequence:0 size:0
00
00
00
00
entry :27
filename: sequence:0 size:0
00
00
00
00
entry :28
filename: sequence:0 size:0
00
00
00
00
entry :29
filename: sequence:0 size:0
00
00
00
00
entry :30
filename: sequence:0 size:0
00
00
00
00
entry :31
filename: sequence:0 size:0
00
00
00
00
No more room
Подробнее здесь: https://stackoverflow.com/questions/784 ... an-archive