Permalink
Browse files

scanner: Move main() function from bin/ to mopidy.scanner

  • Loading branch information...
1 parent 8a440ff commit 8a292bce58f5c95c42a2c09e59dc4002b7abf830 @jodal jodal committed Nov 14, 2012
Showing with 42 additions and 52 deletions.
  1. +4 −47 bin/mopidy-scan
  2. +38 −5 mopidy/scanner.py
View
51 bin/mopidy-scan
@@ -1,48 +1,5 @@
-#!/usr/bin/env python
+#! /usr/bin/env python
-from __future__ import unicode_literals
-
-import sys
-import logging
-
-from mopidy import settings
-from mopidy.utils.log import setup_console_logging, setup_root_logger
-from mopidy.scanner import Scanner, translator
-from mopidy.frontends.mpd.translator import tracks_to_tag_cache_format
-
-
-setup_root_logger()
-setup_console_logging(2)
-
-
-tracks = []
-
-
-def store(data):
- track = translator(data)
- tracks.append(track)
- logging.debug('Added %s', track.uri)
-
-
-def debug(uri, error, debug):
- logging.error('Failed %s: %s - %s', uri, error, debug)
-
-
-logging.info('Scanning %s', settings.LOCAL_MUSIC_PATH)
-
-
-scanner = Scanner(settings.LOCAL_MUSIC_PATH, store, debug)
-try:
- scanner.start()
-except KeyboardInterrupt:
- scanner.stop()
-
-
-logging.info('Done')
-
-
-for a in tracks_to_tag_cache_format(tracks):
- if len(a) == 1:
- print ('%s' % a).encode('utf-8')
- else:
- print ('%s: %s' % a).encode('utf-8')
+if __name__ == '__main__':
+ from mopidy.scanner import main
+ main()
View
43 mopidy/scanner.py
@@ -1,16 +1,49 @@
from __future__ import unicode_literals
+import logging
+import datetime
+
import gobject
gobject.threads_init()
import pygst
pygst.require('0.10')
import gst
-import datetime
-
-from mopidy.utils.path import path_to_uri, find_files
+from mopidy import settings
+from mopidy.frontends.mpd import translator as mpd_translator
from mopidy.models import Track, Artist, Album
+from mopidy.utils import log, path
+
+
+def main():
+ log.setup_root_logger()
+ log.setup_console_logging(2)
+
+ tracks = []
+
+ def store(data):
+ track = translator(data)
+ tracks.append(track)
+ logging.debug('Added %s', track.uri)
+
+ def debug(uri, error, debug):
+ logging.error('Failed %s: %s - %s', uri, error, debug)
+
+ logging.info('Scanning %s', settings.LOCAL_MUSIC_PATH)
+ scanner = Scanner(settings.LOCAL_MUSIC_PATH, store, debug)
+ try:
+ scanner.start()
+ except KeyboardInterrupt:
+ scanner.stop()
+
+ logging.info('Done')
+
+ for row in mpd_translator.tracks_to_tag_cache_format(tracks):
+ if len(row) == 1:
+ print ('%s' % row).encode('utf-8')
+ else:
+ print ('%s: %s' % row).encode('utf-8')
def translator(data):
@@ -56,7 +89,7 @@ def _retrieve(source_key, target_key, target):
class Scanner(object):
def __init__(self, folder, data_callback, error_callback=None):
- self.files = find_files(folder)
+ self.files = path.find_files(folder)
self.data_callback = data_callback
self.error_callback = error_callback
self.loop = gobject.MainLoop()
@@ -119,7 +152,7 @@ def get_duration(self):
def next_uri(self):
try:
- uri = path_to_uri(self.files.next())
+ uri = path.path_to_uri(self.files.next())
except StopIteration:
self.stop()
return False

0 comments on commit 8a292bc

Please sign in to comment.