2021-11-20 14:40:07 +00:00
|
|
|
from classes.file import File
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
|
2021-11-25 12:42:00 +00:00
|
|
|
from configparser import SectionProxy
|
2021-11-25 15:31:49 +00:00
|
|
|
from typing import Union, Optional
|
2021-11-25 12:42:00 +00:00
|
|
|
|
|
|
|
|
2021-11-20 14:40:07 +00:00
|
|
|
class Directory:
|
2021-11-25 12:42:00 +00:00
|
|
|
"""Class representing a Directory on the local filesystem
|
|
|
|
"""
|
2021-11-20 14:40:07 +00:00
|
|
|
@classmethod
|
2021-11-25 15:31:49 +00:00
|
|
|
def fromConfig(cls, config: SectionProxy):
|
2021-11-25 12:42:00 +00:00
|
|
|
"""Create Directory object from a Directory section in the Config file
|
|
|
|
|
|
|
|
Args:
|
|
|
|
config (configparser.SectionProxy): Configuration section defining
|
|
|
|
a Directory
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: Raised if section does not contain Location parameter
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
classes.directory.Directory: Directory object for the location
|
|
|
|
specified in the config section
|
|
|
|
"""
|
2021-11-20 14:40:07 +00:00
|
|
|
if "Location" in config.keys():
|
|
|
|
return cls(config.name.split()[1], config["Location"])
|
|
|
|
else:
|
2021-11-25 12:42:00 +00:00
|
|
|
raise ValueError("Definition for Directory " +
|
|
|
|
config.name.split()[1] + " does not contain Location!")
|
|
|
|
|
|
|
|
def __init__(self, name: str, location: Union[str, pathlib.Path]):
|
|
|
|
"""Initialize a new Directory object
|
2021-11-20 14:40:07 +00:00
|
|
|
|
2021-11-25 12:42:00 +00:00
|
|
|
Args:
|
|
|
|
name (str): Name of the Directory object
|
|
|
|
location (str, pathlib.Path): Filesystem location of the Directory
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: Raised if passed location does not exist or is not a
|
|
|
|
directory
|
|
|
|
"""
|
2021-11-20 14:40:07 +00:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
if os.path.isdir(location):
|
|
|
|
self.location = pathlib.Path(location)
|
2021-11-29 13:50:16 +00:00
|
|
|
self.assertCompletedDirectory()
|
2021-11-20 14:40:07 +00:00
|
|
|
else:
|
2021-11-29 13:50:16 +00:00
|
|
|
location = str(location)
|
2021-11-25 12:42:00 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"Location {location} for Directory {name} does not exist or is not a directory.")
|
|
|
|
|
2021-11-29 13:50:16 +00:00
|
|
|
@property
|
|
|
|
def completeddir(self):
|
|
|
|
return self.location / "completed"
|
|
|
|
|
|
|
|
def assertCompletedDirectory(self):
|
|
|
|
if not os.path.isdir(self.completeddir):
|
|
|
|
if os.path.isfile(self.completeddir):
|
|
|
|
raise FileExistsError("Cannot create directory %s - path exists but is not a directory!" % str(self.completeddir))
|
|
|
|
|
2021-11-30 06:25:24 +00:00
|
|
|
os.mkdir(self.completeddir)
|
2021-11-29 13:50:16 +00:00
|
|
|
|
2021-11-25 12:42:00 +00:00
|
|
|
def getFiles(self) -> list[File]:
|
|
|
|
"""Get all Files in Directory
|
2021-11-20 14:40:07 +00:00
|
|
|
|
2021-11-26 05:51:03 +00:00
|
|
|
N.B.: Returns only Files that have a size greater than 0
|
|
|
|
|
2021-11-25 12:42:00 +00:00
|
|
|
Returns:
|
2021-11-25 15:31:49 +00:00
|
|
|
list: List of File objects for files within the Directory
|
2021-11-25 12:42:00 +00:00
|
|
|
"""
|
2021-11-26 05:50:07 +00:00
|
|
|
files = [f for f in os.listdir(self.location) if os.path.isfile(
|
|
|
|
self.location / f) and os.path.getsize(self.location / f)]
|
2021-11-25 12:42:00 +00:00
|
|
|
return [File(f, self) for f in files]
|
2021-11-25 15:31:49 +00:00
|
|
|
|
|
|
|
def getFile(self, name: str) -> Optional[File]:
|
|
|
|
"""Get a file in the Directory by name
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): Filename of the File to get
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
File, optional: File object if the file was found, else None
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
return File(name, self)
|
|
|
|
except FileNotFoundError:
|
2021-11-26 05:50:07 +00:00
|
|
|
return None
|