]> git.phdru.name Git - m_librarian.git/blob - scripts/ml-import.py
Feat(glst): Report imported glst counter
[m_librarian.git] / scripts / ml-import.py
1 #! /usr/bin/env python
2 from __future__ import print_function
3
4 import argparse
5 import sys
6
7 from m_librarian.config import get_config
8 from m_librarian.db import open_db, init_db, update_counters
9 from m_librarian.glst import import_glst
10 from m_librarian.inp import import_inpx
11 from m_librarian.pbar import ttyProgressBar
12 if ttyProgressBar:
13     from m_librarian.pbar import ml_ttyProgressBar
14
15 if __name__ == '__main__':
16     parser = argparse.ArgumentParser(description='Import')
17     parser.add_argument('-C', '--config', help='configuration file')
18     parser.add_argument('-D', '--database', help='database URI')
19     parser.add_argument('-P', '--no-pbar', action='store_true',
20                         help='inhibit progress bar')
21     parser.add_argument('inpx', nargs='+', help='INPX files to import')
22     args = parser.parse_args()
23
24     if args.config:
25         get_config(args.config)  # Get and cache config file
26
27     open_db(args.database)
28     init_db()
29
30     count_old, count_new = import_glst()
31     if count_old:
32         print("Imported %d genres (ignored %d existing)" % (
33             count_new, count_old))
34     else:
35         print("Imported %d genres" % count_new)
36
37     use_pbar = ttyProgressBar and not args.no_pbar and sys.stdout.isatty()
38     for inpx in args.inpx:
39         if use_pbar:
40             if len(inpx) > 25:
41                 pbar_fname = inpx[:22] + '...'
42             else:
43                 pbar_fname = inpx
44             print(pbar_fname.ljust(20), end=': ')
45             sys.stdout.flush()
46             pbar = ml_ttyProgressBar()
47             import_inpx(inpx, pbar_cb=pbar)
48             print("Ok")
49         else:
50             import_inpx(inpx)
51     if use_pbar:
52         print("Updating counters", end=': ')
53         sys.stdout.flush()
54         pbar = ml_ttyProgressBar()
55         update_counters(pbar_cb=pbar)
56         print("Ok")
57     else:
58         update_counters()