iso3166-country-codes (empty) → 0.1
raw patch · 8 files changed
+413/−0 lines, 8 filesdep +HTTPdep +basedep +utf8-stringsetup-changed
Dependencies added: HTTP, base, utf8-string
Files
- COPYING.LESSER +165/−0
- Data/ISO3166_CountryCodes.hsc +0/−0
- Data/ISO3166_CountryCodes.preamble.hs +61/−0
- Data/ISO3166_CountryCodes/GenerateDataDeclaration.hs +67/−0
- Data/ISO3166_CountryCodes/Getter.hs +62/−0
- Makefile +32/−0
- Setup.hs +2/−0
- iso3166-country-codes.cabal +24/−0
+ COPYING.LESSER view
@@ -0,0 +1,165 @@+ GNU LESSER GENERAL PUBLIC LICENSE+ Version 3, 29 June 2007++ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>+ Everyone is permitted to copy and distribute verbatim copies+ of this license document, but changing it is not allowed.+++ This version of the GNU Lesser General Public License incorporates+the terms and conditions of version 3 of the GNU General Public+License, supplemented by the additional permissions listed below.++ 0. Additional Definitions.++ As used herein, "this License" refers to version 3 of the GNU Lesser+General Public License, and the "GNU GPL" refers to version 3 of the GNU+General Public License.++ "The Library" refers to a covered work governed by this License,+other than an Application or a Combined Work as defined below.++ An "Application" is any work that makes use of an interface provided+by the Library, but which is not otherwise based on the Library.+Defining a subclass of a class defined by the Library is deemed a mode+of using an interface provided by the Library.++ A "Combined Work" is a work produced by combining or linking an+Application with the Library. The particular version of the Library+with which the Combined Work was made is also called the "Linked+Version".++ The "Minimal Corresponding Source" for a Combined Work means the+Corresponding Source for the Combined Work, excluding any source code+for portions of the Combined Work that, considered in isolation, are+based on the Application, and not on the Linked Version.++ The "Corresponding Application Code" for a Combined Work means the+object code and/or source code for the Application, including any data+and utility programs needed for reproducing the Combined Work from the+Application, but excluding the System Libraries of the Combined Work.++ 1. Exception to Section 3 of the GNU GPL.++ You may convey a covered work under sections 3 and 4 of this License+without being bound by section 3 of the GNU GPL.++ 2. Conveying Modified Versions.++ If you modify a copy of the Library, and, in your modifications, a+facility refers to a function or data to be supplied by an Application+that uses the facility (other than as an argument passed when the+facility is invoked), then you may convey a copy of the modified+version:++ a) under this License, provided that you make a good faith effort to+ ensure that, in the event an Application does not supply the+ function or data, the facility still operates, and performs+ whatever part of its purpose remains meaningful, or++ b) under the GNU GPL, with none of the additional permissions of+ this License applicable to that copy.++ 3. Object Code Incorporating Material from Library Header Files.++ The object code form of an Application may incorporate material from+a header file that is part of the Library. You may convey such object+code under terms of your choice, provided that, if the incorporated+material is not limited to numerical parameters, data structure+layouts and accessors, or small macros, inline functions and templates+(ten or fewer lines in length), you do both of the following:++ a) Give prominent notice with each copy of the object code that the+ Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the object code with a copy of the GNU GPL and this license+ document.++ 4. Combined Works.++ You may convey a Combined Work under terms of your choice that,+taken together, effectively do not restrict modification of the+portions of the Library contained in the Combined Work and reverse+engineering for debugging such modifications, if you also do each of+the following:++ a) Give prominent notice with each copy of the Combined Work that+ the Library is used in it and that the Library and its use are+ covered by this License.++ b) Accompany the Combined Work with a copy of the GNU GPL and this license+ document.++ c) For a Combined Work that displays copyright notices during+ execution, include the copyright notice for the Library among+ these notices, as well as a reference directing the user to the+ copies of the GNU GPL and this license document.++ d) Do one of the following:++ 0) Convey the Minimal Corresponding Source under the terms of this+ License, and the Corresponding Application Code in a form+ suitable for, and under terms that permit, the user to+ recombine or relink the Application with a modified version of+ the Linked Version to produce a modified Combined Work, in the+ manner specified by section 6 of the GNU GPL for conveying+ Corresponding Source.++ 1) Use a suitable shared library mechanism for linking with the+ Library. A suitable mechanism is one that (a) uses at run time+ a copy of the Library already present on the user's computer+ system, and (b) will operate properly with a modified version+ of the Library that is interface-compatible with the Linked+ Version.++ e) Provide Installation Information, but only if you would otherwise+ be required to provide such information under section 6 of the+ GNU GPL, and only to the extent that such information is+ necessary to install and execute a modified version of the+ Combined Work produced by recombining or relinking the+ Application with a modified version of the Linked Version. (If+ you use option 4d0, the Installation Information must accompany+ the Minimal Corresponding Source and Corresponding Application+ Code. If you use option 4d1, you must provide the Installation+ Information in the manner specified by section 6 of the GNU GPL+ for conveying Corresponding Source.)++ 5. Combined Libraries.++ You may place library facilities that are a work based on the+Library side by side in a single library together with other library+facilities that are not Applications and are not covered by this+License, and convey such a combined library under terms of your+choice, if you do both of the following:++ a) Accompany the combined library with a copy of the same work based+ on the Library, uncombined with any other library facilities,+ conveyed under the terms of this License.++ b) Give prominent notice with the combined library that part of it+ is a work based on the Library, and explaining where to find the+ accompanying uncombined form of the same work.++ 6. Revised Versions of the GNU Lesser General Public License.++ The Free Software Foundation may publish revised and/or new versions+of the GNU Lesser General Public License from time to time. Such new+versions will be similar in spirit to the present version, but may+differ in detail to address new problems or concerns.++ Each version is given a distinguishing version number. If the+Library as you received it specifies that a certain numbered version+of the GNU Lesser General Public License "or any later version"+applies to it, you have the option of following the terms and+conditions either of that published version or of any later version+published by the Free Software Foundation. If the Library as you+received it does not specify a version number of the GNU Lesser+General Public License, you may choose any version of the GNU Lesser+General Public License ever published by the Free Software Foundation.++ If the Library as you received it specifies that a proxy can decide+whether future versions of the GNU Lesser General Public License shall+apply, that proxy's public statement of acceptance of any version is+permanent authorization for you to choose that version for the+Library.
+ Data/ISO3166_CountryCodes.hsc view
+ Data/ISO3166_CountryCodes.preamble.hs view
@@ -0,0 +1,61 @@+{- | Two letter Country Codes++ Defines the type CountryCode with constructors for each+ of the two-letter codes defined in+ <http://www.iso.org/iso/iso3166_en_code_lists.txt> and+ instances for 'Eq', 'Read', 'Show', 'Enum', 'Bounded'+ and 'Ord'.++ Also defines @'countryNameFromCode'@, which gives the+ official short country name all in uppercase and+ @'readableCountryName'@, which produces somewhat more user-friendly output++ Intended to be imported qualified as some country codes+ are the same as some standard Haskell constructors.++-}+module Data.ISO3166_CountryCodes+ (CountryCode(..),+ countryNameFromCode,+ readableCountryName+ ) where+import qualified Prelude as P+import Prelude ((.),not,(==),otherwise,(&&),(==),(/=))+import Control.Monad+import Data.Char+import Data.List++{- | A human readable version of the official name of a country+ from its country code++ Uses some ad-hockery to rearrange the order of the words.++-}+readableCountryName :: CountryCode -> String++readableCountryName+ = concat . intersperse " " . rearrange . fmap up1 . words .+ countryNameFromCode+ where up1 [] = []+ up1 (c:rest) | not (isAlpha c) = c:up1 rest+ up1 "OF" = "of"+ up1 "THE" = "the"+ up1 "AND" = "and"+ up1 "U.S." = "US" -- gawd+ up1 ('M':'C':l) = "Mc"++up1 l -- Don't do MacEdonia!+ -- but there are no Mac_ countries yet+ up1 ('D':'\'':l) = "d'"++up1 l+ up1 (c:cs) = toUpper c: downup cs+ downup [] = [] -- needed for hyphenated names+ downup (c:cs) | c=='-' = c:up1 cs+ | otherwise = toLower c:downup cs+ rearrange [] = []+ rearrange [c] = [c]+ rearrange ll@(n:l)+ | last l `elem` ["of", "the"] && last n == ','+ = onhead up1 l++[[c|c<-n,c/=',']]+ | otherwise = ll+ onhead f [] = []+ onhead f (h:r) = f h:r+{- Copyright © 2010 Jón Fairbairn+-}
+ Data/ISO3166_CountryCodes/GenerateDataDeclaration.hs view
@@ -0,0 +1,67 @@+{- | generate language code data type from the official list+ downloaded from <http://www.iso.org/iso/iso3166_en_code_lists.txt>++ use the text version because it's easy to parse and the+ xml version doesn't add anything, so I can avoid+ depending on an xml library for the moment++ The code_lists file contains non-ASCII characters,+ output (in the country names) and Haskell source is Unicode+ the output here requires encoding to UTF-8, something that+ Haskell IO ought to do itself.++-}+module Main where+import Getter+import Data.Char+import Codec.Binary.UTF8.String+import Data.List++main+ = do output_preamble+ generate_country_codes++output_preamble+ = readFile preamble >>= putStr++generate_country_codes+ = do text <- fmap lines $+ get_country_code_list+ let codes = [(cc,name) |+ l <- text,+ let cc = filter (isAlpha) $+ dropWhile (==';') $+ dropWhile (/=';') $+ l+ name = takeWhile (/=';') l,+ length cc == 2+ ]+ gen_data_decl codes+ gen_alist_decl codes++gen_data_decl codes+ = do putStrLn "data CountryCode = "+ sequence_ $ fmap putStrLn $ intersperse " |" $ fmap country_alternative codes+ putStrLn " deriving (P.Eq,P.Read,P.Show,P.Enum,P.Bounded,P.Ord)"++country_alternative (code, name)+ = " "++code++" -- ^ " ++ encodeString name++gen_alist_decl codes+ = do mapM_ putStrLn $+ ["",+ "{-|",+ " convert a country code to the official (English) name of the country",+ "",+ " see @'readableCountryName'@ for something with a more pleasing word order and capitalisation",+ "-}",+ "countryNameFromCode:: CountryCode -> String"+ ]+ sequence_ $ fmap make_clause codes++make_clause (constr, countryName)+ = putStrLn $ "countryNameFromCode " ++ constr ++ " = \"" ++ encodeString countryName ++ "\""++preamble = "Data/ISO3166_CountryCodes.preamble.hs"+{- Copyright © 2010 Jón Fairbairn+-}
+ Data/ISO3166_CountryCodes/Getter.hs view
@@ -0,0 +1,62 @@+module Getter(get_country_code_list) where++{- This operates essentially as a single file http cache+-}++import Network.HTTP+import System.Directory+import System.Time+import IO++country_code_URL = "http://www.iso.org/iso/iso3166_en_code_lists.txt"+local_copy = "Data/ISO3166_CountryCodes/iso3166_en_code_lists.txt"++last_modified_file = local_copy ++ ".last-modified"++{-+most errors are handled by failing...++-}++get_country_code_list+ = do local_copy_date <- getSomeModificationTime+ let r = getRequest country_code_URL+ Right response <- simpleHTTP r{rqHeaders=Header HdrIfModifiedSince local_copy_date:getHeaders r}++ case rspCode response of+ (3,0,4) -> do -- not modified since fetched+ readFile local_copy+ (2,0,0) -> do let content = rspBody response -- doesn't seem lazy enough+ writeFile local_copy content+ case [date| Header HdrLastModified date <- rspHeaders response] of+ [d] -> do writeFile last_modified_file d+ return content+ _ -> error $ "unhandled HTTP response: " ++ show response++{-++Using the file modification time might be better in some+ways, but we don't know whether the server holding the+source file implements if-modified-since correctly, so+storing the modification time returned by the server in its+own file is about as good as we can manage.++Besides, at time of writing Haskell's time manipulation is+in a mess.++-}++getSomeModificationTime+ = do already_here <- doesFileExist local_copy+ last_modified_recorded <- doesFileExist last_modified_file+ if already_here && last_modified_recorded -- we have a copy, but is it up to date?+ then readFile last_modified_file+ else return epoch -- this should be long enough ago to ensure a fetch++{- I originally used+epoch = "Thu, 1 Jan 1970 00:00:00 GMT"+but for some reason the server returns "not modified" if you do that++-}+epoch = "Sat, 27 May 2000 08:12:00 GMT"+
+ Makefile view
@@ -0,0 +1,32 @@+all: Data/ISO3166_CountryCodes.o docs++Data/ISO3166_CountryCodes.hs: Data/ISO3166_CountryCodes/GenerateDataDeclaration Data/ISO3166_CountryCodes.preamble.hs+ Data/ISO3166_CountryCodes/GenerateDataDeclaration > $@++Data/ISO3166_CountryCodes/GenerateDataDeclaration: Data/ISO3166_CountryCodes/GenerateDataDeclaration.hs+ ghc --make -iData/ISO3166_CountryCodes $@++%.o: %.hs+ ghc --make $<++.PHONY: docs++docs: Data/Documentation/haddock/ISO3166_CountryCodes.haddock++Data/Documentation/haddock/ISO3166_CountryCodes.haddock: Data/Documentation/haddock Data/ISO3166_CountryCodes.hs+ haddock -o Data/Documentation/haddock -h\+ --read-interface=http://www.haskell.org/ghc/docs/latest/html/libraries/base,/usr/share/doc/ghc/libraries/base/base.haddock \+ --read-interface=http://www.haskell.org/ghc/docs/latest/html/libraries/haskell98,/usr/share/doc/ghc/libraries/haskell98/haskell98.haddock \+ --read-interface=http://www.haskell.org/ghc/docs/latest/html/libraries/template-haskell/,/usr/share/doc/ghc/libraries/template-haskell/template-haskell.haddock \+ --read-interface=http://www.haskell.org/ghc/docs/latest/html/libraries/time/,/usr/share/doc/ghc/libraries/time/time.haddock \+ --read-interface=http://hackage.haskell.org/packages/archive/bytestring/0.9.1.4/doc/html/,/usr/share/doc/ghc/libraries/bytestring/bytestring.haddock \+ --read-interface=http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/,/usr/share/doc/ghc/libraries/containers/containers.haddock \+ --dump-interface Data/Documentation/haddock/ISO3166_CountryCodes.haddock \+ Data/ISO3166_CountryCodes.hs++Data/Documentation/haddock:+ mkdir -p Data/Documentation/haddock++.PHONY: clean+clean:+ rm -rf Data/*.hi Data/*.o Data/ISO3166_CountryCodes.hs Data/ISO3166_CountryCodes/*.hi Data/ISO3166_CountryCodes/*.o Data/ISO3166_CountryCodes/GenerateDataDeclaration Data/ISO3166_CountryCodes/iso3166_en_code_lists.txt Data/ISO3166_CountryCodes/iso3166_en_code_lists.txt.last-modified Data/Documentation/haddock
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Make+main = defaultMain
+ iso3166-country-codes.cabal view
@@ -0,0 +1,24 @@+Name: iso3166-country-codes+Version: 0.1+Cabal-Version: >= 1.6+Build-type: Make+License: LGPL+License-File: COPYING.LESSER+Author: Jón Fairbairn+maintainer: jon.fairbairn@cl.cam.ac.uk+Category: Data+Synopsis: A datatype for ISO 3166 country codes++Description: Defines datatype CountryCode that has a two+ letter constructor corresponding to each of the two letter+ ISO 3166 country codes, and mappings from that type to the+ official name of the country.++Extra-source-files: Data/ISO3166_CountryCodes/GenerateDataDeclaration.hs, Data/ISO3166_CountryCodes/Getter.hs, Data/ISO3166_CountryCodes.preamble.hs, Makefile++extra-tmp-files: Data/ISO3166_CountryCodes.hs, Data/ISO3166_CountryCodes/GenerateDataDeclaration++Library+ Build-Depends: base ==4.*, HTTP == 4000.*, utf8-string ==0.*+ Exposed-modules: Data.ISO3166_CountryCodes+