def post(self, dataset, fmt): params = common._get_request_params(self.request) params['format'] = fmt rid = common._get_request_id(self.request, params) bust = params.get('bust') entry = Cache.get(rid, dataset, params, fmt, bust) if entry and entry.blob_key: self.send_blob(entry.blob_key) elif entry and entry.cdb_url: self._redirect(entry.cdb_url) else: url = None try: url = _download(dataset, params) except Exception, error: name = error.__class__.__name__ trace = traceback.format_exc() msg = 'CartoDB %s download failure: %s: %s - URL: %s' % \ (dataset, name, error, url) monitor.log(self.request.url, msg, error=trace, headers=self.request.headers) self._send_error() return DownloadEntry(id=rid, cdb_url=url).put() self._redirect(url)
def server_loop(port, args=(), line_callback=None, quit_on_disconnect=True): if osutil.is_android: serverpath = os.path.join(os.path.dirname(client.freeciv.freecivclient.__file__), 'freecivserver') else: serverpath = 'server/freeciv-server' args = ('--Ppm', '-p', str(port), '-s', get_save_dir(), ) + args print 'starting server - executable at', serverpath stat = os.stat(serverpath) try: os.chmod(serverpath, 0o744) # octal!!!! except OSError as err: print 'chmodding server failed', err piddir = get_save_dir() cmd = (serverpath, ) + args if osutil.is_desktop: os.environ['LD_PRELOAD'] = '' if quit_on_disconnect: os.environ['FREECIV_QUIT_ON_DISCONNECT'] = 'true' else: del os.environ['FREECIV_QUIT_ON_DISCONNECT'] print cmd serv_in, stream = os.popen4(cmd, bufsize=1) # line buffering p = subprocess.Popen(cmd, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) stream = p.stdout while True: line = stream.readline() if not line: break if line_callback: line_callback(line) monitor.log('server', line.rstrip())
def server_loop(port, args=(), line_callback=None, quit_on_disconnect=True): if osutil.is_android: serverpath = os.path.join(os.path.dirname(client.freeciv.freecivclient.__file__), 'freecivserver') else: serverpath = 'server/freeciv-server' args = ('--Ppm', '-p', str(port), '-s', get_save_dir(), ) + args print 'starting server - executable at', serverpath stat = os.stat(serverpath) os.chmod(serverpath, 0o744) # octal!!!! piddir = get_save_dir() cmd = (serverpath, ) + args if osutil.is_desktop: os.environ['LD_PRELOAD'] = '' if quit_on_disconnect: os.environ['FREECIV_QUIT_ON_DISCONNECT'] = 'true' else: del os.environ['FREECIV_QUIT_ON_DISCONNECT'] print cmd serv_in, stream = os.popen4(cmd, bufsize=1) # line buffering p = subprocess.Popen(cmd, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) stream = p.stdout while True: line = stream.readline() if not line: break if line_callback: line_callback(line) monitor.log('server', line.rstrip())
def log(str): global monitor if not monitor: try: import monitor except ImportError: print str return monitor.log('features', str)
def unsubscribe(self): try: params = self._get_params(body=True) pubsub.unsubscribe(params) self._send_response(json.dumps(dict(unsubscribe=True))) except Exception, e: name = e.__class__.__name__ msg = 'Error: PubSub API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def get(self, id): try: params = dict(id=id) result = stories.get(params) if not result: self.response.set_status(404) self._send_response(json.dumps(result)) except Exception, e: name = e.__class__.__name__ msg = 'Error: Story API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def list(self): try: params = self._get_params() result = list_stories(params) if not result: result = [] self._send_response(json.dumps(result)) except Exception, e: name = e.__class__.__name__ msg = 'Error: Story API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def get(self, id): try: params = dict(id=id) result = get_story(params) if not result: self.response.set_status(404) self._send_response(json.dumps(result)) except Exception, e: name = e.__class__.__name__ msg = 'Error: Story API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def list(self): try: params = self._get_params() result = stories.list(params) if not result: result = [] self._send_response(json.dumps(result)) except Exception, e: name = e.__class__.__name__ msg = 'Error: Story API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def publish(self): params = self._get_params(body=True) try: pubsub.publish(params) self._send_response(json.dumps(dict(publish=True))) except Exception, error: name = error.__class__.__name__ trace = traceback.format_exc() msg = 'Publish failure: %s: %s' % \ (name, error) monitor.log(self.request.url, msg, error=trace, headers=self.request.headers) self._send_error()
def site(self): try: params = self._get_params() rid = self._get_id(params) entry = Entry.get_by_id(rid) if not entry or params.get('bust') or runtime_config.get('IS_DEV'): site = wdpa.get_site(params) if site: entry = Entry(id=rid, value=json.dumps(site)) entry.put() self._send_response(entry.value if entry else None) except Exception, e: name = e.__class__.__name__ msg = 'Error: WPDA API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def get(self): try: params = self._get_params() rid = self._get_id(params) if 'interval' not in params: params['interval'] = '12 MONTHS' entry = Entry.get_by_id(rid) if not entry or params.get('bust') or runtime_config.get('IS_DEV'): result = countries.get(params) if result: entry = Entry(id=rid, value=json.dumps(result)) entry.put() self._send_response(entry.value if entry else None) except Exception, e: name = e.__class__.__name__ msg = 'Error: Countries API (%s)' % name monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def publish(self): try: params = self._get_params(body=True) topic = params.get('topic') dry_run = params.get('dry_run') Event.publish(topic,params,dry_run) self._send_response(json.dumps(dict(publish=True))) except Exception, error: name = error.__class__.__name__ trace = traceback.format_exc() msg = 'Publish failure: %s: %s' % (name, error) monitor.log( self.request.url, msg, error=trace, headers=self.request.headers )
def get(self): try: params = self._get_params() rid = self._get_id(params) if 'interval' not in params: params['interval'] = '12 MONTHS' entry = Entry.get_by_id(rid) if not entry or params.get('bust') or runtime_config.get('IS_DEV'): result = countries.get(params) if result: entry = Entry(id=rid, value=json.dumps(result)) entry.put() self._send_response(entry.value if entry else None) except Exception, error: name = error.__class__.__name__ trace = traceback.format_exc() msg = 'Publish failure: %s: %s' % \ (name, error) monitor.log(self.request.url, msg, error=trace, headers=self.request.headers)
def unsubscribe(self): try: params = self._get_params(body=True) token = params.get('token') if token: Subscription.unsubscribe_by_token(token) self.response.set_status(201) self._send_response(json.dumps(dict(unsubscribe=True))) else: self.error(404) except Exception, e: name = e.__class__.__name__ msg = 'Error: PubSub API (%s)' % name monitor.log( self.request.url, msg, error=e, headers=self.request.headers )
def subscribe(self): try: params = self._get_params(body=True) subscription = Subscription.subscribe(params) if subscription: token = subscription.key.urlsafe() self.response.set_status(201) self._send_response(json.dumps(dict(subscribe=True,token=token))) else: self.error(404) except Exception, e: name = e.__class__.__name__ msg = 'Error: PubSub API (%s)' % name monitor.log( self.request.url, msg, error=e, headers=self.request.headers )
def post(self, dataset): params = self._get_params() rid = self._get_id(params) bust = params.get('bust') if bust: params.pop('bust') entry = Cache.get(rid, dataset, params, bust) if entry: self._send_response(entry.value) else: response = None error = None try: response = _analyze(dataset, params) except Exception, e: error = e name = error.__class__.__name__ trace = traceback.format_exc() if dataset == 'umd': msg = 'Earth Engine analysis failure: %s: %s' % \ (name, error) else: msg = 'CartoDB %s analysis failure: %s: %s' % \ (dataset, name, error) monitor.log(self.request.url, msg, error=trace, headers=self.request.headers) self._send_error() return if dataset == 'umd': value = json.dumps(response) AnalysisEntry(id=rid, value=value).put() self._send_response(value) else: result = _parse_analysis(dataset, response.content) value = json.dumps(result) AnalysisEntry(id=rid, value=value).put() self._send_response(value)
def create(self): # host = os.environ['HTTP_HOST'] # logging.info(os.environ) # if 'globalforestwatch' not in host and 'localhost' not in host: # self.error(404) # return params = self._get_params(body=True) required = ['title', 'email', 'geom'] if not all(x in params and params.get(x) for x in required): self.response.set_status(400) self._send_response(json.dumps(dict(required=required))) return token = self._gen_token() try: params['token'] = token result = create_story(params) if result: story = json.loads(result.content)['rows'][0] story['media'] = json.loads(story['media']) data = copy.copy(story) data['token'] = token self.response.set_status(201) taskqueue.add(url='/stories/email', params=data, queue_name="story-new-emails") self._send_response(json.dumps(story)) else: story = None self.response.set_status(400) return except Exception, e: error = e name = error.__class__.__name__ trace = traceback.format_exc() payload = self.request.headers payload.update(params) msg = 'Story submit failure: %s: %s' % (name, error) monitor.log(self.request.url, msg, error=trace, headers=payload) self.error(400)
def post(self): """""" try: n = ndb.Key(urlsafe=self.request.get('notification')).get() e = n.params['event'] s = n.params['subscription'] response = forma.subsription(s) if response.status_code == 200: result = json.loads(response.content)['rows'][0] body, html = self._body(result, n, e, s) mail.send_mail( sender='*****@*****.**', to=s['email'], subject='New Forest Change Alerts from Global Forest Watch', body=body, html=html) else: raise Exception('CartoDB Failed (status=%s, content=%s)' % (response.status_code, response.content)) except Exception, e: name = e.__class__.__name__ msg = 'Error: Publish %s (%s)' % (json.dumps(s), name) monitor.log(self.request.url, msg, error=e, headers=self.request.headers)
def console_line(self, text): if self.out_window_callback: self.out_window_callback(text) monitor.log('outwindow', text) if features.get('debug.outwindow'): print '[OutWindow]', text
import traceback from time import sleep from monitor import TempMonitor, log while True: try: TempMonitor().run() except: info = traceback.format_exc() log(info) sleep(2)