def test_prefs(self): p = Preferences([1, 2, 3, 4, 5]) self.assertTrue(p.prefers(1, 2)) self.assertFalse(p.prefers(2, 1)) self.assertTrue(p.prefers(2, 5)) self.assertTrue(p.prefers(2, 3)) self.assertTrue(p.prefers(1, 5)) self.assertFalse(p.prefers(5, 1)) self.assertFalse(p.prefers(5, 5)) self.assertFalse(p.prefers(5, 10)) self.assertEqual([1, 2, 3, 4, 5], list(p))
def set_preferences(self, preferences, filename='user.js'): """Adds preferences dict to profile preferences""" # append to the file prefs_file = os.path.join(self.profile, filename) f = open(prefs_file, 'a') if preferences: # note what files we've touched self.written_prefs.add(filename) # opening delimeter f.write('\n%s\n' % self.delimeters[0]) # write the preferences Preferences.write(f, preferences) # closing delimeter f.write('%s\n' % self.delimeters[1]) f.close()
def preferences(self): """profile preferences""" # object to hold preferences prefs = Preferences() # add preferences files for prefs_file in self.options.prefs_files: prefs.add_file(prefs_file) # change CLI preferences into 2-tuples separator = ':' cli_prefs = [] for pref in self.options.prefs: if separator not in pref: self.parser.error("Preference must be a key-value pair separated by a ':' (You gave: %s)" % pref) cli_prefs.append(pref.split(separator, 1)) # string preferences prefs.add(cli_prefs, cast=True) return prefs()
def summary(self, return_parts=False): """ returns string summarizing profile information. if return_parts is true, return the (Part_name, value) list of tuples instead of the assembled string """ parts = [('Path', self.profile)] # profile path # directory tree parts.append(('Files', '\n%s' % mozfile.tree(self.profile))) # preferences for prefs_file in ('user.js', 'prefs.js'): path = os.path.join(self.profile, prefs_file) if os.path.exists(path): # prefs that get their own section # This is currently only 'network.proxy.autoconfig_url' # but could be expanded to include others section_prefs = ['network.proxy.autoconfig_url'] line_length = 80 line_length_buffer = 10 # buffer for 80 character display: length = 80 - len(key) - len(': ') - line_length_buffer line_length_buffer += len(': ') def format_value(key, value): if key not in section_prefs: return value max_length = line_length - len(key) - line_length_buffer if len(value) > max_length: value = '%s...' % value[:max_length] return value prefs = Preferences.read_prefs(path) if prefs: prefs = dict(prefs) parts.append((prefs_file, '\n%s' % ('\n'.join([ '%s: %s' % (key, format_value(key, prefs[key])) for key in sorted(prefs.keys()) ])))) # Currently hardcorded to 'network.proxy.autoconfig_url' # but could be generalized, possibly with a generalized (simple) # JS-parser network_proxy_autoconfig = prefs.get( 'network.proxy.autoconfig_url') if network_proxy_autoconfig and network_proxy_autoconfig.strip( ): network_proxy_autoconfig = network_proxy_autoconfig.strip( ) lines = network_proxy_autoconfig.replace( ';', ';\n').splitlines() lines = [line.strip() for line in lines] origins_string = 'var origins = [' origins_end = '];' if origins_string in lines[0]: start = lines[0].find(origins_string) end = lines[0].find(origins_end, start) splitline = [ lines[0][:start], lines[0][start:start + len(origins_string) - 1], ] splitline.extend( lines[0][start + len(origins_string):end].replace( ',', ',\n').splitlines()) splitline.append(lines[0][end:]) lines[0:1] = [i.strip() for i in splitline] parts.append( ('Network Proxy Autoconfig, %s' % (prefs_file), '\n%s' % '\n'.join(lines))) if return_parts: return parts retval = '%s\n' % ('\n\n'.join( ['[%s]: %s' % (key, value) for key, value in parts])) return retval
def summary(self, return_parts=False): """ returns string summarizing profile information. if return_parts is true, return the (Part_name, value) list of tuples instead of the assembled string """ parts = [('Path', self.profile)] # profile path # directory tree parts.append(('Files', '\n%s' % mozfile.tree(self.profile))) # preferences for prefs_file in ('user.js', 'prefs.js'): path = os.path.join(self.profile, prefs_file) if os.path.exists(path): # prefs that get their own section # This is currently only 'network.proxy.autoconfig_url' # but could be expanded to include others section_prefs = ['network.proxy.autoconfig_url'] line_length = 80 # buffer for 80 character display: # length = 80 - len(key) - len(': ') - line_length_buffer line_length_buffer = 10 line_length_buffer += len(': ') def format_value(key, value): if key not in section_prefs: return value max_length = line_length - len(key) - line_length_buffer if len(value) > max_length: value = '%s...' % value[:max_length] return value prefs = Preferences.read_prefs(path) if prefs: prefs = dict(prefs) parts.append((prefs_file, '\n%s' % ('\n'.join( ['%s: %s' % (key, format_value(key, prefs[key])) for key in sorted(prefs.keys())])))) # Currently hardcorded to 'network.proxy.autoconfig_url' # but could be generalized, possibly with a generalized (simple) # JS-parser network_proxy_autoconfig = prefs.get('network.proxy.autoconfig_url') if network_proxy_autoconfig and network_proxy_autoconfig.strip(): network_proxy_autoconfig = network_proxy_autoconfig.strip() lines = network_proxy_autoconfig.replace(';', ';\n').splitlines() lines = [line.strip() for line in lines] origins_string = 'var origins = [' origins_end = '];' if origins_string in lines[0]: start = lines[0].find(origins_string) end = lines[0].find(origins_end, start) splitline = [lines[0][:start], lines[0][start:start + len(origins_string) - 1], ] splitline.extend(lines[0][start + len(origins_string):end].replace( ',', ',\n').splitlines()) splitline.append(lines[0][end:]) lines[0:1] = [i.strip() for i in splitline] parts.append(('Network Proxy Autoconfig, %s' % (prefs_file), '\n%s' % '\n'.join(lines))) if return_parts: return parts retval = '%s\n' % ('\n\n'.join(['[%s]: %s' % (key, value) for key, value in parts])) return retval
def test_string_keys(self): p = Preferences(('A', 'B')) self.assertTrue(p.prefers('A', 'B')) self.assertFalse(p.prefers('B', 'A')) self.assertEqual({'A'}, set(p.all_preferred_to('B')))
def test_all_preferred_to(self): p = Preferences([1, 2, 3, 4, 5]) self.assertEqual({1, 2, 3}, set(p.all_preferred_to(4))) self.assertEqual({1, 2, 3, 4}, set(p.all_preferred_to(5))) self.assertEqual(set(), set(p.all_preferred_to(1))) self.assertEqual(set(), set(p.all_preferred_to(6)))
def test_pref_rankings(self): total = list(range(0, 1000)) for _ in range(1000): samp = sample(total, randint(20, 700)) self.assertEqual(samp, list(Preferences(samp)))
def __init__(self, prefs): prefs2 = {} for k, v in prefs.items(): prefs2[k] = Preferences(v) if isinstance(v, list) else v self.__prefs = prefs2