def __init__(self, name, **kwargs): super().__init__(name=name, **kwargs) if type(self.data) is dict: self.data = Mapper(self.data) if type(self.location) is not str: self.location = Mapper(self.location)(kwargs) try: es = Elasticsearch( **self.location, request_timeout=0.2, retries=False, ignore=404) # TODO url=self.location, ssl_context, http_auth es.info() self.location = es except ImproperlyConfigured as e: raise NotFound("ElasticSearch rejected {}\n-----\n\t{}".format( pformat(self.location), e)) except TransportError as e: raise NotFound( "Failed to reach ElasticSearch at {}\n-----\n\t{}".format( pformat(self.location), e.error)) except: raise NotFound( "Unable to connect to ElasticSearch at host:{}".format( self.location.get('host')))
def get_metrics_proxy(meter_type, vnf_name, vcpe_name): config = openstack.config.get_cloud_region(cloud='nerds', region_name='RegionOne') conn = connection.Connection(config=config) vcpe = None for project in conn.identity.projects(): if project.name == vcpe_name: vcpe = project if vcpe is None: raise NotFound('VCPE {} not found'.format(vcpe_name), status_code=404) vnf = None for server in conn.compute.servers(details=True, all_projects=True): if server.location.project.id == vcpe.id and server.status == "ACTIVE" and vnf_name.lower() in server.name.lower(): vnf = server break if vnf is None: raise NotFound('VNF {} not found in {}'.format(vnf_name, vcpe_name), status_code=404) if meter_type == "memory_usage": return get_metric_memory_usage(vnf.id) elif meter_type == "cpu_usage": return get_metric_cpu_utilization(vnf.id) elif meter_type == "network_in_usage": return get_metric_network_incoming(vnf.id) elif meter_type == "network_out_usage": return get_metric_network_outgoing(vnf.id) else: raise NotFound('Metric {} for VNF {} not found in {}'.format(type, vnf_name, vcpe_name), status_code=404)
def get_dhcp_clients(vcpe_name, vnf_name='dhcp'): config = openstack.config.get_cloud_region(cloud='nerds', region_name='RegionOne') conn = connection.Connection(config=config) vcpe = None for project in conn.identity.projects(): if project.name == vcpe_name: vcpe = project if vcpe is None: raise NotFound('VCPE {} not found'.format(vcpe_name), status_code=404) vnf = None for server in conn.compute.servers(details=True, all_projects=True): if server.location.project.id == vcpe.id and server.status == "ACTIVE" and vnf_name.lower() in server.name.lower(): vnf = server break if vnf is None: raise NotFound('VNF {} not found in {}'.format(vnf_name, vcpe_name), status_code=404) vnf_ip = None print(vnf.addresses.keys()) for net in vnf.addresses.keys(): if vcpe_name.lower() in net.lower(): for port in vnf.addresses[net]: if port['version'] == 4: vnf_ip = port['addr'] break if vnf_ip is None: raise NotFound('VNF Interface not found', status_code=404) router_proxy = None for server in conn.compute.servers(details=True, all_projects=False): if server.status == "ACTIVE" and "router" in server.name.lower(): router_proxy = server break if router_proxy is None: raise NotFound('Router {} not found in {}'.format(vnf_name, vcpe_name), status_code=404) router_proxy_ip = None for net in router_proxy.addresses.keys(): if "ADMIN".lower() in net.lower(): for port in router_proxy.addresses[net]: if port['version'] == 4: router_proxy_ip = port['addr'] break if router_proxy_ip is None: raise NotFound('Router Interface not found', status_code=404) response = requests.get("http://{}:9999/{}".format(router_proxy_ip, vnf_ip)) return Response(json.dumps(response), mimetype='application/json')
def get(self, pk): key = self.entry.generate_save_key(pk=pk) data = self.entry.db.hgetall(key) if not data: raise NotFound('Entry with pk {0} not found'.format(str(pk))) return self.entry(**data)
def get_publish_request_by_id(self, id): publish_request = PublishRequest.query.get(id) if publish_request: return publish_request else: raise NotFound( "Publish request with id {} was not found".format(id))
def get_console_from_openstack(vcpe_name): config = openstack.config.get_cloud_region(cloud='nerds', region_name='RegionOne') conn = connection.Connection(config=config) # projects = [project.name for project in conn.identity.projects() if project.name not in ['service'] ] # if vcpe_name not in projects: # raise NotFound('VCPE {} not found'.format(vcpe_name), status_code=404) vcpe = None for project in conn.identity.projects(): if project.name == vcpe_name: vcpe = project if vcpe is None: raise NotFound('VCPE {} not found'.format(vcpe_name), status_code=404) vcpe_servers_id = [ server.id for server in conn.compute.servers(all_projects=True) if server.location.project.id == vcpe.id and server.status == "ACTIVE"] print(vcpe_servers_id) nova = get_connection_nova_client() servers_from_nova_client = [ server for server in nova.servers.list(search_opts={'all_tenants': 1}) if server.id in vcpe_servers_id] return [ { 'name': server.name, 'console': server.get_console_url("novnc")['console'] } for server in servers_from_nova_client ]
def start_or_stop_all_vnf(vcpe_name, option): config = openstack.config.get_cloud_region(cloud='nerds', region_name='RegionOne') conn = connection.Connection(config=config) project = conn.identity.find_project(vcpe_name) if project is None: raise NotFound('VCPE {} not found'.format(vcpe_name), status_code=404) if option == "run": shutoff_servers = [ server for server in conn.compute.servers(all_projects=True) if server.location.project.id == project.id and server.status == "SHUTOFF"] for server in shutoff_servers: conn.compute.start_server(server) elif option == "stop": active_servers = [ server for server in conn.compute.servers(all_projects=True) if server.location.project.id == project.id and server.status == "ACTIVE"] for server in active_servers: conn.compute.stop_server(server) else: raise NotFound('Option {} not found'.format(option), status_code=404)
def pokemon(self, ctx, search): url = "https://veekun.com/dex/pokemon/search" params = {'name': search} r = requests.get(url, params=params) if r.status_code != 200: raise BotException() data = r.text if 'Nothing found' in data: raise NotFound() soup = BeautifulSoup(data, "html.parser") tds = soup.find_all("td", class_="name")[0].parent.find_all("td") name = tds[1].text p = OrderedDict() p["types"] = ', '.join( map(lambda img: img["title"], tds[2].find_all("img"))) p["abilities"] = ', '.join(map(lambda a: a.text, tds[3].find_all("a"))) p["rates"] = tds[4].find("img")["title"] p["egg groups"] = tds[5].text[1:-1].replace("\n", ", ") p["hp"] = tds[6].text p["atk"] = tds[7].text p["def"] = tds[8].text p["SpA"] = tds[9].text p["SpD"] = tds[10].text p["Spd"] = tds[11].text p["total"] = tds[12].text poke_url = "https://veekun.com" + tds[1].find("a")["href"] r = requests.get(poke_url) if r.status_code != 200: raise BotException() data = r.text soup2 = BeautifulSoup(data, "html.parser") img = soup2.find("div", id="dex-pokemon-portrait-sprite").find("img") picture = "https://veekun.com" + img["src"] embed = { 'color': 0xCC3C32, 'url': poke_url, 'title': name, 'thumbnail': { 'url': picture }, 'fields': [] } for title, value in p.items(): embed['fields'].append({ 'name': title, 'value': value, 'inline': True }) return Response(embed=embed)
async def get_by_url(url, index=INDEX) -> dict: try: result = await CLIENT.get(index, id=url, _source_excludes='description_vector') return dict(url=result['_id'], description=result['_source']['description']) except elasticsearch.exceptions.NotFoundError as ex: raise NotFound(debug=ex.args[-1])
def login_user(self, user_data): user = self.db.query(UserModel).filter_by( username=user_data.get("username")).first() if not user: raise NotFound('Order with id {} not found'.format( user_data.get("username"))) if not check_password_hash(user.password, user_data.get("password")): raise ValueError schemas = UserSchema() return schemas.dump(user).data
def mal_resource(self, resource_type, search): url = 'https://myanimelist.net/api/' + resource_type + '/search.xml' params = {'q': search} auth = HTTPBasicAuth(config.MAL_USERNAME, config.MAL_PASSWORD) r = requests.get(url, params=params, auth=auth) if r.status_code == 204: raise NotFound() if r.status_code != 200: raise BotException() body = r.text root = ElementTree.fromstring(body) entry = root[0] fields = [ 'english', 'score', 'type', 'episodes', 'volumes', 'chapters', 'status', 'start_date', 'end_date', ] embed = { 'title': entry.find('title').text, 'url': 'https://myanimelist.net/{}/{}'.format(resource_type, entry.find('id').text), 'description': html_parse(entry.find('synopsis').text), 'thumbnail': { 'url': entry.find('image').text, 'width': 100, }, 'fields': [] } for f in fields: node = entry.find(f) if node is None: continue value = html.unescape(node.text) embed['fields'].append({ 'name': f.replace('_', ' '), 'value': value, 'inline': True }) return Response(embed=embed)
def create_exam(db: Session, exam: exam_schemas.ExamCreate, user_id: int): user = db.query(models.User).get(user_id) if user is None: raise NotFound("User not found") exam_dict = exam.dict() exam_dict["user_id"] = user_id exam_dict["attempt"] = 1 db_exam = models.Exam(**exam_dict) db.add(db_exam) db.commit() db.refresh(db_exam) return db_exam
def __init__(self, name, **kwargs): super().__init__(name=name, **kwargs) # create an index in elasticsearch, ignore status code 400 (index already exists) if self.location: print("-=-=-=-=\n{}".format(self.location)) self.location.indices.create(index=self.name, request_timeout=1.0, ignore=400) else: raise NotFound("Unable to connect to ElasticSearch") # expire_date - a function(data) that returns the date this data should expire self.expire_date = kwargs.get('expire_date')
def build(cls, dic, id_name=None): dic_t = type(dic) if dic_t in (tuple, list): rv = [cls.build(d, id_name) for d in dic] elif dic_t is dict: id_name = dic.pop('id', id_name) rv = Input(id=id_name, **dic) elif dic_t is str: rv = Doc._all.get(dic) or Input._all.get(dic) if not rv: raise NotFound("{}: Unable to build {}".format( cls.__name__, pformat(dic))) return rv
def get_subclass(name, type=None, **kwargs): """ return a subclass that handles the input """ if type in Input.types: return Input.types[type] errors = {} best, pick = 0, None for t,cls in Input.types.items(): tmp = cls.validate(kwargs, errors=errors) if tmp > best: pick = cls best = tmp if pick: return pick raise NotFound("Unable to build Input for '{}':\n{}\n-------------".format(name, pformat(errors)))
def update_exam_by_id(db: Session, exam_id: int, exam: exam_schemas.ExamUpdate, user_id: int): db_exam: models.Exam = get_exam_by_id(db, exam_id, user_id) if db_exam is None: raise NotFound("Exam not found") db_exam.name = exam.name db_exam.date = exam.date db_exam.grade = exam.grade db_exam.ects = exam.ects db_exam.attempt = exam.attempt db_exam.passed = exam.passed db.commit() db.refresh(db_exam) return db_exam
def __call__(self): """ Invokes the default view. """ ti = self.getTypeInfo() method_id = ti and ti.queryMethodID('(Default)', context=self) if method_id and method_id != '(Default)': method = getattr(self, method_id) if getattr(aq_base(method), 'isDocTemp', 0): return method(self, self.REQUEST, self.REQUEST['RESPONSE']) else: return method() else: raise NotFound('Cannot find default view for "%s"' % '/'.join(self.getPhysicalPath()))
def manage_addContent(self, id, type, REQUEST=None): """ Add the content type specified by name. """ # self is a FactoryDispatcher. contentinit = self.contentinit obj = None for content_type in contentinit.content_types: if content_type.meta_type == type: obj = content_type(id) break if obj is None: raise NotFound(type) self._setObject(id, obj) if REQUEST is not None: return self.manage_main(self, REQUEST)
async def update(url, new_description, new_description_vector, index=INDEX): try: result = await CLIENT.update(index, id=url, body={ 'doc': { 'description': new_description, 'description_vector': new_description_vector, } }) except elasticsearch.exceptions.NotFoundError as ex: raise NotFound(ex.args[-1]) return result
def manage_addTool(self, type, REQUEST=None): """ Add the tool specified by name. """ # self is a FactoryDispatcher. toolinit = self.toolinit obj = None for tool in toolinit.tools: if tool.meta_type == type: obj = tool() break if obj is None: raise NotFound(type) self._setObject(obj.getId(), obj) if REQUEST is not None: return self.manage_main(self, REQUEST)
def get_buildnumber_for_revision(self, revision, refresh=False): """ :param revision: subversion revision to look for, int :param refresh: boolean, whether or not to refresh the revision -> buildnumber map :return: list of buildnumbers, [int] """ if self.get_scm_type() == 'svn' and not isinstance(revision, int): revision = int(revision) if self._revmap is None or refresh: self._revmap = self.get_revision_dict() try: return self._revmap[revision] except KeyError: raise NotFound("Couldn't find a build with that revision")
def do_dispatch(self): """ Dispatches the request. This will first check if there's a handler_method defined in the matched route, and if not it'll use the method correspondent to the request method (``get()``, ``post()`` etc). """ if self.request.method.upper() == 'OPTIONS': return CorsResponse() route_args = self.get_route_args() route_kwargs = {} matched_url = None for url in getattr(self, 'urls', []): if re.match(url.regex, self.request.path_info): matched_url = url break if matched_url is None: raise NotFound('URL not found on this server') if matched_url.route.detail is True: try: route_kwargs['key'] = self.get_object( urlsafe=route_args['urlsafe']) except Exception: return JsonResponse(status=400, data='Given urlsafe is invalid') if self.request.method.lower() not in matched_url.route.mapping.keys(): raise DispatchError('Invalid method {}'.format( self.request.method)) self.action = matched_url.route.mapping[self.request.method.lower()] method = getattr(self, self.action, None) # The handler only receives *args if no named variables are set. args, kwargs = route_args, route_kwargs if kwargs: args = () if self.check_action_permissions() is False: raise NotAuthorized() return method(self, *args, **kwargs)
def imgur(self, ctx, search): url = 'https://api.imgur.com/3/gallery/search/viral' headers = {'Authorization': 'Client-ID {}'.format(config.IMGUR_ID)} params = {'q': search} r = requests.get(url, headers=headers, params=params) if r.status_code != 200: raise BotException() body = r.json() data = body.get('data') if not data: raise NotFound() result = data[0] message = result['link'] return Response(message)
def twitch(self, ctx, search): url = "https://api.twitch.tv/kraken/search/channels" params = {'q': search, 'client_id': config.TWITCH_CLIENT_ID} r = requests.get(url, params=params) if r.status_code != 200: raise BotException() body = r.json() channels = body.get('channels') if not channels: raise NotFound() channel = channels[0] embed = Embed({ 'color': 0x6441a5, 'title': channel['name'], 'description': channel['status'], 'url': channel['url'], 'thumbnail': { 'url': channel['logo'], 'width': 100, 'height': 100 }, 'fields': [{ 'name': 'followers', 'value': channel['followers'], 'inline': True }, { 'name': 'views', 'value': channel['views'], 'inline': True }, { 'name': 'last played game', 'value': channel['game'] }] }) return Response(embed=embed)
def process_request(self, request): """Gets the requested path from the headers and matches it to a route, function, group and method stored in self.routes. The view funtion itself then gets executed to generate the response-data. If the requested path is not found in self.routes or the request method used is not supported by the view we return an error Response object. We also execute any "first" and "last" functions. """ path = request.headers['PATH_INFO'] method = request.headers['REQUEST_METHOD'] #: Match to a route route_match = self.get_route_match(path) if route_match: kwargs, methods, group, view_function = route_match #: Execute any first functions for route group first_group_rv = self.__execute_first_groups(request, group) if first_group_rv is not None: return first_group_rv if method not in methods: raise MethodNotAllowed() else: rv = view_function(request, **kwargs) #: Execute any last functions for a route group. If we don't want #: functions without a group to be executed on top of the ones with #: a group, we should build and return the response here. last_group_rv = self.__execute_last_groups(rv, group) if last_group_rv is not None: return last_group_rv else: raise NotFound() return rv
def get_site(self, site_id=None, url=None): """ Get site by url or site_id. :param site_id: id of looking site :param url: Url of page :return: Dict with site """ if site_id: site = self.db.sites.find_one({'site_id': str(site_id)}) elif url: site = self.db.sites.find_one({'url': url}) else: raise NoArgument('Give me site_id or url.') if site is None: raise NotFound('No site with this site_id or url.') return self.process(site)
def youtube(self, ctx, search): url = "https://www.googleapis.com/youtube/v3/search" params = { 'type': 'video', 'q': search, 'part': 'snippet', 'key': config.GOOGLE_API_KEY } r = requests.get(url, params=params) if r.status_code != 200: raise BotException() body = r.json() data = body.get('items') if not data: raise NotFound() video = data[0] message = 'https://youtu.be/' + video['id']['videoId'] return Response(message)
def urban(self, ctx, search): url = "http://api.urbandictionary.com/v0/define" params = {'term': search} r = requests.get(url, params=params) if r.status_code != 200: raise BotException() body = r.json() data = body.get('list') if not data: raise NotFound() entry = data[0] embed = { 'thumbnail': { 'url': 'https://upload.wikimedia.org/wikipedia/en/b/b7/Urban_dictionary_--_logo.jpg', 'width': 100, 'height': 100 }, 'color': 0xCC3C32, 'title': entry['word'], 'description': entry['definition'], 'url': entry['permalink'], 'fields': [ { 'name': 'example', 'value': entry['example'], 'inline': True }, ] } embed = Embed(embed) return Response(embed=embed)
def _getViewFor(obj, view='view'): warn( '__call__() and view() methods using _getViewFor() as well as ' '_getViewFor() itself are deprecated and will be removed in CMF 2.0. ' 'Bypass these methods by defining \'(Default)\' and \'view\' Method ' 'Aliases.', DeprecationWarning) ti = obj.getTypeInfo() if ti is not None: context = getActionContext(obj) actions = ti.listActions() for action in actions: if action.getId() == view: if _verifyActionPermissions(obj, action): target = action.action(context).strip() if target.startswith('/'): target = target[1:] __traceback_info__ = (ti.getId(), target) return obj.restrictedTraverse(target) # "view" action is not present or not allowed. # Find something that's allowed. for action in actions: if _verifyActionPermissions(obj, action): target = action.action(context).strip() if target.startswith('/'): target = target[1:] __traceback_info__ = (ti.getId(), target) return obj.restrictedTraverse(target) raise AccessControl_Unauthorized('No accessible views available for ' '%s' % '/'.join(obj.getPhysicalPath())) else: raise NotFound('Cannot find default view for "%s"' % '/'.join(obj.getPhysicalPath()))
def get_user_by_id(self, id): user = User.query.get(id) if user: return user else: raise NotFound("User with id {} was not found".format(id))