OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 from base64 import b64encode | 5 from base64 import b64encode |
6 from hashlib import sha1 | 6 from hashlib import sha1 |
7 import os | 7 import os |
8 | 8 |
9 def FormatKey(key): | 9 def FormatKey(key): |
10 '''Normalize a key by making sure it has a .html extension, and convert any | 10 '''Normalize a key by making sure it has a .html extension, and convert any |
(...skipping 15 matching lines...) Expand all Loading... |
26 def StringIdentity(first, *more): | 26 def StringIdentity(first, *more): |
27 '''Creates a small hash of a string. | 27 '''Creates a small hash of a string. |
28 ''' | 28 ''' |
29 def encode(string): | 29 def encode(string): |
30 return b64encode(sha1(string).digest()) | 30 return b64encode(sha1(string).digest()) |
31 identity = encode(first) | 31 identity = encode(first) |
32 for m in more: | 32 for m in more: |
33 identity = encode(identity + m) | 33 identity = encode(identity + m) |
34 return identity[:8] | 34 return identity[:8] |
35 | 35 |
| 36 def MarkFirst(dicts): |
| 37 '''Adds a property 'first' == True to the first element in a list of dicts. |
| 38 ''' |
| 39 if len(dicts) > 0: |
| 40 dicts[0]['first'] = True |
| 41 |
36 def MarkLast(dicts): | 42 def MarkLast(dicts): |
37 '''Adds a property 'last' == True to the last element in a list of dicts. | 43 '''Adds a property 'last' == True to the last element in a list of dicts. |
38 ''' | 44 ''' |
39 if len(dicts) > 0: | 45 if len(dicts) > 0: |
40 dicts[-1]['last'] = True | 46 dicts[-1]['last'] = True |
41 | 47 |
42 def ToUnicode(data): | 48 def ToUnicode(data): |
43 '''Returns the str |data| as a unicode object. It's expected to be utf8, but | 49 '''Returns the str |data| as a unicode object. It's expected to be utf8, but |
44 there are also latin-1 encodings in there for some reason. Fall back to that. | 50 there are also latin-1 encodings in there for some reason. Fall back to that. |
45 ''' | 51 ''' |
46 try: | 52 try: |
47 return unicode(data, 'utf-8') | 53 return unicode(data, 'utf-8') |
48 except: | 54 except: |
49 return unicode(data, 'latin-1') | 55 return unicode(data, 'latin-1') |
OLD | NEW |