Skip to content

Commit ce80924

Browse files
committed
Code cleanup and a couple of silly bug fixes
1 parent fb3cabb commit ce80924

File tree

3 files changed

+36
-58
lines changed

3 files changed

+36
-58
lines changed

lycheeupload.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def parse_arguments():
9898

9999
def parse_server_string(server_string):
100100
"""
101-
Parse a server string user@host:path and store values in the global configuration
102-
:param server_string:
101+
Parse a server string and store values in the global configuration
102+
:param server_string: Server string in the form of user@host:path
103103
:return: True if successful, False if parsing fails
104104
"""
105105
match = re.match("(.+)@([\w\d\.]+):(.+)", server_string)
@@ -122,6 +122,8 @@ def parse_mac_arguments(args):
122122
"""
123123
conf.originals = args.originals
124124

125+
library_dir = None
126+
125127
if args.iphoto:
126128
conf.source = "iPhoto"
127129
library_dir = args.iphoto

photo.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,13 @@ def __str__(self):
5757

5858
class LycheePhoto:
5959
"""
60-
Use to store photo data
60+
Store photo data such as location and file info, EXIF data, checksums and path to thumbnails. Checksum and
61+
thumbnails are generated here.
6162
"""
6263

6364
SMALL_THUMB_SIZE = (200, 200)
6465
BIG_THUMB_SIZE = (400, 400)
6566

66-
"""
67-
originalname = "" # import_name
68-
originalpath = ""
69-
id = ""
70-
albumname = ""
71-
albumid = ""
72-
thumbnailfullpath = ""
73-
thumbnailx2fullpath = ""
74-
title = ""
75-
description = ""
76-
url = ""
77-
public = 0 # private by default
78-
type = ""
79-
width = None
80-
height = None
81-
size = None
82-
star = 0 # no star by default
83-
thumb2xUrl = ""
84-
srcfullpath = ""
85-
destfullpath = ""
86-
exif = None
87-
datetime = None
88-
checksum = None
89-
"""
9067

9168
def __init__(self, full_path, album_id):
9269
logger.setLevel(conf.verbose)
@@ -202,10 +179,13 @@ def generateThumbnail(self, res):
202179

203180
def cleanup(self):
204181
"""
205-
Delete thumbnail files
182+
Delete thumbnail files from the local disk. Called after the photo was successfully uploaded.
206183
"""
207-
os.remove(self.thumbnailfullpath)
208-
os.remove(self.thumbnailx2fullpath)
184+
try:
185+
os.remove(self.thumbnailfullpath)
186+
os.remove(self.thumbnailx2fullpath)
187+
except Exception:
188+
logger.error("Cannot delete thumbnail {}".format(self.thumbnailfullpath), exc_info=True)
209189

210190

211191
def rotatephoto(self, photo, rotation):

upload.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
class Upload:
1515
"""
16-
This class contains the logic behind this program
17-
It consist mainly in filesystem operations
18-
It relies on:
19-
- LycheeDAO for dtabases operations
20-
- LycheePhoto to store (and compute) photos propreties
16+
High-level logic for uploading images to the remote server. The class is responsible for initiating SSH and
17+
database connections, checking whether albums exist and creating if needed and uploading photos to appropriate
18+
albums.
2119
"""
2220

2321
def __init__(self):
@@ -30,18 +28,12 @@ def __init__(self):
3028
raise Exception("Lychee configuration file not found. Please check the path to Lychee installation")
3129

3230

33-
def albumExists(self, album):
34-
"""
35-
Takes an album properties list as input. At least the relpath sould be specified (relative albumpath)
36-
Returns an albumid or None if album does not exists
37-
"""
3831

3932
def createAlbum(self, album_name):
4033
"""
41-
Creates an album
42-
Inputs:
43-
- album: an album properties list. at least path should be specified (relative albumpath)
44-
Returns an albumid or None if album does not exists
34+
Create a new album
35+
:param album_name: The name of an album to create
36+
:return: Album id. None if the album cannot be created
4537
"""
4638
album_id = None
4739
if album_name != "":
@@ -52,10 +44,11 @@ def createAlbum(self, album_name):
5244

5345
def uploadPhoto(self, photo):
5446
"""
55-
add a file to an album, the albumid must be previously stored in the LycheePhoto parameter
56-
Parameters:
57-
- photo: a valid LycheePhoto object
58-
Returns True if everything went ok
47+
Upload a photo to the remote server and add it to the album it belongs to in the database. If database update
48+
fails for some reason, photos are deleted from the server.
49+
50+
:param photo: a valid LycheePhoto object
51+
:return: True if everything went ok
5952
"""
6053
album_name = os.path.dirname(photo.srcfullpath).split(os.sep)[-1]
6154
file_name = os.path.basename(photo.srcfullpath)
@@ -91,10 +84,7 @@ def uploadPhoto(self, photo):
9184
def deleteFiles(self, filelist):
9285
"""
9386
Delete files in the Lychee file tree (uploads/big and uploads/thumbnails)
94-
Give it the file name and it will delete relatives files and thumbnails
95-
Parameters:
96-
- filelist: a list of filenames
97-
Returns nothing
87+
:param filelist: a list of filenames to delte
9888
"""
9989

10090
for url in filelist:
@@ -111,22 +101,28 @@ def deleteFiles(self, filelist):
111101

112102

113103
def upload(self, albums):
104+
"""
105+
Upload photos stored in the provided dictionary, create albums. Accept a dictionary of album names and image
106+
paths as input parameter and convert each image path into a LycheePhoto object, which is passed to the
107+
uploadPhoto funciton.
108+
:param albums: a dictionary with albums and path names
109+
"""
114110
print("Uploading photos...")
115111

116112
createdalbums, discoveredphotos, importedphotos = 0, 0, 0
117113

118-
for album, files in albums.items():
114+
for album_name, files in albums.items():
119115
album_date = None
120-
if album == "{unsorted}":
116+
if album_name == "{unsorted}":
121117
album_id = 0
122118
else:
123-
album_id = self.dao.albumExists(album)
119+
album_id = self.dao.albumExists(album_name)
124120

125121
if album_id is None: # create album
126-
album_id = self.createAlbum(album)
122+
album_id = self.createAlbum(album_name)
127123
createdalbums += 1
128124
elif conf.replace: # drop album photos
129-
filelist = self.dao.eraseAlbum(album)
125+
filelist = self.dao.eraseAlbum(album_id)
130126
self.deleteFiles(filelist)
131127

132128
for full_path in files:
@@ -141,7 +137,7 @@ def upload(self, albums):
141137
importedphotos += 1
142138
else:
143139
file_name = os.path.basename(photo.srcfullpath)
144-
logger.info("Photo {}/{} already exists".format(album, file_name))
140+
logger.info("Photo {}/{} already exists".format(album_name, file_name))
145141

146142
if album_id: # set correct album date
147143
self.dao.updateAlbumDate(album_id, album_date)

0 commit comments

Comments
 (0)