def get_file_from_request(ext=None, folder=None, name='file'): """ Get file from a request, save it locally and return its path """ if ext is None: ext = [] print("get_file_from_request() INVOKED. We have: request.files = %r" % request.files) if name not in request.files: raise NotFoundError('File not found') uploaded_file = request.files[name] if uploaded_file.filename == '': raise NotFoundError('File not found') if not _allowed_file(uploaded_file.filename, ext): raise NotFoundError('Invalid file type') if not folder: folder = app.config['UPLOAD_FOLDER'] else: with app.app_context(): folder = app.config['BASE_DIR'] + folder filename = secure_filename(uploaded_file.filename) uploaded_file.save(os.path.join(folder, filename)) return os.path.join(folder, filename)
def get_file_from_request(ext=[], folder='static/temp/', name='file'): """ Get file from a request, save it locally and return its path """ if 'file' not in request.files: raise NotFoundError('File not found') file = request.files['file'] if file.filename == '': raise NotFoundError('File not found') if not _allowed_file(file.filename, ext): raise NotFoundError('Invalid file type') filename = secure_filename(file.filename) path = folder + filename file.save(path) return path
def job(self, sequence_job_id): sequence_job = [ x for x in self.sequence_jobs if x.id == sequence_job_id ] if sequence_job: return sequence_job[0] raise NotFoundError()
def wait_until_ready(self, timeout=30): started = time.time() while not self.is_ready(): if time.time() - started > timeout: raise NotFoundError('File {} not found or not ready'.format( self.path)) time.sleep(0.5)
def _GetInventoryItem(self, model): """Retrieves a single InventoryItem. Args: model: string, The sku / model of the product being searched. Returns: InventoryItem, The product being searched. Raises: NotFoundError: The sku / model of the product is not in the database. """ cursor = self._db_client.cursor() cursor.execute( """ SELECT model, stocks, last_sync_batch_id FROM inventory WHERE model=? """, (model, )) result = cursor.fetchone() if result is None: raise NotFoundError('InventoryItem not found: %s' % model) return InventoryItem(model=result[0], stocks=result[1], last_sync_batch_id=result[2])
def GetOauth2Tokens(self, system): """Retrieves latest tokens from a system. Returns: dict, detail of Oauth2 tokens for a system. """ cursor = self._db_client.cursor() cursor.execute( """ SELECT system, access_token, refresh_token, created_on, expires_on FROM oauth2 WHERE system=? """, (system, )) result = cursor.fetchone() if result is None: raise NotFoundError('Oauth2 for system not found: %s' % system) return { 'system': result[0], 'access_token': result[1], 'refresh_token': result[2], 'created_on': result[3], 'expires_on': result[4], }
def lookup_astrometry(id): astrometry = [ a for a in controller.indi_server.astrometry_drivers() if a.id == id ] if not astrometry: raise NotFoundError('Astrometry driver {} not found'.format(id)) return astrometry[0]
def directory_browser(): path = request.args.get('path', '/') if not os.path.exists(path): parent_dir = path while not os.path.isdir(parent_dir): parent_dir = os.path.dirname(parent_dir) raise NotFoundError("Directory {} doesn't exists".format(path), payload={ 'requested_path': path, 'redirect_to': parent_dir }) if not os.path.isdir(path): raise BadRequestError('Path {} is not a directory'.format(path)) entries = sorted(os.listdir(path)) subdirectories = [ dir for dir in entries if os.path.isdir(os.path.join(path, dir)) ] parent = os.path.dirname(path) response = { 'path': os.path.normpath(path), 'subdirectories': subdirectories, 'parent': os.path.normpath(parent) if parent != path else None, } if request.args.get('show_files', 'false') == 'true': response['files'] = [ dir for dir in entries if os.path.isfile(os.path.join(path, dir)) ] return response
def create(session: Session, steam_id: int): d = session.query(Dota).get(steam_id) if d is not None: raise AlreadyExistingError(repr(d)) r = requests.get( f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}" ) if r.status_code != 200: raise RequestError("OpenDota returned {r.status_code}") data = r.json() if "profile" not in data: raise NotFoundError( "The specified user has never played Dota or has a private match history" ) r = requests.get( f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}/wl" ) if r.status_code != 200: raise RequestError("OpenDota returned {r.status_code}") wl = r.json() new_record = Dota(steam_id=str(steam_id), rank_tier=data["rank_tier"], wins=wl["win"], losses=wl["lose"]) return new_record
def find_indi_device(self): if not self.indi_device: devices = [d for d in self.client.devices() if d.name == self.name] if not devices: raise NotFoundError('device {} not found.'.format(self.name)) self.indi_device = devices[0] return self.indi_device
def run(self, server, root_directory, event_listener, logger, on_update=None): camera = [c for c in server.cameras() if c.id == self.camera] if not camera: raise NotFoundError('Camera with id {} not found'.format(self.camera)) camera = camera[0] filter_wheel = None if self.filter_wheel: filter_wheel = [f for f in server.filter_wheels() if f.id == self.filter_wheel] if not filter_wheel: raise NotFoundError('Filter wheel with id {} not found'.format(self.filter_wheel)) filter_wheel = filter_wheel[0] sequence_root_path = os.path.join(root_directory, self.upload_path) logger.info('Starting sequence with camera: {}={} and filter_wheel: {}={}'.format(self.camera, camera.device.name, self.filter_wheel, filter_wheel.device.name if filter_wheel else 'N/A')) self.status = 'starting' #self.reset() on_update() try: os.makedirs(sequence_root_path, exist_ok=True) self.status = 'running' self.stopped = False for index, sequence_job in enumerate(self.sequence_jobs): if self.stopped: return if self.is_todo(sequence_job): self.running_sequence_job = sequence_job sequence_job.run(server, {'camera': camera, 'filter_wheel': filter_wheel}, sequence_root_path, logger, event_listener, on_update, index=index) if not self.stopped: self.status = 'finished' except StopSequence as e: logger.info('Stopping sequence: {}'.format(e.message)) self.stop(on_update=on_update) except Exception as e: logger.exception('error running sequence') self.status = 'error' raise e finally: on_update() self.running_sequence_job = None
def run(self, command_id, request_obj): commands = [c for c in self.commands if c.id == command_id] if not commands: raise NotFoundError( 'Command with id {} not found'.format(command_id)) command = commands[0] return command.run(request_obj)
def get_file_from_request(ext=[], folder='/static/temp/', name='file'): """ Get file from a request, save it locally and return its path """ with app.app_context(): folder = app.config['BASE_DIR'] + folder if 'file' not in request.files: raise NotFoundError('File not found') file = request.files['file'] if file.filename == '': raise NotFoundError('File not found') if not _allowed_file(file.filename, ext): raise NotFoundError('Invalid file type') filename = secure_filename(file.filename) path = _available_path(folder, filename) file.save(path) return path
def __init__(self, settings, client, device=None, name=None): self.settings = settings self.client = client if device: self.device = Device(client, logger, device) elif name: device = [c for c in self.client.devices() if c.name == name] self.device = device if device else None if not self.device: raise NotFoundError('Telescope device not found: {}'.format(name))
def GetProductDirect(self, model): """Refreshes product records directly from Lazada instead of consulting the map. Args: model: string, The sku / model of the product being retrieved. Returns: Map, The product being searched. Raises: CommunicationError: Cannot communicate properly with Lazada. NotFoundError: The sku / model of the product is not in Lazada. MultipleResultsError: The sku / model is not unique in Lazada. """ items = [] def data_parser(data): for product in data['products']: sku = product['skus'][0] attrs = product['Attributes'] images = [] for img in sku['Images']: if img: imgurl = string.replace(img, 'catalog.jpg', 'zoom.jpg') images.append(imgurl) p = { 'name': attrs['name'], 'description': attrs['short_description'], 'model': sku['SellerSku'], 'stocks': int(sku['Available']) or int(sku['quantity']), 'price': float(sku['price']), 'images': images, 'weight': float(sku['package_weight']) or 0.9, # 'category': 'PENDING', # 'logistics': 'PENDING', # Not in lazada } items.append(p) result = self._Request('/products/get', {'search': model}) if result.error_code: raise CommunicationError('Error communicating: %s' % result.error_description) data_parser(result.result) items = [x for x in items if x['model'] == model] if len(items) == 0: raise NotFoundError('No results for %s' % model) elif len(items) > 1: raise MultipleResultsError('Multiple results for %s' % model) return items[0]
def find_indi_property(self): if not self.indi_property: indi_device = device.Device(self.client, self.logger, name=self.device).find_indi_device() self.indi_property = indi_device.get_property(self.name) if not self.indi_property: raise NotFoundError( 'Unable to find INDI property {} on device {}'.format( self.name, self.device)) return self.indi_property
def lookup(self, catalog_name, entry_name): if catalog_name == 'SIMBAD': return self.simbad_lookup(entry_name) entry_key = Catalogs.entry_key(catalog=catalog_name, name=entry_name) logger.debug('Looking up for %s on catalog %s with Redis key %s', entry_name, catalog_name, entry_key) entry = self.__decorate_entry(redis_client.dict_get(entry_key)) if not entry: raise NotFoundError( 'Object with name {} from catalog {} not found'.format( entry_name, catalog_name)) return [entry]
def include_parse(state, obj): inc_files = [] LOGGER.debug("Include parse for {}".format(obj.value)) for i in obj.value: LOGGER.debug(" filename: {}".format(i.value)) ffile = _resolve_header(i.name, state.inclpath) if ffile: inc_files.append(ffile) else: raise NotFoundError("Unable to resolve include file {}".format( i.name)) [_IHDR_PARSE((x, state, ParseLevel.LITE)) for x in inc_files]
def __init__(self, server, device=None, name=None): self.server = server self.client = server.client if device: self.device = Device(self.client, logger, device) elif name: device = [c for c in self.client.devices() if c.name == name] self.device = device if device else None if not self.device: raise NotFoundError('Astrometry device not found: {}'.format(name)) self.event_listener = AstrometryEventListener(self.device)
def _exception_for_response(self, response): if response.status_code == 404: return NotFoundError(response.reason) elif response.status_code == 400 and "OAuthException" in response.text: return InvalidAccessTokenError(response.reason) elif response.status_code == 401: return UnauthorizedError(response.reason) elif response.status_code == 429: return RateLimitExceededError(response.reason) else: return ResponseError("%d error: %s" % ( response.status_code, response.reason, ))
def create(session: Session, steam_id: int) -> "Dota": d = session.query(Dota).get(steam_id) if d is not None: raise AlreadyExistingError(repr(d)) r = requests.get( f"https://api.opendota.com/api/players/{Steam.to_steam_id_3(steam_id)}" ) r.raise_for_status() data = r.json() if "profile" not in data: raise NotFoundError( "The specified user has never played Dota or has a private match history" ) new_record = Dota(steam_id=str(steam_id)) new_record.update() return new_record
def __init__(self, client, logger, device=None, filter_wheel=None): self.client = client self.logger = logger if device: self.device = device self.filter_wheel = [ c for c in self.client.filter_wheels() if c.name == device.name ] if not self.filter_wheel: raise NotFoundError('FilterWheel {} not found'.format( device.name)) self.filter_wheel = self.filter_wheel[0] elif filter_wheel: self.filter_wheel = filter_wheel self.device = Device(client, logger, name=filter_wheel.name)
def import_event_json(zip_path): """ Imports and creates event from json zip """ global CUR_ID path = 'static/temp/import_event' # delete existing files if os.path.isdir(path): shutil.rmtree(path, ignore_errors=True) # extract files from zip with zipfile.ZipFile(zip_path, "r") as z: z.extractall('static/temp/import_event') # create event try: data = json.loads(open(path + '/event.json', 'r').read()) _, data = _trim_id(data) data = _delete_fields(('event', EventDAO), data) new_event = EventDAO.create(data, 'dont')[0] except BaseError as e: raise make_error('event', er=e) except Exception: raise make_error('event') # create other services try: service_ids = {} for item in IMPORT_SERIES: data = open(path + '/%s.json' % item[0], 'r').read() dic = json.loads(data) changed_ids = create_service_from_json(dic, item, new_event.id, service_ids) service_ids[item[0]] = changed_ids.copy() CUR_ID = None except BaseError as e: EventDAO.delete(new_event.id) raise make_error(item[0], er=e, id_=CUR_ID) except IOError: EventDAO.delete(new_event.id) raise NotFoundError('File %s.json missing in event zip' % item[0]) except ValueError: EventDAO.delete(new_event.id) raise make_error(item[0], er=ServerError('Invalid json')) except Exception: EventDAO.delete(new_event.id) raise make_error(item[0], id_=CUR_ID) # return return new_event
def create(session: Session, royal_username, telegram_user: TelegramUser): t = session.query(Telegram).filter_by( telegram_id=telegram_user.id).first() if t is not None: raise AlreadyExistingError(repr(t)) r = session.query(Royal).filter( Royal.username == royal_username).first() if r is None: raise NotFoundError("No Royal exists with that username") t = session.query(Telegram).filter(Telegram.royal_id == r.id).first() if t is not None: raise AlreadyExistingError(repr(t)) return Telegram(royal=r, telegram_id=telegram_user.id, first_name=telegram_user.first_name, last_name=telegram_user.last_name, username=telegram_user.username)
def _exception_for_response(self, response): if response.status_code == 404: return NotFoundError(response.reason) elif response.status_code == 400 and 'OAuthException' in response.text: return InvalidAccessTokenError(response.reason) elif response.status_code == 401: return UnauthorizedError(response.reason) elif response.status_code == 403: return ForbiddenError(response.reason) elif response.status_code == 429: return RateLimitExceededError(response.reason) else: return ResponseError(u'{} error: {}\nresponse: {}'.format( response.status_code, response.reason, response.text, ))
def __init__(self, settings, client, logger, device=None, camera=None): self.settings = settings self.client = client self.logger = logger self.images_db = camera_images_db if device: self.device = device self.camera = [ c for c in self.client.cameras() if c.name == device.name ] if not self.camera: raise NotFoundError('Camera {} not found'.format(device.name)) self.camera = self.camera[0] elif camera: self.camera = camera self.device = Device(client, logger, name=camera.name)
def move_sequence_job(sequence_id, sequence_job_id, json): app.logger.info('moving sequence job {}: direction: {}'.format( sequence_job_id, json['direction'])) with controller.sequences.lookup_edit(sequence_id) as sequence: index = [ index for index, job in enumerate(sequence.sequence_jobs) if job.id == sequence_job_id ] if not index: raise NotFoundError( 'Sequence job {} not found in sequence {}'.format( sequence_job_id, sequence_id)) index = index[0] new_index = index - 1 if json['direction'] == 'up' else index + 1 if new_index >= 0 and new_index < len(sequence.sequence_jobs): sequence.sequence_jobs.insert(new_index, sequence.sequence_jobs.pop(index)) return sequence.to_map()
def create(session: Session, royal_username, discord_user: DiscordUser): d = session.query(Discord).filter( Discord.discord_id == discord_user.id).first() if d is not None: raise AlreadyExistingError(repr(d)) r = session.query(Royal).filter( Royal.username == royal_username).first() if r is None: raise NotFoundError("No Royal exists with that username") d = session.query(Discord).filter(Discord.royal_id == r.id).first() if d is not None: raise AlreadyExistingError(repr(d)) d = Discord(royal=r, discord_id=discord_user.id, name=discord_user.name, discriminator=discord_user.discriminator, avatar_hex=discord_user.avatar) return d
def preprocess_runtime(state, args): """Pre process well known runtime includes""" # If not skipping run-time (used for compiler run-time) # preprocess well known headers if not args.rt: includes = ['foidlrt', 'langcore'] inc_files = [] for i in includes: ffile = _resolve_header(i, state.inclpath) if ffile: inc_files.append(ffile) else: raise NotFoundError( "Unable to resolve include file {}".format(i)) [_IHDR_PARSE((x, state, ParseLevel.LITE)) for x in inc_files] return state
def simbad_lookup(self, entry_name): def sanitize_name(name): name_entries = [x for x in name.split(' ') if x] cat = name_entries[0] if len(name_entries) > 1 else 'N/A' if name_entries[0].startswith('MCG'): cat = 'MCG' if cat == 'NAME': name_entries = name_entries[1:] return cat, ' '.join(name_entries) simbad_objects = Simbad.query_object(entry_name) if not simbad_objects: raise NotFoundError( 'Object with name {} not found in SIMBAD'.format(entry_name)) results = [] for simbad_object in simbad_objects: coordinates = SkyCoord(ra=simbad_object['RA_d'] * u.deg, dec=simbad_object['DEC_d'] * u.deg, equinox='J2000') object_id = simbad_object['MAIN_ID'].decode() catalog, object_name = sanitize_name(object_id) object_names = [ sanitize_name(x) for x in simbad_object['IDS'].decode().split('|') if x != object_id ] object_names = [{ 'catalog': catalog, 'name': name } for catalog, name in object_names] results.append( self.__decorate_entry({ 'raj2000': coordinates.ra.deg, 'dej2000': coordinates.dec.deg, 'displayName': object_name, 'objectNames': object_names, 'catalog': catalog, 'id': object_id, })) return results