plumber.py 1.85 KB
Newer Older
1
2
import os.path

3
from collector import Paths, DefectData, DEFECT, UPDATE
4
5
6
7
8
9
10
11
12
13
14
15
16

class Plumber(threading.Thread):
    """Check and find out how to repair the database."""

    def __init__(self, session_fac):
        super().__init__(target=self)
        self._session = session_fac()
        self.log = logging.getLogger('plumber')

    def run(self):
        check_recordings()
        check_artist_credits()
        check_artist_credit_names()
17
18
19
        check_artists()
        check_release_groups()
        check_releases()
20
21
        check_tracks()
        check_mediums()
22
23

    def check_entitys(self, table, check_fail, check_defect=lambda x: False):
24
        for entity in self._session.query(table).all():
25
            #clean file if necessary
26
            if check_fail(entity) or (hasattr(entity, 'gid') and mbid_error(entity.gid)):
27
28
29
30
31
32
                self._session.delete(entity)

            #check for defect
            if check_defect(entity):
                Paths.put((DEFECT, DefectData(mbid=entity.gid, table=table)))

33
            check_update_time(entity)
34

35
36
37
38
    def check_recordings(self):
        check_entitys(Recording,
                lambda x: not os.path.isfile(x.path),
                check_defect=lambda x: x.artist_credit == None)
39

40
41
42
    def check_artist_credits(self):
        check_entitys(ArtistCredit,
                lambda ac: self._session.query(Recording).filter(ac == Recording.artist_credit).first() == None)
43

44
45
46
47
48
49
50
51
    def check_artist_credit_names(self):
        pass

    def check_artists(self):
        pass

    def check_release_groups(self):
        pass
52

53
54
    def check_releases(self):
        pass
55

56
57
    def check_tracks(self):
        pass
58

59
    def check_mediums(self):
60
61
        pass

62
    def mbid_error(mbid):
63
64
        return False

65
    def check_update_time(entity):
66
67
        print("last_updated:")
        print(entity.last_updated)
68
        print(type(entity.last_updated))
69