<s> """<STR_LIT>""" <EOL> import os <EOL> import wx <EOL> from wx . xrc import * <EOL> from wx . lib . pubsub import Publisher as pub <EOL> from dicompyler import guiutil , util <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> class PreferencesManager ( ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , name = None , appname = "<STR_LIT>" , <EOL> filename = '<STR_LIT>' ) : <EOL> res = XmlResource ( util . GetResourcePath ( '<STR_LIT>' ) ) <EOL> self . dlgPreferences = res . LoadDialog ( None , "<STR_LIT>" ) <EOL> self . dlgPreferences . Init ( name , appname ) <EOL> pub . subscribe ( self . SetPreferenceTemplate , '<STR_LIT>' ) <EOL> pub . subscribe ( self . SavePreferenceValues , '<STR_LIT>' ) <EOL> pub . subscribe ( self . GetPreferenceValue , '<STR_LIT>' ) <EOL> pub . subscribe ( self . GetPreferenceValues , '<STR_LIT>' ) <EOL> pub . subscribe ( self . SetPreferenceValue , '<STR_LIT>' ) <EOL> self . preftemplate = [ ] <EOL> self . values = { } <EOL> self . filename = os . path . join ( guiutil . get_data_dir ( ) , filename ) <EOL> self . LoadPreferenceValues ( ) <EOL> def __del__ ( self ) : <EOL> if self . dlgPreferences : <EOL> self . dlgPreferences . Destroy ( ) <EOL> def Show ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . dlgPreferences . IsShown ( ) : <EOL> self . dlgPreferences . LoadPreferences ( self . preftemplate , self . values ) <EOL> self . dlgPreferences . Hide ( ) <EOL> else : <EOL> self . dlgPreferences . Hide ( ) <EOL> self . dlgPreferences . Show ( ) <EOL> def SetPreferenceTemplate ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . preftemplate = msg . data <EOL> self . dlgPreferences . LoadPreferences ( self . preftemplate , self . values ) <EOL> def LoadPreferenceValues ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isfile ( self . filename ) : <EOL> with open ( self . filename , mode = '<STR_LIT:r>' ) as f : <EOL> try : <EOL> self . values = json . load ( f ) <EOL> except ValueError : <EOL> self . values = { } <EOL> else : <EOL> self . values = { } <EOL> def SavePreferenceValues ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . values = msg . data <EOL> with open ( self . filename , mode = '<STR_LIT:w>' ) as f : <EOL> json . dump ( self . values , f , sort_keys = True , indent = <NUM_LIT:4> ) <EOL> def GetPreferenceValue ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> query = msg . data . split ( '<STR_LIT:.>' ) <EOL> v = self . values <EOL> if v . has_key ( query [ <NUM_LIT:0> ] ) : <EOL> if v [ query [ <NUM_LIT:0> ] ] . has_key ( query [ <NUM_LIT:1> ] ) : <EOL> if v [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] . has_key ( query [ <NUM_LIT:2> ] ) : <EOL> pub . sendMessage ( msg . data , v [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] [ query [ <NUM_LIT:2> ] ] ) <EOL> def GetPreferenceValues ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> query = msg . data . split ( '<STR_LIT:.>' ) <EOL> v = self . values <EOL> if v . has_key ( query [ <NUM_LIT:0> ] ) : <EOL> if v [ query [ <NUM_LIT:0> ] ] . has_key ( query [ <NUM_LIT:1> ] ) : <EOL> for setting , value in v [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] . iteritems ( ) : <EOL> message = msg . data + '<STR_LIT:.>' + setting <EOL> pub . sendMessage ( message , value ) <EOL> def SetPreferenceValue ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> SetValue ( self . values , msg . data . keys ( ) [ <NUM_LIT:0> ] , msg . data . values ( ) [ <NUM_LIT:0> ] ) <EOL> pub . sendMessage ( '<STR_LIT>' , self . values ) <EOL> pub . sendMessage ( msg . data . keys ( ) [ <NUM_LIT:0> ] , msg . data . values ( ) [ <NUM_LIT:0> ] ) <EOL> class PreferencesDialog ( wx . Dialog ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pre = wx . PreDialog ( ) <EOL> self . PostCreate ( pre ) <EOL> def Init ( self , name = None , appname = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if guiutil . IsMac ( ) : <EOL> XRCCTRL ( self , '<STR_LIT>' ) . Hide ( ) <EOL> else : <EOL> self . SetIcon ( guiutil . get_icon ( ) ) <EOL> if name : <EOL> self . SetTitle ( name ) <EOL> self . notebook = XRCCTRL ( self , '<STR_LIT>' ) <EOL> for child in self . GetChildren ( ) : <EOL> guiutil . adjust_control ( child ) <EOL> self . Bind ( wx . EVT_CLOSE , self . OnClose ) <EOL> self . Bind ( wx . EVT_WINDOW_DESTROY , self . OnClose ) <EOL> wx . EVT_BUTTON ( self , XRCID ( '<STR_LIT>' ) , self . OnClose ) <EOL> self . preftemplate = [ ] <EOL> self . values = { } <EOL> self . appname = appname <EOL> def LoadPreferences ( self , preftemplate , values ) : <EOL> """<STR_LIT>""" <EOL> self . preftemplate = preftemplate <EOL> self . values = values <EOL> self . notebook . DeleteAllPages ( ) <EOL> self . callbackdict = { } <EOL> for template in self . preftemplate : <EOL> panel = self . CreatePreferencePanel ( template . values ( ) [ <NUM_LIT:0> ] ) <EOL> self . notebook . AddPage ( panel , template . keys ( ) [ <NUM_LIT:0> ] ) <EOL> def CreatePreferencePanel ( self , prefpaneldata ) : <EOL> """<STR_LIT>""" <EOL> panel = wx . Panel ( self . notebook , - <NUM_LIT:1> ) <EOL> border = wx . BoxSizer ( wx . VERTICAL ) <EOL> show_restart = False <EOL> for group in prefpaneldata : <EOL> bsizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> bsizer . Add ( ( <NUM_LIT:0> , <NUM_LIT:5> ) ) <EOL> hsizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> hsizer . Add ( ( <NUM_LIT:12> , <NUM_LIT:0> ) ) <EOL> h = wx . StaticText ( panel , - <NUM_LIT:1> , group . keys ( ) [ <NUM_LIT:0> ] ) <EOL> font = h . GetFont ( ) <EOL> font . SetWeight ( wx . FONTWEIGHT_BOLD ) <EOL> h . SetFont ( font ) <EOL> hsizer . Add ( h ) <EOL> bsizer . Add ( hsizer ) <EOL> bsizer . Add ( ( <NUM_LIT:0> , <NUM_LIT:7> ) ) <EOL> fgsizer = wx . FlexGridSizer ( len ( group . values ( ) [ <NUM_LIT:0> ] ) , <NUM_LIT:4> , <NUM_LIT:10> , <NUM_LIT:4> ) <EOL> fgsizer . AddGrowableCol ( <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> for setting in group . values ( ) [ <NUM_LIT:0> ] : <EOL> fgsizer . Add ( ( <NUM_LIT> , <NUM_LIT:0> ) ) <EOL> restart = str ( '<STR_LIT:*>' if '<STR_LIT>' in setting else '<STR_LIT>' ) <EOL> if ( '<STR_LIT>' in setting ) : <EOL> if ( setting [ '<STR_LIT>' ] == True ) : <EOL> show_restart = True <EOL> t = wx . StaticText ( panel , - <NUM_LIT:1> , setting [ '<STR_LIT:name>' ] + restart + '<STR_LIT::>' , <EOL> style = wx . ALIGN_RIGHT ) <EOL> fgsizer . Add ( t , <NUM_LIT:0> , wx . ALIGN_CENTER_VERTICAL | wx . ALIGN_RIGHT ) <EOL> sizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> value = GetValue ( self . values , setting ) <EOL> SetValue ( self . values , setting [ '<STR_LIT>' ] , value ) <EOL> if ( setting [ '<STR_LIT:type>' ] == '<STR_LIT>' ) : <EOL> c = wx . Choice ( panel , - <NUM_LIT:1> , choices = setting [ '<STR_LIT>' ] ) <EOL> c . SetStringSelection ( value ) <EOL> sizer . Add ( c , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> self . callbackdict [ c ] = setting [ '<STR_LIT>' ] <EOL> self . Bind ( wx . EVT_CHOICE , self . OnUpdateChoice , c ) <EOL> elif ( setting [ '<STR_LIT:type>' ] == '<STR_LIT>' ) : <EOL> c = wx . CheckBox ( panel , - <NUM_LIT:1> , setting [ '<STR_LIT:name>' ] + restart ) <EOL> c . SetValue ( value ) <EOL> sizer . Add ( c , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> t = self . FindWindowById ( c . PrevControlId ( c . GetId ( ) ) ) <EOL> t . SetLabel ( '<STR_LIT>' ) <EOL> fgsizer . GetItem ( <NUM_LIT:0> ) . SetSpacer ( ( <NUM_LIT:20> , <NUM_LIT:0> ) ) <EOL> self . callbackdict [ c ] = setting [ '<STR_LIT>' ] <EOL> self . Bind ( wx . EVT_CHECKBOX , self . OnUpdateCheckbox , c ) <EOL> elif ( setting [ '<STR_LIT:type>' ] == '<STR_LIT>' ) : <EOL> s = wx . Slider ( panel , - <NUM_LIT:1> , value , <EOL> setting [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , setting [ '<STR_LIT>' ] [ <NUM_LIT:1> ] , <EOL> size = ( <NUM_LIT> , - <NUM_LIT:1> ) , style = wx . SL_HORIZONTAL ) <EOL> sizer . Add ( s , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> t = wx . StaticText ( panel , - <NUM_LIT:1> , str ( value ) ) <EOL> sizer . Add ( ( <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> sizer . Add ( t , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> sizer . Add ( ( <NUM_LIT:6> , <NUM_LIT:0> ) ) <EOL> t = wx . StaticText ( panel , - <NUM_LIT:1> , setting [ '<STR_LIT>' ] ) <EOL> sizer . Add ( t , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> self . callbackdict [ s ] = setting [ '<STR_LIT>' ] <EOL> self . Bind ( wx . EVT_COMMAND_SCROLL_THUMBTRACK , self . OnUpdateSlider , s ) <EOL> self . Bind ( wx . EVT_COMMAND_SCROLL_CHANGED , self . OnUpdateSlider , s ) <EOL> elif ( setting [ '<STR_LIT:type>' ] == '<STR_LIT>' ) : <EOL> if not os . path . isdir ( value ) : <EOL> value = setting [ '<STR_LIT:default>' ] <EOL> SetValue ( self . values , setting [ '<STR_LIT>' ] , value ) <EOL> t = wx . TextCtrl ( panel , - <NUM_LIT:1> , value , style = wx . TE_READONLY ) <EOL> sizer . Add ( t , <NUM_LIT:1> , wx . ALIGN_CENTER ) <EOL> sizer . Add ( ( <NUM_LIT:5> , <NUM_LIT:0> ) ) <EOL> b = wx . Button ( panel , - <NUM_LIT:1> , "<STR_LIT>" ) <EOL> sizer . Add ( b , <NUM_LIT:0> , wx . ALIGN_CENTER ) <EOL> self . callbackdict [ b ] = setting [ '<STR_LIT>' ] <EOL> self . Bind ( wx . EVT_BUTTON , self . OnUpdateDirectory , b ) <EOL> for child in panel . GetChildren ( ) : <EOL> guiutil . adjust_control ( child ) <EOL> fgsizer . Add ( sizer , <NUM_LIT:1> , wx . EXPAND | wx . ALL ) <EOL> fgsizer . Add ( ( <NUM_LIT:12> , <NUM_LIT:0> ) ) <EOL> bsizer . Add ( fgsizer , <NUM_LIT:0> , wx . EXPAND | wx . ALL ) <EOL> border . Add ( bsizer , <NUM_LIT:0> , wx . EXPAND | wx . ALL , <NUM_LIT:2> ) <EOL> border . Add ( ( <NUM_LIT> , <NUM_LIT:20> ) , <NUM_LIT:0> , wx . EXPAND | wx . ALL ) <EOL> if show_restart : <EOL> r = wx . StaticText ( panel , - <NUM_LIT:1> , <EOL> '<STR_LIT>' + self . appname + '<STR_LIT>' , <EOL> style = wx . ALIGN_CENTER ) <EOL> font = r . GetFont ( ) <EOL> font . SetWeight ( wx . FONTWEIGHT_BOLD ) <EOL> r . SetFont ( font ) <EOL> border . Add ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , wx . EXPAND | wx . ALL ) <EOL> rhsizer = wx . BoxSizer ( wx . HORIZONTAL ) <EOL> rhsizer . Add ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , wx . EXPAND | wx . ALL ) <EOL> rhsizer . Add ( r ) <EOL> rhsizer . Add ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , <NUM_LIT:1> , wx . EXPAND | wx . ALL ) <EOL> border . Add ( rhsizer , <NUM_LIT:0> , wx . EXPAND | wx . ALL ) <EOL> border . Add ( ( <NUM_LIT:0> , <NUM_LIT:5> ) ) <EOL> panel . SetSizer ( border ) <EOL> return panel <EOL> def OnUpdateChoice ( self , evt ) : <EOL> """<STR_LIT>""" <EOL> c = evt . GetEventObject ( ) <EOL> pub . sendMessage ( self . callbackdict [ c ] , evt . GetString ( ) ) <EOL> SetValue ( self . values , self . callbackdict [ c ] , evt . GetString ( ) ) <EOL> def OnUpdateCheckbox ( self , evt ) : <EOL> """<STR_LIT>""" <EOL> c = evt . GetEventObject ( ) <EOL> pub . sendMessage ( self . callbackdict [ c ] , evt . IsChecked ( ) ) <EOL> SetValue ( self . values , self . callbackdict [ c ] , evt . IsChecked ( ) ) <EOL> def OnUpdateSlider ( self , evt ) : <EOL> """<STR_LIT>""" <EOL> s = evt . GetEventObject ( ) <EOL> t = self . FindWindowById ( s . NextControlId ( s . GetId ( ) ) ) <EOL> t . SetLabel ( str ( s . GetValue ( ) ) ) <EOL> pub . sendMessage ( self . callbackdict [ s ] , s . GetValue ( ) ) <EOL> SetValue ( self . values , self . callbackdict [ s ] , s . GetValue ( ) ) <EOL> def OnUpdateDirectory ( self , evt ) : <EOL> """<STR_LIT>""" <EOL> b = evt . GetEventObject ( ) <EOL> t = self . FindWindowById ( b . PrevControlId ( b . GetId ( ) ) ) <EOL> dlg = wx . DirDialog ( self , defaultPath = t . GetValue ( ) ) <EOL> if dlg . ShowModal ( ) == wx . ID_OK : <EOL> d = unicode ( dlg . GetPath ( ) ) <EOL> t . SetValue ( d ) <EOL> pub . sendMessage ( self . callbackdict [ b ] , d ) <EOL> SetValue ( self . values , self . callbackdict [ b ] , d ) <EOL> dlg . Destroy ( ) <EOL> def OnClose ( self , evt ) : <EOL> """<STR_LIT>""" <EOL> pub . sendMessage ( '<STR_LIT>' , self . values ) <EOL> self . Hide ( ) <EOL> def GetValue ( values , setting ) : <EOL> """<STR_LIT>""" <EOL> query = setting [ '<STR_LIT>' ] . split ( '<STR_LIT:.>' ) <EOL> value = setting [ '<STR_LIT:default>' ] <EOL> if values . has_key ( query [ <NUM_LIT:0> ] ) : <EOL> if values [ query [ <NUM_LIT:0> ] ] . has_key ( query [ <NUM_LIT:1> ] ) : <EOL> if values [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] . has_key ( query [ <NUM_LIT:2> ] ) : <EOL> value = values [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] [ query [ <NUM_LIT:2> ] ] <EOL> return value <EOL> def SetValue ( values , setting , value ) : <EOL> """<STR_LIT>""" <EOL> query = setting . split ( '<STR_LIT:.>' ) <EOL> if values . has_key ( query [ <NUM_LIT:0> ] ) : <EOL> if values [ query [ <NUM_LIT:0> ] ] . has_key ( query [ <NUM_LIT:1> ] ) : <EOL> values [ query [ <NUM_LIT:0> ] ] [ query [ <NUM_LIT:1> ] ] [ query [ <NUM_LIT:2> ] ] = value <EOL> else : <EOL> values [ query [ <NUM_LIT:0> ] ] . update ( { query [ <NUM_LIT:1> ] : { query [ <NUM_LIT:2> ] : value } } ) <EOL> else : <EOL> values [ query [ <NUM_LIT:0> ] ] = { query [ <NUM_LIT:1> ] : { query [ <NUM_LIT:2> ] : value } } <EOL> def main ( ) : <EOL> import tempfile , os <EOL> import wx <EOL> from wx . lib . pubsub import Publisher as pub <EOL> app = wx . App ( False ) <EOL> t = tempfile . NamedTemporaryFile ( delete = False ) <EOL> sp = wx . StandardPaths . Get ( ) <EOL> frame = wx . Frame ( None , wx . ID_ANY , "<STR_LIT>" ) <EOL> frame . Centre ( ) <EOL> frame . Show ( True ) <EOL> app . SetTopWindow ( frame ) <EOL> filename = t . name <EOL> frame . prefmgr = PreferencesManager ( parent = frame , appname = '<STR_LIT>' , <EOL> filename = filename ) <EOL> grp1template = [ <EOL> { '<STR_LIT>' : <EOL> [ { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:default>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:default>' : unicode ( sp . GetDocumentsDir ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] <EOL> } , <EOL> { '<STR_LIT>' : <EOL> [ { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:100> ] , <EOL> '<STR_LIT:default>' : <NUM_LIT:50> , <EOL> '<STR_LIT>' : '<STR_LIT:%>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] <EOL> } ] <EOL> grp2template = [ <EOL> { '<STR_LIT>' : <EOL> [ { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:100> ] , <EOL> '<STR_LIT:default>' : <NUM_LIT:50> , <EOL> '<STR_LIT>' : '<STR_LIT:%>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True } ] <EOL> } , <EOL> { '<STR_LIT>' : <EOL> [ { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:default>' : unicode ( sp . GetUserDataDir ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:default>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ] <EOL> } ] <EOL> preftemplate = [ { '<STR_LIT>' : grp1template } , { '<STR_LIT>' : grp2template } ] <EOL> def print_template_value ( msg ) : <EOL> """<STR_LIT>""" <EOL> print msg . topic , msg . data <EOL> pub . subscribe ( print_template_value , '<STR_LIT>' ) <EOL> pub . subscribe ( print_template_value , '<STR_LIT>' ) <EOL> pub . sendMessage ( '<STR_LIT>' , preftemplate ) <EOL> frame . prefmgr . Show ( ) <EOL> app . MainLoop ( ) <EOL> with open ( filename , mode = '<STR_LIT:r>' ) as f : <EOL> for line in f : <EOL> print line , <EOL> try : <EOL> os . remove ( filename ) <EOL> except WindowsError : <EOL> print '<STR_LIT>' + filename + '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
