<s> import os <EOL> import wx <EOL> from wx . lib . filebrowsebutton import DirBrowseButton <EOL> from robotide . controller . commands import CreateNewResource , AddTestDataDirectory , AddTestCaseFile , CreateNewDirectoryProject , CreateNewFileProject , SetFileFormat , SetFileFormatRecuresively <EOL> from robotide . utils import overrides <EOL> from robotide . widgets import Label , Dialog <EOL> from robotide . validators import NonEmptyValidator , NewSuitePathValidator , SuiteFileNameValidator <EOL> DirBrowseButton . createLabel = lambda self : Label ( self , size = ( <NUM_LIT> , - <NUM_LIT:1> ) , label = self . labelText ) <EOL> class _CreationDialog ( Dialog ) : <EOL> def __init__ ( self , default_dir , title ) : <EOL> sizer = self . _init_dialog ( title ) <EOL> label_sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> self . _name_editor = self . _create_name_editor ( label_sizer ) <EOL> self . _parent_chooser = self . _create_parent_chooser ( <EOL> label_sizer , default_dir ) <EOL> self . _path_display = self . _create_path_display ( <EOL> label_sizer , default_dir ) <EOL> radio_group_sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> self . _type_chooser = self . _create_type_chooser ( radio_group_sizer ) <EOL> self . _format_chooser = self . _create_format_chooser ( radio_group_sizer ) <EOL> edit_sizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> edit_sizer . Add ( label_sizer , <NUM_LIT:1> , wx . EXPAND ) <EOL> edit_sizer . Add ( radio_group_sizer ) <EOL> sizer . Add ( edit_sizer , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . _finalize_dialog ( sizer ) <EOL> self . _name_editor . SetFocus ( ) <EOL> def _init_dialog ( self , title ) : <EOL> Dialog . __init__ ( self , title ) <EOL> return wx . BoxSizer ( wx . VERTICAL ) <EOL> def _finalize_dialog ( self , sizer ) : <EOL> self . _create_horizontal_line ( sizer ) <EOL> self . _create_buttons ( sizer ) <EOL> sizer . Fit ( self ) <EOL> self . SetSizer ( sizer ) <EOL> def _create_name_editor ( self , sizer ) : <EOL> disp_sizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> self . _add_label ( disp_sizer , '<STR_LIT:Name>' ) <EOL> name_editor = wx . TextCtrl ( self ) <EOL> name_editor . SetValidator ( NonEmptyValidator ( '<STR_LIT:Name>' ) ) <EOL> self . Bind ( wx . EVT_TEXT , self . OnPathChanged , name_editor ) <EOL> disp_sizer . Add ( name_editor , <NUM_LIT:1> , wx . ALIGN_CENTRE | wx . ALL | wx . EXPAND , <NUM_LIT:3> ) <EOL> sizer . Add ( disp_sizer , <NUM_LIT:1> , wx . EXPAND ) <EOL> return name_editor <EOL> def _add_label ( self , sizer , text ) : <EOL> label = Label ( self , label = text , size = ( <NUM_LIT> , - <NUM_LIT:1> ) ) <EOL> sizer . Add ( label , flag = wx . CENTER | wx . ALL , border = <NUM_LIT:3> ) <EOL> def _create_type_chooser ( self , sizer ) : <EOL> return self . _create_radiobuttons ( sizer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _create_format_chooser ( self , sizer , callback = True ) : <EOL> return self . _create_radiobuttons ( <EOL> sizer , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , callback ) <EOL> def _create_radiobuttons ( self , sizer , label , choices , callback = True ) : <EOL> radios = wx . RadioBox ( self , label = label , choices = choices ) <EOL> if callback : <EOL> self . Bind ( wx . EVT_RADIOBOX , self . OnPathChanged , radios ) <EOL> sizer . Add ( radios , flag = wx . ALIGN_LEFT | wx . ALL , border = <NUM_LIT:5> ) <EOL> return radios <EOL> def _create_parent_chooser ( self , sizer , default_dir ) : <EOL> browser = DirBrowseButton ( self , labelText = '<STR_LIT>' , <EOL> dialogTitle = '<STR_LIT>' , <EOL> startDirectory = default_dir , <EOL> size = ( <NUM_LIT> , - <NUM_LIT:1> ) , newDirectory = True , <EOL> changeCallback = self . OnPathChanged ) <EOL> browser . SetValue ( default_dir ) <EOL> sizer . Add ( browser , <NUM_LIT:1> , wx . EXPAND ) <EOL> return browser <EOL> def _create_parent_display ( self , sizer , path ) : <EOL> return self . _create_display ( sizer , '<STR_LIT>' , path ) <EOL> def _create_path_display ( self , sizer , path ) : <EOL> return self . _create_display ( sizer , '<STR_LIT>' , path , <EOL> NewSuitePathValidator ( ) ) <EOL> def _create_display ( self , sizer , title , value , validator = None ) : <EOL> disp_sizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> self . _add_label ( disp_sizer , title ) <EOL> disp = wx . TextCtrl ( self , value = value ) <EOL> disp . SetSizeHints ( self . GetTextExtent ( value ) [ <NUM_LIT:0> ] + <NUM_LIT:100> , - <NUM_LIT:1> ) <EOL> disp . SetEditable ( False ) <EOL> disp . SetBackgroundColour ( '<STR_LIT>' ) <EOL> if validator : <EOL> disp . SetValidator ( validator ) <EOL> disp_sizer . Add ( disp , <NUM_LIT:1> , wx . ALL | wx . EXPAND , <NUM_LIT:3> ) <EOL> sizer . Add ( disp_sizer , <NUM_LIT:1> , wx . EXPAND ) <EOL> return disp <EOL> def _get_path ( self ) : <EOL> name = self . _name_editor . GetValue ( ) <EOL> path = os . path . join ( self . _parent_chooser . GetValue ( ) , <EOL> name . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) ) <EOL> if self . _is_dir_type ( ) : <EOL> path = os . path . join ( path , '<STR_LIT>' ) <EOL> return path + '<STR_LIT:.>' + self . _get_extension ( ) <EOL> def _is_dir_type ( self ) : <EOL> if not self . _type_chooser : <EOL> return False <EOL> return self . _type_chooser . GetStringSelection ( ) == '<STR_LIT>' <EOL> def _get_extension ( self ) : <EOL> if not self . _format_chooser : <EOL> return '<STR_LIT:html>' <EOL> return self . _format_chooser . GetStringSelection ( ) . lower ( ) <EOL> def OnPathChanged ( self , event ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> self . _path_display . SetValue ( self . _get_path ( ) ) <EOL> event . Skip ( ) <EOL> class _WithImmutableParent ( object ) : <EOL> def _create_parent_chooser ( self , sizer , default_dir ) : <EOL> return self . _create_parent_display ( sizer , self . _path ) <EOL> class NewProjectDialog ( _CreationDialog ) : <EOL> def __init__ ( self , project ) : <EOL> self . _controller = project <EOL> _CreationDialog . __init__ ( self , project . default_dir , '<STR_LIT>' ) <EOL> def _execute ( self ) : <EOL> cmd = CreateNewDirectoryProject if self . _is_dir_type ( ) else CreateNewFileProject <EOL> cmd ( self . _get_path ( ) ) . execute ( self . _controller ) <EOL> class NewResourceDialog ( _WithImmutableParent , _CreationDialog ) : <EOL> def __init__ ( self , controller , settings ) : <EOL> self . _path = controller . directory <EOL> _CreationDialog . __init__ ( self , controller . default_dir , <EOL> '<STR_LIT>' ) <EOL> self . _format_chooser . SetStringSelection ( <EOL> settings [ '<STR_LIT>' ] ) <EOL> self . _controller = controller <EOL> def _execute ( self ) : <EOL> self . _controller . execute ( CreateNewResource ( self . _get_path ( ) ) ) <EOL> def _create_type_chooser ( self , sizer ) : <EOL> return None <EOL> class AddSuiteDialog ( _WithImmutableParent , _CreationDialog ) : <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , controller , settings ) : <EOL> self . _controller = controller <EOL> self . _path = controller . directory <EOL> _CreationDialog . __init__ ( self , self . _path , self . NAME ) <EOL> self . _format_chooser . SetStringSelection ( <EOL> settings [ '<STR_LIT>' ] ) <EOL> @ overrides ( _CreationDialog ) <EOL> def _create_name_editor ( self , sizer ) : <EOL> name_editor = _CreationDialog . _create_name_editor ( self , sizer ) <EOL> name_editor . SetValidator ( <EOL> SuiteFileNameValidator ( '<STR_LIT:Name>' , self . _is_dir_type ) ) <EOL> return name_editor <EOL> def _execute ( self ) : <EOL> cmd = AddTestDataDirectory if self . _is_dir_type ( ) else AddTestCaseFile <EOL> self . _controller . execute ( cmd ( self . _get_path ( ) ) ) <EOL> class AddDirectoryDialog ( AddSuiteDialog ) : <EOL> NAME = '<STR_LIT>' <EOL> def _create_type_chooser ( self , sizer ) : <EOL> return None <EOL> def _is_dir_type ( self ) : <EOL> return True <EOL> class _FileFormatDialog ( _CreationDialog ) : <EOL> def __init__ ( self , controller ) : <EOL> sizer = self . _init_dialog ( '<STR_LIT>' ) <EOL> self . _controller = controller <EOL> self . _create_help ( sizer ) <EOL> self . _chooser = self . _create_format_chooser ( sizer , callback = False ) <EOL> self . _chooser . SetStringSelection ( controller . get_format ( ) or '<STR_LIT>' ) <EOL> self . _recursive = self . _create_recursion_selector ( sizer ) <EOL> self . _finalize_dialog ( sizer ) <EOL> def _create_help ( self , sizer ) : <EOL> pass <EOL> def _create_recursion_selector ( self , sizer ) : <EOL> return None <EOL> def _get_format ( self ) : <EOL> return self . _chooser . GetStringSelection ( ) <EOL> class ChangeFormatDialog ( _FileFormatDialog ) : <EOL> def _create_recursion_selector ( self , sizer ) : <EOL> if not self . _controller . is_directory_suite ( ) : <EOL> return None <EOL> selector = wx . CheckBox ( self , label = '<STR_LIT>' ) <EOL> selector . SetValue ( True ) <EOL> sizer . Add ( selector , flag = wx . ALL , border = <NUM_LIT:5> ) <EOL> return selector <EOL> def _execute ( self ) : <EOL> cmd = SetFileFormat if not self . _get_recursive ( ) else SetFileFormatRecuresively <EOL> self . _controller . execute ( cmd ( self . _get_format ( ) ) ) <EOL> def _get_recursive ( self ) : <EOL> return self . _recursive and self . _recursive . IsChecked ( ) <EOL> class InitFileFormatDialog ( _FileFormatDialog ) : <EOL> def _create_help ( self , sizer ) : <EOL> help = '<STR_LIT>' % self . _controller . directory <EOL> sizer . Add ( Label ( self , label = help ) , flag = wx . ALL , border = <NUM_LIT:5> ) <EOL> def _execute ( self ) : <EOL> self . _controller . execute ( SetFileFormat ( self . _get_format ( ) ) ) <EOL> class RobotFilePathDialog ( wx . FileDialog ) : <EOL> def __init__ ( self , window , controller , settings ) : <EOL> self . _controller = controller <EOL> wx . FileDialog . __init__ ( <EOL> self , window , style = wx . OPEN , wildcard = self . _get_wildcard ( settings ) , <EOL> defaultDir = self . _controller . default_dir , message = '<STR_LIT>' ) <EOL> def _get_wildcard ( self , settings ) : <EOL> fileTypes = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:all>' , '<STR_LIT>' ) <EOL> ] <EOL> default_format = settings [ '<STR_LIT>' ] <EOL> if default_format not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> default_format = '<STR_LIT:all>' <EOL> first = [ ft for ft in fileTypes if ft [ <NUM_LIT:0> ] == default_format ] <EOL> rest = [ ft for ft in fileTypes if ft [ <NUM_LIT:0> ] != default_format ] <EOL> return '<STR_LIT:|>' . join ( ft [ <NUM_LIT:1> ] for ft in first + rest ) <EOL> def execute ( self ) : <EOL> if self . ShowModal ( ) == wx . ID_OK : <EOL> path = self . GetPath ( ) <EOL> self . _controller . update_default_dir ( path ) <EOL> else : <EOL> path = None <EOL> self . Destroy ( ) <EOL> return path </s>
