#NEWLINE# def run( self , info ) : #NEWLINE# #INDENT# ' Set extended attributes on downloaded file (if xattr support is found). ' #NEWLINE# try : #NEWLINE# #INDENT# import xattr #NEWLINE# def write_xattr( path , key , value ) : #NEWLINE# #INDENT# return xattr . setxattr ( path , key , value ) #NEWLINE# #UNINDENT# #UNINDENT# except ImportError : #NEWLINE# #INDENT# if ( os . name == 'nt' ) : #NEWLINE# #INDENT# def write_xattr( path , key , value ) : #NEWLINE# #INDENT# assert ( ':' not in key ) #NEWLINE# assert os . path . exists ( path ) #NEWLINE# ads_fn = ( ( path + ':' ) + key ) #NEWLINE# with open ( ads_fn , 'wb' ) as f : #NEWLINE# #INDENT# f . write ( value ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# user_has_setfattr = check_executable ( 'setfattr' , [ '--version' ] ) #NEWLINE# user_has_xattr = check_executable ( 'xattr' , [ '-h' ] ) #NEWLINE# if ( user_has_setfattr or user_has_xattr ) : #NEWLINE# #INDENT# def write_xattr( path , key , value ) : #NEWLINE# #INDENT# if user_has_setfattr : #NEWLINE# #INDENT# cmd = [ 'setfattr' , '-n' , key , '-v' , value , path ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if user_has_xattr : #NEWLINE# #INDENT# cmd = [ 'xattr' , '-w' , key , value , path ] #NEWLINE# #UNINDENT# #UNINDENT# subprocess_check_output ( cmd ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if sys . platform . startswith ( 'linux' ) : #NEWLINE# #INDENT# self . _downloader . report_error ( "Couldn't find a tool to set the xattrs. Install either the python 'pyxattr' or 'xattr' modules, or the GNU 'attr' package (which contains the 'setfattr' tool)." ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . _downloader . report_error ( "Couldn't find a tool to set the xattrs. Install either the python 'xattr' module, or the 'xattr' binary." ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# self . _downloader . to_screen ( "[metadata] Writing metadata to file's xattrs" ) #NEWLINE# filename = info [ 'filepath' ] #NEWLINE# try : #NEWLINE# #INDENT# xattr_mapping = { #NEWLINE# #INDENT# 'user.xdg.referrer.url' : 'webpage_url' , #NEWLINE# 'user.dublincore.title' : 'title' , #NEWLINE# 'user.dublincore.date' : 'upload_date' , #NEWLINE# 'user.dublincore.description' : 'description' , #NEWLINE# 'user.dublincore.contributor' : 'uploader' , #NEWLINE# 'user.dublincore.format' : 'format' , #NEWLINE# #UNINDENT# } #NEWLINE# for ( xattrname , infoname ) in xattr_mapping . items ( ) : #NEWLINE# #INDENT# value = info . get ( infoname ) #NEWLINE# if value : #NEWLINE# #INDENT# if ( infoname == 'upload_date' ) : #NEWLINE# #INDENT# value = hyphenate_date ( value ) #NEWLINE# #UNINDENT# byte_value = value . encode ( 'utf-8' ) #NEWLINE# write_xattr ( filename , xattrname , byte_value ) #NEWLINE# #UNINDENT# #UNINDENT# return ( True , info ) #NEWLINE# #UNINDENT# except ( subprocess . CalledProcessError , OSError ) : #NEWLINE# #INDENT# self . _downloader . report_error ( "This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)" ) #NEWLINE# return ( False , info )