brainzfs.py 3.36 KB
Newer Older
Gerion Entrup's avatar
initial  
Gerion Entrup committed
1
2
#!/usr/bin/env python3

3
import argparse
4
import sys
5
import logging
6

7
8
9
10
#workaround for sqlite pseudo concurrency
#remove it once python supports fully concurrency in sqlite
import sqlitequeue

11
from sqlalchemy import create_engine
12
from sqlalchemy.orm import sessionmaker, scoped_session
Gerion Entrup's avatar
Gerion Entrup committed
13
from sqlalchemy.pool import StaticPool
14
15

from collector import Collector
Gerion Entrup's avatar
Gerion Entrup committed
16
from translator import Translator
17
from walker import Walker
18
from mbdata.models import Base
19
from mbdata import patch_model_schemas, NO_SCHEMAS
20
21

class Main:
22
    """Main class, that starts all other parts of the program"""
Gerion Entrup's avatar
initial  
Gerion Entrup committed
23

24
25
26
27
28
29
    def _init_database(self, dbfile):
        """Initialize the database. Currently only sqlite

        Arguments:
        dbfile -- the path of the database
        """
30
31
        database = 'sqlite:///'
        engine = create_engine(database + dbfile,
32
33
34
                               connect_args={'check_same_thread':False},
                               #echo = True,
                               poolclass=StaticPool)
35
36
        patch_model_schemas(NO_SCHEMAS)
        Base.metadata.create_all(engine)
37
        session_factory = sessionmaker(bind=engine)
38
        session = scoped_session(session_factory)
39
40
41
        if database == 'sqlite:///':
            #dirty workaroud for sqlite non concurrent mode in python
            #remove it once python supports fully concurrency in sqlite
42
            self.session = sqlitequeue.get_Session(session)
43
        else:
44
            self.session = session
45

46
    def main(self, args):
47
48
49
50
51
52
53
        """Main method. Parses cmdline arguments and starts the program."""
        version = "0.1-alpha"
        parser = argparse.ArgumentParser(add_help=True,
                                         description="Create a directory structure of music with" \
                                                     "symlinks based on musicbrainz data")
        parser.add_argument('--version', '-V', action='version',
                            version='%(prog)s {0}'.format(version))
54
55
        parser.add_argument('sourcedir', help="filepath of source file directory")
        parser.add_argument('mountpoint', help="where it should be mounted")
56
57
        parser.add_argument('--verbose', '-v', action="store_true", default=False,
                            help="change to loglevel to info")
58
        parser.add_argument('--database', '-d', default="music.db", help="path of the database")
59
        parser.add_argument('--logfile', '-l', help="log into a logfile")
Gerion Entrup's avatar
initial  
Gerion Entrup committed
60

61
        arg = parser.parse_args(args[1:])
62
63
64
65
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(levelname)-5s %(name)-18s %(message)s',
                            datefmt='%y-%m-%d %H:%M')
        logger = logging.getLogger('main')
Gerion Entrup's avatar
initial  
Gerion Entrup committed
66

67
        self._init_database(arg.database)
68
        logger.info("database initialized")
69

70
71
        collector = Collector(self.session)
        collector.start()
Gerion Entrup's avatar
initial  
Gerion Entrup committed
72

73
74
        walker = Walker(self.session, arg.sourcedir)
        walker.start()
75

76
77
78
        #from mbdata.models import Recording, Release, ReleaseGroup, Track, Artist, ArtistCredit, ArtistCreditName, Medium
        #from sqlalchemy.sql import select
        #se = self.session()
79
80
81
        #from utils import debug_breakpoint
        #debug_breakpoint()

82
83
        translator = Translator(self.session, arg.mountpoint)
        translator.start()
Gerion Entrup's avatar
Gerion Entrup committed
84

85
86
        collector.join()
        translator.join()
Gerion Entrup's avatar
Gerion Entrup committed
87

Gerion Entrup's avatar
initial  
Gerion Entrup committed
88
if __name__ == "__main__":
89
    Main().main(sys.argv)