Changeset 1323

Show
Ignore:
Timestamp:
05/11/08 07:16:36 (8 months ago)
Author:
klattimer
Message:

Cleaning up preferences

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wine-doors/branches/0.2/src/log.py

    r1322 r1323  
    1616 
    1717        if not logpath: 
    18             logpath = os.expanduser("~/.wine-doors/wine-doors.log") 
     18            logpath = os.path.expanduser("~/.wine-doors/wine-doors.log") 
    1919        (path, logfile) = os.path.split( logpath ) 
    2020        if not os.path.exists( logpath ): 
  • wine-doors/branches/0.2/src/preferences.py

    r1215 r1323  
    88    """ 
    99    XML parser for the preferences file, the spec for this can be found at 
    10     http://www.wine-doors.org/trac/wiki/Preferences02 TODO 
     10    http://www.wine-doors.org/trac/wiki/Preferences02 
    1111    """ 
    1212    def __init__ ( self, preferences, repositories, bottles ): 
     
    2323        self.repo_password = None 
    2424        self.bottle_path = None 
     25        self.bottles_base = None 
    2526     
    2627    def startElement( self, name, attrs ): 
    27         """ 
    28         Most elements are simple <tag>data</tag> type elements 
    29         however there are also repositories and bottles; 
    30  
    31         <repositories> 
    32              <repo name="..." src="..."  
    33                    username="..." password="..." /> 
    34         </repositories> 
    35  
    36         <bottles> 
    37             <bottle path="..." ...? /> 
    38         </bottle> 
    39         """ 
    4028        if name == "winedoors-prefernces": 
    4129            self.in_conf_file = True 
     
    4432        elif name == "bottles" and self.in_conf_file: 
    4533            self.in_bottles = True 
    46         elif name == "repo" and self.in_repo_list: 
     34            self.bottles_base = attrs.get( 'root' ) 
     35        elif name == "repository" and self.in_repo_list: 
    4736            self.repo_name = attrs.get( 'name' ) 
    4837            self.repo_name = self.repo_name.rstrip() 
     
    8877 
    8978class Preferences: 
    90     def __init__( self, parent=None ): 
     79    def __init__( self, parent=None, preferences_file=None ): 
    9180        self.parent = parent 
    9281        self.bottles = [] 
     
    9483        self.licenses = [] 
    9584        self.preferences = {} 
     85 
     86        if not preferences_file or not os.path.isfile( preferences_file ): 
     87            preferences_file = os.path.expanduser( "~/.wine-doors/preferences.xml") 
     88            if os.path.isfile( preferences_file ): 
     89                self.preferences_file = preferences_file 
     90            else: 
     91                preferences_file = "/etc/wine-doors/preferences.xml" 
     92                if not os.path.isfile( preferences_file ): 
     93                    os.popen( "mkdir " + os.path.expanduser( "~/.wine-doors" ) ) 
     94                    self.preferences_file = os.path.expanduser( "~/.wine-doors/preferences.xml" ) 
     95                    self.Default() 
     96                    self.DefaultRepos() 
     97                    self.Save() 
     98        else: 
     99            self.preferences_file = preferences_file 
    96100 
    97101    def __setitem__( self, key, value ): 
     
    133137        pass 
    134138 
    135     def Load( self, prefsfile=None ): 
     139    def Load( self ): 
    136140        """ 
    137141        Load the preferences.xml file which is found in the <wine root>/wine-doors/ 
    138142        """ 
    139         if not prefsfile or not os.path.isfile( prefsfile ): 
    140             userprefs = os.path.expanduser( "~/.wine-doors/preferences.xml") 
    141             if os.path.isfile( userprefs ): 
    142                 prefsfile = userprefs 
    143             else: 
    144                 prefsfile = "/etc/wine-doors/preferences.xml" 
    145                 if not os.path.isfile( prefsfile ): 
    146                     os.popen( "mkdir " + os.path.expanduser( "~/.wine-doors" ) ) 
    147                     prefsfile = os.path.expanduser( "~/.wine-doors/preferences.xml" ) 
    148                     self.Default() 
    149                     self.DefaultRepos() 
    150                     self.Save() 
    151  
    152143        temp_repos = [] 
    153144        temp_bottles = [] 
     
    155146        parser = make_parser() 
    156147        parser.setContentHandler( handler ) 
    157         parser.parse( prefsfile ) 
     148        parser.parse( self.preferences_file ) 
    158149 
    159150        for repo in temp_repos: 
     
    164155            self.AddBottle( ''.join( repo[0:1] ), ''.join( repo[1:2] ), ''.join( repo[2:] ) ) 
    165156 
    166     def Save( self, prefsfile=None ): 
     157    def Save( self, preferences_file=None ): 
    167158        """ Spit out an xml file, this could do with some sanity checking, to make sure its a valid file """ 
    168         if not prefsfile: 
    169             prefsfile = os.path.expanduser( "~/.wine-doors/preferences.xml" ) 
    170             (path, filename) = os.path.split( prefsfile ) 
     159        if not prefernces_file and self.preferences_file: 
     160            preferences_file = self.preferences_file 
     161        elif not preferences_file and not self.preferences_file: 
     162            preferences_file = os.path.expanduser( "~/.wine-doors/preferences.xml" ) 
     163            (path, filename) = os.path.split( preferences_file ) 
    171164 
    172165            if not os.path.exists( path ): 
    173166                os.makedirs( path ) 
    174167 
    175         f = open( prefsfile, "wt" ) 
     168        f = open( preferences_file, "wt" ) 
    176169        f.write( "<winedoors-preferences>\n" ) 
    177170 
     
    197190 
    198191if __name__ == "__main__": 
    199     print "----------Preferences----------" 
     192    print "Unit testing: Preferences" 
     193    print "------------------------------------------------" 
    200194    preferences = Preferences() 
     195    print "Keys/Values:" 
    201196    for key, value in preferences.preferences.iteritems(): 
    202         print key, '=', value 
    203     print "-------------------------------"   
    204     print "----------Repositories---------
     197        print key, ' = ', value 
     198    print 
     199    print "Repositories:
    205200    for name, uri, username, password in preferences.repositories: 
    206         print 'Repository name: '.name, ' URI: '.uri  
    207     print "-------------------------------"  
    208  
    209     print "Setting name to \"testing save\"" 
     201        print "Repository, name: " + name + " URI: " + uri 
     202    print 
     203    print "Bottles:" 
     204    for name, path, icon in preferences.bottles: 
     205        print "Bottle, name: " + name + " path: " + path 
     206    print 
     207 
     208    print "Testing load/save:" 
     209    print "Setting name to \"testing\"" 
    210210    oldpref = preferences["name"] 
    211     preferences["name"] = "testing save" 
    212     print "----------Preferences----------" 
    213     for key, value in preferences.preferences.iteritems(): 
    214         print key, '=', value 
    215     print "-------------------------------"  
     211    preferences["name"] = "testing" 
    216212    preferences.Save() 
    217213    print "Saved" 
    218214    print "Loading" 
    219215    preferences.Load() 
    220     print "----------Prefs---------" 
    221     for key, value in prefs.iteritems(): 
    222         print key, '=', value 
    223     print "------------------------"   
     216    print "preferences['name'] = " + preferences['name'] 
    224217    preferences["name"] = oldpref 
    225218    preferences.Save() 
  • wine-doors/branches/0.2/src/winedoors.py

    r1322 r1323  
    124124    except KeyboardInterrupt: 
    125125        print "^c caught.  Quitting." 
     126        winedoors.log.End() 
    126127        quit() 
    127128        #TODO: Check whats happening before quiting.