aif-ng/aif/utils/file_handler.py

57 lines
2.0 KiB
Python
Raw Permalink Normal View History

2019-12-11 04:32:07 -05:00
import os
import pathlib
class File(object):
def __init__(self, file_path):
self.orig_path = file_path
self.fullpath = os.path.abspath(os.path.expanduser(self.orig_path))
self.path_rel = pathlib.PurePosixPath(self.orig_path)
self.path_full = pathlib.PurePosixPath(self.fullpath)
def __str__(self):
2019-12-11 06:33:24 -05:00
return(self.fullpath)
2019-12-11 04:32:07 -05:00
class Directory(object):
def __init__(self, dir_path):
self.orig_path = dir_path
self.fullpath = os.path.abspath(os.path.expanduser(self.orig_path))
self.path_rel = pathlib.PurePosixPath(self.orig_path)
self.path_full = pathlib.PurePosixPath(self.fullpath)
self.files = []
self.dirs = []
2019-12-11 06:33:24 -05:00
def __str__(self):
return(self.fullpath)
def populateFilesDirs(self, recursive = False, native = False):
2019-12-11 04:32:07 -05:00
if not recursive:
for i in os.listdir(self.fullpath):
if os.path.isdir(os.path.join(self.fullpath, i)):
self.dirs.append(i)
elif os.path.isfile(os.path.join(self.fullpath, i)):
2019-12-11 06:33:24 -05:00
if not native:
self.files.append(i)
else:
self.files.append(File(i))
2019-12-11 04:32:07 -05:00
else:
for root, dirs, files in os.walk(self.fullpath):
for f in files:
fpath = os.path.join(root, f)
2019-12-11 06:33:24 -05:00
relfpath = str(pathlib.PurePosixPath(fpath).relative_to(self.path_full))
if not native:
self.files.append(relfpath)
else:
self.files.append(relfpath)
2019-12-11 04:32:07 -05:00
for d in dirs:
dpath = os.path.join(root, d)
2019-12-11 06:33:24 -05:00
reldpath = str(pathlib.PurePosixPath(dpath).relative_to(self.path_full))
2019-12-11 04:32:07 -05:00
self.dirs.append(reldpath)
if root not in self.dirs:
2019-12-11 06:33:24 -05:00
self.dirs.append(root)
if not native:
self.dirs.sort()
self.files.sort()
return(None)