def areas():
    """returns a list of IstatArea objects representing all the area used to classify datasets"""
    global __istat__
    if __istat__ is None:
        downloader = jsonstat.Downloader(cache_dir="./istat_cached",
                                         time_to_live=None)
        __istat__ = IstatRoot(downloader)
    return __istat__.areas()
def lang(lg):
    global __istat__
    if __istat__ is None:
        downloader = jsonstat.Downloader(cache_dir="./istat_cached",
                                         time_to_live=None)
        __istat__ = IstatRoot(downloader, lang=lg)
    __istat__.lang(lg)
    return lg
Exemple #3
0
def test_downloader_with_no_cache_dir():
    uri = 'http://json-stat.org/samples/oecd-canada.json'
    body = 'This is a test'

    with requests_mock.mock() as m:
        m.get(uri, text=body)
        d = jsonstat.Downloader(cache_dir=None)
        response = d.download(uri)

    assert body == response
def area(spec):
    """returns a IstatArea object conforming to ``spec``.
    :param spec: name of istat area
    """
    global __istat__
    if __istat__ is None:
        downloader = jsonstat.Downloader(cache_dir="./istat_cached",
                                         time_to_live=None)
        __istat__ = IstatRoot(downloader)
    return __istat__.area(spec)
Exemple #5
0
def test_downloader(tmpdir):
    # tmpdir is a LocalPath type in 3.6
    # it must be converted it in str for < 3.6
    uri = 'http://json-stat.org/samples/oecd-canada.json'
    body = 'This is a test'

    with requests_mock.mock() as m:
        m.get(uri, text=body)
        d = jsonstat.Downloader(cache_dir=str(tmpdir))
        response = d.download(uri)

    assert body == response
def dataset(spec_area, spec_dataset):
    """returns the IstatDataset identified by ``spec_dataset``` (name of the dataset)
    contained into the IstatArea identified by ```spec_area``` (name of the area)
    :param spec_area: name of istat area
    :param spec_dataset: name of istat dataset
    """
    global __istat__
    if __istat__ is None:
        downloader = jsonstat.Downloader(cache_dir="./istat_cached",
                                         time_to_live=None)
        __istat__ = IstatRoot(downloader)
    return __istat__.dataset(spec_area, spec_dataset)
    def __init__(self, downloader=None, lang=1):
        """Initialize Istat class.
        :param downloader: where to store the cached file
        :param lang: 1=english, otherwise italian
        """

        if downloader is None:
            downloader = jsonstat.Downloader(cache_dir="./istat_cached",
                                             time_to_live=None)

        self.__istat_helper = IstatHelper(downloader, lang)
        self.__id2area = None
        self.__cod2area = None
        self.__desc2area = None
def cache_dir(cache_dir=None, time_to_live=None):
    """Manage the directory ``cached_dir`` where to store downloaded files

    without parameter get the directory
    with a parameter set the directory
    :param time_to_live:
    :param cache_dir:
    """
    global __istat__

    if cache_dir is None:
        if __istat__ is None:
            __istat__ = IstatRoot()
        return __istat__.cache_dir()

    downloader = jsonstat.Downloader(cache_dir, time_to_live)
    __istat__ = IstatRoot(downloader, lang=1)
    return __istat__.cache_dir()
# stdlib
from __future__ import print_function
from __future__ import unicode_literals

import os

# external modules
import pytest

# jsonstat/istat
import jsonstat
import istat

fixture_dir = os.path.join(os.path.dirname(__file__), "fixtures", "istat")
downloader = jsonstat.Downloader(fixture_dir)
i_en = istat.IstatRoot(downloader, lang=1)


def test_istat_root():
    i_it = istat.IstatRoot(downloader, lang=0)
    assert i_it.cache_dir() == fixture_dir


def test_istat_italian():
    i_it = istat.IstatRoot(downloader, lang=0)
    n = i_it.area(26).desc
    assert "Lavoro" == n

    d = i_it.area(26).dataset('DCCV_INATTIVMENS').name
    assert u'Inattivi - dati mensili' == d
    """
    :type istat_helper: IstatHelper
    :param istat_helper:
    :return:
    """
    json_data = istat_helper.area(show=False)
    for area in json_data:
        list_dataset_dim(istat_helper, area)


if __name__ == "__main__":
    # cache_dir where to store downloaded data file
    JSONSTAT_HOME = os.path.join(os.path.dirname(__file__), "..")
    cache_dir = os.path.normpath(
        os.path.join(JSONSTAT_HOME, "istat-tests", "fixtures", "istat_cached"))
    downloader = jsonstat.Downloader(cache_dir)
    istat = IstatHelper(downloader, lang=1)

    # list_area_dataset_dim(istat_helper)

    istat.help()

    print("*** all areas")
    istat.area()

    # ...
    # {
    #      "Cod": "LAB",
    #      "Desc": "Lavoro",
    #      "Id": 26
    #  }