brainzfs.py 3.42 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
19
20
from mbdata.utils import patch_model_schemas, NO_SCHEMAS
from model import patch_recording, Base

21
22

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

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

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

48
    def main(self, args):
49
50
51
52
53
54
55
        """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))
56
57
        parser.add_argument('sourcedir', help="filepath of source file directory")
        parser.add_argument('mountpoint', help="where it should be mounted")
58
59
        parser.add_argument('--verbose', '-v', action="store_true", default=False,
                            help="change to loglevel to info")
60
        parser.add_argument('--database', '-d', default="music.db", help="path of the database")
61
        parser.add_argument('--logfile', '-l', help="log into a logfile")
Gerion Entrup's avatar
initial  
Gerion Entrup committed
62

63
        arg = parser.parse_args(args[1:])
64
65
66
67
        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
68

69
        self._init_database(arg.database)
70
        logger.info("database initialized")
71

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

75
76
        walker = Walker(self.session, arg.sourcedir)
        walker.start()
77

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

85
86
        translator = Translator(self.session, arg.mountpoint)
        translator.start()
Gerion Entrup's avatar
Gerion Entrup committed
87

88
89
        collector.join()
        translator.join()
Gerion Entrup's avatar
Gerion Entrup committed
90

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