Merge lp:~stefanor/ibid/country-codes-533518 into lp:~ibid-core/ibid/old-release-0.1-1.6

Proposed by Stefano Rivera
Status: Merged
Approved by: Stefano Rivera
Approved revision: 914
Merged at revision: 914
Proposed branch: lp:~stefanor/ibid/country-codes-533518
Merge into: lp:~ibid-core/ibid/old-release-0.1-1.6
Diff against target: 156 lines (+48/-34)
4 files modified
ibid/plugins/conversions.py (+3/-2)
ibid/plugins/network.py (+12/-10)
ibid/utils/__init__.py (+32/-0)
ibid/utils/html.py (+1/-22)
To merge this branch: bzr merge lp:~stefanor/ibid/country-codes-533518
Reviewer Review Type Date Requested Status
Michael Gorven Approve
Jonathan Hitchcock Approve
Max Rabkin Approve
Review via email: mp+20855@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Max Rabkin (max-rabkin) :
review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/conversions.py'
2--- ibid/plugins/conversions.py 2010-02-20 21:58:38 +0000
3+++ ibid/plugins/conversions.py 2010-03-07 10:12:33 +0000
4@@ -10,8 +10,9 @@
5 import ibid
6 from ibid.plugins import Processor, handler, match
7 from ibid.config import Option
8-from ibid.utils import file_in_path, unicode_output, human_join
9-from ibid.utils.html import get_country_codes, get_html_parse_tree
10+from ibid.utils import file_in_path, get_country_codes, human_join, \
11+ unicode_output
12+from ibid.utils.html import get_html_parse_tree
13
14 features = {}
15 log = logging.getLogger('plugins.conversions')
16
17=== modified file 'ibid/plugins/network.py'
18--- ibid/plugins/network.py 2010-02-24 12:42:45 +0000
19+++ ibid/plugins/network.py 2010-03-07 10:12:33 +0000
20@@ -21,9 +21,8 @@
21 import ibid
22 from ibid.plugins import Processor, match, authorise
23 from ibid.config import Option, IntOption, FloatOption, DictOption
24-from ibid.utils import file_in_path, unicode_output, human_join, \
25- url_to_bytestring, get_process_output
26-from ibid.utils.html import get_country_codes
27+from ibid.utils import file_in_path, get_country_codes, get_process_output, \
28+ human_join, unicode_output, url_to_bytestring
29
30 features = {}
31
32@@ -430,27 +429,30 @@
33 tld = tld.upper()
34
35 if tld in self.country_codes:
36- event.addresponse(u'%(tld)s is the TLD for %(country)s', {
37+ event.addresponse(u'%(tld)s is the ccTLD for %(country)s', {
38 'tld': tld,
39 'country': self.country_codes[tld],
40 })
41 else:
42- event.addresponse(u"ISO doesn't know about any such TLD")
43+ event.addresponse(u"ISO doesn't know about any such ccTLD")
44
45- @match(r'^tld\s+for\s+(.+)$')
46+ @match(r'^(?:cc)?tld\s+for\s+(.+)$')
47 def country_to_tld(self, event, location):
48 if not self.country_codes:
49 self.country_codes = get_country_codes()
50
51+ output = []
52 for tld, country in self.country_codes.iteritems():
53 if location.lower() in country.lower():
54- event.addresponse(u'%(tld)s is the TLD for %(country)s', {
55+ output.append(u'%(tld)s is the ccTLD for %(country)s' % {
56 'tld': tld,
57 'country': country,
58 })
59- return
60-
61- event.addresponse(u"ISO doesn't know about any TLD for %s", location)
62+ if output:
63+ event.addresponse(human_join(output))
64+ else:
65+ event.addresponse(u"ISO doesn't know about any TLD for %s",
66+ location)
67
68 features['ports'] = {
69 'description': u'Looks up port numbers for protocols',
70
71=== modified file 'ibid/utils/__init__.py'
72--- ibid/utils/__init__.py 2010-03-02 20:26:22 +0000
73+++ ibid/utils/__init__.py 2010-03-07 10:12:33 +0000
74@@ -1,6 +1,7 @@
75 # Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
76 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
77
78+import codecs
79 from gzip import GzipFile
80 from htmlentitydefs import name2codepoint
81 import logging
82@@ -277,4 +278,35 @@
83 code = process.wait()
84 return output, error, code
85
86+def get_country_codes():
87+ filename = cacheable_download(
88+ 'http://www.iso.org/iso/list-en1-semic-3.txt',
89+ 'lookup/iso-3166-1_list_en.txt')
90+
91+ f = codecs.open(filename, 'r', 'ISO-8859-1')
92+ countries = {
93+ u'AC': u'Ascension Island',
94+ u'UK': u'United Kingdom',
95+ u'SU': u'Soviet Union',
96+ u'EU': u'European Union',
97+ u'TP': u'East Timor',
98+ u'YU': u'Yugoslavia',
99+ }
100+
101+ started = False
102+ for line in f:
103+ line = line.strip()
104+ if started:
105+ country, code = line.split(u';')
106+ if u',' in country:
107+ country = u' '.join(reversed(country.split(u',', 1)))
108+ country = country.title()
109+ countries[code] = country
110+ elif line == u'':
111+ started = True
112+
113+ f.close()
114+
115+ return countries
116+
117 # vi: set et sta sw=4 ts=4:
118
119=== modified file 'ibid/utils/html.py'
120--- ibid/utils/html.py 2010-02-21 10:12:10 +0000
121+++ ibid/utils/html.py 2010-03-07 10:12:33 +0000
122@@ -12,7 +12,7 @@
123 from BeautifulSoup import BeautifulSoup
124
125 from ibid.compat import ElementTree
126-from ibid.utils import cacheable_download, url_to_bytestring
127+from ibid.utils import url_to_bytestring
128
129 class ContentTypeException(Exception):
130 pass
131@@ -62,25 +62,4 @@
132
133 return parser.parse(data, encoding = encoding)
134
135-def get_country_codes():
136- # The XML download doesn't include things like UK, so we consume this steaming pile of crud instead
137- filename = cacheable_download('http://www.iso.org/iso/country_codes/iso_3166_code_lists/iso-3166-1_decoding_table.htm', 'lookup/iso-3166-1_decoding_table.htm')
138- etree = get_html_parse_tree('file://' + filename, treetype='etree')
139- table = [x for x in etree.getiterator('table')][2]
140-
141- countries = {}
142- for tr in table.getiterator('tr'):
143- abbr = [x.text for x in tr.getiterator('div')][0]
144- eng_name = [x.text for x in tr.getchildren()][1]
145-
146- if eng_name and eng_name.strip():
147- # Cleanup:
148- if u',' in eng_name:
149- eng_name = u' '.join(reversed(eng_name.split(',', 1)))
150- eng_name = u' '.join(eng_name.split())
151-
152- countries[abbr.upper()] = eng_name.title()
153-
154- return countries
155-
156 # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches