-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.py
63 lines (50 loc) · 1.72 KB
/
links.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
from models import Event, Tag, MediaList, Media, MediaCollection
class LinkCreator(object):
def __init__(self, tagsPath, eventsPath, dryRun, logger):
super(LinkCreator, self).__init__()
self._tagsPath = tagsPath
self._eventsPath = eventsPath
self._dryRun = dryRun
self._logger = logger
self._dirsCreated = 0
self._linksCreated = 0
def createLinks(self, collection: MediaCollection):
self._dirsCreated = 0
self._linksCreated = 0
for tag in collection.tags:
self._processMediaList(tag, self._getTagPath(tag))
for event in collection.events:
self._processMediaList(event, self._getEventPath(event))
def dirsCreated(self):
return self._dirsCreated
def linksCreated(self):
return self._linksCreated
def _processMediaList(self, mediaList: MediaList, path):
self._ensurePath(path, mediaList)
for media in mediaList.objects:
self._createLink(media, path)
def _createLink(self, media: Media, linkPath):
fileName = os.path.basename(media.path)
linkPath = os.path.join(linkPath, fileName)
pathExists = os.path.exists(linkPath)
self._logger.logLinkCreation(media, linkPath, pathExists)
if not pathExists:
self._linksCreated += 1
if not self._dryRun:
os.link(media.path, linkPath)
def _ensurePath(self, path, mediaList: MediaList):
pathExists = os.path.exists(path)
self._logger.logPathCreation(mediaList, path, pathExists)
if not pathExists:
self._dirsCreated += 1
if not self._dryRun:
os.makedirs(path)
def _getTagPath(self, tag: Tag):
tagName = tag.name
if tagName.startswith("/"):
return self._tagsPath + tagName
else:
return os.path.join(self._tagsPath, tagName)
def _getEventPath(self, event: Event):
return os.path.join(self._eventsPath, event.name)