示例#1
0
def isoCountry(arg):
    """Given a country name or an ISO 3166 code, return the pair {name,code}.

        >>> country = isoCountry(country_or_cc)

    Examples
    --------

        >>> geo.isoCountry('CZ')
            {'code': 'CZ', 'country': 'Czechia'}
        >>> geo.isoCountry('Greece')
            {'code': 'EL', 'country': 'Greece'}
    """
    country, cc = None, None
    if not (arg is None or isinstance(arg, (string_types,Mapping))):
        raise TypeError("Wrong format for country/code '%s' - must be a string or a dictionary" % arg)
    elif isinstance(arg, string_types):
        if arg in COUNTRIES.keys():
            cc, country = arg, None
        elif arg in COUNTRIES.values():
            cc, country = None, arg
        else:
            raise IOError("Country/code '%s' not recognised" % arg)
    elif isinstance(arg, Mapping):
        if 'code' in arg or 'name' in arg:
            cc, country = arg.get('code', None), arg.get('name', None)
        else:
            try:
                cc, country = list(next(iter(arg.items()))) # list(arg.items())[0]
            except:
                pass
    if cc in ('', None) and country in ('', {}, None):
        raise IOError("Missing parameters to define country/code")
    elif cc in ('', None): # and NOT country in ('', {}, None)
        try:
            cc = dict(map(reversed, COUNTRIES.items())).get(country)
        except:
            #cc = country.split()
            #if len(cc) >1:              cc = ''.join([c[0].upper() for c in country.split()])
            #else:                       cc = country # cc[0]
            cc = None
    elif country in ('', {}, None): # and NOT cc in ('', None)
        try:
            country = COUNTRIES.get(cc)
        except:     country = None
    return {'code': cc, 'name': country}
示例#2
0
def __main():
    """Parse and check the command line with default arguments.
    """
    parser = OptionParser(                                                  \
        description=                                                        \
    """Validate output harmonised data on health care services.""",
        usage=                                                              \
    """usage:         validate facility <code>
    facility :        Type of service.
    <code> :          country code."""                                      \
                        )

    parser.add_option("-c", "--cc", action="store", dest="country",
                      help="Country.",
                      default=None)
    (opts, args) = parser.parse_args()

    if not args in (None,()):
        facility = args[0]
    #else:
    #    facility = list(FACILITIES.keys())[0]

    country = opts.country
    if isinstance(coder, string_types):
        if country.upper() == 'ALL':
            country = None
        elif country.upper() in AREAS:
            country = AREAS.get(country)
    elif country is not None:
        parser.error("country name is required.")
    if country in (None,[]) :
        # parser.error("country name is required.")
        country = list(COUNTRIES.values())[0]

    # run the generator
    try:
        run(facility, country)
    except IOError:
        logging.warning('\n!!!  ERROR: data file not validated !!!')
    else:
        logging.warning('\n!  OK: data file correctly validated !')
示例#3
0
def __main():
    """Parse and check the command line with default arguments.
    """
    parser = OptionParser(                                                  \
        description=                                                        \
    """Harmonise input national data on facility services.""",
        usage=                                                              \
    """usage:         harmonise facility <code> <gc> <key>
    facility :        Type of service.
    <code> :          Country code.
    <gc> ;            Geocoder.
    <key> :           Geocoder key"""                                      \
                       )

    #parser.add_option("-s", "--service", action="store", dest="facility",
    #                  help="Facility/service type.",
    #                  default=None)
    parser.add_option("-c",
                      "--cc",
                      action="store",
                      dest="country",
                      help="Country.",
                      default=None)
    parser.add_option("-g",
                      "--geocoder",
                      action="store",
                      dest="gc",
                      help="geocoder.",
                      default=None)
    parser.add_option("-k",
                      "--geokey",
                      action="store",
                      dest="key",
                      help="geocoder key.",
                      default=None)
    #parser.add_option("-r", "--dry-run", action="store_true", dest="dryrun",
    #                  help="run the script without creating the file")
    (opts, args) = parser.parse_args()

    opts.test = 1

    if not args in (None, ()):
        facility = args[0]
    #else:
    #    facility = list(FACILITIES.keys())[0]

    country = opts.country
    if isinstance(country, string_types):
        if country.upper() == 'ALL':
            country = None
        elif country.upper() in AREAS:
            country = AREAS.get(country)
    elif country is not None:
        parser.error("country name is required.")
    if country in (None, []):
        # parser.error("country name is required.")
        country = list(COUNTRIES.values())[0]

    gc = opts.gc
    if isinstance(gc, string_types):
        gc = {gc: opts.key}
    elif gc is not None:
        parser.error("geocoder is required.")

    # run the generator
    try:
        run(facility, country, gc)
    except IOError:
        logging.warning('\n!!!  ERROR: data file not created !!!')
    else:
        logging.warning('\n!  OK: data file correctly created !')