-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Needed: Test of fileSystem
- Loading branch information
lalo73
committed
Jan 6, 2013
1 parent
77a7256
commit ce8d9e7
Showing
6 changed files
with
185 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from hardware.fileSystem import * | ||
|
||
root = RootDirectory() | ||
|
||
file_1 = File("file_1","data of file 1") | ||
file_2 = File("file_2",1024) | ||
file_3 = File("file_3") | ||
|
||
dir_1 = Directory("dir_1",root) | ||
dir_2 = Directory("dir_2") | ||
|
||
dir_1.addObject(file_1) | ||
dir_1.addObject(file_2) | ||
dir_1.addObject(dir_2) | ||
root.addObject(dir_1) | ||
|
||
dir_2.addObject(file_3) | ||
|
||
disk = HardDisk() | ||
disk.root = root | ||
|
||
disk.getObjectByPath("dir_1/dir_2/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from hardware.fileSystem import * | ||
|
||
file_1 = File("File_1","Data of file 1") | ||
file_2 = File("File_1",1024) | ||
file_3 = File("File_3") | ||
|
||
dir_1 = Directory("Dir_1") | ||
dir_2 = Directory("Dir_2") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,140 @@ | ||
__author__ = 'leandro' | ||
|
||
from system_tools.systemTools import checkFileSystemObjectName, checkPathFormat | ||
|
||
class HardDisk: | ||
def __init__(self): | ||
pass | ||
def __init__(self, name="HardDisk"): | ||
self.name = name | ||
self.root = RootDirectory() | ||
|
||
def getObjectByPath(self, path): | ||
checkPathFormat(path) | ||
split_path = path.split("/") | ||
return self.__getObjectBySplitPath(split_path) | ||
|
||
def __getObjectBySplitPath(self, split_path): | ||
last_object = self.root | ||
size = len(split_path) | ||
for object_name in split_path: | ||
isDirectory = size > 1 | ||
last_object = self.__nextObjectFrom(object_name, last_object, isDirectory) | ||
size -= 1 | ||
|
||
return last_object | ||
|
||
def __nextObjectFrom(self, object_name, last_object, isDirectory): | ||
if object_name == "": | ||
return last_object | ||
elif isDirectory: | ||
return last_object.getDirectory(object_name) | ||
else: | ||
return last_object.getFile(object_name) | ||
|
||
|
||
class GeneralFileSystemObject(object): | ||
def __unicode__(self): | ||
return u"%(name)s" % {"name": self.name} | ||
|
||
def __str__(self): | ||
return "%(name)s" % {"name": self.name} | ||
|
||
def __repr__(self): | ||
return "%(name)s" % {"name": self.name} | ||
|
||
class GeneralFileSystemObject: | ||
def __init__(self,name): | ||
def __init__(self, name): | ||
checkFileSystemObjectName(name) | ||
self.name = name | ||
|
||
def addObject(self, object): | ||
raise IsNotDirectory() | ||
|
||
def getDirectory(self, name): | ||
raise IsNotDirectory() | ||
|
||
def getFile(self, name): | ||
raise IsNotDirectory() | ||
|
||
def isDirectory(self): | ||
return False | ||
|
||
def getData(self): | ||
raise IsNotFile() | ||
|
||
def include(self, object_name): | ||
raise IsNotDirectory() | ||
|
||
def father(self): | ||
raise IsNotDirectory() | ||
|
||
|
||
class File(GeneralFileSystemObject): | ||
def __init__(self, name, data=None): | ||
super(File, self).__init__(name) | ||
self.data = data | ||
|
||
def getData(self): | ||
return self.data | ||
|
||
|
||
class Directory(GeneralFileSystemObject): | ||
def __init__(self,name): | ||
super(Directory,self).__init__(name) | ||
self.filesAndDirectories = {} | ||
def __init__(self, name, father=None): | ||
super(Directory, self).__init__(name) | ||
self.__father = father | ||
self.objects = {} | ||
|
||
def father(self): | ||
return self.__father | ||
|
||
def isDirectory(self): | ||
return True | ||
|
||
def addObject(self, object): | ||
name = object.name | ||
if object.isDirectory(): | ||
name += "/" | ||
object.father = self | ||
self.objects[name] = object | ||
|
||
def getDirectory(self, name): | ||
return self.__getObjectByType(name, True) | ||
|
||
def getFile(self, name): | ||
return self.__getObjectByType(name, False) | ||
|
||
def addFile(self,file,path=""): | ||
pass | ||
def includes(self, object_name): | ||
return object_name in self.objects.keys() | ||
|
||
def __getObjectByType(self, name, directory=True): | ||
def nameMatch(name, local_object_name, directory): | ||
local_name = name | ||
if directory: | ||
local_name = name + "/" | ||
return local_name == local_object_name | ||
|
||
for local_object_name in self.objects: | ||
if nameMatch(name, local_object_name, directory): | ||
object = self.objects[local_object_name] | ||
return object | ||
|
||
raise CantFindDirectoryOrFile() | ||
|
||
|
||
class RootDirectory(Directory): | ||
def __init__(self, name="root"): | ||
super(RootDirectory, self).__init__(name, self) | ||
|
||
#Exceptions | ||
|
||
class CantFindDirectory(Exception): | ||
def __init__(self): | ||
super(CantFindDirectory,self).__init__() | ||
class CantFindDirectoryOrFile(Exception): | ||
def __str__(self): | ||
return "Can't find directory or file" | ||
|
||
|
||
class IsNotDirectory(Exception): | ||
def __str__(self): | ||
return "This is not a directory" | ||
|
||
|
||
class IsNotFile(Exception): | ||
def __str__(self): | ||
return "This is not a file" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__author__ = 'leandro' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
__author__ = 'leandro' | ||
|
||
def checkFileSystemObjectName(name): | ||
if "/" in name: | ||
raise BadNameFormat("/") | ||
|
||
def checkPathFormat(path): | ||
if " " in path: | ||
raise BadPatFormat() | ||
|
||
#Exceptions | ||
class BadNameFormat(Exception): | ||
def __init__(self, invalid=""): | ||
self.invalid_character = invalid | ||
|
||
def __str__(self): | ||
return "invalid character '%(invalid)s' in name" % {"invalid": self.invalid_character} | ||
|
||
class BadPatFormat(Exception): | ||
def __init__(self,invalid=""): | ||
self.invalid_character = invalid | ||
|
||
def __str__(self): | ||
return "invalid character '%(invalid)s' in name" % {"invalid": self.invalid_character} |