<s> from __future__ import unicode_literals <EOL> from django . core . files import File as DjangoFile <EOL> from django . core . management . base import BaseCommand , NoArgsCommand <EOL> from filer . models . filemodels import File <EOL> from leonardo . module . media . models import * <EOL> from filer . settings import FILER_IS_PUBLIC_DEFAULT <EOL> from filer . utils . compatibility import upath <EOL> from optparse import make_option <EOL> import os <EOL> MEDIA_MODELS = [ Image , Document , Vector , Video ] <EOL> class FileImporter ( object ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . path = kwargs . get ( '<STR_LIT:path>' ) <EOL> self . base_folder = kwargs . get ( '<STR_LIT>' ) <EOL> self . verbosity = int ( kwargs . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> self . file_created = <NUM_LIT:0> <EOL> self . image_created = <NUM_LIT:0> <EOL> self . folder_created = <NUM_LIT:0> <EOL> def import_file ( self , file_obj , folder ) : <EOL> """<STR_LIT>""" <EOL> created = False <EOL> for cls in MEDIA_MODELS : <EOL> if cls . matches_file_type ( file_obj . name ) : <EOL> obj , created = cls . objects . get_or_create ( <EOL> original_filename = file_obj . name , <EOL> file = file_obj , <EOL> folder = folder , <EOL> is_public = FILER_IS_PUBLIC_DEFAULT ) <EOL> if created : <EOL> self . image_created += <NUM_LIT:1> <EOL> if not created : <EOL> obj , created = File . objects . get_or_create ( <EOL> original_filename = file_obj . name , <EOL> file = file_obj , <EOL> folder = folder , <EOL> is_public = FILER_IS_PUBLIC_DEFAULT ) <EOL> if created : <EOL> self . file_created += <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" % ( self . file_created , <EOL> self . image_created , <EOL> obj , created ) ) <EOL> return obj <EOL> def get_or_create_folder ( self , folder_names ) : <EOL> """<STR_LIT>""" <EOL> if not len ( folder_names ) : <EOL> return None <EOL> current_parent = None <EOL> for folder_name in folder_names : <EOL> current_parent , created = Folder . objects . get_or_create ( <EOL> name = folder_name , parent = current_parent ) <EOL> if created : <EOL> self . folder_created += <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" % ( self . folder_created , <EOL> current_parent , created ) ) <EOL> return current_parent <EOL> def walker ( self , path = None , base_folder = None ) : <EOL> """<STR_LIT>""" <EOL> path = path or self . path <EOL> base_folder = base_folder or self . base_folder <EOL> path = os . path . normpath ( upath ( path ) ) <EOL> if base_folder : <EOL> base_folder = os . path . normpath ( upath ( base_folder ) ) <EOL> print ( "<STR_LIT>" % ( base_folder , ) ) <EOL> if self . verbosity >= <NUM_LIT:1> : <EOL> print ( "<STR_LIT>" % ( path , ) ) <EOL> root_folder_name = os . path . basename ( path ) <EOL> for root , dirs , files in os . walk ( path ) : <EOL> rel_folders = root . partition ( path ) [ <NUM_LIT:2> ] . strip ( os . path . sep ) . split ( os . path . sep ) <EOL> while '<STR_LIT>' in rel_folders : <EOL> rel_folders . remove ( '<STR_LIT>' ) <EOL> if base_folder : <EOL> folder_names = base_folder . split ( '<STR_LIT:/>' ) + [ root_folder_name ] + rel_folders <EOL> else : <EOL> folder_names = [ root_folder_name ] + rel_folders <EOL> folder = self . get_or_create_folder ( folder_names ) <EOL> for file_obj in files : <EOL> dj_file = DjangoFile ( open ( os . path . join ( root , file_obj ) ) , <EOL> name = file_obj ) <EOL> self . import_file ( file_obj = dj_file , folder = folder ) <EOL> if self . verbosity >= <NUM_LIT:1> : <EOL> print ( ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) % ( <EOL> self . folder_created , self . file_created , <EOL> self . image_created ) ) <EOL> class Command ( NoArgsCommand ) : <EOL> """<STR_LIT>""" <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT:path>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle_noargs ( self , ** options ) : <EOL> file_importer = FileImporter ( ** options ) <EOL> file_importer . walker ( ) </s>
