Commit 513eace47d63161cabfd3cce82f3e075525b164a

Authored by Simon Glass
1 parent ade1e3864f

patman: Move unicode helpers to tools

Create helper functions in the tools module to deal with the differences
between unicode in Python 2 (where we use the 'unicode' type) and Python 3
(where we use the 'str' type).

Signed-off-by: Simon Glass <sjg@chromium.org>

Showing 4 changed files with 39 additions and 16 deletions Side-by-side Diff

tools/patman/gitutil.py
... ... @@ -12,6 +12,7 @@
12 12  
13 13 import checkpatch
14 14 import settings
  15 +import tools
15 16  
16 17 # True to use --no-decorate - we check this in Setup()
17 18 use_no_decorate = True
18 19  
... ... @@ -325,9 +326,8 @@
325 326 raw += LookupEmail(item, alias, raise_on_error=raise_on_error)
326 327 result = []
327 328 for item in raw:
  329 + item = tools.FromUnicode(item)
328 330 if not item in result:
329   - if type(item) == unicode:
330   - item = item.encode('utf-8')
331 331 result.append(item)
332 332 if tag:
333 333 return ['%s %s%s%s' % (tag, quote, email, quote) for email in result]
tools/patman/series.py
... ... @@ -11,6 +11,7 @@
11 11 import gitutil
12 12 import settings
13 13 import terminal
  14 +import tools
14 15  
15 16 # Series-xxx tags that we understand
16 17 valid_series = ['to', 'cc', 'version', 'changes', 'prefix', 'notes', 'name',
... ... @@ -249,7 +250,7 @@
249 250 cover_cc = gitutil.BuildEmailList(self.get('cover_cc', ''))
250 251 cover_cc = [m.encode('utf-8') if type(m) != str else m
251 252 for m in cover_cc]
252   - cc_list = ', '.join([x.decode('utf-8')
  253 + cc_list = ', '.join([tools.ToUnicode(x)
253 254 for x in set(cover_cc + all_ccs)])
254 255 print(cover_fname, cc_list.encode('utf-8'), file=fd)
255 256  
tools/patman/settings.py
... ... @@ -14,6 +14,7 @@
14 14  
15 15 import command
16 16 import gitutil
  17 +import tools
17 18  
18 19 """Default settings per-project.
19 20  
... ... @@ -99,17 +100,6 @@
99 100 for setting_name, setting_value in project_defaults.items():
100 101 self.set(project_settings, setting_name, setting_value)
101 102  
102   - def _to_unicode(self, val):
103   - """Make sure a value is of type 'unicode'
104   -
105   - Args:
106   - val: string or unicode object
107   -
108   - Returns:
109   - unicode version of val
110   - """
111   - return val if isinstance(val, unicode) else val.decode('utf-8')
112   -
113 103 def get(self, section, option, *args, **kwargs):
114 104 """Extend SafeConfigParser to try project_section before section.
115 105  
... ... @@ -127,7 +117,7 @@
127 117 val = ConfigParser.SafeConfigParser.get(
128 118 self, section, option, *args, **kwargs
129 119 )
130   - return self._to_unicode(val)
  120 + return tools.ToUnicode(val)
131 121  
132 122 def items(self, section, *args, **kwargs):
133 123 """Extend SafeConfigParser to add project_section to section.
... ... @@ -162,7 +152,7 @@
162 152  
163 153 item_dict = dict(top_items)
164 154 item_dict.update(project_items)
165   - return {(self._to_unicode(item), self._to_unicode(val))
  155 + return {(tools.ToUnicode(item), tools.ToUnicode(val))
166 156 for item, val in item_dict.items()}
167 157  
168 158 def ReadGitAliases(fname):
tools/patman/tools.py
... ... @@ -258,4 +258,36 @@
258 258 else:
259 259 data = chr(byte) * size
260 260 return data
  261 +
  262 +def ToUnicode(val):
  263 + """Make sure a value is a unicode string
  264 +
  265 + This allows some amount of compatibility between Python 2 and Python3. For
  266 + the former, it returns a unicode object.
  267 +
  268 + Args:
  269 + val: string or unicode object
  270 +
  271 + Returns:
  272 + unicode version of val
  273 + """
  274 + if sys.version_info[0] >= 3:
  275 + return val
  276 + return val if isinstance(val, unicode) else val.decode('utf-8')
  277 +
  278 +def FromUnicode(val):
  279 + """Make sure a value is a non-unicode string
  280 +
  281 + This allows some amount of compatibility between Python 2 and Python3. For
  282 + the former, it converts a unicode object to a string.
  283 +
  284 + Args:
  285 + val: string or unicode object
  286 +
  287 + Returns:
  288 + non-unicode version of val
  289 + """
  290 + if sys.version_info[0] >= 3:
  291 + return val
  292 + return val if isinstance(val, str) else val.encode('utf-8')