def get_set_state(): """Get the current reported state from a camera request.form: id (str): Identifier name of camera token (str): Secret token to allow viewing of state Returns: JSON attachment """ if request.method == "POST": id = request.form["id"] token = request.form["token"] if token != app.config["UPLOAD_TOKEN"]: return "", 401 try: client_state = json.loads(request.form["state"]) if refresh_state(id, client_state, "client"): return send_from_directory( os.path.join(app.config["UPLOAD_PATH"], "state"), filename=safe_name("{}.json".format(id)), as_attachment=True, ) except FileNotFoundError: return "", 404 return "", 200
def all_stories(self): ''' API link to all an orgs stories ''' # Make a nice org name organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/stories' % ( request.scheme, request.host, organization_name)
def save_organization_info(session, org_dict): ''' Save a dictionary of organization info to the datastore session. Return an app.Organization instance. ''' # Select an existing organization by name. filter = Organization.name == org_dict['name'] existing_org = session.query(Organization).filter(filter).first() # :::here (organization/true) # If this is a new organization, save and return it. The keep parameter is True by default. if not existing_org: new_organization = Organization(**org_dict) session.add(new_organization) return new_organization # Check that the id exists if not existing_org.id: existing_org.id = safe_name(raw_name(existing_org.name)) # Timestamp the existing organization existing_org.last_updated = time() # :::here (organization/true) existing_org.keep = True # Update existing organization details. for (field, value) in org_dict.items(): setattr(existing_org, field, value) return existing_org
def past_events(self): ''' API link to an orgs past events ''' # Make a nice org name organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/past_events' % ( request.scheme, request.host, organization_name)
def __init__(self, name, website=None, events_url=None, members_count=None, rss=None, projects_list_url=None, type=None, city=None, latitude=None, longitude=None, last_updated=time.time()): self.name = name self.website = website self.events_url = events_url self.rss = rss self.projects_list_url = projects_list_url self.type = type self.city = city self.latitude = latitude self.longitude = longitude self.keep = True self.last_updated = last_updated self.started_on = unicode(date.today()) self.id = safe_name(raw_name(name)) self.members_count = members_count
def _dowload_file(path, sub_link): str_file = utils.safe_name(subtitle_name(path)) resp = requests.get(sub_link, allow_redirects=True) gz_file = str_file + ".gz" with open(gz_file, 'wb') as gz_stream: gz_stream.write(resp.content) with gzip.open(gz_file, 'rb') as f_in: with open(str_file, 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(gz_file)
def upload(): """Accept data to be uploaded. Either JSON format generic data, or thumbnails as jpegs from camera request.form: id (str): Identifier name of camera token (str): Secret token to allow uploads type (str): "data" for json upload, "small" or "crop" for JPGs Returns: None """ if request.method == "POST": id = request.form["id"] token = request.form["token"] upload_type = request.form["type"] if token != app.config["UPLOAD_TOKEN"]: return "", 401 if upload_type == "data": s_filename = safe_name("{}-{}-{}.json".format( id, upload_type, int(time.time()))) filename = os.path.join(app.config["UPLOAD_PATH"], "data", s_filename) logging.info("Saving file {}".format(s_filename)) save_data(request.form["data"], filename) set_rights(filename) if upload_type in ("small", "crop"): file = request.files["file"] if file: s_filename = safe_name("{}-{}.jpg".format(id, upload_type)) filename = os.path.join(app.config["UPLOAD_PATH"], "view", s_filename) logging.info("Saving file {}".format(s_filename)) file.save(filename) set_rights(filename) return "", 200 return "", 200
def image_view(id, image): """Get JPG image from data folder Args: id (str): Identifier name of camera image (str): "small" or "crop" for type of image Returns: A JPG binary """ try: return send_from_directory( os.path.join(app.config["UPLOAD_PATH"], "view"), filename=safe_name("{}-{}.jpg".format(id, image)), as_attachment=True, ) except FileNotFoundError: return "", 404
def __init__(self, name, **kwargs): self.name = name self.website = kwargs.get('website') self.events_url = kwargs.get('events_url') self.rss = kwargs.get('rss') self.projects_list_url = kwargs.get('projects_list_url') self.tags = kwargs.get('tags', []) self.type = kwargs.get('type') self.city = kwargs.get('city') self.latitude = kwargs.get('latitude') self.longitude = kwargs.get('longitude') self.keep = True self.last_updated = kwargs.get('last_updated', time.time()) self.social_profiles = kwargs.get('social_profiles', {}) self.started_on = unicode(date.today()) self.id = safe_name(raw_name(name)) self.members_count = kwargs.get('members_count') self.logo_url = kwargs.get('logo_url')
def get_new_path(self): if self._new_path != None: return self._new_path self.serie_name = tvdb.get_serie_name(self.name) if self.serie_name == None: self.serie_name = self.name self.episode_name = tvdb.get_episode_name(self.name, self.season, self.episode) _, ext = os.path.splitext(self.path) new_path = None if self.episode_name == None: new_path = "{0}/Season {1:02d}/{0} - S{1:02d}E{2:02d}{3}".format( self.serie_name, self.season, self.episode, ext) else: new_path = "{0}/Season {1:02d}/{0} - S{1:02d}E{2:02d} - {3}{4}".format( self.serie_name, self.season, self.episode, self.episode_name, ext) self._new_path = utils.safe_name(new_path) return self._new_path
def get_intra_images(mat_file, work_dir, memory=Memory(None)): mat = memory.cache(load_matfile)(mat_file)['SPM'] images = {} images['beta_maps'] = [] images['c_maps'] = {} images['t_maps'] = {} images['contrasts'] = {} for c in mat.xCon: name = safe_name(str(c.name)) try: images['c_maps'][name] = check_path( os.path.join(work_dir, str(c.Vcon.fname))) images['t_maps'][name] = check_path( os.path.join(work_dir, str(c.Vspm.fname))) images['contrasts'][name] = c.c.tolist() except: pass # sometimes c.Vcon is an empty array for i, b in enumerate(mat.Vbeta): images['beta_maps'].append( check_path(os.path.join(work_dir, str(b.fname)))) return images
def api_id(self): ''' Return organization name made safe for use in a URL. ''' return safe_name(self.name)
def all_attendance(self): ''' API link to orgs attendance ''' organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/attendance' % (request.scheme, request.host, organization_name)
def all_stories(self): ''' API link to all an orgs stories ''' # Make a nice org name organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/stories' % (request.scheme, request.host, organization_name)
def all_attendance(self): ''' API link to orgs attendance ''' organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/attendance' % ( request.scheme, request.host, organization_name)
def past_events(self): ''' API link to an orgs past events ''' # Make a nice org name organization_name = safe_name(self.name) return '%s://%s/api/organizations/%s/past_events' % (request.scheme, request.host, organization_name)