mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Implement directory recursion, and type filtering
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
books/*
|
books/*
|
||||||
|
data/shelf.json
|
||||||
|
*.pyc
|
||||||
Binary file not shown.
21
library.py
21
library.py
@@ -9,6 +9,7 @@ config = Config()
|
|||||||
class Catalogue:
|
class Catalogue:
|
||||||
"""Decodes and stores book information"""
|
"""Decodes and stores book information"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.file_list = []
|
||||||
with open(config.book_shelf, 'r') as f:
|
with open(config.book_shelf, 'r') as f:
|
||||||
try:
|
try:
|
||||||
self.catalogue = json.load(f)
|
self.catalogue = json.load(f)
|
||||||
@@ -17,24 +18,26 @@ class Catalogue:
|
|||||||
with open(config.book_shelf, 'w') as f:
|
with open(config.book_shelf, 'w') as f:
|
||||||
json.dump(self.filter_books(), f)
|
json.dump(self.filter_books(), f)
|
||||||
|
|
||||||
def scan_folder(self):
|
def scan_folder(self, folder=config.book_path):
|
||||||
file_list = []
|
for f in os.listdir(folder):
|
||||||
for f in os.listdir(config.book_path):
|
_path = os.path.abspath(folder+'/'+f)
|
||||||
file_list.append(f)
|
#_path = os.path.abspath('.')+'/'+folder+f+'/'
|
||||||
return file_list
|
_is_dir = os.path.isdir(_path.strip()+'/')
|
||||||
|
if _is_dir:
|
||||||
|
self.file_list.append(self.scan_folder(_path))
|
||||||
|
self.file_list.append(_path)
|
||||||
|
|
||||||
def filter_books(self):
|
def filter_books(self):
|
||||||
scan = self.scan_folder()
|
scan = self.scan_folder()
|
||||||
breakpoint()
|
|
||||||
regx = re.compile(r"\.epub")
|
regx = re.compile(r"\.epub")
|
||||||
self.books = list(filter(regx.search, scan))
|
self.books = list(filter(regx.search, filter(None, self.file_list)))
|
||||||
return self.books
|
with open(config.book_shelf, 'w') as f:
|
||||||
|
json.dump(self.books, f)
|
||||||
|
|
||||||
def compare_shelf_current(self):
|
def compare_shelf_current(self):
|
||||||
try:
|
try:
|
||||||
self.books
|
self.books
|
||||||
except Exception:
|
except Exception:
|
||||||
self.filter_books()
|
self.filter_books()
|
||||||
breakpoint()
|
|
||||||
unique = set(self.books) - set(self.catalogue)
|
unique = set(self.books) - set(self.catalogue)
|
||||||
return unique
|
return unique
|
||||||
|
|||||||
Binary file not shown.
@@ -7,8 +7,8 @@ class Testing(unittest.TestCase):
|
|||||||
def test_libray_catalogue(self):
|
def test_libray_catalogue(self):
|
||||||
self.assertIsNotNone(Catalogue())
|
self.assertIsNotNone(Catalogue())
|
||||||
|
|
||||||
def test_library_catalogue_scan_folder(self):
|
def test_library_catalogue_filter_books(self):
|
||||||
self.assertIsInstance(Catalogue().scan_folder(), object)
|
self.assertIsNotNone(Catalogue().filter_books())
|
||||||
|
|
||||||
def test_library_catalogue_compare_shelf_current(self):
|
def test_library_catalogue_compare_shelf_current(self):
|
||||||
self.assertIsInstance(Catalogue().compare_shelf_current(), Catalogue)
|
self.assertIsInstance(Catalogue().compare_shelf_current(), Catalogue)
|
||||||
|
|||||||
Reference in New Issue
Block a user