def cacheRegionCities2Json(self, limit, showDone=False):
     # TODO - refactor to Locator/LocationContext - make available via command line
     wd = Wikidata()
     config = LocationContext.getDefaultConfig()
     countryManager = CountryManager(config=config)
     countryManager.fromCache()
     regionManager = RegionManager(config=config)
     regionManager.fromCache()
     regionList = regionManager.getList()
     total = len(regionList)
     cachePath = f"{config.getCachePath()}/regions"
     if not os.path.exists(cachePath):
         os.makedirs(cachePath)
     for index, region in enumerate(regionList):
         if index >= limit:
             break
         regionId = region.wikidataid
         msg = f"{index+1:4d}/{total:4d}:getting cities for {region.name} {region.iso} {region.wikidataid}"
         jsonFileName = f"{cachePath}/{region.iso}.json"
         if os.path.isfile(jsonFileName):
             if showDone:
                 print(msg)
         else:
             try:
                 regionCities = wd.getCitiesForRegion(regionId, msg)
                 jsonStr = json.dumps(regionCities)
                 with open(jsonFileName, "w") as jsonFile:
                     jsonFile.write(jsonStr)
             except Exception as ex:
                 self.handleWikidataException(ex)
 def testCityFromCityStates(self):
     '''
     tests if city states are queried correctly if given the region
     For city states the city is region and city (in some cases also country).
     This test ensures that by querying for the cities of a region the city states include themself in the result
     (the result for cities in city-states often includes the municipalities)
     '''
     wd = Wikidata()
     cityStateRecords = wd.getCityStates()
     for cityStateRecord in cityStateRecords:
         regionId = cityStateRecord.get('wikidataid')
         regionCities = wd.getCitiesForRegion(
             regionId,
             msg=f"Query for cities in {cityStateRecord.get('name')}")
         foundCities = [c.get('wikidataid') for c in regionCities]
         self.assertTrue(regionId in foundCities)