<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> from beets import ui , util , library , config <EOL> from beets . plugins import BeetsPlugin <EOL> import subprocess <EOL> import shutil <EOL> import os <EOL> import tempfile <EOL> class IPFSPlugin ( BeetsPlugin ) : <EOL> def __init__ ( self ) : <EOL> super ( IPFSPlugin , self ) . __init__ ( ) <EOL> self . config . add ( { <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . import_stages = [ self . auto_add ] <EOL> def commands ( self ) : <EOL> cmd = ui . Subcommand ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> cmd . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> def func ( lib , opts , args ) : <EOL> if opts . add : <EOL> for album in lib . albums ( ui . decargs ( args ) ) : <EOL> if len ( album . items ( ) ) == <NUM_LIT:0> : <EOL> self . _log . info ( '<STR_LIT>' , <EOL> album ) <EOL> self . ipfs_add ( album ) <EOL> album . store ( ) <EOL> if opts . get : <EOL> self . ipfs_get ( lib , ui . decargs ( args ) ) <EOL> if opts . publish : <EOL> self . ipfs_publish ( lib ) <EOL> if opts . _import : <EOL> self . ipfs_import ( lib , ui . decargs ( args ) ) <EOL> if opts . _list : <EOL> self . ipfs_list ( lib , ui . decargs ( args ) ) <EOL> if opts . play : <EOL> self . ipfs_play ( lib , opts , ui . decargs ( args ) ) <EOL> cmd . func = func <EOL> return [ cmd ] <EOL> def auto_add ( self , session , task ) : <EOL> if task . is_album : <EOL> if self . ipfs_add ( task . album ) : <EOL> task . album . store ( ) <EOL> def ipfs_play ( self , lib , opts , args ) : <EOL> from beetsplug . play import PlayPlugin <EOL> jlib = self . get_remote_lib ( lib ) <EOL> player = PlayPlugin ( ) <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = None <EOL> player . album = True <EOL> player . play_music ( jlib , player , args ) <EOL> def ipfs_add ( self , album ) : <EOL> try : <EOL> album_dir = album . item_dir ( ) <EOL> except AttributeError : <EOL> return False <EOL> try : <EOL> if album . ipfs : <EOL> self . _log . debug ( '<STR_LIT>' , album_dir ) <EOL> return False <EOL> except AttributeError : <EOL> pass <EOL> self . _log . info ( '<STR_LIT>' , album_dir ) <EOL> cmd = "<STR_LIT>" . split ( ) <EOL> cmd . append ( album_dir ) <EOL> try : <EOL> output = util . command_output ( cmd ) . split ( ) <EOL> except ( OSError , subprocess . CalledProcessError ) as exc : <EOL> self . _log . error ( u'<STR_LIT>' , album_dir , exc ) <EOL> return False <EOL> length = len ( output ) <EOL> for linenr , line in enumerate ( output ) : <EOL> line = line . strip ( ) <EOL> if linenr == length - <NUM_LIT:1> : <EOL> self . _log . info ( "<STR_LIT>" , line ) <EOL> album . ipfs = line <EOL> else : <EOL> try : <EOL> item = album . items ( ) [ linenr ] <EOL> self . _log . info ( "<STR_LIT>" , line ) <EOL> item . ipfs = line <EOL> item . store ( ) <EOL> except IndexError : <EOL> pass <EOL> return True <EOL> def ipfs_get ( self , lib , query ) : <EOL> query = query [ <NUM_LIT:0> ] <EOL> if query . startswith ( "<STR_LIT>" ) and len ( query ) == <NUM_LIT> : <EOL> self . ipfs_get_from_hash ( lib , query ) <EOL> else : <EOL> albums = self . query ( lib , query ) <EOL> for album in albums : <EOL> self . ipfs_get_from_hash ( lib , album . ipfs ) <EOL> def ipfs_get_from_hash ( self , lib , _hash ) : <EOL> try : <EOL> cmd = "<STR_LIT>" . split ( ) <EOL> cmd . append ( _hash ) <EOL> util . command_output ( cmd ) <EOL> except ( OSError , subprocess . CalledProcessError ) as err : <EOL> self . _log . error ( '<STR_LIT>' , <EOL> _hash , err . output ) <EOL> return False <EOL> self . _log . info ( '<STR_LIT>' , _hash ) <EOL> imp = ui . commands . TerminalImportSession ( lib , loghandler = None , <EOL> query = None , paths = [ _hash ] ) <EOL> imp . run ( ) <EOL> shutil . rmtree ( _hash ) <EOL> def ipfs_publish ( self , lib ) : <EOL> with tempfile . NamedTemporaryFile ( ) as tmp : <EOL> self . ipfs_added_albums ( lib , tmp . name ) <EOL> try : <EOL> cmd = "<STR_LIT>" . split ( ) <EOL> cmd . append ( tmp . name ) <EOL> output = util . command_output ( cmd ) <EOL> except ( OSError , subprocess . CalledProcessError ) as err : <EOL> msg = "<STR_LIT>" . format ( err ) <EOL> self . _log . error ( msg ) <EOL> return False <EOL> self . _log . info ( "<STR_LIT>" , output ) <EOL> def ipfs_import ( self , lib , args ) : <EOL> _hash = args [ <NUM_LIT:0> ] <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> lib_name = args [ <NUM_LIT:1> ] <EOL> else : <EOL> lib_name = _hash <EOL> lib_root = os . path . dirname ( lib . path ) <EOL> remote_libs = lib_root + "<STR_LIT>" <EOL> if not os . path . exists ( remote_libs ) : <EOL> try : <EOL> os . makedirs ( remote_libs ) <EOL> except OSError as e : <EOL> msg = "<STR_LIT>" . format ( remote_libs , e ) <EOL> self . _log . error ( msg ) <EOL> return False <EOL> path = remote_libs + "<STR_LIT:/>" + lib_name + "<STR_LIT>" <EOL> if not os . path . exists ( path ) : <EOL> cmd = "<STR_LIT>" . format ( _hash ) . split ( ) <EOL> cmd . append ( path ) <EOL> try : <EOL> util . command_output ( cmd ) <EOL> except ( OSError , subprocess . CalledProcessError ) : <EOL> self . _log . error ( "<STR_LIT>" . format ( _hash ) ) <EOL> return False <EOL> jpath = remote_libs + "<STR_LIT>" <EOL> jlib = library . Library ( jpath ) <EOL> nlib = library . Library ( path ) <EOL> for album in nlib . albums ( ) : <EOL> if not self . already_added ( album , jlib ) : <EOL> new_album = [ ] <EOL> for item in album . items ( ) : <EOL> item . id = None <EOL> new_album . append ( item ) <EOL> added_album = jlib . add_album ( new_album ) <EOL> added_album . ipfs = album . ipfs <EOL> added_album . store ( ) <EOL> def already_added ( self , check , jlib ) : <EOL> for jalbum in jlib . albums ( ) : <EOL> if jalbum . mb_albumid == check . mb_albumid : <EOL> return True <EOL> return False <EOL> def ipfs_list ( self , lib , args ) : <EOL> fmt = config [ '<STR_LIT>' ] . get ( ) <EOL> try : <EOL> albums = self . query ( lib , args ) <EOL> except IOError : <EOL> ui . print_ ( "<STR_LIT>" ) <EOL> return <EOL> for album in albums : <EOL> ui . print_ ( format ( album , fmt ) , "<STR_LIT>" , album . ipfs ) <EOL> def query ( self , lib , args ) : <EOL> rlib = self . get_remote_lib ( lib ) <EOL> albums = rlib . albums ( args ) <EOL> return albums <EOL> def get_remote_lib ( self , lib ) : <EOL> lib_root = os . path . dirname ( lib . path ) <EOL> remote_libs = lib_root + "<STR_LIT>" <EOL> path = remote_libs + "<STR_LIT>" <EOL> if not os . path . isfile ( path ) : <EOL> raise IOError <EOL> return library . Library ( path ) <EOL> def ipfs_added_albums ( self , rlib , tmpname ) : <EOL> """<STR_LIT>""" <EOL> tmplib = library . Library ( tmpname ) <EOL> for album in rlib . albums ( ) : <EOL> try : <EOL> if album . ipfs : <EOL> self . create_new_album ( album , tmplib ) <EOL> except AttributeError : <EOL> pass <EOL> return tmplib <EOL> def create_new_album ( self , album , tmplib ) : <EOL> items = [ ] <EOL> for item in album . items ( ) : <EOL> try : <EOL> if not item . ipfs : <EOL> break <EOL> except AttributeError : <EOL> pass <EOL> item . path = '<STR_LIT>' . format ( album . ipfs , <EOL> os . path . basename ( item . path ) ) <EOL> item . id = None <EOL> items . append ( item ) <EOL> if len ( items ) < <NUM_LIT:1> : <EOL> return False <EOL> self . _log . info ( "<STR_LIT>" , album ) <EOL> new_album = tmplib . add_album ( items ) <EOL> new_album . ipfs = album . ipfs <EOL> new_album . store ( ) </s>
