<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> import locale <EOL> import optparse <EOL> import textwrap <EOL> import sys <EOL> from difflib import SequenceMatcher <EOL> import sqlite3 <EOL> import errno <EOL> import re <EOL> import struct <EOL> import traceback <EOL> import os . path <EOL> from beets import logging <EOL> from beets import library <EOL> from beets import plugins <EOL> from beets import util <EOL> from beets . util . functemplate import Template <EOL> from beets import config <EOL> from beets . util import confit <EOL> from beets . autotag import mb <EOL> from beets . dbcore import query as db_query <EOL> if sys . platform == b'<STR_LIT:win32>' : <EOL> try : <EOL> import colorama <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> colorama . init ( ) <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> if not log . handlers : <EOL> log . addHandler ( logging . StreamHandler ( ) ) <EOL> log . propagate = False <EOL> PF_KEY_QUERIES = { <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> '<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> class UserError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def _in_encoding ( default = u'<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> return config [ '<STR_LIT>' ] . get ( ) or getattr ( sys . stdin , '<STR_LIT>' , <EOL> default ) <EOL> def _out_encoding ( ) : <EOL> """<STR_LIT>""" <EOL> encoding = config [ '<STR_LIT>' ] . get ( ) <EOL> if encoding : <EOL> return encoding <EOL> if not hasattr ( sys . stdout , '<STR_LIT>' ) : <EOL> return '<STR_LIT:utf8>' <EOL> return sys . stdout . encoding or '<STR_LIT:utf8>' <EOL> def _arg_encoding ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return locale . getdefaultlocale ( ) [ <NUM_LIT:1> ] or '<STR_LIT:utf8>' <EOL> except ValueError : <EOL> return '<STR_LIT:utf8>' <EOL> def decargs ( arglist ) : <EOL> """<STR_LIT>""" <EOL> return [ s . decode ( _arg_encoding ( ) ) for s in arglist ] <EOL> def print_ ( * strings , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> end = kwargs . get ( '<STR_LIT:end>' ) <EOL> if not strings or isinstance ( strings [ <NUM_LIT:0> ] , unicode ) : <EOL> txt = u'<STR_LIT:U+0020>' . join ( strings ) <EOL> txt += u'<STR_LIT:\n>' if end is None else end <EOL> else : <EOL> txt = b'<STR_LIT:U+0020>' . join ( strings ) <EOL> txt += b'<STR_LIT:\n>' if end is None else end <EOL> if isinstance ( txt , unicode ) : <EOL> txt = txt . encode ( _out_encoding ( ) , '<STR_LIT:replace>' ) <EOL> sys . stdout . write ( txt ) <EOL> def _bool_fallback ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if a is None : <EOL> assert isinstance ( b , bool ) <EOL> return b <EOL> else : <EOL> assert isinstance ( a , bool ) <EOL> return a <EOL> def should_write ( write_opt = None ) : <EOL> """<STR_LIT>""" <EOL> return _bool_fallback ( write_opt , config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( bool ) ) <EOL> def should_move ( move_opt = None ) : <EOL> """<STR_LIT>""" <EOL> return _bool_fallback ( <EOL> move_opt , <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( bool ) or <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( bool ) <EOL> ) <EOL> def input_ ( prompt = None ) : <EOL> """<STR_LIT>""" <EOL> if prompt : <EOL> print_ ( prompt , end = '<STR_LIT:U+0020>' ) <EOL> try : <EOL> resp = raw_input ( ) <EOL> except EOFError : <EOL> raise UserError ( u'<STR_LIT>' ) <EOL> return resp . decode ( _in_encoding ( ) , '<STR_LIT:ignore>' ) <EOL> def input_options ( options , require = False , prompt = None , fallback_prompt = None , <EOL> numrange = None , default = None , max_width = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> letters = { } <EOL> display_letters = [ ] <EOL> capitalized = [ ] <EOL> first = True <EOL> for option in options : <EOL> for letter in option : <EOL> if letter . isalpha ( ) and letter . upper ( ) == letter : <EOL> found_letter = letter <EOL> break <EOL> else : <EOL> for letter in option : <EOL> if not letter . isalpha ( ) : <EOL> continue <EOL> if letter not in letters : <EOL> found_letter = letter <EOL> break <EOL> else : <EOL> raise ValueError ( u'<STR_LIT>' ) <EOL> letters [ found_letter . lower ( ) ] = option <EOL> index = option . index ( found_letter ) <EOL> if not require and ( <EOL> ( default is None and not numrange and first ) or <EOL> ( isinstance ( default , basestring ) and <EOL> found_letter . lower ( ) == default . lower ( ) ) ) : <EOL> show_letter = '<STR_LIT>' % found_letter . upper ( ) <EOL> is_default = True <EOL> else : <EOL> show_letter = found_letter . upper ( ) <EOL> is_default = False <EOL> show_letter = colorize ( '<STR_LIT>' if is_default else '<STR_LIT:action>' , <EOL> show_letter ) <EOL> capitalized . append ( <EOL> option [ : index ] + show_letter + option [ index + <NUM_LIT:1> : ] <EOL> ) <EOL> display_letters . append ( found_letter . upper ( ) ) <EOL> first = False <EOL> if require : <EOL> default = None <EOL> elif default is None : <EOL> if numrange : <EOL> default = numrange [ <NUM_LIT:0> ] <EOL> else : <EOL> default = display_letters [ <NUM_LIT:0> ] . lower ( ) <EOL> if not prompt : <EOL> prompt_parts = [ ] <EOL> prompt_part_lengths = [ ] <EOL> if numrange : <EOL> if isinstance ( default , int ) : <EOL> default_name = unicode ( default ) <EOL> default_name = colorize ( '<STR_LIT>' , default_name ) <EOL> tmpl = '<STR_LIT>' <EOL> prompt_parts . append ( tmpl % default_name ) <EOL> prompt_part_lengths . append ( len ( tmpl % unicode ( default ) ) ) <EOL> else : <EOL> prompt_parts . append ( '<STR_LIT>' ) <EOL> prompt_part_lengths . append ( len ( prompt_parts [ - <NUM_LIT:1> ] ) ) <EOL> prompt_parts += capitalized <EOL> prompt_part_lengths += [ len ( s ) for s in options ] <EOL> prompt = '<STR_LIT>' <EOL> line_length = <NUM_LIT:0> <EOL> for i , ( part , length ) in enumerate ( zip ( prompt_parts , <EOL> prompt_part_lengths ) ) : <EOL> if i == len ( prompt_parts ) - <NUM_LIT:1> : <EOL> part += '<STR_LIT:?>' <EOL> else : <EOL> part += '<STR_LIT:U+002C>' <EOL> length += <NUM_LIT:1> <EOL> if line_length + length + <NUM_LIT:1> > max_width : <EOL> prompt += '<STR_LIT:\n>' <EOL> line_length = <NUM_LIT:0> <EOL> if line_length != <NUM_LIT:0> : <EOL> part = '<STR_LIT:U+0020>' + part <EOL> length += <NUM_LIT:1> <EOL> prompt += part <EOL> line_length += length <EOL> if not fallback_prompt : <EOL> fallback_prompt = u'<STR_LIT>' <EOL> if numrange : <EOL> fallback_prompt += u'<STR_LIT>' % numrange <EOL> fallback_prompt += '<STR_LIT:U+002CU+0020>' . join ( display_letters ) + '<STR_LIT::>' <EOL> resp = input_ ( prompt ) <EOL> while True : <EOL> resp = resp . strip ( ) . lower ( ) <EOL> if default is not None and not resp : <EOL> resp = default <EOL> if numrange : <EOL> try : <EOL> resp = int ( resp ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> low , high = numrange <EOL> if low <= resp <= high : <EOL> return resp <EOL> else : <EOL> resp = None <EOL> if resp : <EOL> resp = resp [ <NUM_LIT:0> ] <EOL> if resp in letters : <EOL> return resp <EOL> resp = input_ ( fallback_prompt ) <EOL> def input_yn ( prompt , require = False ) : <EOL> """<STR_LIT>""" <EOL> sel = input_options ( <EOL> ( '<STR_LIT:y>' , '<STR_LIT:n>' ) , require , prompt , u'<STR_LIT>' <EOL> ) <EOL> return sel == u'<STR_LIT:y>' <EOL> def input_select_objects ( prompt , objs , rep ) : <EOL> """<STR_LIT>""" <EOL> choice = input_options ( <EOL> ( u'<STR_LIT:y>' , u'<STR_LIT:n>' , u'<STR_LIT:s>' ) , False , <EOL> u'<STR_LIT>' % prompt ) <EOL> print ( ) <EOL> if choice == u'<STR_LIT:y>' : <EOL> return objs <EOL> elif choice == u'<STR_LIT:s>' : <EOL> out = [ ] <EOL> for obj in objs : <EOL> rep ( obj ) <EOL> if input_yn ( u'<STR_LIT>' % prompt , True ) : <EOL> out . append ( obj ) <EOL> print ( ) <EOL> return out <EOL> else : <EOL> return [ ] <EOL> def human_bytes ( size ) : <EOL> """<STR_LIT>""" <EOL> powers = [ u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT:M>' , u'<STR_LIT>' , u'<STR_LIT:T>' , u'<STR_LIT:P>' , u'<STR_LIT:E>' , u'<STR_LIT>' , u'<STR_LIT:Y>' , u'<STR_LIT:H>' ] <EOL> unit = '<STR_LIT:B>' <EOL> for power in powers : <EOL> if size < <NUM_LIT> : <EOL> return u"<STR_LIT>" % ( size , power , unit ) <EOL> size /= <NUM_LIT> <EOL> unit = u'<STR_LIT>' <EOL> return u"<STR_LIT>" <EOL> def human_seconds ( interval ) : <EOL> """<STR_LIT>""" <EOL> units = [ <EOL> ( <NUM_LIT:1> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT:7> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT> , u'<STR_LIT>' ) , <EOL> ( <NUM_LIT:10> , u'<STR_LIT>' ) , <EOL> ] <EOL> for i in range ( len ( units ) - <NUM_LIT:1> ) : <EOL> increment , suffix = units [ i ] <EOL> next_increment , _ = units [ i + <NUM_LIT:1> ] <EOL> interval /= float ( increment ) <EOL> if interval < next_increment : <EOL> break <EOL> else : <EOL> increment , suffix = units [ - <NUM_LIT:1> ] <EOL> interval /= float ( increment ) <EOL> return u"<STR_LIT>" % ( interval , suffix ) <EOL> def human_seconds_short ( interval ) : <EOL> """<STR_LIT>""" <EOL> interval = int ( interval ) <EOL> return u'<STR_LIT>' % ( interval // <NUM_LIT> , interval % <NUM_LIT> ) <EOL> COLOR_ESCAPE = "<STR_LIT>" <EOL> DARK_COLORS = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : <NUM_LIT:4> , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , <EOL> "<STR_LIT>" : <NUM_LIT:7> <EOL> } <EOL> LIGHT_COLORS = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : <NUM_LIT:4> , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , <EOL> "<STR_LIT>" : <NUM_LIT:7> <EOL> } <EOL> RESET_COLOR = COLOR_ESCAPE + "<STR_LIT>" <EOL> COLOR_NAMES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:action>' ] <EOL> COLORS = None <EOL> def _colorize ( color , text ) : <EOL> """<STR_LIT>""" <EOL> if color in DARK_COLORS : <EOL> escape = COLOR_ESCAPE + "<STR_LIT>" % ( DARK_COLORS [ color ] + <NUM_LIT:30> ) <EOL> elif color in LIGHT_COLORS : <EOL> escape = COLOR_ESCAPE + "<STR_LIT>" % ( LIGHT_COLORS [ color ] + <NUM_LIT:30> ) <EOL> else : <EOL> raise ValueError ( u'<STR_LIT>' , color ) <EOL> return escape + text + RESET_COLOR <EOL> def colorize ( color_name , text ) : <EOL> """<STR_LIT>""" <EOL> if config [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> global COLORS <EOL> if not COLORS : <EOL> COLORS = dict ( ( name , config [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ name ] . get ( unicode ) ) <EOL> for name in COLOR_NAMES ) <EOL> color = COLORS . get ( color_name ) <EOL> if not color : <EOL> log . debug ( u'<STR_LIT>' , color_name ) <EOL> color = color_name <EOL> return _colorize ( color , text ) <EOL> else : <EOL> return text <EOL> def _colordiff ( a , b , highlight = '<STR_LIT>' , <EOL> minor_highlight = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( a , basestring ) or not isinstance ( b , basestring ) : <EOL> a = unicode ( a ) <EOL> b = unicode ( b ) <EOL> if a == b : <EOL> return a , b <EOL> else : <EOL> return colorize ( highlight , a ) , colorize ( highlight , b ) <EOL> if isinstance ( a , bytes ) or isinstance ( b , bytes ) : <EOL> a = util . displayable_path ( a ) <EOL> b = util . displayable_path ( b ) <EOL> a_out = [ ] <EOL> b_out = [ ] <EOL> matcher = SequenceMatcher ( lambda x : False , a , b ) <EOL> for op , a_start , a_end , b_start , b_end in matcher . get_opcodes ( ) : <EOL> if op == '<STR_LIT>' : <EOL> a_out . append ( a [ a_start : a_end ] ) <EOL> b_out . append ( b [ b_start : b_end ] ) <EOL> elif op == '<STR_LIT>' : <EOL> b_out . append ( colorize ( highlight , b [ b_start : b_end ] ) ) <EOL> elif op == '<STR_LIT>' : <EOL> a_out . append ( colorize ( highlight , a [ a_start : a_end ] ) ) <EOL> elif op == '<STR_LIT:replace>' : <EOL> if a [ a_start : a_end ] . lower ( ) != b [ b_start : b_end ] . lower ( ) : <EOL> color = highlight <EOL> else : <EOL> color = minor_highlight <EOL> a_out . append ( colorize ( color , a [ a_start : a_end ] ) ) <EOL> b_out . append ( colorize ( color , b [ b_start : b_end ] ) ) <EOL> else : <EOL> assert ( False ) <EOL> return u'<STR_LIT>' . join ( a_out ) , u'<STR_LIT>' . join ( b_out ) <EOL> def colordiff ( a , b , highlight = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if config [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> return _colordiff ( a , b , highlight ) <EOL> else : <EOL> return unicode ( a ) , unicode ( b ) <EOL> def get_path_formats ( subview = None ) : <EOL> """<STR_LIT>""" <EOL> path_formats = [ ] <EOL> subview = subview or config [ '<STR_LIT>' ] <EOL> for query , view in subview . items ( ) : <EOL> query = PF_KEY_QUERIES . get ( query , query ) <EOL> path_formats . append ( ( query , Template ( view . get ( unicode ) ) ) ) <EOL> return path_formats <EOL> def get_replacements ( ) : <EOL> """<STR_LIT>""" <EOL> replacements = [ ] <EOL> for pattern , repl in config [ '<STR_LIT:replace>' ] . get ( dict ) . items ( ) : <EOL> repl = repl or '<STR_LIT>' <EOL> try : <EOL> replacements . append ( ( re . compile ( pattern ) , repl ) ) <EOL> except re . error : <EOL> raise UserError ( <EOL> u'<STR_LIT>' . format ( <EOL> pattern <EOL> ) <EOL> ) <EOL> return replacements <EOL> def term_width ( ) : <EOL> """<STR_LIT>""" <EOL> fallback = config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( int ) <EOL> try : <EOL> import fcntl <EOL> import termios <EOL> except ImportError : <EOL> return fallback <EOL> try : <EOL> buf = fcntl . ioctl ( <NUM_LIT:0> , termios . TIOCGWINSZ , '<STR_LIT:U+0020>' * <NUM_LIT:4> ) <EOL> except IOError : <EOL> return fallback <EOL> try : <EOL> height , width = struct . unpack ( b'<STR_LIT>' , buf ) <EOL> except struct . error : <EOL> return fallback <EOL> return width <EOL> FLOAT_EPSILON = <NUM_LIT> <EOL> def _field_diff ( field , old , new ) : <EOL> """<STR_LIT>""" <EOL> oldval = old . get ( field ) <EOL> newval = new . get ( field ) <EOL> if isinstance ( oldval , float ) and isinstance ( newval , float ) and abs ( oldval - newval ) < FLOAT_EPSILON : <EOL> return None <EOL> elif oldval == newval : <EOL> return None <EOL> oldstr = old . formatted ( ) . get ( field , u'<STR_LIT>' ) <EOL> newstr = new . formatted ( ) . get ( field , u'<STR_LIT>' ) <EOL> if isinstance ( oldval , basestring ) : <EOL> oldstr , newstr = colordiff ( oldval , newstr ) <EOL> else : <EOL> oldstr = colorize ( '<STR_LIT>' , oldstr ) <EOL> newstr = colorize ( '<STR_LIT>' , newstr ) <EOL> return u'<STR_LIT>' . format ( oldstr , newstr ) <EOL> def show_model_changes ( new , old = None , fields = None , always = False ) : <EOL> """<STR_LIT>""" <EOL> old = old or new . _db . _get ( type ( new ) , new . id ) <EOL> changes = [ ] <EOL> for field in old : <EOL> if field == '<STR_LIT>' or ( fields and field not in fields ) : <EOL> continue <EOL> line = _field_diff ( field , old , new ) <EOL> if line : <EOL> changes . append ( u'<STR_LIT>' . format ( field , line ) ) <EOL> for field in set ( new ) - set ( old ) : <EOL> if fields and field not in fields : <EOL> continue <EOL> changes . append ( u'<STR_LIT>' . format ( <EOL> field , <EOL> colorize ( '<STR_LIT>' , new . formatted ( ) [ field ] ) <EOL> ) ) <EOL> if changes or always : <EOL> print_ ( format ( old ) ) <EOL> if changes : <EOL> print_ ( u'<STR_LIT:\n>' . join ( changes ) ) <EOL> return bool ( changes ) <EOL> def show_path_changes ( path_changes ) : <EOL> """<STR_LIT>""" <EOL> sources , destinations = zip ( * path_changes ) <EOL> sources = map ( util . displayable_path , sources ) <EOL> destinations = map ( util . displayable_path , destinations ) <EOL> col_width = ( term_width ( ) - len ( '<STR_LIT>' ) ) // <NUM_LIT:2> <EOL> max_width = len ( max ( sources + destinations , key = len ) ) <EOL> if max_width > col_width : <EOL> for source , dest in zip ( sources , destinations ) : <EOL> log . info ( u'<STR_LIT>' , source , dest ) <EOL> else : <EOL> title_pad = max_width - len ( '<STR_LIT>' ) + len ( '<STR_LIT>' ) <EOL> log . info ( u'<STR_LIT>' , '<STR_LIT:U+0020>' * title_pad ) <EOL> for source , dest in zip ( sources , destinations ) : <EOL> pad = max_width - len ( source ) <EOL> log . info ( u'<STR_LIT>' , source , '<STR_LIT:U+0020>' * pad , dest ) <EOL> class CommonOptionsParser ( optparse . OptionParser , object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CommonOptionsParser , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _album_flags = False <EOL> def add_album_option ( self , flags = ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> album = optparse . Option ( * flags , action = '<STR_LIT:store_true>' , <EOL> help = u'<STR_LIT>' ) <EOL> self . add_option ( album ) <EOL> self . _album_flags = set ( flags ) <EOL> def _set_format ( self , option , opt_str , value , parser , target = None , <EOL> fmt = None , store_true = False ) : <EOL> """<STR_LIT>""" <EOL> if store_true : <EOL> setattr ( parser . values , option . dest , True ) <EOL> value = fmt or value and unicode ( value ) or '<STR_LIT>' <EOL> parser . values . format = value <EOL> if target : <EOL> config [ target . _format_config_key ] . set ( value ) <EOL> else : <EOL> if self . _album_flags : <EOL> if parser . values . album : <EOL> target = library . Album <EOL> else : <EOL> if self . _album_flags & set ( parser . rargs ) : <EOL> target = library . Album <EOL> else : <EOL> target = library . Item <EOL> config [ target . _format_config_key ] . set ( value ) <EOL> else : <EOL> config [ library . Item . _format_config_key ] . set ( value ) <EOL> config [ library . Album . _format_config_key ] . set ( value ) <EOL> def add_path_option ( self , flags = ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> path = optparse . Option ( * flags , nargs = <NUM_LIT:0> , action = '<STR_LIT>' , <EOL> callback = self . _set_format , <EOL> callback_kwargs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:store_true>' : True } , <EOL> help = u'<STR_LIT>' ) <EOL> self . add_option ( path ) <EOL> def add_format_option ( self , flags = ( '<STR_LIT>' , '<STR_LIT>' ) , target = None ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if target : <EOL> if isinstance ( target , basestring ) : <EOL> target = { '<STR_LIT>' : library . Item , <EOL> '<STR_LIT>' : library . Album } [ target ] <EOL> kwargs [ '<STR_LIT:target>' ] = target <EOL> opt = optparse . Option ( * flags , action = '<STR_LIT>' , <EOL> callback = self . _set_format , <EOL> callback_kwargs = kwargs , <EOL> help = u'<STR_LIT>' ) <EOL> self . add_option ( opt ) <EOL> def add_all_common_options ( self ) : <EOL> """<STR_LIT>""" <EOL> self . add_album_option ( ) <EOL> self . add_path_option ( ) <EOL> self . add_format_option ( ) <EOL> class Subcommand ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , parser = None , help = '<STR_LIT>' , aliases = ( ) , hide = False ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . parser = parser or CommonOptionsParser ( ) <EOL> self . aliases = aliases <EOL> self . help = help <EOL> self . hide = hide <EOL> self . _root_parser = None <EOL> def print_help ( self ) : <EOL> self . parser . print_help ( ) <EOL> def parse_args ( self , args ) : <EOL> return self . parser . parse_args ( args ) <EOL> @ property <EOL> def root_parser ( self ) : <EOL> return self . _root_parser <EOL> @ root_parser . setter <EOL> def root_parser ( self , root_parser ) : <EOL> self . _root_parser = root_parser <EOL> self . parser . prog = '<STR_LIT>' . format ( <EOL> root_parser . get_prog_name ( ) . decode ( '<STR_LIT:utf8>' ) , self . name ) <EOL> class SubcommandsOptionParser ( CommonOptionsParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = u"""<STR_LIT>""" <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> super ( SubcommandsOptionParser , self ) . __init__ ( * args , ** kwargs ) <EOL> self . disable_interspersed_args ( ) <EOL> self . subcommands = [ ] <EOL> def add_subcommand ( self , * cmds ) : <EOL> """<STR_LIT>""" <EOL> for cmd in cmds : <EOL> cmd . root_parser = self <EOL> self . subcommands . append ( cmd ) <EOL> def format_help ( self , formatter = None ) : <EOL> out = super ( SubcommandsOptionParser , self ) . format_help ( formatter ) <EOL> if formatter is None : <EOL> formatter = self . formatter <EOL> result = [ "<STR_LIT:\n>" ] <EOL> result . append ( formatter . format_heading ( '<STR_LIT>' ) ) <EOL> formatter . indent ( ) <EOL> disp_names = [ ] <EOL> help_position = <NUM_LIT:0> <EOL> subcommands = [ c for c in self . subcommands if not c . hide ] <EOL> subcommands . sort ( key = lambda c : c . name ) <EOL> for subcommand in subcommands : <EOL> name = subcommand . name <EOL> if subcommand . aliases : <EOL> name += '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( subcommand . aliases ) <EOL> disp_names . append ( name ) <EOL> proposed_help_position = len ( name ) + formatter . current_indent + <NUM_LIT:2> <EOL> if proposed_help_position <= formatter . max_help_position : <EOL> help_position = max ( help_position , proposed_help_position ) <EOL> for subcommand , name in zip ( subcommands , disp_names ) : <EOL> name_width = help_position - formatter . current_indent - <NUM_LIT:2> <EOL> if len ( name ) > name_width : <EOL> name = "<STR_LIT>" % ( formatter . current_indent , "<STR_LIT>" , name ) <EOL> indent_first = help_position <EOL> else : <EOL> name = "<STR_LIT>" % ( formatter . current_indent , "<STR_LIT>" , <EOL> name_width , name ) <EOL> indent_first = <NUM_LIT:0> <EOL> result . append ( name ) <EOL> help_width = formatter . width - help_position <EOL> help_lines = textwrap . wrap ( subcommand . help , help_width ) <EOL> result . append ( "<STR_LIT>" % ( indent_first , "<STR_LIT>" , help_lines [ <NUM_LIT:0> ] ) ) <EOL> result . extend ( [ "<STR_LIT>" % ( help_position , "<STR_LIT>" , line ) <EOL> for line in help_lines [ <NUM_LIT:1> : ] ] ) <EOL> formatter . dedent ( ) <EOL> return out + "<STR_LIT>" . join ( result ) <EOL> def _subcommand_for_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> for subcommand in self . subcommands : <EOL> if name == subcommand . name or name in subcommand . aliases : <EOL> return subcommand <EOL> return None <EOL> def parse_global_options ( self , args ) : <EOL> """<STR_LIT>""" <EOL> options , subargs = self . parse_args ( args ) <EOL> if options . help : <EOL> subargs = [ '<STR_LIT>' ] <EOL> elif options . version : <EOL> subargs = [ '<STR_LIT:version>' ] <EOL> return options , subargs <EOL> def parse_subcommand ( self , args ) : <EOL> """<STR_LIT>""" <EOL> if not args : <EOL> args = [ '<STR_LIT>' ] <EOL> cmdname = args . pop ( <NUM_LIT:0> ) <EOL> subcommand = self . _subcommand_for_name ( cmdname ) <EOL> if not subcommand : <EOL> raise UserError ( u"<STR_LIT>" . format ( cmdname ) ) <EOL> suboptions , subargs = subcommand . parse_args ( args ) <EOL> return subcommand , suboptions , subargs <EOL> optparse . Option . ALWAYS_TYPED_ACTIONS += ( '<STR_LIT>' , ) <EOL> def vararg_callback ( option , opt_str , value , parser ) : <EOL> """<STR_LIT>""" <EOL> value = [ value ] <EOL> def floatable ( str ) : <EOL> try : <EOL> float ( str ) <EOL> return True <EOL> except ValueError : <EOL> return False <EOL> for arg in parser . rargs : <EOL> if arg [ : <NUM_LIT:2> ] == "<STR_LIT>" and len ( arg ) > <NUM_LIT:2> : <EOL> break <EOL> if arg [ : <NUM_LIT:1> ] == "<STR_LIT:->" and len ( arg ) > <NUM_LIT:1> and not floatable ( arg ) : <EOL> break <EOL> value . append ( arg ) <EOL> del parser . rargs [ : len ( value ) - <NUM_LIT:1> ] <EOL> setattr ( parser . values , option . dest , value ) <EOL> def _load_plugins ( config ) : <EOL> """<STR_LIT>""" <EOL> paths = config [ '<STR_LIT>' ] . get ( confit . StrSeq ( split = False ) ) <EOL> paths = map ( util . normpath , paths ) <EOL> log . debug ( u'<STR_LIT>' , util . displayable_path ( paths ) ) <EOL> import beetsplug <EOL> beetsplug . __path__ = paths + beetsplug . __path__ <EOL> sys . path += paths <EOL> plugins . load_plugins ( config [ '<STR_LIT>' ] . as_str_seq ( ) ) <EOL> plugins . send ( "<STR_LIT>" ) <EOL> return plugins <EOL> def _setup ( options , lib = None ) : <EOL> """<STR_LIT>""" <EOL> mb . configure ( ) <EOL> config = _configure ( options ) <EOL> plugins = _load_plugins ( config ) <EOL> from beets . ui . commands import default_commands <EOL> subcommands = list ( default_commands ) <EOL> subcommands . extend ( plugins . commands ( ) ) <EOL> if lib is None : <EOL> lib = _open_library ( config ) <EOL> plugins . send ( "<STR_LIT>" , lib = lib ) <EOL> library . Item . _types . update ( plugins . types ( library . Item ) ) <EOL> library . Album . _types . update ( plugins . types ( library . Album ) ) <EOL> return subcommands , plugins , lib <EOL> def _configure ( options ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( options , b'<STR_LIT>' , None ) is not None : <EOL> config_path = options . config <EOL> del options . config <EOL> config . set_file ( config_path ) <EOL> config . set_args ( options ) <EOL> if config [ '<STR_LIT>' ] . get ( int ) : <EOL> log . set_global_level ( logging . DEBUG ) <EOL> else : <EOL> log . set_global_level ( logging . INFO ) <EOL> if config [ '<STR_LIT>' ] . exists ( ) : <EOL> log . warning ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . set ( config [ '<STR_LIT>' ] . get ( bool ) ) <EOL> for elem in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> old_key = '<STR_LIT>' . format ( elem ) <EOL> if config [ old_key ] . exists ( ) : <EOL> new_key = '<STR_LIT>' . format ( elem ) <EOL> log . warning ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' , <EOL> old_key , <EOL> new_key , <EOL> ) <EOL> config [ new_key ] . set ( config [ old_key ] ) <EOL> config_path = config . user_config_path ( ) <EOL> if os . path . isfile ( config_path ) : <EOL> log . debug ( u'<STR_LIT>' , <EOL> util . displayable_path ( config_path ) ) <EOL> else : <EOL> log . debug ( u'<STR_LIT>' , <EOL> util . displayable_path ( config_path ) ) <EOL> log . debug ( u'<STR_LIT>' , <EOL> util . displayable_path ( config . config_dir ( ) ) ) <EOL> return config <EOL> def _open_library ( config ) : <EOL> """<STR_LIT>""" <EOL> dbpath = config [ '<STR_LIT>' ] . as_filename ( ) <EOL> try : <EOL> lib = library . Library ( <EOL> dbpath , <EOL> config [ '<STR_LIT>' ] . as_filename ( ) , <EOL> get_path_formats ( ) , <EOL> get_replacements ( ) , <EOL> ) <EOL> lib . get_item ( <NUM_LIT:0> ) <EOL> except ( sqlite3 . OperationalError , sqlite3 . DatabaseError ) : <EOL> log . debug ( u'<STR_LIT:{}>' , traceback . format_exc ( ) ) <EOL> raise UserError ( u"<STR_LIT>" . format ( <EOL> util . displayable_path ( dbpath ) <EOL> ) ) <EOL> log . debug ( u'<STR_LIT>' <EOL> u'<STR_LIT>' , <EOL> util . displayable_path ( lib . path ) , <EOL> util . displayable_path ( lib . directory ) ) <EOL> return lib <EOL> def _raw_main ( args , lib = None ) : <EOL> """<STR_LIT>""" <EOL> parser = SubcommandsOptionParser ( ) <EOL> parser . add_format_option ( flags = ( '<STR_LIT>' , ) , target = library . Item ) <EOL> parser . add_format_option ( flags = ( '<STR_LIT>' , ) , target = library . Album ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = u'<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = u"<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:count>' , <EOL> help = u'<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = u'<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = u'<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT:version>' , action = '<STR_LIT:store_true>' , <EOL> help = optparse . SUPPRESS_HELP ) <EOL> options , subargs = parser . parse_global_options ( args ) <EOL> if subargs and subargs [ <NUM_LIT:0> ] == '<STR_LIT>' and ( '<STR_LIT>' in subargs or '<STR_LIT>' in subargs ) : <EOL> from beets . ui . commands import config_edit <EOL> return config_edit ( ) <EOL> subcommands , plugins , lib = _setup ( options , lib ) <EOL> parser . add_subcommand ( * subcommands ) <EOL> subcommand , suboptions , subargs = parser . parse_subcommand ( subargs ) <EOL> subcommand . func ( lib , suboptions , subargs ) <EOL> plugins . send ( '<STR_LIT>' , lib = lib ) <EOL> def main ( args = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _raw_main ( args ) <EOL> except UserError as exc : <EOL> message = exc . args [ <NUM_LIT:0> ] if exc . args else None <EOL> log . error ( u'<STR_LIT>' , message ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except util . HumanReadableException as exc : <EOL> exc . log ( log ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except library . FileOperationError as exc : <EOL> log . debug ( '<STR_LIT:{}>' , traceback . format_exc ( ) ) <EOL> log . error ( '<STR_LIT:{}>' , exc ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except confit . ConfigError as exc : <EOL> log . error ( u'<STR_LIT>' , exc ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except db_query . InvalidQueryError as exc : <EOL> log . error ( u'<STR_LIT>' , exc ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> except IOError as exc : <EOL> if exc . errno == errno . EPIPE : <EOL> pass <EOL> else : <EOL> raise <EOL> except KeyboardInterrupt : <EOL> log . debug ( u'<STR_LIT:{}>' , traceback . format_exc ( ) ) </s>
