<s> import sct_utils as sct <EOL> from msct_types import Coordinate <EOL> class Option : <EOL> OPTION_TYPES = [ "<STR_LIT:str>" , "<STR_LIT:int>" , "<STR_LIT:float>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> OPTION_PATH_INPUT = [ "<STR_LIT:file>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> OPTION_PATH_OUTPUT = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def __init__ ( self , name , type_value , description , mandatory , example , default_value , help , parser , order = <NUM_LIT:0> , deprecated_by = None , deprecated_rm = False , deprecated = False ) : <EOL> self . name = name <EOL> self . type_value = type_value <EOL> self . description = description <EOL> self . mandatory = mandatory <EOL> self . example = example <EOL> self . default_value = default_value <EOL> self . help = help <EOL> self . parser = parser <EOL> self . order = order <EOL> self . deprecated_by = deprecated_by <EOL> self . deprecated_rm = deprecated_rm <EOL> self . deprecated = deprecated <EOL> def __safe_cast__ ( self , val , to_type ) : <EOL> return to_type ( val ) <EOL> def check_integrity ( self , param , type = None ) : <EOL> """<STR_LIT>""" <EOL> type_option = self . type_value <EOL> if type is not None : <EOL> type_option = type <EOL> if type_option in self . OPTION_TYPES : <EOL> return self . checkStandardType ( param , type ) <EOL> elif type_option == "<STR_LIT>" : <EOL> return self . checkIfNifti ( param ) <EOL> elif type_option == "<STR_LIT:file>" : <EOL> return self . checkFile ( param ) <EOL> elif type_option == "<STR_LIT>" : <EOL> if not sct . check_write_permission ( param ) : <EOL> self . parser . usage . error ( "<STR_LIT>" + param ) <EOL> return param <EOL> elif type_option == "<STR_LIT>" : <EOL> return self . checkFolder ( param ) <EOL> elif type_option == "<STR_LIT>" : <EOL> return self . checkFolderCreation ( param ) <EOL> elif type_option == "<STR_LIT>" : <EOL> """<STR_LIT>""" <EOL> if param not in self . example : <EOL> self . parser . usage . error ( self . name + "<STR_LIT>" + self . parser . usage . print_list_with_brackets ( self . example ) + "<STR_LIT>" ) <EOL> return param <EOL> elif isinstance ( type_option , list ) : <EOL> """<STR_LIT>""" <EOL> delimiter = type_option [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> sub_type = type_option [ <NUM_LIT:1> ] <EOL> param_splitted = param . split ( delimiter ) <EOL> if len ( param_splitted ) != <NUM_LIT:0> : <EOL> if not param_splitted [ <NUM_LIT:0> ] . find ( '<STR_LIT:U+0020>' ) == - <NUM_LIT:1> : <EOL> param_splitted = param_splitted [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' ) <EOL> return list ( [ self . check_integrity ( val , sub_type ) for val in param_splitted ] ) <EOL> else : <EOL> self . parser . usage . error ( "<STR_LIT>" + self . name + "<STR_LIT>" ) <EOL> else : <EOL> sct . printv ( "<STR_LIT>" + str ( self . type_value ) + "<STR_LIT>" , "<STR_LIT>" ) <EOL> sct . printv ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def checkStandardType ( self , param , type = None ) : <EOL> type_option = self . type_value <EOL> if type is not None : <EOL> type_option = type <EOL> try : <EOL> return self . __safe_cast__ ( param , eval ( type_option ) ) <EOL> except ValueError : <EOL> self . parser . usage . error ( "<STR_LIT>" + self . name + "<STR_LIT>" + type_option ) <EOL> def checkFile ( self , param ) : <EOL> sct . printv ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> if self . parser . check_file_exist : <EOL> sct . check_file_exist ( param , <NUM_LIT:0> ) <EOL> return param <EOL> def checkIfNifti ( self , param ) : <EOL> import os <EOL> sct . printv ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> nii = False <EOL> niigz = False <EOL> viewer = False <EOL> param_tmp = str ( ) <EOL> if param . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> if self . parser . check_file_exist : <EOL> nii = os . path . isfile ( param ) <EOL> niigz = os . path . isfile ( param + '<STR_LIT>' ) <EOL> else : <EOL> nii , niigz = True , False <EOL> param_tmp = param [ : - <NUM_LIT:4> ] <EOL> pass <EOL> elif param . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> if self . parser . check_file_exist : <EOL> niigz = os . path . isfile ( param ) <EOL> nii = os . path . isfile ( param [ : - <NUM_LIT:3> ] ) <EOL> else : <EOL> nii , niigz = False , True <EOL> param_tmp = param [ : - <NUM_LIT:7> ] <EOL> pass <EOL> elif param . lower ( ) == "<STR_LIT>" : <EOL> viewer = True <EOL> pass <EOL> else : <EOL> sct . printv ( "<STR_LIT>" , type = '<STR_LIT:error>' ) <EOL> if nii : <EOL> return param_tmp + '<STR_LIT>' <EOL> elif niigz : <EOL> return param_tmp + '<STR_LIT>' <EOL> elif viewer : <EOL> return param <EOL> else : <EOL> sct . printv ( "<STR_LIT>" + param + "<STR_LIT>" , type = '<STR_LIT:error>' ) <EOL> def checkFolder ( self , param ) : <EOL> if self . parser . check_file_exist : <EOL> sct . printv ( "<STR_LIT>" ) <EOL> sct . check_folder_exist ( param , <NUM_LIT:0> ) <EOL> return param <EOL> def checkFolderCreation ( self , param ) : <EOL> sct . printv ( "<STR_LIT>" ) <EOL> if self . parser . check_file_exist : <EOL> result_creation = sct . create_folder ( param ) <EOL> else : <EOL> result_creation = <NUM_LIT:0> <EOL> if result_creation == <NUM_LIT:2> : <EOL> sct . printv ( "<STR_LIT>" , type = "<STR_LIT:error>" ) <EOL> elif result_creation == <NUM_LIT:1> : <EOL> sct . printv ( "<STR_LIT>" + param + "<STR_LIT>" , <NUM_LIT:0> , type = '<STR_LIT>' ) <EOL> param = sct . slash_at_the_end ( param , <NUM_LIT:1> ) <EOL> return param <EOL> class Parser : <EOL> def __init__ ( self , file_name ) : <EOL> self . file_name = file_name <EOL> self . options = dict ( ) <EOL> self . spelling = SpellingChecker ( ) <EOL> self . errors = '<STR_LIT>' <EOL> self . usage = Usage ( self , file_name ) <EOL> self . check_file_exist = True <EOL> def add_option ( self , name , type_value = None , description = None , mandatory = False , example = None , help = None , default_value = None , deprecated_by = None , deprecated_rm = False , deprecated = False ) : <EOL> order = len ( self . options ) + <NUM_LIT:1> <EOL> self . options [ name ] = Option ( name , type_value , description , mandatory , example , default_value , help , self , order , deprecated_by , deprecated_rm , deprecated ) <EOL> def parse ( self , arguments , check_file_exist = True ) : <EOL> self . check_file_exist = check_file_exist <EOL> if len ( arguments ) == <NUM_LIT:0> and len ( [ opt for opt in self . options if self . options [ opt ] . mandatory ] ) != <NUM_LIT:0> : <EOL> self . usage . error ( ) <EOL> if "<STR_LIT>" in arguments : <EOL> print ( self . usage . generate ( ) ) <EOL> exit ( <NUM_LIT:1> ) <EOL> if "<STR_LIT>" in arguments : <EOL> doc_sourceforge = DocSourceForge ( self , self . file_name ) <EOL> doc_sourceforge . generate ( ) <EOL> exit ( <NUM_LIT:1> ) <EOL> dictionary = dict ( ) <EOL> self . spelling . setWordsAsList ( [ name for name in self . options ] ) <EOL> arguments_temp = [ ] <EOL> index_next = <NUM_LIT:0> <EOL> for index in range ( <NUM_LIT:0> , len ( arguments ) ) : <EOL> if index == index_next : <EOL> if arguments [ index ] [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> arguments_temp . append ( arguments [ index ] ) <EOL> index_next = index + <NUM_LIT:1> <EOL> else : <EOL> temp_str = arguments [ index ] <EOL> index_temp = index <EOL> if index_temp < len ( arguments ) - <NUM_LIT:1> : <EOL> if arguments [ index ] [ <NUM_LIT:0> ] == '<STR_LIT:">' : <EOL> while arguments [ index_temp + <NUM_LIT:1> ] [ - <NUM_LIT:1> ] != '<STR_LIT:">' : <EOL> temp_str += '<STR_LIT:U+0020>' + arguments [ index_temp + <NUM_LIT:1> ] <EOL> index_temp += <NUM_LIT:1> <EOL> if index_temp >= len ( arguments ) - <NUM_LIT:1> : <EOL> break <EOL> temp_str += '<STR_LIT:U+0020>' + arguments [ index_temp + <NUM_LIT:1> ] <EOL> temp_str = temp_str [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> else : <EOL> while arguments [ index_temp + <NUM_LIT:1> ] [ <NUM_LIT:0> ] != '<STR_LIT:->' : <EOL> temp_str += '<STR_LIT:U+0020>' + arguments [ index_temp + <NUM_LIT:1> ] <EOL> index_temp += <NUM_LIT:1> <EOL> if index_temp >= len ( arguments ) - <NUM_LIT:1> : <EOL> break <EOL> index_next = index_temp + <NUM_LIT:1> <EOL> if '<STR_LIT:">' not in temp_str : <EOL> arguments_temp . append ( temp_str ) <EOL> arguments = arguments_temp <EOL> skip = False <EOL> for index , arg in enumerate ( arguments ) : <EOL> if skip : <EOL> skip = False <EOL> continue <EOL> if arg in self . options : <EOL> if self . options [ arg ] . deprecated_rm : <EOL> sct . printv ( "<STR_LIT>" + arg + "<STR_LIT>" , <NUM_LIT:1> , '<STR_LIT:error>' ) <EOL> if self . options [ arg ] . deprecated : <EOL> sct . printv ( "<STR_LIT>" + arg + "<STR_LIT>" , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> if self . options [ arg ] . deprecated_by is not None : <EOL> try : <EOL> sct . printv ( "<STR_LIT>" + arg + "<STR_LIT>" + self . options [ arg ] . deprecated_by + "<STR_LIT:.>" , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> arg = self . options [ arg ] . deprecated_by <EOL> except KeyError as e : <EOL> sct . printv ( "<STR_LIT>" + e . message , <NUM_LIT:1> , '<STR_LIT:error>' ) <EOL> if self . options [ arg ] . type_value : <EOL> if len ( arguments ) > index + <NUM_LIT:1> : <EOL> param = arguments [ index + <NUM_LIT:1> ] <EOL> else : <EOL> self . usage . error ( "<STR_LIT>" + self . options [ arg ] . name + "<STR_LIT>" ) <EOL> if param in self . options : <EOL> self . usage . error ( "<STR_LIT>" + self . options [ arg ] . name + "<STR_LIT>" ) <EOL> dictionary [ arg ] = self . options [ arg ] . check_integrity ( param ) <EOL> skip = True <EOL> else : <EOL> dictionary [ arg ] = True <EOL> else : <EOL> spelling_candidates = self . spelling . correct ( arg ) <EOL> if len ( spelling_candidates ) != <NUM_LIT:0> : <EOL> self . usage . error ( "<STR_LIT>" + arg + "<STR_LIT>" + '<STR_LIT:U+002CU+0020>' . join ( spelling_candidates ) + '<STR_LIT:?>' ) <EOL> else : <EOL> self . usage . error ( "<STR_LIT>" + arg + "<STR_LIT>" ) <EOL> for option in [ opt for opt in self . options if self . options [ opt ] . mandatory and self . options [ opt ] . deprecated_by is None ] : <EOL> if option not in dictionary : <EOL> self . usage . error ( '<STR_LIT>' + option + '<STR_LIT>' ) <EOL> for option in [ opt for opt in self . options if not self . options [ opt ] . mandatory ] : <EOL> if option not in dictionary and self . options [ option ] . default_value : <EOL> dictionary [ option ] = self . options [ option ] . default_value <EOL> return dictionary <EOL> def add_path_to_file ( self , dictionary , path_to_add , input_file = True , output_file = False ) : <EOL> """<STR_LIT>""" <EOL> for key , option in dictionary . iteritems ( ) : <EOL> if key in self . options : <EOL> if ( input_file and self . options [ key ] . type_value in Option . OPTION_PATH_INPUT ) or ( output_file and self . options [ key ] . type_value in Option . OPTION_PATH_OUTPUT ) : <EOL> if isinstance ( self . options [ key ] . type_value , list ) : <EOL> for i , value in enumerate ( option ) : <EOL> option [ i ] = path_to_add + value <EOL> dictionary [ key ] = option <EOL> else : <EOL> dictionary [ key ] = str ( path_to_add ) + str ( option ) <EOL> else : <EOL> sct . printv ( "<STR_LIT>" , verbose = <NUM_LIT:1> , type = '<STR_LIT:error>' ) <EOL> return dictionary <EOL> def dictionary_to_string ( self , dictionary ) : <EOL> """<STR_LIT>""" <EOL> result = "<STR_LIT>" <EOL> for key , option in dictionary . iteritems ( ) : <EOL> if isinstance ( option , list ) : <EOL> result = result + '<STR_LIT:U+0020>' + key + '<STR_LIT:U+0020>' + self . options [ key ] . type_value [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . join ( [ str ( op ) for op in option ] ) <EOL> else : <EOL> result = result + '<STR_LIT:U+0020>' + key + '<STR_LIT:U+0020>' + str ( option ) <EOL> return result <EOL> class Usage : <EOL> def __init__ ( self , parser , file ) : <EOL> self . file = file <EOL> self . header = '<STR_LIT>' <EOL> self . version = '<STR_LIT>' <EOL> self . usage = '<STR_LIT>' <EOL> self . example = '<STR_LIT>' <EOL> self . description = '<STR_LIT>' <EOL> self . arguments = parser . options <EOL> self . arguments_string = '<STR_LIT>' <EOL> self . section = dict ( ) <EOL> def set_description ( self , description ) : <EOL> self . description = '<STR_LIT>' + self . align ( description , length = <NUM_LIT:100> , pad = <NUM_LIT:0> ) <EOL> def addSection ( self , section ) : <EOL> self . section [ len ( self . arguments ) + <NUM_LIT:1> ] = section <EOL> def set_usage ( self ) : <EOL> from os . path import basename <EOL> self . usage = '<STR_LIT>' + basename ( self . file ) . strip ( '<STR_LIT>' ) <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> mandatory = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory ] <EOL> for opt in mandatory : <EOL> self . usage += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + self . refactor_type_value ( opt ) <EOL> self . usage += '<STR_LIT:\n>' <EOL> def set_arguments ( self ) : <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> mandatory = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory and not self . arguments [ opt [ <NUM_LIT:0> ] ] . deprecated_by ] <EOL> optional = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if not self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory and not self . arguments [ opt [ <NUM_LIT:0> ] ] . deprecated_by ] <EOL> if mandatory : <EOL> self . arguments_string = '<STR_LIT>' <EOL> for opt in mandatory : <EOL> if self . arguments [ opt ] . order in self . section : <EOL> self . arguments_string += self . section [ self . arguments [ opt ] . order ] + '<STR_LIT:\n>' <EOL> type_value = self . refactor_type_value ( opt ) <EOL> line = [ "<STR_LIT:U+0020>" + opt + "<STR_LIT:U+0020>" + type_value , self . align ( self . arguments [ opt ] . description ) ] <EOL> self . arguments_string += self . tab ( line ) + '<STR_LIT:\n>' <EOL> if optional : <EOL> self . arguments_string += '<STR_LIT>' <EOL> for opt in optional : <EOL> if self . arguments [ opt ] . order in self . section : <EOL> self . arguments_string += self . section [ self . arguments [ opt ] . order ] + '<STR_LIT:\n>' <EOL> type_value = self . refactor_type_value ( opt ) <EOL> description = self . arguments [ opt ] . description <EOL> if self . arguments [ opt ] . default_value : <EOL> description += "<STR_LIT>" + str ( self . arguments [ opt ] . default_value ) <EOL> if self . arguments [ opt ] . deprecated : <EOL> description += "<STR_LIT>" <EOL> line = [ "<STR_LIT:U+0020>" + opt + "<STR_LIT:U+0020>" + type_value , self . align ( description ) ] <EOL> self . arguments_string += self . tab ( line ) + '<STR_LIT:\n>' <EOL> if len ( self . arguments ) + <NUM_LIT:1> in self . section : <EOL> self . arguments_string += self . section [ len ( self . arguments ) + <NUM_LIT:1> ] + '<STR_LIT:\n>' <EOL> def refactor_type_value ( self , opt ) : <EOL> if self . arguments [ opt ] . type_value is None : <EOL> type_value = '<STR_LIT>' <EOL> elif self . arguments [ opt ] . type_value == '<STR_LIT>' : <EOL> type_value = self . print_list_with_brackets ( self . arguments [ opt ] . example ) <EOL> elif type ( self . arguments [ opt ] . type_value ) is list : <EOL> type_value = '<STR_LIT>' + str ( self . arguments [ opt ] . type_value [ <NUM_LIT:1> ] ) + '<STR_LIT:>>' <EOL> else : <EOL> type_value = '<STR_LIT:<>' + self . arguments [ opt ] . type_value + '<STR_LIT:>>' <EOL> return type_value <EOL> def set_example ( self ) : <EOL> from os . path import basename <EOL> self . example = '<STR_LIT>' + basename ( self . file ) . strip ( '<STR_LIT>' ) <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> for opt in [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . example and not self . arguments [ opt [ <NUM_LIT:0> ] ] . deprecated_by ] : <EOL> if type ( self . arguments [ opt ] . example ) is list : <EOL> self . example += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + str ( self . arguments [ opt ] . example [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . example += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + str ( self . arguments [ opt ] . example ) <EOL> def generate ( self , error = None ) : <EOL> self . set_arguments ( ) <EOL> self . set_usage ( ) <EOL> self . set_example ( ) <EOL> usage = self . header + self . description + self . usage + self . arguments_string + self . example + '<STR_LIT:\n>' <EOL> if error : <EOL> sct . printv ( error + '<STR_LIT>' , type = '<STR_LIT>' ) <EOL> sct . printv ( usage , type = '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> else : <EOL> return usage <EOL> def error ( self , error = None ) : <EOL> if error : <EOL> self . generate ( error ) <EOL> else : <EOL> print self . generate ( ) <EOL> from sys import exit <EOL> exit ( <NUM_LIT:0> ) <EOL> def print_list_with_brackets ( self , l ) : <EOL> type_value = '<STR_LIT:{>' <EOL> for char in l : <EOL> type_value += str ( char ) + '<STR_LIT:U+002C>' <EOL> type_value = type_value [ : - <NUM_LIT:1> ] <EOL> type_value += '<STR_LIT:}>' <EOL> return type_value <EOL> def tab ( self , strings ) : <EOL> """<STR_LIT>""" <EOL> tab = '<STR_LIT>' <EOL> for string in strings : <EOL> if len ( string ) < <NUM_LIT:30> : <EOL> spaces = '<STR_LIT:U+0020>' * ( <NUM_LIT:30> - len ( string ) ) <EOL> string += spaces <EOL> tab += string <EOL> return tab <EOL> def align ( self , string , length = <NUM_LIT> , pad = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> s = '<STR_LIT>' <EOL> strings = [ ] <EOL> string_split_line = string . split ( '<STR_LIT:\n>' ) <EOL> if len ( string_split_line ) > <NUM_LIT:1> : <EOL> for i in range ( <NUM_LIT:0> , len ( string_split_line ) ) : <EOL> if i != <NUM_LIT:0> : <EOL> string_split_line [ i ] = '<STR_LIT:U+0020>' + string_split_line [ i ] <EOL> for k , stri in enumerate ( string_split_line ) : <EOL> i = <NUM_LIT:0> <EOL> for c in stri : <EOL> i += <NUM_LIT:1> <EOL> if c == '<STR_LIT:U+0020>' : <EOL> last_space = i <EOL> if i % length == <NUM_LIT:0> : <EOL> strings . append ( stri [ <NUM_LIT:0> : last_space ] ) <EOL> stri = stri [ last_space : ] <EOL> if k != <NUM_LIT:0> : <EOL> stri = '<STR_LIT:U+0020>' + stri <EOL> i = i - last_space <EOL> strings . append ( stri ) <EOL> for i , yes in enumerate ( strings ) : <EOL> if i != <NUM_LIT:0> : <EOL> s += '<STR_LIT:U+0020>' * pad <EOL> s += yes <EOL> if i != len ( strings ) - <NUM_LIT:1> : <EOL> s += '<STR_LIT:\n>' <EOL> return s <EOL> class DocSourceForge : <EOL> def __init__ ( self , parser , file ) : <EOL> self . file = file <EOL> self . parser = parser <EOL> self . header = '<STR_LIT>' <EOL> self . version = '<STR_LIT>' <EOL> self . usage = '<STR_LIT>' <EOL> self . example = '<STR_LIT>' <EOL> self . description = '<STR_LIT>' <EOL> self . arguments = parser . options <EOL> self . arguments_string = '<STR_LIT>' <EOL> self . section = dict ( ) <EOL> def set_description ( self , description ) : <EOL> self . description = '<STR_LIT>' + self . align ( description , length = <NUM_LIT:100> , pad = <NUM_LIT:0> ) <EOL> def addSection ( self , section ) : <EOL> self . section [ len ( self . arguments ) + <NUM_LIT:1> ] = section <EOL> def set_usage ( self ) : <EOL> from os . path import basename <EOL> self . usage = '<STR_LIT>' + basename ( self . file ) <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> mandatory = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory ] <EOL> for opt in mandatory : <EOL> if self . arguments [ opt ] . type_value == '<STR_LIT>' : <EOL> self . usage += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + str ( self . arguments [ opt ] . example ) <EOL> else : <EOL> self . usage += '<STR_LIT:U+0020>' + opt + '<STR_LIT>' + str ( self . arguments [ opt ] . type_value ) + '<STR_LIT:>>' <EOL> self . usage += '<STR_LIT>' <EOL> def set_arguments ( self ) : <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> mandatory = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory ] <EOL> optional = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if not self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory ] <EOL> if mandatory : <EOL> self . arguments_string = '<STR_LIT>' <EOL> for opt in mandatory : <EOL> self . arguments_string += '<STR_LIT>' <EOL> if self . arguments [ opt ] . order in self . section : <EOL> self . arguments_string += self . section [ self . arguments [ opt ] . order ] + '<STR_LIT:\n>' <EOL> type_value = self . refactor_type_value ( opt ) <EOL> line = [ "<STR_LIT:U+0020>" + opt + "<STR_LIT:U+0020>" + type_value + '<STR_LIT>' , '<STR_LIT:|>' + self . arguments [ opt ] . description ] <EOL> self . arguments_string += self . tab ( line ) + '<STR_LIT:\n>' <EOL> if optional : <EOL> self . arguments_string += '<STR_LIT>' <EOL> for opt in optional : <EOL> if not self . arguments [ opt ] . deprecated_rm and not self . arguments [ opt ] . deprecated and self . arguments [ opt ] . deprecated_by is None : <EOL> self . arguments_string += '<STR_LIT>' <EOL> if self . arguments [ opt ] . order in self . section : <EOL> self . arguments_string += self . section [ self . arguments [ opt ] . order ] + '<STR_LIT:\n>' <EOL> type_value = self . refactor_type_value ( opt ) <EOL> line = [ "<STR_LIT:U+0020>" + opt + "<STR_LIT:U+0020>" + type_value + '<STR_LIT>' , '<STR_LIT:|>' + self . arguments [ opt ] . description ] <EOL> self . arguments_string += self . tab ( line ) + '<STR_LIT:\n>' <EOL> def refactor_type_value ( self , opt ) : <EOL> if self . arguments [ opt ] . type_value is None : <EOL> type_value = '<STR_LIT>' <EOL> elif self . arguments [ opt ] . type_value == '<STR_LIT>' : <EOL> type_value = self . print_list_with_brackets ( self . arguments [ opt ] . example ) <EOL> elif type ( self . arguments [ opt ] . type_value ) is list : <EOL> type_value = '<STR_LIT>' <EOL> else : <EOL> type_value = '<STR_LIT:<>' + self . arguments [ opt ] . type_value + '<STR_LIT:>>' <EOL> return type_value <EOL> def set_example ( self ) : <EOL> from os . path import basename <EOL> self . example = '<STR_LIT>' + '<STR_LIT>' + basename ( self . file ) <EOL> sorted_arguments = sorted ( self . arguments . items ( ) , key = lambda x : x [ <NUM_LIT:1> ] . order ) <EOL> mandatory = [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . mandatory ] <EOL> for opt in [ opt [ <NUM_LIT:0> ] for opt in sorted_arguments if self . arguments [ opt [ <NUM_LIT:0> ] ] . example ] : <EOL> if type ( self . arguments [ opt ] . example ) is list : <EOL> self . example += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + str ( self . arguments [ opt ] . example [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . example += '<STR_LIT:U+0020>' + opt + '<STR_LIT:U+0020>' + str ( self . arguments [ opt ] . example ) <EOL> self . example += '<STR_LIT>' <EOL> def generate ( self , error = None ) : <EOL> self . set_description ( self . parser . usage . description [ <NUM_LIT:2> + len ( '<STR_LIT:description>' ) : ] ) <EOL> self . set_arguments ( ) <EOL> self . set_usage ( ) <EOL> self . set_example ( ) <EOL> doc = self . header + self . description + self . usage + self . arguments_string + self . example <EOL> from os . path import basename <EOL> file_doc_sf = open ( '<STR_LIT>' + basename ( self . file ) [ : - <NUM_LIT:3> ] + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> file_doc_sf . write ( doc ) <EOL> file_doc_sf . close ( ) <EOL> print doc <EOL> def error ( self , error = None ) : <EOL> if error : <EOL> self . generate ( error ) <EOL> else : <EOL> print self . generate ( ) <EOL> from sys import exit <EOL> exit ( <NUM_LIT:0> ) <EOL> def print_list_with_brackets ( self , l ) : <EOL> type_value = '<STR_LIT:{>' <EOL> for char in l : <EOL> type_value += str ( char ) + '<STR_LIT:U+002C>' <EOL> type_value = type_value [ : - <NUM_LIT:1> ] <EOL> type_value += '<STR_LIT:}>' <EOL> return type_value <EOL> def tab ( self , strings ) : <EOL> """<STR_LIT>""" <EOL> tab = '<STR_LIT>' <EOL> for string in strings : <EOL> if len ( string ) < <NUM_LIT:30> : <EOL> spaces = '<STR_LIT:U+0020>' * ( <NUM_LIT:30> - len ( string ) ) <EOL> string += spaces <EOL> tab += string <EOL> return tab <EOL> def align ( self , string , length = <NUM_LIT> , pad = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> s = '<STR_LIT>' <EOL> strings = [ ] <EOL> string_split_line = string . split ( '<STR_LIT:\n>' ) <EOL> if len ( string_split_line ) > <NUM_LIT:1> : <EOL> for i in range ( <NUM_LIT:0> , len ( string_split_line ) ) : <EOL> if i != <NUM_LIT:0> : <EOL> string_split_line [ i ] = '<STR_LIT:U+0020>' + string_split_line [ i ] <EOL> for k , stri in enumerate ( string_split_line ) : <EOL> i = <NUM_LIT:0> <EOL> for c in stri : <EOL> i += <NUM_LIT:1> <EOL> if c == '<STR_LIT:U+0020>' : <EOL> last_space = i <EOL> if i % length == <NUM_LIT:0> : <EOL> strings . append ( stri [ <NUM_LIT:0> : last_space ] ) <EOL> stri = stri [ last_space : ] <EOL> if k != <NUM_LIT:0> : <EOL> stri = '<STR_LIT:U+0020>' + stri <EOL> i = i - last_space <EOL> strings . append ( stri ) <EOL> for i , yes in enumerate ( strings ) : <EOL> if i != <NUM_LIT:0> : <EOL> s += '<STR_LIT:U+0020>' * pad <EOL> s += yes <EOL> if i != len ( strings ) - <NUM_LIT:1> : <EOL> s += '<STR_LIT:\n>' <EOL> return s <EOL> class SpellingChecker : <EOL> def __init__ ( self ) : <EOL> self . alphabet = '<STR_LIT>' <EOL> def setWordsAsList ( self , words_dict ) : <EOL> self . NWORDS = self . train ( words_dict ) <EOL> def setWordsAsText ( self , text ) : <EOL> self . NWORDS = self . train ( self . words ( text ) ) <EOL> def setWordsAsFile ( self , fname ) : <EOL> self . NWORDS = self . train ( self . words ( file ( fname ) . read ( ) ) ) <EOL> def words ( self , text ) : <EOL> from re import findall <EOL> return findall ( '<STR_LIT>' , text . lower ( ) ) <EOL> def train ( self , features ) : <EOL> from collections import defaultdict <EOL> model = defaultdict ( lambda : <NUM_LIT:1> ) <EOL> for f in features : <EOL> model [ f ] += <NUM_LIT:1> <EOL> return model <EOL> def edits1 ( self , word ) : <EOL> splits = [ ( word [ : i ] , word [ i : ] ) for i in range ( len ( word ) + <NUM_LIT:1> ) ] <EOL> deletes = [ a + b [ <NUM_LIT:1> : ] for a , b in splits if b ] <EOL> transposes = [ a + b [ <NUM_LIT:1> ] + b [ <NUM_LIT:0> ] + b [ <NUM_LIT:2> : ] for a , b in splits if len ( b ) > <NUM_LIT:1> ] <EOL> replaces = [ a + c + b [ <NUM_LIT:1> : ] for a , b in splits for c in self . alphabet if b ] <EOL> inserts = [ a + c + b for a , b in splits for c in self . alphabet ] <EOL> return set ( deletes + transposes + replaces + inserts ) <EOL> def known_edits2 ( self , word ) : <EOL> return set ( e2 for e1 in self . edits1 ( word ) for e2 in self . edits1 ( e1 ) if e2 in self . NWORDS ) <EOL> def known ( self , words ) : return set ( w for w in words if w in self . NWORDS ) <EOL> def correct ( self , word ) : <EOL> return self . known ( [ word ] ) or self . known ( self . edits1 ( word ) ) or self . known_edits2 ( word ) </s>
