<s> from __future__ import absolute_import , unicode_literals <EOL> import re <EOL> class MpdUriMapper ( object ) : <EOL> """<STR_LIT>""" <EOL> core = None <EOL> _invalid_browse_chars = re . compile ( r'<STR_LIT>' ) <EOL> _invalid_playlist_chars = re . compile ( r'<STR_LIT>' ) <EOL> def __init__ ( self , core = None ) : <EOL> self . core = core <EOL> self . _uri_from_name = { } <EOL> self . _browse_name_from_uri = { } <EOL> self . _playlist_name_from_uri = { } <EOL> def _create_unique_name ( self , name , uri ) : <EOL> stripped_name = self . _invalid_browse_chars . sub ( '<STR_LIT:U+0020>' , name ) <EOL> name = stripped_name <EOL> i = <NUM_LIT:2> <EOL> while name in self . _uri_from_name : <EOL> if self . _uri_from_name [ name ] == uri : <EOL> return name <EOL> name = '<STR_LIT>' % ( stripped_name , i ) <EOL> i += <NUM_LIT:1> <EOL> return name <EOL> def insert ( self , name , uri , playlist = False ) : <EOL> """<STR_LIT>""" <EOL> name = self . _create_unique_name ( name , uri ) <EOL> self . _uri_from_name [ name ] = uri <EOL> if playlist : <EOL> self . _playlist_name_from_uri [ uri ] = name <EOL> else : <EOL> self . _browse_name_from_uri [ uri ] = name <EOL> return name <EOL> def uri_from_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _uri_from_name . get ( name ) <EOL> def refresh_playlists_mapping ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . core is None : <EOL> return <EOL> for playlist_ref in self . core . playlists . as_list ( ) . get ( ) : <EOL> if not playlist_ref . name : <EOL> continue <EOL> name = self . _invalid_playlist_chars . sub ( '<STR_LIT:|>' , playlist_ref . name ) <EOL> self . insert ( name , playlist_ref . uri , playlist = True ) <EOL> def playlist_uri_from_name ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . _uri_from_name : <EOL> self . refresh_playlists_mapping ( ) <EOL> return self . _uri_from_name . get ( name ) <EOL> def playlist_name_from_uri ( self , uri ) : <EOL> """<STR_LIT>""" <EOL> if uri not in self . _playlist_name_from_uri : <EOL> self . refresh_playlists_mapping ( ) <EOL> return self . _playlist_name_from_uri [ uri ] </s>
