Exemple #1
0
    def run_adventure(self, root_block):
        if root_block is None:
            utils.log('[!] Cannot run NONE adventure for player ' + self.id)
            return None

        try:
            # Tag
            message = self.get_tag(True) + ": "

            # Preambles
            if root_block.public:
                category_data = world.get_category(root_block.category)
                if category_data is not None:
                    preambles = category_data.preambles
                    message += utils.select(preambles) + '\n'

            # Iterate through the blocks!
            message += self.run_adventure_block(root_block)

            # Increment Visits
            self.increment_visits(root_block.handle)

            # Calc Level up
            message += self.check_level_up()

            # Broadcast message
            return message

        except Exception as e:
            utils.log('[!] Encountered an exception in adventure ' +
                      root_block.handle)
            utils.log_exception(e)
            return None
Exemple #2
0
    def embark(self):

        message = ""
        self.roll()
        try:
            if self.next is not None:
                # QUEST CHAIN
                adventure_instance = world.get_adventure(self.next)
                self.next = None
                message = self.run_adventure(adventure_instance)
            elif self.wounded:
                # Handle wounded
                message = self.run_hospital()
            elif self.wants_to_visit_shop() and utils.chance(1 / 3):
                # Handle shop
                message = self.run_shop()
            else:
                # Handle default.
                category = self.get_valid_category()
                adventure_instance = self.get_valid_adventure_from_category(
                    category)
                message = self.run_adventure(adventure_instance)

            self.schedule_next_adventure()
            return message
        except Exception as e:
            utils.log_exception(e)
            return None
Exemple #3
0
    def decode(self):
        """
        Verifies that the receipt can be decoded and that the initial
        contents of the receipt are correct.

        If its invalid, then just return invalid rather than give out any
        information.
        """
        try:
            receipt = decode_receipt(self.receipt)
        except:
            log_exception({
                'receipt': '%s...' % self.receipt[:10],
                'addon': self.addon_id
            })
            log_info('Error decoding receipt')
            raise InvalidReceipt

        try:
            assert receipt['user']['type'] == 'directed-identifier'
        except (AssertionError, KeyError):
            log_info('No directed-identifier supplied')
            raise InvalidReceipt

        return receipt
Exemple #4
0
def quick_dirty_fallback_geocode(addr, parse=True):
    """
    Try to get SOME x,y even with bad blocks data,
    by falling back to external geocoders.
    """
    from ebdata.nlp.addresses import parse_addresses
    from ebpub.geocoder import SmartGeocoder
    if parse:
        addrs = parse_addresses(addr)
    else:
        addrs = [addr]
    for addr, unused in addrs:
        try:
            try:
                result = SmartGeocoder().geocode(addr)
                point = result['point']
                logger.debug("internally geocoded %r" % addr)
                return point.x, point.y
            except GeocodingException:
                logger.debug("internal geocoder failed on %r:\n" % addr)
                log_exception(level=logging.DEBUG)
                x, y = None, None
                # XXX Don't bother, external geocoding rarely gives us
                # anything inside Boston now that we have decent
                # blocks data.  But I want to preserve this script for
                # now till we figure out what to do with geocoding
                # more generally
                continue
        except:
            logger.error('uncaught geocoder exception on %r\n' % addr)
            log_exception()

    return None, None
Exemple #5
0
 def set_day_night_theme(self, dayornight, themename, themefile):
     ''' Sets a new daynight theme'''
     currenttimevalue = xbmc.getInfoLabel(
         "Skin.String(SkinHelper.ColorTheme.%s.time)" % dayornight)
     if not currenttimevalue:
         currenttimevalue = "20:00" if dayornight == "night" else "07:00"
     timevalue = xbmcgui.Dialog().input(
         self.addon.getLocalizedString(32017),
         currenttimevalue).decode("utf-8")
     try:
         # check if the time is valid
         check_date = datetime(*(time.strptime(timevalue, "%H:%M")[0:6]))
         del check_date
         base_setting = "SkinHelper.ColorTheme.%s" % dayornight
         xbmc.executebuiltin("Skin.SetString(%s.theme,%s)" %
                             (base_setting, themename.encode("utf-8")))
         xbmc.executebuiltin("Skin.SetString(%s.time,%s)" %
                             (base_setting, timevalue))
         label = "%s  (%s %s)" % (themename.encode("utf-8"),
                                  self.addon.getLocalizedString(32019),
                                  timevalue)
         xbmc.executebuiltin("Skin.SetString(%s.label,%s)" %
                             (base_setting, label))
         xbmc.executebuiltin("Skin.SetString(%s.file,%s)" %
                             (base_setting, themefile.encode("utf-8")))
     except Exception as exc:
         log_exception(__name__, exc)
         xbmcgui.Dialog().ok(xbmc.getLocalizedString(329),
                             self.addon.getLocalizedString(32018))
Exemple #6
0
    def __init__(self, config):
        fn = config.ship_obj_fn("ui/mainwindow.glade")
        if not os.path.exists(fn):
            print fn
            raise Exception("Unable to load UI file")
        wtree = gtk.glade.XML(fn, "srcs_dialog", "D-RATS")

        self.__config = config
        self.__dialog = wtree.get_widget("srcs_dialog")
        self.__view = wtree.get_widget("srcs_view")
        addbtn = wtree.get_widget("srcs_add")
        editbtn = wtree.get_widget("srcs_edit")
        delbtn = wtree.get_widget("srcs_delete")

        self._setup_view()
        typesel = self._setup_typesel(wtree)

        addbtn.connect("clicked", self._add, typesel)
        editbtn.connect("clicked", self._edit)
        delbtn.connect("clicked", self._rem)

        for stype, (edclass, srcclass) in SOURCE_TYPES.items():
            for key in srcclass.enumerate(self.__config):
                try:
                    src = srcclass.open_source_by_name(self.__config, key)
                    sed = edclass(self.__config, src)
                    self.__store.append(
                        (stype, sed.get_source().get_name(), sed))
                except Exception, e:
                    utils.log_exception()
                    print "Failed to open source %s:%s" % (stype, key)
    def __init__(self):
        win = xbmcgui.Window(10000)
        self.addon = xbmcaddon.Addon(id=ADDON_ID)

        # initialize lmsserver object - grab details from window props set by the service
        lmsplayerid = win.getProperty("lmsplayerid").decode("utf-8")
        lmshost = win.getProperty("lmshost").decode("utf-8")
        lmsport = win.getProperty("lmsport").decode("utf-8")

        if "select_output" in sys.argv[2]:
            self.select_output()
        elif not lmsplayerid:
            log_msg("Service not yet ready - try again later!")
            xbmcplugin.endOfDirectory(handle=ADDON_HANDLE)
        else:
            # show plugin listing
            self.lmsserver = LMSServer(lmshost, lmsport, lmsplayerid)

            # initialize plugin listing
            try:
                self.params = dict(
                    urlparse.parse_qsl(sys.argv[2].replace(
                        '?', '').decode("utf-8")))
                log_msg("plugin called with parameters: %s" % self.params,
                        xbmc.LOGDEBUG)
                self.main()
            except Exception as exc:
                log_exception(__name__, exc)
                xbmcplugin.endOfDirectory(handle=ADDON_HANDLE)

        # cleanup when done processing
        del win
        del self.addon
Exemple #8
0
    def get_config(self):
        '''gets various settings for the script as set by the skinner or user'''

        # skinner (or user) enables the random fanart images by setting the randomfanartdelay skin string
        try:
            self.backgrounds_delay = int(
                xbmc.getInfoLabel("Skin.String(SkinHelper.RandomFanartDelay)"))
        except Exception:
            pass

        self.walls_delay = int(self.addon.getSetting("wallimages_delay"))
        self.wallimages.max_wallimages = int(
            self.addon.getSetting("max_wallimages"))
        self.pvr_bg_recordingsonly = self.addon.getSetting(
            "pvr_bg_recordingsonly") == "true"
        self.enable_walls = xbmc.getCondVisibility(
            "Skin.HasSetting(SkinHelper.EnableWallBackgrounds)")
        if self.addon.getSetting("enable_custom_images_path") == "true":
            self.custom_picturespath = self.addon.getSetting(
                "custom_images_path")
        else:
            self.custom_picturespath = ""
        try:
            # skinner can enable manual wall images generation so check for these settings
            # store in memory so wo do not have to query the skin settings too often
            if self.walls_delay:
                for key in self.all_backgrounds_keys.iterkeys():
                    limitrange = xbmc.getInfoLabel(
                        "Skin.String(%s.EnableWallImages)" % key)
                    if limitrange:
                        self.wallimages.manual_walls[key] = int(limitrange)
        except Exception as exc:
            log_exception(__name__, exc)
Exemple #9
0
 def get_folderandprefix(self):
     '''get the current folder and prefix'''
     cur_folder = ""
     cont_prefix = ""
     try:
         widget_container = self.win.getProperty(
             "SkinHelper.WidgetContainer").decode('utf-8')
         if xbmc.getCondVisibility("Window.IsActive(movieinformation)"):
             cont_prefix = ""
             cur_folder = xbmc.getInfoLabel(
                 "movieinfo-$INFO[Container.FolderPath]"
                 "$INFO[Container.NumItems]"
                 "$INFO[Container.Content]").decode('utf-8')
         elif widget_container:
             cont_prefix = "Container(%s)." % widget_container
             cur_folder = xbmc.getInfoLabel(
                 "widget-%s-$INFO[Container(%s).NumItems]-$INFO[Container(%s).ListItemAbsolute(1).Label]"
                 % (widget_container, widget_container,
                    widget_container)).decode('utf-8')
         else:
             cont_prefix = ""
             cur_folder = xbmc.getInfoLabel(
                 "$INFO[Container.FolderPath]$INFO[Container.NumItems]$INFO[Container.Content]"
             ).decode('utf-8')
     except Exception as exc:
         log_exception(__name__, exc)
         cur_folder = ""
         cont_prefix = ""
     return (cur_folder, cont_prefix)
Exemple #10
0
    def parse_blocks(self):
        while ddt2.ENCODED_HEADER in self.inbuf and \
                ddt2.ENCODED_TRAILER in self.inbuf:
            s = self.inbuf.index(ddt2.ENCODED_HEADER)
            e = self.inbuf.index(ddt2.ENCODED_TRAILER) + \
                len(ddt2.ENCODED_TRAILER)

            if e < s:
                # Excise the extraneous end
                _tmp = self.inbuf[:e-len(ddt2.ENCODED_TRAILER)] + \
                    self.inbuf[e:]
                self.inbuf = _tmp
                continue

            block = self.inbuf[s:e]
            self.inbuf = self.inbuf[e:]

            f = ddt2.DDT2EncodedFrame()
            try:
                if f.unpack(block):
                    print "Got a block: %s" % f
                    self._handle_frame(f)
                elif self.compat:
                    self._send_text_block(block)
                else:
                    print "Found a broken block (S:%i E:%i len(buf):%i" % (\
                        s, e, len(self.inbuf))
                    utils.hexprint(block)
            except Exception, e:
                print "Failed to process block:"
                utils.log_exception()
Exemple #11
0
def quick_dirty_fallback_geocode(addr, parse=True):
    """
    Try to get SOME x,y even with bad blocks data,
    by falling back to external geocoders.
    """
    from ebdata.nlp.addresses import parse_addresses
    from ebpub.geocoder import SmartGeocoder
    if parse:
        addrs = parse_addresses(addr)
    else:
        addrs = [addr]
    for addr, unused in addrs:
        try:
            try:
                result = SmartGeocoder().geocode(addr)
                point = result['point']
                logger.debug("internally geocoded %r" % addr)
                return point.x, point.y
            except GeocodingException:
                logger.debug("internal geocoder failed on %r:\n" % addr)
                log_exception(level=logging.DEBUG)
                x,y = None, None
                # XXX Don't bother, external geocoding rarely gives us
                # anything inside Boston now that we have decent
                # blocks data.  But I want to preserve this script for
                # now till we figure out what to do with geocoding
                # more generally
                continue
        except:
            logger.error('uncaught geocoder exception on %r\n' % addr)
            log_exception()

    return None, None
Exemple #12
0
def application(environ, start_response):
    """
    Developing locally?

        gunicorn -b 0.0.0.0:7000 -w 12 -k sync -t 90 --max-requests 5000 \
            -n gunicorn-theme_update services.wsgi.theme_update:application

    """

    status = '200 OK'
    with statsd.timer('services.theme_update'):
        data = environ['wsgi.input'].read()
        try:
            locale, id_ = url_re.match(environ['PATH_INFO']).groups()
            locale = (locale or 'en-US').lstrip('/')
            id_ = int(id_)
        except AttributeError:  # URL path incorrect.
            start_response('404 Not Found', [])
            return ['']

        try:
            update = ThemeUpdate(locale, id_, environ.get('QUERY_STRING'))
            output = update.get_json()
            if not output:
                start_response('404 Not Found', [])
                return ['']
            start_response(status, update.get_headers(len(output)))
        except:
            log_exception(data)
            raise

    return [output]
Exemple #13
0
 def write(self, buf):
     try:
         self._serial.write(buf)
     except Exception ,e:
         print "Serial write exception: %s" % e
         utils.log_exception()
         raise DataPathIOError("Failed to write to serial port")
Exemple #14
0
    def send_audio_stream(self, track_id, filesize, wave_header, range_l):
        '''chunked transfer of audio data from spotty binary'''
        if self.spotty_bin != None and \
           self.spotty_trackid == track_id and \
           self.spotty_range_l == range_l:
            # leave the existing spotty running and don't start a new one.
            log_msg("WHOOPS!!! Running spotty still handling same request - leave it alone.", \
                    xbmc.LOGERROR)
            return
        elif self.spotty_bin != None:
            # If spotty binary still attached for a different request, try to terminate it.
            log_msg("WHOOPS!!! Running spotty detected - killing it to continue.", \
                    xbmc.LOGERROR)
            self.kill_spotty()

        log_msg("start transfer for track %s - range: %s" % (track_id, range_l), \
                xbmc.LOGDEBUG)
        try:
            self.spotty_trackid = track_id
            self.spotty_range_l = range_l

            # Initialize some loop vars
            max_buffer_size = 524288
            bytes_written = 0

            # Write wave header
            # only count bytes actually from the spotify stream
            # bytes_written = len(wave_header)
            if not range_l:
                yield wave_header

            # get pcm data from spotty stdout and append to our buffer
            args = ["-n", "temp", "--single-track", track_id]
            self.spotty_bin = self.__spotty.run_spotty(args, use_creds=True)

            # ignore the first x bytes to match the range request
            if range_l:
                self.spotty_bin.stdout.read(range_l)

            # Loop as long as there's something to output
            frame = self.spotty_bin.stdout.read(max_buffer_size)
            while frame:
                if cherrypy.response.timed_out:
                    # A timeout occured on the cherrypy session and has been flagged - so exit
                    # The session timer was set to be longer than the track being played so this
                    # would probably require network problems or something bad elsewhere.
                    log_msg("SPOTTY cherrypy response timeout: %r - %s" % \
                            (repr(cherrypy.response.timed_out), cherrypy.response.status), xbmc.LOGERROR)
                    break
                bytes_written += len(frame)
                yield frame
                frame = self.spotty_bin.stdout.read(max_buffer_size)
        except Exception as exc:
            log_exception(__name__, exc)
        finally:
            # make sure spotty always gets terminated
            if self.spotty_bin != None:
                self.kill_spotty()
            log_msg("FINISH transfer for track %s - range %s" % (track_id, range_l), \
                    xbmc.LOGDEBUG)
Exemple #15
0
 def read(self, count):
     try:
         data = self._serial.read(count)
     except Exception, e:
         print "Serial read exception: %s" % e
         utils.log_exception()
         raise DataPathIOError("Failed to read from serial port")
Exemple #16
0
 def read(self, count):
     try:
         data = self._serial.read(count)
     except Exception, e:
         print("Comm      : Serial read exception: %s" % e)
         utils.log_exception()
         raise DataPathIOError("Failed to read from serial port")
Exemple #17
0
    def __init__(self):
        utils.log("Started service")

        revision = utils.get_revision()
        utils.log("Board revision: {}".format(revision))
        if revision is not None:
            utils.set_property_setting('revision', revision)

        max_ram = utils.get_max_ram()
        utils.log("RAM size: {}MB".format(max_ram))
        utils.set_property_setting('max_ram', max_ram)

        board_type = utils.get_type()
        utils.log("Board type: {}".format(board_type))
        if board_type is not None:
            utils.set_property_setting('type', board_type)

        try:
            utils.maybe_init_settings()
        except IOError:
            utils.log_exception()

        self.monitor = MyMonitor(updated_settings_callback=self.apply_config)
        
        while (not xbmc.abortRequested):
            xbmc.sleep(1000)
Exemple #18
0
    def decode(self):
        """
        Verifies that the receipt can be decoded and that the initial
        contents of the receipt are correct.

        If its invalid, then just return invalid rather than give out any
        information.
        """
        try:
            receipt = decode_receipt(self.receipt)
        except:
            log_exception({
                'receipt': '%s...' % self.receipt[:10],
                'app': self.get_app_id(raise_exception=False)
            })
            log_info('Error decoding receipt')
            raise InvalidReceipt('ERROR_DECODING')

        try:
            assert receipt['user']['type'] == 'directed-identifier'
        except (AssertionError, KeyError):
            log_info('No directed-identifier supplied')
            raise InvalidReceipt('NO_DIRECTED_IDENTIFIER')

        return receipt
Exemple #19
0
def application(environ, start_response):
    status = "200 OK"
    with statsd.timer("services.verify"):

        data = environ["wsgi.input"].read()
        try:
            addon_id = id_re.search(environ["PATH_INFO"]).group("addon_id")
        except AttributeError:
            output = ""
            log_info({"receipt": "%s..." % data[:10], "addon": "empty"}, "Wrong url %s" % environ["PATH_INFO"][:20])
            start_response("500 Internal Server Error", [])
            return [output]

        try:
            verify = Verify(addon_id, data, environ)
            output = verify()
            start_response(status, verify.get_headers(len(output)))
            receipt_cef.log(environ, addon_id, "verify", "Receipt verification")
        except:
            output = ""
            log_exception({"receipt": "%s..." % data[:10], "addon": addon_id})
            receipt_cef.log(environ, addon_id, "verify", "Receipt verification error")
            start_response("500 Internal Server Error", [])

    return [output]
Exemple #20
0
def application(environ, start_response):
    status = '200 OK'
    with statsd.timer('services.verify'):

        data = environ['wsgi.input'].read()
        try:
            addon_id = id_re.search(environ['PATH_INFO']).group('addon_id')
        except AttributeError:
            output = ''
            log_info({
                'receipt': '%s...' % data[:10],
                'addon': 'empty'
            }, 'Wrong url %s' % environ['PATH_INFO'][:20])
            start_response('500 Internal Server Error', [])
            return [output]

        try:
            verify = Verify(addon_id, data, environ)
            output = verify()
            start_response(status, verify.get_headers(len(output)))
            receipt_cef.log(environ, addon_id, 'verify',
                            'Receipt verification')
        except:
            output = ''
            log_exception({'receipt': '%s...' % data[:10], 'addon': addon_id})
            receipt_cef.log(environ, addon_id, 'verify',
                            'Receipt verification error')
            start_response('500 Internal Server Error', [])

    return [output]
Exemple #21
0
    def _refresh_location(self):
        fix = self.get_position()

        if not self.__map_point:
            self.__map_point = map_sources.MapStation(fix.station,
                                                      fix.latitude,
                                                      fix.longitude,
                                                      fix.altitude,
                                                      fix.comment)
        else:
            self.__map_point.set_latitude(fix.latitude)
            self.__map_point.set_longitude(fix.longitude)
            self.__map_point.set_altitude(fix.altitude)
            self.__map_point.set_comment(fix.comment)
            self.__map_point.set_name(fix.station)

        try:
            comment = self.config.get("settings", "default_gps_comment")
            fix.APRSIcon = gps.dprs_to_aprs(comment)
        except Exception as e:
            log_exception()
            fix.APRSIcon = "\?"
        self.__map_point.set_icon_from_aprs_sym(fix.APRSIcon)

        self.stations_overlay.add_point(self.__map_point)
        self.map.update_gps_status(self.gps.status_string())

        return True
Exemple #22
0
 def get_data(self, search_query):
     """helper method to get data from google images by scraping and parsing"""
     params = {
         "site": "imghp",
         "tbm": "isch",
         "tbs": "isz:l",
         "q": search_query
     }
     headers = {
         'User-agent':
         'Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; \
         IEMobile/7.0; LG; GW910)'
     }
     html = ''
     try:
         html = requests.get('https://www.google.com/search',
                             headers=headers,
                             params=params,
                             timeout=5).text
     except Exception as exc:
         log_exception(__name__, exc)
     soup = BeautifulSoup.BeautifulSoup(html, features="html.parser")
     results = []
     for div in soup.findAll('div'):
         if not div.get("id") == "images":
             for a_link in div.findAll("a"):
                 page = a_link.get("href")
                 try:
                     img = page.split("imgurl=")[-1]
                     img = img.split("&imgrefurl=")[0]
                     results.append(img)
                 except Exception:
                     pass
     return results
Exemple #23
0
def get_facebook_access_token(request):
    if 'error' in request.GET:
        log_exception(
            'Error in get_facebook_access_token',
            extra={'error': request.GET.get('error'),
                   'error_reason': request.GET.get('error_reason'),
                   'error_description': request.GET.get('error_description')})
        raise FacebookError(request.GET['error'])
    if not 'code' in request.GET:
        raise BadRequestError

    code = request.GET.get('code', '')
    if not code:
        raise Exception

    params = {
        'client_id': settings.FACEBOOK_APP_ID,
        'client_secret': settings.FACEBOOK_SECRET_KEY,
        'redirect_uri': absolute_uri(request.path, request),
        'code': code,
    }
    response = urlopen('https://graph.facebook.com/oauth/access_token?'
                       + urlencode(params)).read()

    return parse_qs(response)['access_token'][-1]
Exemple #24
0
def application(environ, start_response):
    """
    Developing locally?

        gunicorn -b 0.0.0.0:7000 -w 12 -k sync -t 90 --max-requests 5000 \
            -n gunicorn-theme_update services.wsgi.theme_update:application

    """

    status = '200 OK'
    with statsd.timer('services.theme_update'):
        data = environ['wsgi.input'].read()
        try:
            locale, id_ = url_re.match(environ['PATH_INFO']).groups()
            locale = (locale or 'en-US').lstrip('/')
            id_ = int(id_)
        except AttributeError:  # URL path incorrect.
            start_response('404 Not Found', [])
            return ['']

        try:
            update = ThemeUpdate(locale, id_, environ.get('QUERY_STRING'))
            output = update.get_json()
            if not output:
                start_response('404 Not Found', [])
                return ['']
            start_response(status, update.get_headers(len(output)))
        except:
            log_exception(data)
            raise

    return [output]
    def __init__(self, config):
        fn = config.ship_obj_fn("ui/mainwindow.glade")
        if not os.path.exists(fn):
            print fn
            raise Exception("Unable to load UI file")
        wtree = gtk.glade.XML(fn, "srcs_dialog", "D-RATS")

        self.__config = config
        self.__dialog = wtree.get_widget("srcs_dialog")
        self.__view = wtree.get_widget("srcs_view")
        addbtn = wtree.get_widget("srcs_add")
        editbtn = wtree.get_widget("srcs_edit")
        delbtn = wtree.get_widget("srcs_delete")

        self._setup_view()
        typesel = self._setup_typesel(wtree)

        addbtn.connect("clicked", self._add, typesel)
        editbtn.connect("clicked", self._edit)
        delbtn.connect("clicked", self._rem)

        for stype, (edclass, srcclass) in SOURCE_TYPES.items():
            for key in srcclass.enumerate(self.__config):
                try:
                    src = srcclass.open_source_by_name(self.__config, key)
                    sed = edclass(self.__config, src)
                    self.__store.append((stype,
                                         sed.get_source().get_name(),
                                         sed))
                except Exception, e:
                    utils.log_exception()
                    print "Failed to open source %s:%s" % (stype, key)
 def get_folderandprefix(self):
     '''get the current folder and prefix'''
     cur_folder = ""
     cont_prefix = ""
     try:
         widget_container = self.win.getProperty("SkinHelper.WidgetContainer").decode('utf-8')
         if xbmc.getCondVisibility("Window.IsActive(movieinformation)"):
             cont_prefix = ""
             cur_folder = xbmc.getInfoLabel(
                 "movieinfo-$INFO[Container.FolderPath]"
                 "$INFO[Container.NumItems]"
                 "$INFO[Container.Content]").decode('utf-8')
         elif widget_container:
             cont_prefix = "Container(%s)." % widget_container
             cur_folder = xbmc.getInfoLabel(
                 "widget-%s-$INFO[Container(%s).NumItems]-$INFO[Container(%s).ListItemAbsolute(1).Label]" %
                 (widget_container, widget_container, widget_container)).decode('utf-8')
         else:
             cont_prefix = ""
             cur_folder = xbmc.getInfoLabel(
                 "$INFO[Container.FolderPath]$INFO[Container.NumItems]$INFO[Container.Content]").decode(
                 'utf-8')
     except Exception as exc:
         log_exception(__name__, exc)
         cur_folder = ""
         cont_prefix = ""
     return (cur_folder, cont_prefix)
Exemple #27
0
    def parse_blocks(self):
        while ddt2.ENCODED_HEADER in self.inbuf and \
                ddt2.ENCODED_TRAILER in self.inbuf:
            s = self.inbuf.index(ddt2.ENCODED_HEADER)
            e = self.inbuf.index(ddt2.ENCODED_TRAILER) + \
                len(ddt2.ENCODED_TRAILER)

            if e < s:
                # Excise the extraneous end
                _tmp = self.inbuf[:e-len(ddt2.ENCODED_TRAILER)] + \
                    self.inbuf[e:]
                self.inbuf = _tmp
                continue

            block = self.inbuf[s:e]
            self.inbuf = self.inbuf[e:]

            f = ddt2.DDT2EncodedFrame()
            try:
                if f.unpack(block):
                    print "Got a block: %s" % f
                    self._handle_frame(f)
                elif self.compat:
                    self._send_text_block(block)
                else:
                    print "Found a broken block (S:%i E:%i len(buf):%i" % (\
                        s, e, len(self.inbuf))
                    utils.hexprint(block)
            except Exception, e:
                print "Failed to process block:"
                utils.log_exception()
Exemple #28
0
def application(environ, start_response):
    status = '200 OK'
    with statsd.timer('services.verify'):

        data = environ['wsgi.input'].read()
        try:
            addon_id = id_re.search(environ['PATH_INFO']).group('addon_id')
        except AttributeError:
            output = ''
            log_info({'receipt': '%s...' % data[:10], 'addon': 'empty'},
                     'Wrong url %s' % environ['PATH_INFO'][:20])
            start_response('500 Internal Server Error', [])
            return [output]

        try:
            verify = Verify(addon_id, data, environ)
            output = verify()
            start_response(status, verify.get_headers(len(output)))
            receipt_cef.log(environ, addon_id, 'verify',
                            'Receipt verification')
        except:
            output = ''
            log_exception({'receipt': '%s...' % data[:10], 'addon': addon_id})
            receipt_cef.log(environ, addon_id, 'verify',
                            'Receipt verification error')
            start_response('500 Internal Server Error', [])

    return [output]
Exemple #29
0
    def __init__(self):
        utils.log("Started service")

        revision = utils.get_revision()
        utils.log("Board revision: {}".format(revision))
        if revision is not None:
            utils.set_property_setting('revision', revision)

        max_ram = utils.get_max_ram()
        utils.log("RAM size: {}MB".format(max_ram))
        utils.set_property_setting('max_ram', max_ram)

        board_type = utils.get_type()
        utils.log("Board type: {}".format(board_type))
        if board_type is not None:
            utils.set_property_setting('type', board_type)

        try:
            utils.maybe_init_settings()
        except IOError:
            utils.log_exception()

        self.monitor = MyMonitor(updated_settings_callback=self.apply_config)

        while (not xbmc.abortRequested):
            xbmc.sleep(1000)
Exemple #30
0
def save_pickle(content, filename):
    try:
        file = open(filename, 'wb')
        pickle.dump(content, file)
        file.close()
    except Exception as e:
        utils.log_exception(e)
Exemple #31
0
 def write(self, buf):
     try:
         self._serial.write(buf)
     except Exception ,e:
         print("Comm      : Serial write exception: %s" % e)
         utils.log_exception()
         raise DataPathIOError("Failed to write to serial port")
Exemple #32
0
def run(worker_name):
    mongo = MongoStorage(db_name=os.getenv('DB_NAME', DB_NAME),
                         host=os.getenv('DB_HOST', MONGO_HOST),
                         port=os.getenv('DB_PORT', MONGO_PORT))

    while True:
        try:
            if worker_name == 'scrape_operations':
                mongo.ensure_indexes()
                scrape_operations(mongo)
            elif worker_name == 'post_processing':
                post_processing(mongo)
            elif worker_name == 'scrape_all_users':
                scrape_all_users(mongo, quick=False)
            elif worker_name == 'refresh_dbstats':
                refresh_dbstats(mongo)
            else:
                print(f'Worker "{worker_name}" does not exist!')
                quit(1)
        except (KeyboardInterrupt, SystemExit):
            print('Quitting...')
            exit(0)
        except RuntimeError:
            log_exception()
            exit(1)
        except Exception as e:
            print('Exception in worker:', worker_name)
            log_exception()
            time.sleep(5)

        # prevent IO overflow
        time.sleep(0.5)
Exemple #33
0
def application(environ, start_response):
    """
    Developing locally?

        gunicorn -b 0.0.0.0:7000 -w 12 -k sync -t 90 --max-requests 5000 \
            -n gunicorn-theme_update services.wsgi.theme_update:application

    """

    status = "200 OK"
    with statsd.timer("services.theme_update"):
        data = environ["wsgi.input"].read()
        try:
            locale, id_ = url_re.match(environ["PATH_INFO"]).groups()
            locale = (locale or "en-US").lstrip("/")
            id_ = int(id_)
        except AttributeError:  # URL path incorrect.
            start_response("404 Not Found", [])
            return [""]

        try:
            update = ThemeUpdate(locale, id_, environ.get("QUERY_STRING"))
            output = update.get_json()
            if not output:
                start_response("404 Not Found", [])
                return [""]
            start_response(status, update.get_headers(len(output)))
        except:
            log_exception(environ["PATH_INFO"])
            raise

    return [output]
 def start_squeezelite(self):
     '''On supported platforms we include squeezelite binary'''
     playername = xbmc.getInfoLabel("System.FriendlyName").decode("utf-8")
     if self.addon.getSetting("disable_auto_squeezelite") != "true":
         sl_binary = get_squeezelite_binary()
         if sl_binary and self.lmsserver:
             try:
                 sl_output = get_audiodevice(sl_binary)
                 self.kill_squeezelite()
                 log_msg(
                     "Starting Squeezelite binary - Using audio device: %s"
                     % sl_output)
                 args = [
                     sl_binary, "-s", self.lmsserver.host, "-a", "80", "-C",
                     "1", "-m", self.lmsserver.playerid, "-n", playername,
                     "-M", "Kodi", "-o", sl_output
                 ]
                 startupinfo = None
                 if os.name == 'nt':
                     startupinfo = subprocess.STARTUPINFO()
                     startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
                 self._sl_exec = subprocess.Popen(args,
                                                  startupinfo=startupinfo,
                                                  stderr=subprocess.STDOUT)
             except Exception as exc:
                 log_exception(__name__, exc)
     if not self._sl_exec:
         log_msg(
             "The Squeezelite binary was not automatically started, "
             "you should make sure of starting it yourself, e.g. as a service."
         )
         self._sl_exec = False
Exemple #35
0
 def check_daynighttheme(self):
     '''check if a specific day or night theme should be applied'''
     if xbmc.getCondVisibility(
             "Skin.HasSetting(SkinHelper.EnableDayNightThemes) + "
             "Skin.String(SkinHelper.ColorTheme.Day.time) + "
             "Skin.String(SkinHelper.ColorTheme.Night.time)"):
         try:
             daytime = xbmc.getInfoLabel(
                 "Skin.String(SkinHelper.ColorTheme.Day.time)")
             daytime = datetime(
                 *(time.strptime(daytime, "%H:%M")[0:6])).time()
             nighttime = xbmc.getInfoLabel(
                 "Skin.String(SkinHelper.ColorTheme.Night.time)")
             nighttime = datetime(
                 *(time.strptime(nighttime, "%H:%M")[0:6])).time()
             timestamp = datetime.now().time()
             if daytime <= timestamp <= nighttime:
                 dayornight = "Day"
             else:
                 dayornight = "Night"
             current_theme = xbmc.getInfoLabel(
                 "Skin.String(SkinHelper.LastColorTheme)")
             newtheme = xbmc.getInfoLabel(
                 "Skin.String(SkinHelper.ColorTheme.%s.theme)" % dayornight)
             if current_theme != newtheme:
                 themefile = xbmc.getInfoLabel(
                     "Skin.String(SkinHelper.ColorTheme.%s.file)" %
                     dayornight)
                 self.load_colortheme(themefile)
         except Exception as exc:
             log_exception(__name__, exc)
Exemple #36
0
    def onNotification(self, sender, method, data):
        """builtin function for the xbmc.Monitor class"""
        try:
            log_msg("Kodi_Monitor: sender %s - method: %s  - data: %s" %
                    (sender, method, data))
            data = json.loads(data.decode('utf-8'))
            mediatype = ""
            dbid = 0
            transaction = False
            if data and isinstance(data, dict):
                if data.get("item"):
                    mediatype = data["item"].get("type", "")
                    dbid = data["item"].get("id", 0)
                elif data.get("type"):
                    mediatype = data["type"]
                    dbid = data.get("id", 0)
                if data.get("transaction"):
                    transaction = True

            if method == "System.OnQuit":
                self.win.setProperty("SkinHelperShutdownRequested", "shutdown")

            if method == "VideoLibrary.OnUpdate":
                self.process_db_update(mediatype, dbid, transaction)

            if method == "AudioLibrary.OnUpdate":
                self.process_db_update(mediatype, dbid, transaction)

            if method == "Player.OnStop":
                self.monitoring_stream = False
                self.infopanelshown = False
                self.win.clearProperty("Skinhelper.PlayerPlaying")
                self.win.clearProperty("TrailerPlaying")
                self.reset_win_props()

            if method == "Player.OnPlay":
                if not self.monitoring_stream and not getCondVisibility(
                        "Player.DisplayAfterSeek"):
                    self.reset_win_props()
                if self.wait_for_player():
                    if getCondVisibility("Player.HasAudio"):
                        if getCondVisibility("Player.IsInternetStream"):
                            self.monitor_radiostream()
                        else:
                            self.set_music_properties()
                    if getCondVisibility("Pvr.IsPlayingRadio"):
                        if getCondVisibility("!Player.IsInternetStream"):
                            self.monitor_radiostream()
                        else:
                            self.set_music_properties()
                    elif getCondVisibility(
                            "VideoPlayer.Content(livetv) | String.StartsWith(Player.FileNameAndPath,pvr://)"
                    ):
                        self.monitor_livetv()
                    else:
                        self.set_video_properties(mediatype, dbid)
                        self.show_info_panel()
        except Exception as exc:
            log_exception(__name__, exc)
Exemple #37
0
 def explore(self, user):
     if not self.can_explore(user):
         raise utils.MyException("Cannot explore")
     try:
         ev = utils.random_chance(self.events, lambda ev: ev.get_chance(user))
         user.event_happened(ev)
     except:
         utils.log_exception()
Exemple #38
0
 def ask_update_depth(self):
     try:
         self.update_depth()
         # self.convert_to_usd()
         self.depth_updated = time.time()
     except Exception as e:
         logging.error("Can't update market: %s - %s" % (self.name, str(e)))
         log_exception(logging.DEBUG)
Exemple #39
0
    def show_widget_listing(self):
        '''display the listing for the provided action and mediatype'''
        media_type = self.options["mediatype"]
        action = self.options["action"]
        # set widget content type
        xbmcplugin.setContent(ADDON_HANDLE, media_type)

        # try to get from cache first...
        # we use a checksum based on the options to make sure the cache is ignored when needed
        all_items = []
        cache_str = "SkinHelper.Widgets.%s.%s" % (media_type, action)
        if not self.win.getProperty("widgetreload2"):
            # at startup we simply accept whatever is in the cache
            cache_checksum = None
        else:
            cache_checksum = ""
            for key in sorted(self.options):
                cache_checksum += "%s.%s" % (key, self.options[key])
        cache = self.cache.get(cache_str, checksum=cache_checksum)
        if cache and not self.options.get("skipcache") == "true":
            log_msg("MEDIATYPE: %s - ACTION: %s -- got items from cache - CHECKSUM: %s"
                    % (media_type, action, cache_checksum))
            all_items = cache

        # Call the correct method to get the content from json when no cache
        if not all_items:
            log_msg("MEDIATYPE: %s - ACTION: %s -- no cache, quering kodi api to get items - CHECKSUM: %s"
                    % (media_type, action, cache_checksum))

            # dynamically import and load the correct module, class and function
            try:
                media_module = __import__(media_type)
                media_class = getattr(
                    media_module,
                    media_type.capitalize())(self.addon, self.metadatautils, self.options)
                all_items = getattr(media_class, action)()
                del media_class
            except AttributeError:
                log_exception(__name__, "Incorrect widget action or type called")
            except Exception as exc:
                log_exception(__name__, exc)

            # randomize output if requested by skinner or user
            if self.options.get("randomize", "") == "true":
                all_items = sorted(all_items, key=lambda k: random.random())

            # prepare listitems and store in cache
            all_items = process_method_on_list(self.metadatautils.kodidb.prepare_listitem, all_items)
            self.cache.set(cache_str, all_items, checksum=cache_checksum)

        # fill that listing...
        xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_UNSORTED)
        all_items = process_method_on_list(self.metadatautils.kodidb.create_listitem, all_items)
        xbmcplugin.addDirectoryItems(ADDON_HANDLE, all_items, len(all_items))

        # end directory listing
        xbmcplugin.endOfDirectory(handle=ADDON_HANDLE)
 def run(self):
     '''called to start our webservice'''
     log_msg("WebService - start helper webservice on port %s" % PORT, xbmc.LOGNOTICE)
     try:
         server = StoppableHttpServer(('127.0.0.1', PORT), StoppableHttpRequestHandler)
         server.artutils = self.artutils
         server.serve_forever()
     except Exception as exc:
         log_exception(__name__, exc)
Exemple #41
0
 def do_update(self):
     print "[River %s] Doing update..." % self.__site
     if  not self.__have_site:
         try:
             self.__parse_site()
             self.__have_site = True
         except Exception, e:
             utils.log_exception()
             print "[River %s] Failed to parse site: %s" % (self.__site, e)
             self.set_name("Invalid river %s" % self.__site)
Exemple #42
0
 def do_update(self):
     print "[River %s] Doing update..." % self.__site
     if not self.__have_site:
         try:
             self.__parse_site()
             self.__have_site = True
         except Exception, e:
             utils.log_exception()
             print "[River %s] Failed to parse site: %s" % (self.__site, e)
             self.set_name("Invalid river %s" % self.__site)
Exemple #43
0
 def run(self):
     '''called to start our webservice'''
     log_msg("WebService - start helper webservice on port %s" % PORT, xbmc.LOGNOTICE)
     try:
         server = StoppableHttpServer(('127.0.0.1', PORT), StoppableHttpRequestHandler)
         server.metadatautils = self.metadatautils
         server.serve_forever()
     except Exception as exc:
         if "10053" not in exc: # ignore host diconnected errors
             log_exception(__name__, exc)
Exemple #44
0
def save_yaml(content, filename):
    if content is None:
        return

    try:
        file = open(filename, 'w')
        yaml.dump_all(content, file, default_flow_style=False)
        file.close()
    except Exception as e:
        utils.log_exception(e)
Exemple #45
0
 def ask_update_depth(self, pair):
     try:
         self.update_depth(pair)
         self.depth_updated = time.time()
     except (urllib.error.HTTPError, urllib.error.URLError) as e:
         logging.error("HTTPError, can't update market: %s" % self.name)
         log_exception(logging.DEBUG)
     except Exception as e:
         logging.error("Can't update market: %s - %s" % (self.name, str(e)))
         log_exception(logging.DEBUG)
Exemple #46
0
def is_commit_pullable(ref):
    try:
        # git diff-tree --no-commit-id --name-only -r <>
        files = git('diff-tree', '--no-commit-id', '--name-only', '-r',
                    ref.strip())
        files = str(files).split()
        return "noexec" not in files
    except GitError as e:
        log_exception(e)
        return False
Exemple #47
0
def quick_dirty_fallback_geocode(addr, parse=True):
    """
    Try to get SOME x,y even with bad blocks data,
    by falling back to external geocoders.
    """
    from ebdata.nlp.addresses import parse_addresses
    from ebpub.geocoder import SmartGeocoder
    if parse:
        addrs = parse_addresses(addr)
    else:
        addrs = [addr]
    for addr, unused in addrs:
        try:
            try:
                result = SmartGeocoder().geocode(addr)
                point = result['point']
                print "YAY internally geocoded %r" % addr
                return point.x, point.y
            except:
                x,y = None, None
                sys.stderr.write("BOO internal geocoder failed on %r:\n" % addr)
                log_exception()
                # XXX Don't bother, external geocoding rarely gives us
                # anything inside Boston now that we have decent
                # blocks data.  But I want to preserve this script for
                # now till we figure out what to do with geocoding
                # more generally
                continue
            if None in (x, y):
                # XXX log something
                # Other geocoders need to know the city
                addr += ', Boston, MA'
                from geopy import geocoders
                g = geocoders.Google(resource='maps', output_format='json')
                import urllib2
                try:
                    for unused, (lat, lon) in g.geocode(addr, exactly_one=False):
                        print "YAY google geocoded %r" % addr
                        return (lon, lat)
                except urllib2.HTTPError:
                    # Rate throttled? Try another.
                    pass
                except ValueError:
                    # Bad JSON response? why?
                    pass
                us = geocoders.GeocoderDotUS()
                for unused, (lat, lon) in us.geocode(addr, exactly_one=False):
                    print "YAY geocoder.us geocoded %r" % addr
                    return (lon, lat)
        except:
            sys.stderr.write( '===== uncaught geocoder exception on %r\n' % addr)
            log_exception()
            sys.stderr.write('======================\n')

    return None, None
    def __init__(self):
        utils.log("Started service")
        try:          
            utils.maybe_init_settings()
        except IOError:
            utils.log_exception()

        self.monitor = MyMonitor(updated_settings_callback=self.apply_config)
        
        while (not xbmc.abortRequested):
            xbmc.sleep(1000)
Exemple #49
0
 def ask_update_depth(self):
     try:
         self.update_depth()
         self.convert_to_usd()
         self.depth_updated = time.time()
     except (urllib.error.HTTPError, urllib.error.URLError) as e:
         logging.error("HTTPError, can't update market: %s" % self.name)
         log_exception(logging.DEBUG)
     except Exception as e:
         logging.error("Can't update market: %s - %s" % (self.name, str(e)))
         log_exception(logging.DEBUG)
Exemple #50
0
def receipt_check(environ):
    output = ''
    with statsd.timer('services.verify'):
        data = environ['wsgi.input'].read()
        try:
            verify = Verify(data, environ)
            return 200, json.dumps(verify.check_full())
        except:
            log_exception('<none>')
            return 500, ''
    return output
Exemple #51
0
 def base64_icon(self, addon_id):
     path = self.image_path('icon.jpg')
     try:
         with open(path, 'r') as f:
             return base64.b64encode(f.read())
     except IOError, e:
         if len(e.args) == 1:
             log_exception('I/O error: {0}'.format(e[0]))
         else:
             log_exception('I/O error({0}): {1}'.format(e[0], e[1]))
         return ''
 def stop(self):
     '''called when the thread needs to stop'''
     try:
         log_msg("WebService - stop called", 0)
         conn = httplib.HTTPConnection("127.0.0.1:%d" % PORT)
         conn.request("QUIT", "/")
         conn.getresponse()
         self.exit = True
         self.event.set()
     except Exception as exc:
         log_exception(__name__, exc)
Exemple #53
0
    def add_form(self, filename):
        try:
            form = FormFile(filename)
            id = form.id
            title = form.title_text
            del form
        except Exception, e:
            import utils

            utils.log_exception()
            id = "broken"
            title = "Broken Form - Delete me"
Exemple #54
0
def msg_unlock(fn):
    success = True
    MSG_LOCK_LOCK.acquire()
    try:
        os.remove(__msg_lockfile(fn))
    except OSError:
        utils.log_exception()
        success = False
    MSG_LOCK_LOCK.release()

    #print "Unlocked %s: %s" % (fn, success)
    return success
 def do_background_work(self):
     '''stuff that's processed in the background'''
     try:
         if self.exit:
             return
         log_msg("Started Background worker...")
         self.set_generic_props()
         self.listitem_details = {}
         self.cache.check_cleanup()
         log_msg("Ended Background worker...")
     except Exception as exc:
         log_exception(__name__, exc)
    def onNotification(self, sender, method, data):
        '''builtin function for the xbmc.Monitor class'''
        try:
            log_msg("Kodi_Monitor: sender %s - method: %s  - data: %s" % (sender, method, data))
            data = json.loads(data.decode('utf-8'))
            mediatype = ""
            dbid = 0
            transaction = False
            if data and isinstance(data, dict):
                if data.get("item"):
                    mediatype = data["item"].get("type", "")
                    dbid = data["item"].get("id", 0)
                elif data.get("type"):
                    mediatype = data["type"]
                    id = data.get("id", 0)
                if data.get("transaction"):
                    transaction = True

            if method == "System.OnQuit":
                self.win.setProperty("SkinHelperShutdownRequested", "shutdown")

            if method == "VideoLibrary.OnUpdate":
                self.process_db_update(mediatype, dbid, transaction)

            if method == "AudioLibrary.OnUpdate":
                self.process_db_update(mediatype, dbid, transaction)

            if method == "Player.OnStop":
                self.monitoring_stream = False
                self.infopanelshown = False
                self.win.clearProperty("Skinhelper.PlayerPlaying")
                self.win.clearProperty("TrailerPlaying")
                self.reset_win_props()
                if not dbid:
                    self.process_db_update(mediatype, "", transaction)

            if method == "Player.OnPlay":
                self.reset_win_props()
                if self.wait_for_player():
                    if xbmc.getCondVisibility("Player.HasAudio"):
                        if xbmc.getCondVisibility("Player.IsInternetStream"):
                            self.monitor_radiostream()
                        else:
                            self.set_music_properties()
                    elif xbmc.getCondVisibility("VideoPlayer.Content(livetv)"):
                        self.monitor_livetv()
                    else:
                        self.set_video_properties(mediatype, dbid)
                        self.show_info_panel()
        except Exception as exc:
            log_exception(__name__, exc)
Exemple #57
0
def application(environ, start_response):
    status = '200 OK'
    with statsd.timer('services.verify'):
        data = environ['wsgi.input'].read()
        try:
            verify = Verify(data, environ)
            output = verify()
            start_response(status, verify.get_headers(len(output)))
        except:
            output = ''
            log_exception()
            start_response('500 Internal Server Error', [])

    return [output]
Exemple #58
0
    def base64_icon(self, addon_id):
        path = self.image_path("icon.jpg")
        if not os.path.isfile(path):
            return ""

        try:
            with open(path, "r") as f:
                return base64.b64encode(f.read())
        except IOError, e:
            if len(e.args) == 1:
                log_exception("I/O error: {0}".format(e[0]))
            else:
                log_exception("I/O error({0}): {1}".format(e[0], e[1]))
            return ""
    def __init__(self):
        self.cache = SimpleCache()
        self.kodi_db = KodiDb()
        self.win = xbmcgui.Window(10000)
        try:
            self.params = dict(urlparse.parse_qsl(sys.argv[2].replace("?", "").lower().decode("utf-8")))
            log_msg("plugin called with parameters: %s" % self.params)
            self.main()
        except Exception as exc:
            log_exception(__name__, exc)
            xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))

        # cleanup when done processing
        self.close()
 def __init__(self):
     '''Initialization and main code run'''
     self.win = xbmcgui.Window(10000)
     self.addon = xbmcaddon.Addon(ADDON_ID)
     self.thetvdb = TheTvDb()
     self.set_dates()
     action, action_param = self.get_params()
     self.improve_dates = self.addon.getSetting("ImproveDates") == 'true'
     log_msg("MainModule called with action: %s - parameter: %s" % (action, action_param))
     # launch module for action provided by this script
     try:
         getattr(self, action)(action_param)
     except Exception as exc:
         log_exception(__name__, exc)
     finally:
         self.close()