@@ -41,18 +41,31 @@ def new_plist():
41
41
return et .ElementTree (root )
42
42
43
43
def parse_plist (plist ):
44
+ #Warning: new_plist() when not new, will wipe out books from device!
44
45
if os .path .exists (plist ):
45
46
tree = et .parse (plist )
46
47
else :
48
+ #print (os.path.dirname(plist))
49
+ #print (os.path.dirname( os.path.dirname( plist ) ) )
47
50
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
+
50
60
try :
51
61
if not tree .getroot ()[0 ][1 ].tag == 'array' :
52
62
raise IndexError
53
- except :
63
+ except IndexError :
64
+ print "Creating new-plist due to error"
54
65
tree = new_plist ()
66
+
55
67
finally :
68
+ #If tree = new_plist() here but there were books, iBooks removes them!
56
69
return (tree , tree .getroot ()[0 ][1 ])
57
70
58
71
class Book (et .Element ):
@@ -102,6 +115,8 @@ def addbooks(root_dir, books):
102
115
tree = biplist .readPlist (plist )
103
116
assert tree .items ()[0 ][0 ] == 'Books'
104
117
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.
105
120
optimized = True #biplist optimized.
106
121
print "Optimized plist"
107
122
dest = os .path .dirname (plist )
@@ -129,14 +144,21 @@ def addbooks(root_dir, books):
129
144
array .append (Book (book ))
130
145
shutil .copyfile (book , os .path .join (dest , os .path .basename (book )))
131
146
elif '.EPUB' == ext :
132
- if optimized :
133
- array .append ( optimizedEntry (book ) )
134
- else :
135
- array .append (Book (book ))
136
147
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 )
140
162
else :
141
163
fail_list .append (book )
142
164
step_len += bar_len
0 commit comments