def find_city(self, pattern, max_results):
		pattern = pattern.decode(self.locale.codeset).lower()
		MODULE.info('pattern: %s' % pattern)
		if not pattern:
			return []

		# for the given pattern, find matching cities
		city_data = util.get_city_data()
		matches = []
		for icity in city_data:
			match = None
			for jlabel in icity.get('label', {}).itervalues():
				label = jlabel.decode(self.locale.codeset).lower()
				if pattern in label:
					# matching score is the overlap if the search pattern and the matched text
					# (as fraction between 0 and 1)
					match_score = len(pattern) / float(len(label))
					if match and match_score < match['match_score']:
						# just keep the best match of a city
						continue
					if match_score > 0.1:
						# found a match with more than 10% overlap :)
						match = icity.copy()
						match['match'] = jlabel
						match['match_score'] = match_score
			if match:
				matches.append(match)
		MODULE.info('Search for pattern "%s" with %s matches' % (pattern, len(matches)))
		if not matches:
			return None

		# add additional score w.r.t. the population size of the city
		# such that the largest city gains additional 0.4 on top
		max_population = max([imatch['population'] for imatch in matches])
		weighted_inv_max_population = 0.6 / float(max_population)
		for imatch in matches:
			imatch['final_score'] = imatch['match_score'] + weighted_inv_max_population * imatch['population']

		# sort matches...
		matches.sort(key=lambda x: x['final_score'], reverse=True)
		MODULE.info('Top 5 matches: %s' % json.dumps(matches[:5], indent=2))
		matches = matches[:max_results]

		# add additional information about keyboard layout, time zone etc. and
		# get the correct localized labels
		country_data = util.get_country_data()
		for imatch in matches:
			match_country = country_data.get(imatch.get('country'))
			if match_country:
				imatch.update(util.get_random_nameserver(match_country))
				imatch.update(dict(
					default_lang=match_country.get('default_lang'),
					country_label=self._get_localized_label(match_country.get('label', {})),
					label=self._get_localized_label(imatch.get('label')) or imatch.get('match'),
				))

		return matches
	def lang_countrycodes(self, request):
		'''Return a list of all countries with their two letter chcountry codes.'''
		country_data = util.get_country_data()
		countries = [{
			'id': icountry,
			'label': self._get_localized_label(idata.get('label', {})),
		}
			for icountry, idata in country_data.iteritems()
			if idata.get('label')]

		# add the value from ucr value to the list
		# this is required because invalid values will be unset in frontend
		# Bug #26409
		tmpUCR = univention.config_registry.ConfigRegistry()
		tmpUCR.load()
		ssl_country = tmpUCR.get('ssl/country')
		if ssl_country not in [i['id'] for i in countries]:
			countries.append({'label': ssl_country, 'id': ssl_country})

		self.finished(request.id, countries)
Пример #3
0
 def _preload_city_data(self):
     util.get_city_data()
     util.get_country_data()