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
=== modified file 'ibid/plugins/conversions.py'
--- ibid/plugins/conversions.py 2010-02-20 21:58:38 +0000
+++ ibid/plugins/conversions.py 2010-03-07 10:12:33 +0000
@@ -10,8 +10,9 @@
10import ibid10import ibid
11from ibid.plugins import Processor, handler, match11from ibid.plugins import Processor, handler, match
12from ibid.config import Option12from ibid.config import Option
13from ibid.utils import file_in_path, unicode_output, human_join13from ibid.utils import file_in_path, get_country_codes, human_join, \
14from ibid.utils.html import get_country_codes, get_html_parse_tree14 unicode_output
15from ibid.utils.html import get_html_parse_tree
1516
16features = {}17features = {}
17log = logging.getLogger('plugins.conversions')18log = logging.getLogger('plugins.conversions')
1819
=== modified file 'ibid/plugins/network.py'
--- ibid/plugins/network.py 2010-02-24 12:42:45 +0000
+++ ibid/plugins/network.py 2010-03-07 10:12:33 +0000
@@ -21,9 +21,8 @@
21import ibid21import ibid
22from ibid.plugins import Processor, match, authorise22from ibid.plugins import Processor, match, authorise
23from ibid.config import Option, IntOption, FloatOption, DictOption23from ibid.config import Option, IntOption, FloatOption, DictOption
24from ibid.utils import file_in_path, unicode_output, human_join, \24from ibid.utils import file_in_path, get_country_codes, get_process_output, \
25 url_to_bytestring, get_process_output25 human_join, unicode_output, url_to_bytestring
26from ibid.utils.html import get_country_codes
2726
28features = {}27features = {}
2928
@@ -430,27 +429,30 @@
430 tld = tld.upper()429 tld = tld.upper()
431430
432 if tld in self.country_codes:431 if tld in self.country_codes:
433 event.addresponse(u'%(tld)s is the TLD for %(country)s', {432 event.addresponse(u'%(tld)s is the ccTLD for %(country)s', {
434 'tld': tld,433 'tld': tld,
435 'country': self.country_codes[tld],434 'country': self.country_codes[tld],
436 })435 })
437 else:436 else:
438 event.addresponse(u"ISO doesn't know about any such TLD")437 event.addresponse(u"ISO doesn't know about any such ccTLD")
439438
440 @match(r'^tld\s+for\s+(.+)$')439 @match(r'^(?:cc)?tld\s+for\s+(.+)$')
441 def country_to_tld(self, event, location):440 def country_to_tld(self, event, location):
442 if not self.country_codes:441 if not self.country_codes:
443 self.country_codes = get_country_codes()442 self.country_codes = get_country_codes()
444443
444 output = []
445 for tld, country in self.country_codes.iteritems():445 for tld, country in self.country_codes.iteritems():
446 if location.lower() in country.lower():446 if location.lower() in country.lower():
447 event.addresponse(u'%(tld)s is the TLD for %(country)s', {447 output.append(u'%(tld)s is the ccTLD for %(country)s' % {
448 'tld': tld,448 'tld': tld,
449 'country': country,449 'country': country,
450 })450 })
451 return451 if output:
452452 event.addresponse(human_join(output))
453 event.addresponse(u"ISO doesn't know about any TLD for %s", location)453 else:
454 event.addresponse(u"ISO doesn't know about any TLD for %s",
455 location)
454456
455features['ports'] = {457features['ports'] = {
456 'description': u'Looks up port numbers for protocols',458 'description': u'Looks up port numbers for protocols',
457459
=== modified file 'ibid/utils/__init__.py'
--- ibid/utils/__init__.py 2010-03-02 20:26:22 +0000
+++ ibid/utils/__init__.py 2010-03-07 10:12:33 +0000
@@ -1,6 +1,7 @@
1# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera1# Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
2# Released under terms of the MIT/X/Expat Licence. See COPYING for details.2# Released under terms of the MIT/X/Expat Licence. See COPYING for details.
33
4import codecs
4from gzip import GzipFile5from gzip import GzipFile
5from htmlentitydefs import name2codepoint6from htmlentitydefs import name2codepoint
6import logging7import logging
@@ -277,4 +278,35 @@
277 code = process.wait()278 code = process.wait()
278 return output, error, code279 return output, error, code
279280
281def get_country_codes():
282 filename = cacheable_download(
283 'http://www.iso.org/iso/list-en1-semic-3.txt',
284 'lookup/iso-3166-1_list_en.txt')
285
286 f = codecs.open(filename, 'r', 'ISO-8859-1')
287 countries = {
288 u'AC': u'Ascension Island',
289 u'UK': u'United Kingdom',
290 u'SU': u'Soviet Union',
291 u'EU': u'European Union',
292 u'TP': u'East Timor',
293 u'YU': u'Yugoslavia',
294 }
295
296 started = False
297 for line in f:
298 line = line.strip()
299 if started:
300 country, code = line.split(u';')
301 if u',' in country:
302 country = u' '.join(reversed(country.split(u',', 1)))
303 country = country.title()
304 countries[code] = country
305 elif line == u'':
306 started = True
307
308 f.close()
309
310 return countries
311
280# vi: set et sta sw=4 ts=4:312# vi: set et sta sw=4 ts=4:
281313
=== modified file 'ibid/utils/html.py'
--- ibid/utils/html.py 2010-02-21 10:12:10 +0000
+++ ibid/utils/html.py 2010-03-07 10:12:33 +0000
@@ -12,7 +12,7 @@
12from BeautifulSoup import BeautifulSoup12from BeautifulSoup import BeautifulSoup
1313
14from ibid.compat import ElementTree14from ibid.compat import ElementTree
15from ibid.utils import cacheable_download, url_to_bytestring15from ibid.utils import url_to_bytestring
1616
17class ContentTypeException(Exception):17class ContentTypeException(Exception):
18 pass18 pass
@@ -62,25 +62,4 @@
6262
63 return parser.parse(data, encoding = encoding)63 return parser.parse(data, encoding = encoding)
6464
65def get_country_codes():
66 # The XML download doesn't include things like UK, so we consume this steaming pile of crud instead
67 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')
68 etree = get_html_parse_tree('file://' + filename, treetype='etree')
69 table = [x for x in etree.getiterator('table')][2]
70
71 countries = {}
72 for tr in table.getiterator('tr'):
73 abbr = [x.text for x in tr.getiterator('div')][0]
74 eng_name = [x.text for x in tr.getchildren()][1]
75
76 if eng_name and eng_name.strip():
77 # Cleanup:
78 if u',' in eng_name:
79 eng_name = u' '.join(reversed(eng_name.split(',', 1)))
80 eng_name = u' '.join(eng_name.split())
81
82 countries[abbr.upper()] = eng_name.title()
83
84 return countries
85
86# vi: set et sta sw=4 ts=4:65# vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches