Skip to content

Commit a412fb1

Browse files
author
lb1programmer
committed
More safety checks, should eliminate the rare case where new list is written and only new books are in iBooks, iBooks then removed them.
1 parent 06d634d commit a412fb1

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

book2pad.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,31 @@ def new_plist():
4141
return et.ElementTree(root)
4242

4343
def parse_plist(plist):
44+
#Warning: new_plist() when not new, will wipe out books from device!
4445
if os.path.exists(plist):
4546
tree = et.parse(plist)
4647
else:
48+
#print (os.path.dirname(plist))
49+
#print (os.path.dirname( os.path.dirname( plist ) ) )
4750
if not os.path.isdir(os.path.dirname(plist)):
48-
os.makedirs(os.path.dirname(plist))
49-
tree = new_plist()
51+
if os.path.isdir(os.path.dirname( os.path.dirname( plist ) ) ):
52+
#Does exist, not just flakey connection to device.
53+
os.makedirs(os.path.dirname(plist))
54+
print "Initializing a new booklist!!"
55+
tree = new_plist()
56+
else:
57+
print( "Connection problem? %s" % ((os.path.dirname( os.path.dirname( plist ) ) ), ) )
58+
raise IOError
59+
5060
try:
5161
if not tree.getroot()[0][1].tag == 'array':
5262
raise IndexError
53-
except:
63+
except IndexError:
64+
print "Creating new-plist due to error"
5465
tree = new_plist()
66+
5567
finally:
68+
#If tree = new_plist() here but there were books, iBooks removes them!
5669
return (tree, tree.getroot()[0][1])
5770

5871
class Book(et.Element):
@@ -102,6 +115,8 @@ def addbooks(root_dir, books):
102115
tree = biplist.readPlist(plist)
103116
assert tree.items()[0][0] == 'Books'
104117
array = tree.items()[0][1]
118+
# ^ array of {'Path': '20-python-libraries-you-arent-using-but-should.epub', 'Package Hash': '141ADA5A4CCBA7695C002D0F11F29642', 'Name': '20-python-libraries-you-arent-using-but-should'}
119+
# or similar.
105120
optimized = True #biplist optimized.
106121
print "Optimized plist"
107122
dest = os.path.dirname(plist)
@@ -129,14 +144,21 @@ def addbooks(root_dir, books):
129144
array.append(Book(book))
130145
shutil.copyfile(book, os.path.join(dest, os.path.basename(book)))
131146
elif '.EPUB' == ext:
132-
if optimized:
133-
array.append( optimizedEntry(book) )
134-
else:
135-
array.append(Book(book))
136147
folder = os.path.join(dest, os.path.basename(book))
137-
os.mkdir(folder)
138-
with zipfile.ZipFile(book, "r") as z:
139-
z.extractall(folder)
148+
if os.path.exists( folder ):
149+
# overwriting and adding makes duplicate entry
150+
print "Already exists: %s" % (folder,)
151+
#print "replacing %s" % (folder,)
152+
#shutil.rmtree( folder )
153+
else:
154+
if optimized:
155+
array.append( optimizedEntry(book) )
156+
else:
157+
array.append(Book(book))
158+
159+
os.mkdir(folder)
160+
with zipfile.ZipFile(book, "r") as z:
161+
z.extractall(folder)
140162
else:
141163
fail_list.append(book)
142164
step_len += bar_len

0 commit comments

Comments
 (0)