<s> from django . core . management . base import BaseCommand , CommandError <EOL> from django . db import transaction <EOL> from slugify import slugify <EOL> from categories . models import Category <EOL> from categories . settings import SLUG_TRANSLITERATOR <EOL> class Command ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> help = "<STR_LIT>" <EOL> args = "<STR_LIT>" <EOL> def get_indent ( self , string ) : <EOL> """<STR_LIT>""" <EOL> indent_amt = <NUM_LIT:0> <EOL> if string [ <NUM_LIT:0> ] == '<STR_LIT:\t>' : <EOL> return '<STR_LIT:\t>' <EOL> for char in string : <EOL> if char == '<STR_LIT:U+0020>' : <EOL> indent_amt += <NUM_LIT:1> <EOL> else : <EOL> return '<STR_LIT:U+0020>' * indent_amt <EOL> @ transaction . atomic <EOL> def make_category ( self , string , parent = None , order = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> cat = Category ( <EOL> name = string . strip ( ) , <EOL> slug = slugify ( SLUG_TRANSLITERATOR ( string . strip ( ) ) ) [ : <NUM_LIT> ] , <EOL> order = order <EOL> ) <EOL> cat . _tree_manager . insert_node ( cat , parent , '<STR_LIT>' , True ) <EOL> cat . save ( ) <EOL> if parent : <EOL> parent . rght = cat . rght + <NUM_LIT:1> <EOL> parent . save ( ) <EOL> return cat <EOL> def parse_lines ( self , lines ) : <EOL> """<STR_LIT>""" <EOL> indent = '<STR_LIT>' <EOL> level = <NUM_LIT:0> <EOL> if lines [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:U+0020>' or lines [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:\t>' : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> current_parents = { <NUM_LIT:0> : None } <EOL> for line in lines : <EOL> if len ( line ) == <NUM_LIT:0> : <EOL> continue <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:U+0020>' or line [ <NUM_LIT:0> ] == '<STR_LIT:\t>' : <EOL> if indent == '<STR_LIT>' : <EOL> indent = self . get_indent ( line ) <EOL> elif not line [ <NUM_LIT:0> ] in indent : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> level = line . count ( indent ) <EOL> current_parents [ level ] = self . make_category ( line , parent = current_parents [ level - <NUM_LIT:1> ] ) <EOL> else : <EOL> current_parents = { <NUM_LIT:0> : self . make_category ( line ) } <EOL> current_parents [ <NUM_LIT:0> ] . _tree_manager . rebuild ( ) <EOL> def handle ( self , * file_paths , ** options ) : <EOL> """<STR_LIT>""" <EOL> import os <EOL> for file_path in file_paths : <EOL> if not os . path . isfile ( file_path ) : <EOL> print ( "<STR_LIT>" % file_path ) <EOL> continue <EOL> f = open ( file_path , '<STR_LIT:r>' ) <EOL> data = f . readlines ( ) <EOL> f . close ( ) <EOL> self . parse_lines ( data ) </s>
