Skip to content

Commit 2be7b98

Browse files
authored
Merge pull request #9 from bartikowskiw/ANSPB-423_update_to_python_3
Anspb 423 update to python 3
2 parents b7ff10b + 5a7bc76 commit 2be7b98

File tree

11 files changed

+351
-329
lines changed

11 files changed

+351
-329
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: python
22

33
python:
44
- "2.7"
5+
- "3.5"
56

67
install: "make init"
78

@@ -16,6 +17,6 @@ script:
1617

1718
notifications:
1819
email: false
19-
hipchat:
20+
slack:
2021
rooms:
21-
secure: Fs2DhIDlqNM70oxVj9b5BpbZ/OtKvI09SVjWq4fWwFvX4cuD1fxJ4/xknyjWYONTPYQu1PMdomu3HvJ5auVcyM/fuH2dkVffEGlDsH300aId8HoRN/VcCcdLvckqgPgjpZIomOTF1G195SVolQ9Va7uc1LOzbNOb3k4lP1M+Xms=
22+
secure: "Rse+KZpjFlG+ztNF2x0nb+mdckfG3ZMDt7/U7hoti6n3dG/5dGPuTWv53aU12zCC5uo5Owur3hmONqLAp+EOo+b8A5yxnTrjbDKo/GymCvtToXkcg6omlhEBZxcvKyKpGwjlRcx/ZXC6SRc5+NONLsgRnZQy/pDkxZ7WJhrj/yI="

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ deb:
1212
python setup.py --command-packages=stdeb.command bdist_deb
1313

1414
check:
15-
find . -name \*.py | xargs pep8 --first
16-
find bin -type f | xargs pep8 --first
15+
find . -name \*.py | xargs pycodestyle --first
16+
find bin -type f | xargs pycodestyle --first
1717

1818
test:
1919
nosetests -v

bin/locale-decode-category

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,48 @@
22

33
# -*- coding: utf-8 -*-
44

5+
from __future__ import absolute_import
56
import sys
67
from glibc_locale_tools.glibc_locale_tools import *
78

89
lines = []
910
for line in sys.stdin:
10-
lines.append(line)
11-
lines_joined = ''.join(lines).decode('utf-8')
11+
lines.append(line)
12+
lines_joined = ''.join(lines)
13+
if sys.version_info < (3, 0):
14+
lines_joined = lines_joined.decode('utf-8')
1215

1316
unsafe_spans = get_unsafe_spans(lines, lines_joined)
1417

1518
between_quotes_map = []
1619
for between_quotes in reversed(list(re.finditer(BETWEEN_QUOTES_PATTERN, lines_joined))):
17-
between_quotes_match = between_quotes.group(1)
18-
between_quotes_match_start = between_quotes.start(1)
19-
between_quotes_match_end = between_quotes.end(1)
20+
between_quotes_match = between_quotes.group(1)
21+
between_quotes_match_start = between_quotes.start(1)
22+
between_quotes_match_end = between_quotes.end(1)
2023

21-
if in_unsafe_spans(between_quotes_match_start, between_quotes_match_end, unsafe_spans):
22-
continue
24+
if in_unsafe_spans(between_quotes_match_start, between_quotes_match_end, unsafe_spans):
25+
continue
2326

24-
replacement = between_quotes_match
25-
for unicode_matches in reversed(list(re.finditer(UNICODE_PATTERN, between_quotes_match))):
26-
replacement = replace_positional(replacement,
27-
unicode_matches.start(1),
28-
unicode_decode(unicode_matches.group(2)),
29-
unicode_matches.end(1))
27+
replacement = between_quotes_match
28+
for unicode_matches in reversed(list(re.finditer(UNICODE_PATTERN, between_quotes_match))):
29+
replacement = replace_positional(replacement,
30+
unicode_matches.start(1),
31+
unicode_decode(unicode_matches.group(2)),
32+
unicode_matches.end(1))
3033

31-
between_quotes_map.append({'start': between_quotes_match_start,
32-
'end': between_quotes_match_end,
33-
'replacement': replacement})
34+
between_quotes_map.append({'start': between_quotes_match_start,
35+
'end': between_quotes_match_end,
36+
'replacement': replacement})
3437

3538
for between_quotes_map_items in between_quotes_map:
36-
lines_joined = replace_positional(lines_joined,
37-
between_quotes_map_items['start'],
38-
between_quotes_map_items['replacement'],
39-
between_quotes_map_items['end'])
39+
lines_joined = replace_positional(lines_joined,
40+
between_quotes_map_items['start'],
41+
between_quotes_map_items['replacement'],
42+
between_quotes_map_items['end'])
4043

41-
sys.stdout.write(lines_joined.encode('utf-8'))
44+
if sys.version_info < (3, 0):
45+
lines_joined = lines_joined.encode('utf-8')
46+
47+
sys.stdout.write(lines_joined)
4248

4349
sys.exit(0)

bin/locale-encode-category

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,51 @@
22

33
# -*- coding: utf-8 -*-
44

5+
from __future__ import absolute_import
56
import sys
67
from glibc_locale_tools.glibc_locale_tools import *
78

89
lines = []
910
for line in sys.stdin:
10-
lines.append(line)
11-
lines_joined = ''.join(lines).decode('utf-8')
11+
lines.append(line)
12+
lines_joined = ''.join(lines)
13+
14+
# Handle Python 2.x
15+
if sys.version_info < (3, 0):
16+
lines_joined = lines_joined.decode('utf-8')
1217

1318
unsafe_spans = get_unsafe_spans(lines, lines_joined)
1419

1520
between_quotes_map = []
1621
for between_quotes in reverse_iter(re.finditer(BETWEEN_QUOTES_PATTERN, lines_joined)):
17-
between_quotes_match = between_quotes.group(1)
18-
between_quotes_match_start = between_quotes.start(1)
19-
between_quotes_match_end = between_quotes.end(1)
22+
between_quotes_match = between_quotes.group(1)
23+
between_quotes_match_start = between_quotes.start(1)
24+
between_quotes_match_end = between_quotes.end(1)
2025

21-
if in_unsafe_spans(between_quotes_match_start, between_quotes_match_end, unsafe_spans):
22-
continue
26+
if in_unsafe_spans(between_quotes_match_start, between_quotes_match_end, unsafe_spans):
27+
continue
2328

24-
replacement = between_quotes_match
25-
for to_decode_matches in reversed(list(re.finditer(TO_DECODE_PATTERN, between_quotes_match))):
26-
replacement = replace_positional(replacement,
27-
to_decode_matches.start(0),
28-
unicode_encode(to_decode_matches.group(0)),
29-
to_decode_matches.end(0))
29+
replacement = between_quotes_match
30+
for to_decode_matches in reversed(list(re.finditer(TO_DECODE_PATTERN, between_quotes_match))):
31+
replacement = replace_positional(replacement,
32+
to_decode_matches.start(0),
33+
unicode_encode(to_decode_matches.group(0)),
34+
to_decode_matches.end(0))
3035

31-
between_quotes_map.append({'start': between_quotes_match_start,
32-
'end': between_quotes_match_end,
33-
'replacement': replacement})
36+
between_quotes_map.append({'start': between_quotes_match_start,
37+
'end': between_quotes_match_end,
38+
'replacement': replacement})
3439

3540
for between_quotes_map_items in between_quotes_map:
36-
lines_joined = replace_positional(lines_joined,
37-
between_quotes_map_items['start'],
38-
between_quotes_map_items['replacement'],
39-
between_quotes_map_items['end'])
41+
lines_joined = replace_positional(lines_joined,
42+
between_quotes_map_items['start'],
43+
between_quotes_map_items['replacement'],
44+
between_quotes_map_items['end'])
45+
46+
# Handle Python 2.x
47+
if sys.version_info < (3, 0):
48+
lines_joined = lines_joined.encode('utf-8')
4049

41-
sys.stdout.write(lines_joined.encode('utf-8'))
50+
sys.stdout.write(lines_joined)
4251

4352
sys.exit(0)

bin/locale-extract-category

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@
22

33
# -*- coding: utf-8 -*-
44

5+
from __future__ import absolute_import
56
import sys
67
import re
78
from glibc_locale_tools.glibc_locale_tools import *
89

910
if len(sys.argv) != 2:
10-
sys.stderr.write('%s [LC section name (e.g. LC_TIME)]\n' % sys.argv[0])
11-
sys.exit(1)
11+
sys.stderr.write('%s [LC section name (e.g. LC_TIME)]\n' % sys.argv[0])
12+
sys.exit(1)
1213

1314
lc_section = sys.argv[1]
1415

1516
if lc_section not in POSSIBLE_LC_SECTIONS:
16-
sys.stderr.write('Non existing LC section %s\n' % lc_section)
17-
sys.exit(1)
17+
sys.stderr.write('Non existing LC section %s\n' % lc_section)
18+
sys.exit(1)
1819

1920
lc_section_pattern = LC_SECTION_PATTERN.format(lc_section)
2021

2122
lines = ''
2223
for line in sys.stdin:
23-
lines += line
24+
lines += line
2425

2526
match_comment_char = re.search(COMMENT_CHAR_PATTERN, lines, re.MULTILINE)
2627
match_escape_char = re.search(ESCAPE_CHAR_PATTERN, lines, re.MULTILINE)
2728
match_category = re.search(lc_section_pattern, lines, re.DOTALL)
2829

2930
if match_comment_char:
30-
sys.stdout.write(match_comment_char.group(1) + '\n')
31+
sys.stdout.write(match_comment_char.group(1) + '\n')
3132
if match_escape_char:
32-
sys.stdout.write(match_escape_char.group(1) + '\n')
33+
sys.stdout.write(match_escape_char.group(1) + '\n')
3334
if match_category:
34-
sys.stdout.write(match_category.group(1).lstrip('\n'))
35+
sys.stdout.write(match_category.group(1).lstrip('\n'))
3536

3637
sys.exit(0)
Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
import re
4+
import sys
45

56

67
BETWEEN_QUOTES_PATTERN = r'"([^"]*)"'
@@ -46,101 +47,104 @@
4647

4748

4849
def unicode_decode(unicode_char):
49-
"""
50-
Decodes an unicode char (e.g. 20AC to €).
50+
"""
51+
Decodes an unicode char (e.g. 20AC to €).
5152
52-
:param unicode_char: An unicode char
53-
:return: A (decoded) unicode char
54-
"""
55-
56-
return unichr(int(unicode_char, 16))
53+
:param unicode_char: An unicode char
54+
:return: A (decoded) unicode char
55+
"""
56+
if sys.version_info >= (3, 0):
57+
return chr(int(unicode_char, 16))
58+
else:
59+
# Keep this for Python 2.x
60+
return unichr(int(unicode_char, 16))
5761

5862

5963
def unicode_encode(char):
60-
"""
61-
Encodes an char (e.g. € to <U20AC>).
64+
"""
65+
Encodes an char (e.g. € to <U20AC>).
6266
63-
:param char: A char
64-
:return: An (encoded) char
65-
"""
67+
:param char: A char
68+
:return: An (encoded) char
69+
"""
6670

67-
return '<U%04X>' % (ord(char))
71+
return '<U%04X>' % (ord(char))
6872

6973

7074
def replace_positional(original, start, replacement, end):
71-
"""
72-
Replaces a substring in a string between a start and end position.
75+
"""
76+
Replaces a substring in a string between a start and end position.
7377
74-
:param original: An original string
75-
:param start: A start position
76-
:param replacement: A replacement string
77-
:param end: An end position
78-
:return: A (replaced) string
79-
"""
78+
:param original: An original string
79+
:param start: A start position
80+
:param replacement: A replacement string
81+
:param end: An end position
82+
:return: A (replaced) string
83+
"""
8084

81-
return original[:start] + replacement + original[end:]
85+
return original[:start] + replacement + original[end:]
8286

8387

8488
def reverse_iter(iterator):
85-
"""
86-
Reverses an iterator.
89+
"""
90+
Reverses an iterator.
8791
88-
:param iterator: An iterator
89-
:return: A reversed iterator
90-
"""
92+
:param iterator: An iterator
93+
:return: A reversed iterator
94+
"""
9195

92-
return reversed(list(iterator))
96+
return reversed(list(iterator))
9397

9498

9599
def between_range(range1, range2):
96-
"""
97-
Checks whether or not range 2 is between range 1.
100+
"""
101+
Checks whether or not range 2 is between range 1.
98102
99-
:param range1: A range
100-
:param range2: A range
101-
:return: Whether or not range 2 is between range 1
102-
"""
103+
:param range1: A range
104+
:param range2: A range
105+
:return: Whether or not range 2 is between range 1
106+
"""
103107

104-
return range1['start'] <= range2['start'] <= range1['end'] and range1['start'] <= range2['end'] <= range1['end']
108+
return range1['start'] <= range2['start'] <= range1['end'] and range1['start'] <= range2['end'] <= range1['end']
105109

106110

107111
def in_unsafe_spans(match_start, match_end, unsafe_spans):
108-
"""
109-
Checks whether not a range (match start and end) is in unsafe ranges.
112+
"""
113+
Checks whether not a range (match start and end) is in unsafe ranges.
110114
111-
:param match_start: A match start position
112-
:param match_end: A match end position
113-
:param unsafe_spans: A list of unsafe spans
114-
:return: Whether not a range is in unsafe ranges
115-
"""
115+
:param match_start: A match start position
116+
:param match_end: A match end position
117+
:param unsafe_spans: A list of unsafe spans
118+
:return: Whether not a range is in unsafe ranges
119+
"""
116120

117-
for unsafe_span in unsafe_spans:
118-
if between_range(unsafe_span, {'start': match_start, 'end': match_end}):
119-
return True
121+
for unsafe_span in unsafe_spans:
122+
if between_range(unsafe_span, {'start': match_start, 'end': match_end}):
123+
return True
120124

121-
return False
125+
return False
122126

123127

124128
def get_unsafe_spans(lines, lines_joined):
125-
"""
126-
Generates a list of unsafe spans.
129+
"""
130+
Generates a list of unsafe spans.
127131
128-
Unsafe span are comment lines that contain double quotes (that should not be (en|de)coded).
132+
Unsafe span are comment lines that contain double quotes (that should not be (en|de)coded).
129133
130-
:param lines: A list of lines
131-
:param lines_joined: A string of lines
132-
:return: A list of unsafe spans
133-
"""
134+
:param lines: A list of lines
135+
:param lines_joined: A string of lines
136+
:return: A list of unsafe spans
137+
"""
134138

135-
unsafe_lines = []
136-
for line in lines:
137-
if re.search(COMMENT_LINE_WITH_QUOTES_PATTERN, line):
138-
unsafe_lines.append(line)
139+
unsafe_lines = []
140+
for line in lines:
141+
if re.search(COMMENT_LINE_WITH_QUOTES_PATTERN, line):
142+
unsafe_lines.append(line)
139143

140-
unsafe_lines_pattern = '({0})'.format('|'.join(map(re.escape, unsafe_lines)))
144+
unsafe_lines_pattern = '({0})'.format('|'.join(map(re.escape, unsafe_lines)))
141145

142-
unsafe_spans = []
143-
for unsafe_line in re.finditer(unsafe_lines_pattern, lines_joined):
144-
unsafe_spans.append({'start': unsafe_line.start(0), 'end': unsafe_line.end(0)})
146+
unsafe_spans = []
147+
for unsafe_line in re.finditer(unsafe_lines_pattern, lines_joined):
148+
unsafe_spans.append({'start': unsafe_line.start(0), 'end': unsafe_line.end(0)})
145149

146-
return unsafe_spans
150+
return unsafe_spans

0 commit comments

Comments
 (0)