<s> from django . core . management . base import BaseCommand , CommandError <EOL> from optparse import make_option <EOL> from django . utils . encoding import force_unicode <EOL> from ... api import * <EOL> from ... utils . prettytable import PrettyTable <EOL> class Command ( BaseCommand ) : <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT:list>' , default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT:1> , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT> , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , help = '<STR_LIT>' ) , <EOL> ) <EOL> def handle ( self , * args , ** options ) : <EOL> if not len ( args ) or options [ '<STR_LIT:list>' ] : <EOL> page = options . get ( '<STR_LIT>' ) <EOL> per_page = options . get ( '<STR_LIT>' ) <EOL> ordered_by = [ x . strip ( ) for x in options . get ( '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> self . show_options_list ( Option . objects . all ( ) . order_by ( * ordered_by ) , page , per_page ) <EOL> return <EOL> option_key = args [ <NUM_LIT:0> ] . strip ( ) <EOL> if options [ '<STR_LIT>' ] : <EOL> value , action = self . read_value ( options [ '<STR_LIT>' ] , * args [ <NUM_LIT:1> : ] , ** options ) <EOL> if not add_option ( option_key , value ) : <EOL> raise CommandError ( "<STR_LIT>" % option_key ) <EOL> self . stdout . write ( '<STR_LIT>' % ( option_key , value , action ) ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> value , action = self . read_value ( options [ '<STR_LIT>' ] , * args [ <NUM_LIT:1> : ] , ** options ) <EOL> if not update_option ( option_key , value ) : <EOL> raise CommandError ( "<STR_LIT>" % ( option_key , value ) ) <EOL> self . stdout . write ( '<STR_LIT>' % ( option_key , value , action ) ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> if len ( args ) > <NUM_LIT:1> : raise CommandError ( '<STR_LIT>' % args ) <EOL> if not delete_option ( option_key ) : <EOL> raise CommandError ( "<STR_LIT>" % option_key ) <EOL> self . stdout . write ( '<STR_LIT>' % option_key ) <EOL> else : <EOL> self . show_option_value ( option_key ) <EOL> def show_options_list ( self , list , page = <NUM_LIT:1> , per_page = <NUM_LIT> ) : <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> paginator = Paginator ( list , per_page ) <EOL> try : <EOL> options = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> options = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> options = paginator . page ( paginator . num_pages ) <EOL> x = PrettyTable ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> x . align [ "<STR_LIT>" ] = "<STR_LIT:l>" <EOL> x . align [ "<STR_LIT>" ] = "<STR_LIT:l>" <EOL> for opt in options : <EOL> x . add_row ( [ opt . site . pk , opt . key , opt . updated_at . strftime ( "<STR_LIT>" ) , opt . created_at . strftime ( "<STR_LIT>" ) , force_unicode ( opt . value ) , opt . autoload ] ) <EOL> self . stdout . write ( u"<STR_LIT>" % x ) <EOL> if page and paginator . num_pages > <NUM_LIT:1> : <EOL> self . stdout . write ( u'<STR_LIT>' % ( page , paginator . num_pages ) ) <EOL> def show_option_value ( self , option_key ) : <EOL> opt = Option . objects . get ( key = option_key ) <EOL> self . show_options_list ( [ opt ] ) <EOL> def read_value ( self , value , * args , ** options ) : <EOL> action = '<STR_LIT>' <EOL> if options [ '<STR_LIT>' ] : <EOL> action = '<STR_LIT>' % value <EOL> value = eval ( value , { } , { } ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> from importlib import import_module <EOL> action = '<STR_LIT>' % value <EOL> module_name = value . split ( '<STR_LIT:.>' ) <EOL> func_name = module_name . pop ( ) <EOL> module_name = "<STR_LIT:.>" . join ( module_name ) <EOL> try : <EOL> mod = import_module ( module_name ) <EOL> if func_name not in dir ( mod ) : <EOL> raise CommandError ( '<STR_LIT>' % ( func_name , module_name ) ) <EOL> except ImportError : <EOL> raise CommandError ( '<STR_LIT>' % module_name ) <EOL> value = getattr ( mod , func_name ) ( * args ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> import json <EOL> action = '<STR_LIT>' % value <EOL> value = json . loads ( value ) <EOL> return value , action </s>
