Example #1
0
 def request_new_license(self, email):
     license = LICENSE.dump_data()
     if license is None:
         raise umcm.UMC_CommandError(_('Cannot parse License from LDAP'))
     data = {}
     data['email'] = email
     data['licence'] = license
     data = urllib.urlencode(data)
     url = 'https://license.univention.de/keyid/conversion/submit'
     request = urllib2.Request(url,
                               data=data,
                               headers={'User-agent': 'UMC/AppCenter'})
     try:
         util.urlopen(request)
     except Exception as e:
         try:
             # try to parse an html error
             body = e.read()
             detail = re.search(
                 '<span id="details">(?P<details>.*?)</span>',
                 body).group(1)
         except:
             detail = str(e)
         raise umcm.UMC_CommandError(
             _('An error occurred while sending the request: %s') % detail)
     else:
         return True
def get_weather():
    page = util.urlopen(_URL_WEATHER)
    if not page:
        return None
    temperature = None
    pressure = None
    rain = None
    wind = None
    weather = ""
    try:
        j = json.loads(page)
        if 'outsideTemp' in j:
            temperature = j['outsideTemp']
        if 'barometer' in j:
            pressure = j['barometer']
        if 'rain' in j:
            rain = j['rain']
        if 'windDirectionDegrees' in j and 'windSpeed' in j and 'windBeaufortScale':
            wind = (j['windDirectionDegrees'], j['windSpeed'],
                    j['windBeaufortScale'])
    except Exception as e:
        print(e)
    if temperature is not None:
        weather += build_temperature_string(temperature)
    if pressure is not None:
        weather += build_pressure_string(pressure)
    if rain is not None:
        weather += build_rain_string(rain)
    if wind is not None:
        wind = build_wind_string(wind[0], wind[1], wind[2])
        if wind is not None:
            weather += wind

    return weather
Example #3
0
    def _get_category_translations(cls):
        if cls._category_translations == None:
            cls._category_translations = {}
            url = '%s/../categories.ini' % cls.get_metainf_url()
            try:
                # open .ini file
                MODULE.info('opening category translation file: %s' % url)
                fp = util.urlopen(url)
                config = ConfigParser.ConfigParser()
                config.readfp(fp)

                # get the translations for the current language
                loc = locale.getlocale()[0]
                if isinstance(loc, basestring):
                    if not config.has_section(loc):
                        loc = loc.split('_')[0]
                    if config.has_section(loc):
                        for k, v in config.items(loc):
                            cls._category_translations[k] = v
            except (ConfigParser.Error, urllib2.HTTPError) as e:
                MODULE.warn(
                    'Could not load category translations from: %s\n%s' %
                    (url, e))
            MODULE.info('loaded category translations: %s' %
                        cls._category_translations)
        return cls._category_translations
Example #4
0
	def _fetch_file(self, key, url):
		try:
			# open the license file 
			fp = util.urlopen(url)
			self._options[key] = ''.join(fp.readlines()).strip()
		except (urllib2.HTTPError, urllib2.URLError) as e:
			MODULE.warn('No information for %s available (%s): %s' % (key, e, url))
Example #5
0
 def _fetch_file(self, key, url):
     try:
         # open the license file
         fp = util.urlopen(url)
         self._options[key] = ''.join(fp.readlines()).strip()
     except (urllib2.HTTPError, urllib2.URLError) as e:
         MODULE.warn('No information for %s available (%s): %s' %
                     (key, e, url))
Example #6
0
def get_photo_hash(_url):
    photo = util.urlopen(_url)
    if not photo:
        return None
    sha = hashlib.sha256()
    sha.update(photo)
    h = sha.hexdigest()
    return h
Example #7
0
	def _send_information(self, action, status):
		ucr.load()
		server = self.get_server()
		url = 'http://%s/postinst' % (server, )
		uuid = LICENSE.uuid or '00000000-0000-0000-0000-000000000000'
		try:
			values = {'uuid': uuid,
			          'app': self.id,
			          'version': self.version,
			          'action': action,
			          'status': status,
			          'role': ucr.get('server/role'),
			          }
			request_data = urllib.urlencode(values)
			request = urllib2.Request(url, request_data)
			util.urlopen(request)
		except:
			MODULE.warn(traceback.format_exc())
Example #8
0
 def _send_information(self, action, status):
     ucr.load()
     server = self.get_server()
     url = 'http://%s/postinst' % (server, )
     uuid = LICENSE.uuid or '00000000-0000-0000-0000-000000000000'
     try:
         values = {
             'uuid': uuid,
             'app': self.id,
             'version': self.version,
             'action': action,
             'status': status,
             'role': ucr.get('server/role'),
         }
         request_data = urllib.urlencode(values)
         request = urllib2.Request(url, request_data)
         util.urlopen(request)
     except:
         MODULE.warn(traceback.format_exc())
def get_temperature():
    page = util.urlopen(_URL_WEATHER)
    if not page:
        return None
    try:
        j = json.loads(page)
        if 'outsideTemp' in j:
            return j['outsideTemp']
    except Exception as e:
        print(e)
    return None
def get_rain():
    page = util.urlopen(_URL_WEATHER)
    if not page:
        return None
    try:
        j = json.loads(page)
        if 'rain' in j:
            return j['rain']
    except Exception as e:
        print(e)
    return None
def get_pressure():
    page = util.urlopen(_URL_WEATHER)
    if not page:
        return None
    try:
        j = json.loads(page)
        if 'barometer' in j:
            return j['barometer']
    except Exception as e:
        print(e)
    return None
Example #12
0
    def app_center_app_license(self, application):
        application = Application.find(application)
        if not application or not application.get("licensefile"):
            raise umcm.UMC_CommandError(_("No license file available for application: %s") % (application.id))

            # open the license file and replace line breaks with BR-tags
        fp = util.urlopen(application.get("licensefile"))
        txt = "".join(fp.readlines()).strip()
        txt = txt.replace("\n\n\n", "\n<br>\n<br>\n<br>\n")
        txt = txt.replace("\n\n", "\n<br>\n<br>\n")
        return txt
Example #13
0
 def request_new_license(self, email):
     license = LICENSE.dump_data()
     if license is None:
         raise umcm.UMC_CommandError(_("Cannot parse License from LDAP"))
     data = {}
     data["email"] = email
     data["licence"] = license
     data = urllib.urlencode(data)
     url = "https://license.univention.de/keyid/conversion/submit"
     request = urllib2.Request(url, data=data, headers={"User-agent": "UMC/AppCenter"})
     try:
         util.urlopen(request)
     except Exception as e:
         try:
             # try to parse an html error
             body = e.read()
             detail = re.search('<span id="details">(?P<details>.*?)</span>', body).group(1)
         except:
             detail = str(e)
         raise umcm.UMC_CommandError(_("An error occurred while sending the request: %s") % detail)
     else:
         return True
Example #14
0
    def app_center_app_license(self, application):
        application = Application.find(application)
        if not application or not application.get('licensefile'):
            raise umcm.UMC_CommandError(
                _('No license file available for application: %s') %
                (application.id))

        # open the license file and replace line breaks with BR-tags
        fp = util.urlopen(application.get('licensefile'))
        txt = ''.join(fp.readlines()).strip()
        txt = txt.replace('\n\n\n', '\n<br>\n<br>\n<br>\n')
        txt = txt.replace('\n\n', '\n<br>\n<br>\n')
        return txt
Example #15
0
	def _get_category_translations(cls):
		if cls._category_translations == None:
			cls._category_translations = {}
			url = '%s/../categories.ini' % cls.get_metainf_url()
			try:
				# open .ini file
				MODULE.info('opening category translation file: %s' % url)
				fp = util.urlopen(url)
				config = ConfigParser.ConfigParser()
				config.readfp(fp)

				# get the translations for the current language
				loc = locale.getlocale()[0]
				if isinstance(loc, basestring):
					if not config.has_section(loc):
						loc = loc.split('_')[0]
					if config.has_section(loc):
						for k, v in config.items(loc):
							cls._category_translations[k] = v
			except (ConfigParser.Error, urllib2.HTTPError) as e:
				MODULE.warn('Could not load category translations from: %s\n%s' % (url, e))
			MODULE.info('loaded category translations: %s' % cls._category_translations)
		return cls._category_translations
Example #16
0
    def all(cls, force_reread=False):
        # reload ucr variables
        ucr.load()

        # load the first time the category translations
        cls._get_category_translations()

        if force_reread:
            cls._all_applications = None

        if cls._all_applications is None:
            # query all applications from the server
            ucr.load()
            url = cls.get_metainf_url()
            parser = HTMLParser()
            try:
                cls._all_applications = []

                threads = []
                for iline in util.urlopen(url):
                    # parse the server's directory listing
                    m = cls._reg_dir_listing.match(iline)
                    if m:
                        # try to load and parse application's .ini file
                        ifilename = m.group('name')
                        # 'foo%20&amp;%20bar.ini' -> 'foo%20&%20bar.ini'
                        ifilename = parser.unescape(ifilename)
                        iurl = url + '/' + ifilename

                        # thread function
                        def _append_app(myurl):
                            try:
                                cls._all_applications.append(
                                    Application(myurl))
                            except (ConfigParser.Error, urllib2.HTTPError,
                                    urllib2.URLError, ValueError,
                                    KeyError) as e:
                                MODULE.warn(
                                    'Could not open application file: %s\n%s' %
                                    (myurl, e))

                        # start a new thread for fetching the application information
                        thread = threading.Thread(target=_append_app,
                                                  args=(iurl, ))
                        thread.start()
                        threads.append(thread)

                # wait until all threads are finished
                for ithread in threads:
                    ithread.join()

            except (urllib2.HTTPError, urllib2.URLError) as e:
                MODULE.warn('Could not query App Center host at: %s\n%s' %
                            (url, e))
                raise

        # filter function
        def _included(the_list, app):
            if the_list == '*':
                return True
            the_list = map(str.lower, cls._reg_comma.split(the_list))
            if app.name.lower() in the_list:
                return True
            for category in app.get('categories'):
                if category.lower() in the_list:
                    return True
            return False

        # filter blacklisted apps (by name and by category)
        filtered_applications = cls._all_applications
        blacklist = ucr.get('repository/app_center/blacklist')
        if blacklist:
            filtered_applications = [
                app for app in filtered_applications
                if not _included(blacklist, app)
            ]

        # filter whitelisted apps (by name and by category)
        whitelist = ucr.get('repository/app_center/whitelist')
        if whitelist:
            # whitelist is stronger than blacklist: iterate over all_applications
            filtered_applications = [
                app for app in cls._all_applications
                if _included(whitelist, app) or app in filtered_applications
            ]

        # group app entries by their ID
        app_map = {}
        for iapp in filtered_applications:
            if iapp.id not in app_map:
                app_map[iapp.id] = []
            app_map[iapp.id].append(iapp)

        # version string comparison
        def _version_cmp(iapp, japp):
            iver = LooseVersion(iapp.version)
            jver = LooseVersion(japp.version)
            return cmp(iver, jver)

        # pick the latest version of each app
        final_applications = []
        for iid, iapps in app_map.iteritems():
            # sort apps after their version (latest first)
            iapps.sort(cmp=_version_cmp, reverse=True)

            # store all versions
            iapps[0].versions = iapps
            final_applications.append(iapps[0])

        return final_applications
Example #17
0
	def all(cls, force_reread=False):
		# reload ucr variables
		ucr.load()

		# load the first time the category translations
		cls._get_category_translations()

		if force_reread:
			cls._all_applications = None

		if cls._all_applications is None:
			# query all applications from the server
			ucr.load()
			url = cls.get_metainf_url()
			parser = HTMLParser()
			try:
				cls._all_applications = []

				threads = []
				for iline in util.urlopen(url):
					# parse the server's directory listing
					m = cls._reg_dir_listing.match(iline)
					if m:
						# try to load and parse application's .ini file
						ifilename = m.group('name')
						# 'foo%20&amp;%20bar.ini' -> 'foo%20&%20bar.ini'
						ifilename = parser.unescape(ifilename)
						iurl = url + '/' + ifilename

						# thread function
						def _append_app(myurl):
							try:
								cls._all_applications.append(Application(myurl))
							except (ConfigParser.Error, urllib2.HTTPError, urllib2.URLError, ValueError, KeyError) as e:
								MODULE.warn('Could not open application file: %s\n%s' % (myurl, e))

						# start a new thread for fetching the application information
						thread = threading.Thread(target=_append_app, args=(iurl,))
						thread.start()
						threads.append(thread)

				# wait until all threads are finished
				for ithread in threads:
					ithread.join()

			except (urllib2.HTTPError, urllib2.URLError) as e:
				MODULE.warn('Could not query App Center host at: %s\n%s' % (url, e))
				raise

		# filter function
		def _included(the_list, app):
			if the_list == '*':
				return True
			the_list = map(str.lower, cls._reg_comma.split(the_list))
			if app.name.lower() in the_list:
				return True
			for category in app.get('categories'):
				if category.lower() in the_list:
					return True
			return False

		# filter blacklisted apps (by name and by category)
		filtered_applications = cls._all_applications
		blacklist = ucr.get('repository/app_center/blacklist')
		if blacklist:
			filtered_applications = [app for app in filtered_applications if not _included(blacklist, app)]

		# filter whitelisted apps (by name and by category)
		whitelist = ucr.get('repository/app_center/whitelist')
		if whitelist:
			# whitelist is stronger than blacklist: iterate over all_applications
			filtered_applications = [app for app in cls._all_applications if _included(whitelist, app) or app in filtered_applications]

		# group app entries by their ID
		app_map = {}
		for iapp in filtered_applications:
			if iapp.id not in app_map:
				app_map[iapp.id] = []
			app_map[iapp.id].append(iapp)

		# version string comparison
		def _version_cmp(iapp, japp):
			iver = LooseVersion(iapp.version)
			jver = LooseVersion(japp.version)
			return cmp(iver, jver)

		# pick the latest version of each app
		final_applications = []
		for iid, iapps in app_map.iteritems():
			# sort apps after their version (latest first)
			iapps.sort(cmp=_version_cmp, reverse=True)

			# store all versions
			iapps[0].versions = iapps
			final_applications.append(iapps[0])

		return final_applications
Example #18
0
    def __init__(self, url):
        # load config file
        self._options = {}
        fp = util.urlopen(url)
        config = ConfigParser.ConfigParser()
        config.readfp(fp)

        # copy values from config file
        for k, v in config.items('Application'):
            self._options[k] = cgi.escape(v)

        # overwrite english values with localized translations
        loc = locale.getlocale()[0]
        if isinstance(loc, basestring):
            if not config.has_section(loc):
                loc = loc.split('_')[0]
            if config.has_section(loc):
                for k, v in config.items(loc):
                    self._options[k] = v

        # parse boolean values
        for ikey in ('notifyvendor', ):
            if ikey in self._options:
                self._options[ikey] = config.getboolean('Application', ikey)
            else:
                self._options[ikey] = False

        # parse list values
        for ikey in ('categories', 'defaultpackages',
                     'conflictedsystempackages', 'defaultpackagesmaster',
                     'conflictedapps', 'serverrole'):
            ival = self.get(ikey)
            if ival:
                self._options[ikey] = self._reg_comma.split(ival)
            else:
                self._options[ikey] = []

        # localize the category names
        category_translations = self._get_category_translations()
        self._options['categories'] = [
            category_translations.get(icat.lower()) or icat
            for icat in self.get('categories')
        ]

        # return a proper URL for local files
        for ikey in ('screenshot', ):
            if self.get(ikey):
                self._options[ikey] = urllib2.urlparse.urljoin(
                    '%s/' % self.get_metainf_url(), self.get(ikey))

        # save important meta data
        self.id = self._options['id'] = self._options['id'].lower()
        self.name = self._options['name']
        self.icon = self._options['icon'] = '%s.png' % url[:-4]
        self.version = self._options['version']

        # get the name of the component
        m = self._reg_component_id.match(url)
        self.component_id = 'unknown'
        if m:
            self.component_id = m.groupdict()['id']

        # fetch files via threads
        threads = [
            threading.Thread(
                target=self._fetch_file,
                args=('licenseagreement',
                      self.get_repository_url() + '/LICENSE_AGREEMENT')),
            threading.Thread(target=self._fetch_file,
                             args=('readmeupdate', self.get_repository_url() +
                                   '/README_UPDATE')),
        ]
        for ithread in threads:
            ithread.start()
        for ithread in threads:
            ithread.join()
Example #19
0
	def __init__(self, url):
		# load config file
		self._options = {}
		fp = util.urlopen(url)
		config = ConfigParser.ConfigParser()
		config.readfp(fp)

		# copy values from config file
		for k, v in config.items('Application'):
			self._options[k] = cgi.escape(v)

		# overwrite english values with localized translations
		loc = locale.getlocale()[0]
		if isinstance(loc, basestring):
			if not config.has_section(loc):
				loc = loc.split('_')[0]
			if config.has_section(loc):
				for k, v in config.items(loc):
					self._options[k] = v

		# parse boolean values
		for ikey in ('notifyvendor',):
			if ikey in self._options:
				self._options[ikey] = config.getboolean('Application', ikey)
			else:
				self._options[ikey] = False

		# parse list values
		for ikey in ('categories', 'defaultpackages', 'conflictedsystempackages', 'defaultpackagesmaster', 'conflictedapps', 'serverrole'):
			ival = self.get(ikey)
			if ival:
				self._options[ikey] = self._reg_comma.split(ival)
			else:
				self._options[ikey] = []

		# localize the category names
		category_translations = self._get_category_translations()
		self._options['categories'] = [ category_translations.get(icat.lower()) or icat for icat in self.get('categories') ]

		# return a proper URL for local files
		for ikey in ('screenshot',):
			if self.get(ikey):
				self._options[ikey] = urllib2.urlparse.urljoin('%s/' % self.get_metainf_url(), self.get(ikey))

		# save important meta data
		self.id = self._options['id'] = self._options['id'].lower()
		self.name = self._options['name']
		self.icon = self._options['icon'] = '%s.png' % url[:-4]
		self.version = self._options['version']

		# get the name of the component
		m = self._reg_component_id.match(url)
		self.component_id = 'unknown'
		if m:
			self.component_id = m.groupdict()['id']

		# fetch files via threads
		threads = [
			threading.Thread(target=self._fetch_file, args=('licenseagreement', self.get_repository_url() + '/LICENSE_AGREEMENT')),
			threading.Thread(target=self._fetch_file, args=('readmeupdate', self.get_repository_url() + '/README_UPDATE')),
		]
		for ithread in threads:
			ithread.start()
		for ithread in threads:
			ithread.join()