~elachuni/software-center/pep8-test-part22

« back to all changes in this revision

Viewing changes to softwarecenter/backend/fake_review_settings.py

  • Committer: Anthony Lenton
  • Date: 2012-03-19 14:20:55 UTC
  • Revision ID: anthony.lenton@canonical.com-20120319142055-cb2e90mpgytb5mwm
Made all files in softwarecenter.backend pass the pep8 test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import pickle
5
5
from softwarecenter.paths import SOFTWARE_CENTER_CACHE_DIR
6
6
 
7
 
# decorator to add a fake network delay if set 
 
7
 
 
8
# decorator to add a fake network delay if set
8
9
# in FakeReviewSettings.fake_network_delay
9
10
def network_delay(fn):
10
11
    def slp(self, *args, **kwargs):
14
15
            time.sleep(delay)
15
16
        return fn(self, *args, **kwargs)
16
17
    return slp
17
 
   
 
18
 
 
19
 
18
20
class FakeReviewSettings(object):
19
 
    '''An object that simply holds settings which are used by RatingsAndReviewsAPI
20
 
       in the rnrclient_fake module. Using this module allows a developer to test
21
 
       the reviews functionality without any interaction with a reviews server.
22
 
       Each setting here provides complete control over how the 'server' will 
23
 
       respond. Changes to these settings should be made to the class attributes
24
 
       directly without creating an instance of this class.
25
 
       The intended usage is for unit tests where a predictable response is 
26
 
       required and where the application should THINK it has spoken to a 
27
 
       server. 
28
 
       The unit test would make changes to settings in this class before 
 
21
    '''An object that simply holds settings which are used by
 
22
       RatingsAndReviewsAPI in the rnrclient_fake module. Using this module
 
23
       allows a developer to test the reviews functionality without any
 
24
       interaction with a reviews server.  Each setting here provides complete
 
25
       control over how the 'server' will respond. Changes to these settings
 
26
       should be made to the class attributes directly without creating an
 
27
       instance of this class.
 
28
       The intended usage is for unit tests where a predictable response is
 
29
       required and where the application should THINK it has spoken to a
 
30
       server.
 
31
       The unit test would make changes to settings in this class before
29
32
       running the unit test.
30
33
    '''
31
 
    
 
34
 
32
35
    _FAKE_SETTINGS = {}
33
 
    
 
36
 
34
37
    #general settings
35
38
    #*****************************
36
39
    #delay (in seconds) before returning from any of the fake rnr methods
41
44
    #*****************************
42
45
    #raises APIError if True
43
46
    _FAKE_SETTINGS['server_response_error'] = False
44
 
    
 
47
 
45
48
    #review stats
46
49
    #*****************************
47
50
    #raises APIError if True
48
 
    _FAKE_SETTINGS['review_stats_error'] = False    
49
 
    
 
51
    _FAKE_SETTINGS['review_stats_error'] = False
 
52
 
50
53
    #the following has no effect if review_stats_error = True
51
 
    #determines the number of package stats (i.e. ReviewStats list size) to return
52
 
    #max 15 packages (any number higher than 15 will still return 15)
 
54
    #determines the number of package stats (i.e. ReviewStats list size) to
 
55
    #return max 15 packages (any number higher than 15 will still return 15)
53
56
    _FAKE_SETTINGS['packages_returned'] = 10
54
 
    
 
57
 
55
58
    #get reviews
56
59
    #*****************************
57
60
    #raises APIError if True
58
61
    _FAKE_SETTINGS['get_reviews_error'] = False
59
62
 
60
 
    #number of pages of 10 reviews to return before returning the number specified
61
 
    #in the reviews_returned value below
 
63
    #number of pages of 10 reviews to return before returning the number
 
64
    # specified in the reviews_returned value below
62
65
    _FAKE_SETTINGS['review_pages'] = 1
63
66
 
64
67
    #the following has no effect if get_reviews_error = True
65
 
    #determines number of reviews to return 
 
68
    #determines number of reviews to return
66
69
    # (Accepts 0 to n but should really be between 1 and 10)
67
70
    _FAKE_SETTINGS['reviews_returned'] = 3
68
 
    
 
71
 
69
72
    #get review
70
73
    #*****************************
71
74
    #raises APIError if True
72
75
    _FAKE_SETTINGS['get_review_error'] = False
73
 
    
 
76
 
74
77
    #submit review
75
78
    #*****************************
76
79
    #raises APIError if True
77
80
    _FAKE_SETTINGS['submit_review_error'] = False
78
 
    #fake username(str) and review_id(int) to give back with a successful review
 
81
    #fake username(str) and review_id(int) to give back with a successful
 
82
    # review
79
83
    #leave as None to generate a random username and review_id
80
84
    _FAKE_SETTINGS['reviewer_username'] = None
81
85
    _FAKE_SETTINGS['submit_review_id'] = None
82
 
    
 
86
 
83
87
    #flag review
84
88
    #*****************************
85
89
    #raises APIError if True
88
92
    _FAKE_SETTINGS['flagger_username'] = None
89
93
    #fake package name (str) to give back as flagged app
90
94
    _FAKE_SETTINGS['flag_package_name'] = None
91
 
    
 
95
 
92
96
    #submit usefulness
93
97
    #*****************************
94
98
    #raises APIError if True
95
99
    _FAKE_SETTINGS['submit_usefulness_error'] = False
96
 
    
 
100
 
97
101
    #the following has no effect if submit_usefulness_error = True
98
102
    #which string to pretend the server returned
99
103
    #choices are "Created", "Updated", "Not modified"
100
104
    _FAKE_SETTINGS['usefulness_response_string'] = "Created"
101
 
    
 
105
 
102
106
    #get usefulness
103
107
    #*****************************
104
108
    #raises APIError if True
105
109
    _FAKE_SETTINGS['get_usefulness_error'] = False
106
 
    
 
110
 
107
111
    #the following has no effect if get_usefulness_error = True
108
112
    #how many usefulness votes to return
109
113
    _FAKE_SETTINGS['votes_returned'] = 5
110
 
    
111
 
    #pre-configured review ids to return in the result 
 
114
 
 
115
    #pre-configured review ids to return in the result
112
116
    #if you don't complete this or enter less review ids than votes_returned
113
117
    #above, it will be random
114
 
    _FAKE_SETTINGS['required_review_ids'] = [3,6,15]
115
 
    
 
118
    _FAKE_SETTINGS['required_review_ids'] = [3, 6, 15]
 
119
 
116
120
    #THE FOLLOWING SETTINGS RELATE TO LOGIN SSO FUNCTIONALITY
117
121
    # LoginBackendDbusSSO
118
122
    # login()
119
123
    #***********************
120
 
    # what to fake the login response as 
 
124
    # what to fake the login response as
121
125
    # choices (strings): "successful", "failed", "denied"
122
126
    _FAKE_SETTINGS['login_response'] = "successful"
123
 
    
 
127
 
124
128
    # UbuntuSSOAPI
125
129
    # whoami()
126
130
    #***********************
127
 
    # what to fake whoami response as 
 
131
    # what to fake whoami response as
128
132
    # choices (strings): "whoami", "error"
129
133
    _FAKE_SETTINGS['whoami_response'] = "whoami"
130
134
    #this only has effect if whoami_response = 'whoami'
132
136
    #expects a string or None (for a random username)
133
137
    _FAKE_SETTINGS['whoami_username'] = None
134
138
 
135
 
 
136
139
    def __init__(self, defaults=False):
137
 
        '''Initialises the object and loads the settings into the _FAKE_SETTINGS
138
 
           dict.. If defaults is passed as True any existing settings in the cache 
139
 
           file are ignored and the cache file is overwritten with the defaults 
140
 
           set in the class. This is useful if you don't want previously used 
141
 
           settings from the cache file being used again'''
 
140
        '''Initialises the object and loads the settings into the
 
141
           _FAKE_SETTINGS dict.. If defaults is passed as True any existing
 
142
           settings in the cache file are ignored and the cache file is
 
143
           overwritten with the defaults set in the class. This is useful if
 
144
           you don't want previously used settings from the cache file being
 
145
           used again'''
142
146
        fname = 'fake_review_settings.p'
143
147
        self.LOCATION = os.path.join(SOFTWARE_CENTER_CACHE_DIR, fname)
144
148
        if defaults:
145
149
            self._save_settings()
146
150
        else:
147
151
            self._update_from_file()
148
 
        return
149
 
    
 
152
 
150
153
    def update_setting(self, key_name, new_value):
151
 
        '''Takes a string (key_name) which corresponds to a setting in this object 
152
 
        and updates it with the value passed in (new_value).
 
154
        '''Takes a string (key_name) which corresponds to a setting in this
 
155
        object and updates it with the value passed in (new_value).
153
156
        Raises a NameError if the setting name doesn't exist'''
154
 
       
 
157
 
155
158
        if not key_name in self._FAKE_SETTINGS:
156
 
            raise NameError ('Setting key name %s does not exist' % key_name)
 
159
            raise NameError('Setting key name %s does not exist' % key_name)
157
160
        else:
158
161
            self._FAKE_SETTINGS[key_name] = new_value
159
162
            self._save_settings()
160
163
        return
161
 
    
 
164
 
162
165
    def update_multiple(self, settings):
163
 
        '''Takes a dict (settings) of key,value pairs to perform multiple updates 
164
 
           in one action, then saves. Dict being passed should contain only keys that 
165
 
           match settings in this object or a NameError will be raised'''
 
166
        '''Takes a dict (settings) of key,value pairs to perform multiple
 
167
           updates in one action, then saves. Dict being passed should contain
 
168
           only keys that match settings in this object or a NameError will be
 
169
           raised'''
166
170
        for key, value in settings.items():
167
171
            if not key in self._FAKE_SETTINGS:
168
 
                raise NameError ('Setting key name %s does not exist' % key)
169
 
        
 
172
                raise NameError('Setting key name %s does not exist' % key)
 
173
 
170
174
        for key, value in settings.items():
171
175
            self._FAKE_SETTINGS[key] = value
172
176
        self._save_settings()
173
177
        return
174
 
    
 
178
 
175
179
    def get_setting(self, key_name):
176
 
        '''Takes a string (key_name) which corresponds to a setting in this object, 
177
 
        gets the latest copy of it from the file and returns the setting.
178
 
        Raises a NameError if the setting name doesn't exist'''
 
180
        '''Takes a string (key_name) which corresponds to a setting in this
 
181
        object, gets the latest copy of it from the file and returns the
 
182
        setting.  Raises a NameError if the setting name doesn't exist'''
179
183
        if not key_name in self._FAKE_SETTINGS:
180
 
            raise NameError ('Setting %s does not exist' % key_name)
 
184
            raise NameError('Setting %s does not exist' % key_name)
181
185
        else:
182
186
            self._update_from_file()
183
187
            return self._FAKE_SETTINGS[key_name]
184
 
            
 
188
 
185
189
    def _update_from_file(self):
186
190
        '''Loads existing settings from cache file into _FAKE_SETTINGS dict'''
187
191
        if os.path.exists(self.LOCATION):
188
192
            try:
189
193
                self._FAKE_SETTINGS = pickle.load(open(self.LOCATION))
190
194
            except:
191
 
                os.rename(self.LOCATION, self.LOCATION+".fail")
 
195
                os.rename(self.LOCATION, self.LOCATION + ".fail")
192
196
        return
193
 
    
 
197
 
194
198
    def _save_settings(self):
195
199
        """write the dict out to cache file"""
196
200
        try: