本文整理汇总了Python中mongoengine.NotUniqueError方法的典型用法代码示例。如果您正苦于以下问题:Python mongoengine.NotUniqueError方法的具体用法?Python mongoengine.NotUniqueError怎么用?Python mongoengine.NotUniqueError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoengine
的用法示例。
在下文中一共展示了mongoengine.NotUniqueError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_proxies
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def save_proxies(url):
proxies = []
try:
r = fetch(url)
except requests.exceptions.RequestException:
return False
addresses = re.findall(PROXY_REGEX, r.text)
for address in addresses:
proxy = Proxy(address=address)
try:
proxy.save()
except NotUniqueError:
pass
else:
proxies.append(address)
return proxies
示例2: save_article
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def save_article(article_):
img_url = article_.find(class_='img_box2').find(
'img').attrs['src'].split('url=')[1]
text_box = article_.find(class_='txt-box')
title = text_box.find('h4').find('a').text
article_url = text_box.find('h4').find('a').attrs['href']
summary = text_box.find('p').text
create_at = datetime.fromtimestamp(float(text_box.find(
class_='s-p').attrs['t']))
publisher_name = text_box.find(class_='s-p').find('a').attrs['title']
article = Article(img_url=img_url, title=title, article_url=article_url,
summary=summary, create_at=create_at,
publisher=Publisher.get_or_create(publisher_name))
try:
article.save()
except (NotUniqueError, InvalidBSON):
pass
示例3: insert_system_roles
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def insert_system_roles():
"""
Migration which inserts the default system roles.
"""
system_roles = SystemRole.get_valid_values()
LOG.debug('Inserting system roles (%s)' % (str(system_roles)))
for role_name in system_roles:
description = role_name
role_db = RoleDB(name=role_name, description=description, system=True)
try:
role_db.save()
except (StackStormDBObjectConflictError, NotUniqueError):
# Role already exists error is not fatal
pass
示例4: test_create_dup_uuid_fail
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def test_create_dup_uuid_fail(self):
"""
This test will attempt to create targets with the same uuids,
and it will fail as mongo should throw a not unique exception.
"""
# basic test
with self.assertRaises(NotUniqueError):
target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF')
self.assertEqual(target1.uuid, target2.uuid)
# different encoding
with self.assertRaises(NotUniqueError):
target1 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('utf-8'))
target2 = Database.create_target(None, 'AA:BB:CC:DD:EE:FF'.encode('ascii'))
self.assertEqual(target1.uuid, target2.uuid)
示例5: _create_call_sets
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def _create_call_sets(self):
for sample in self.vcf_reader.samples:
try:
cs = VariantCallSet.create_and_save(
name="_".join(
[
sample,
self.method]),
variant_sets=self.variant_sets,
sample_id=sample,
info={
"variant_caller": self.method})
except NotUniqueError:
raise ValueError(
"There is already a call set for sample %s with method %s " %
(sample, self.method))
else:
self.call_sets[sample] = cs
示例6: run_make_probes_from_vcf_file
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def run_make_probes_from_vcf_file(args):
# Make VariantSet from vcf
reference = os.path.basename(args.reference_filepath).split(".fa")[0]
try:
reference_set = ReferenceSet.objects.get(name=reference)
except DoesNotExist:
reference_set = ReferenceSet.create_and_save(name=reference)
# Hack
try:
reference = Reference.create_and_save(
name=reference, reference_sets=[reference_set], md5checksum=reference
)
except NotUniqueError:
pass
vcf = VCF(
args.vcf,
reference_set.id,
method="tmp",
force=True,
append_to_global_variant_set=False,
)
vcf.add_to_database()
示例7: create_group
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def create_group(groupname):
try:
return Group(groupname=groupname).save()
except NotUniqueError:
return False
示例8: save_proxies
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def save_proxies(url):
try:
r = fetch(url)
except requests.exceptions.RequestException:
return False
addresses = re.findall(PROXY_REGEX, r.text)
for address in addresses:
proxy = Proxy(address=address)
try:
proxy.save()
except NotUniqueError:
pass
示例9: insert
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def insert(cls, model_object, publish=True, dispatch_trigger=True,
log_not_unique_error_as_debug=False):
# Late import to avoid very expensive in-direct import (~1 second) when this function
# is not called / used
from mongoengine import NotUniqueError
if model_object.id:
raise ValueError('id for object %s was unexpected.' % model_object)
try:
model_object = cls._get_impl().insert(model_object)
except NotUniqueError as e:
if log_not_unique_error_as_debug:
LOG.debug('Conflict while trying to save in DB: %s.', six.text_type(e))
else:
LOG.exception('Conflict while trying to save in DB.')
# On a conflict determine the conflicting object and return its id in
# the raised exception.
conflict_object = cls._get_by_object(model_object)
conflict_id = str(conflict_object.id) if conflict_object else None
message = six.text_type(e)
raise StackStormDBObjectConflictError(message=message, conflict_id=conflict_id,
model_object=model_object)
# Publish internal event on the message bus
if publish:
try:
cls.publish_create(model_object)
except:
LOG.exception('Publish failed.')
# Dispatch trigger
if dispatch_trigger:
try:
cls.dispatch_create_trigger(model_object)
except:
LOG.exception('Trigger dispatch failed.')
return model_object
示例10: _register_internal_trigger_type
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def _register_internal_trigger_type(trigger_definition):
try:
trigger_type_db = create_trigger_type_db(trigger_type=trigger_definition,
log_not_unique_error_as_debug=True)
except (NotUniqueError, StackStormDBObjectConflictError):
# We ignore conflict error since this operation is idempotent and race is not an issue
LOG.debug('Internal trigger type "%s" already exists, ignoring error...' %
(trigger_definition['name']))
ref = ResourceReference.to_string_reference(name=trigger_definition['name'],
pack=trigger_definition['pack'])
trigger_type_db = get_trigger_type_db(ref)
if trigger_type_db:
LOG.debug('Registered internal trigger: %s.', trigger_definition['name'])
# trigger types with parameters do no require a shadow trigger.
if trigger_type_db and not trigger_type_db.parameters_schema:
try:
trigger_db = create_shadow_trigger(trigger_type_db,
log_not_unique_error_as_debug=True)
extra = {'trigger_db': trigger_db}
LOG.audit('Trigger created for parameter-less internal TriggerType. Trigger.id=%s' %
(trigger_db.id), extra=extra)
except (NotUniqueError, StackStormDBObjectConflictError):
LOG.debug('Shadow trigger "%s" already exists. Ignoring.',
trigger_type_db.get_reference().ref, exc_info=True)
except (ValidationError, ValueError):
LOG.exception('Validation failed in shadow trigger. TriggerType=%s.',
trigger_type_db.get_reference().ref)
raise
return trigger_type_db
示例11: register_internal_trigger_types
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def register_internal_trigger_types():
"""
Register internal trigger types.
NOTE 1: This method blocks until all the trigger types have been registered.
NOTE 2: We log "NotUniqueError" errors under debug and not error. Those errors are not fatal
because this operation is idempotent and NotUniqueError simply means internal trigger type
has already been registered by some other service.
"""
action_sensor_enabled = cfg.CONF.action_sensor.enable
registered_trigger_types_db = []
for _, trigger_definitions in six.iteritems(INTERNAL_TRIGGER_TYPES):
for trigger_definition in trigger_definitions:
LOG.debug('Registering internal trigger: %s', trigger_definition['name'])
is_action_trigger = trigger_definition['name'] == ACTION_SENSOR_TRIGGER['name']
if is_action_trigger and not action_sensor_enabled:
continue
try:
trigger_type_db = _register_internal_trigger_type(
trigger_definition=trigger_definition)
except Exception:
LOG.exception('Failed registering internal trigger: %s.', trigger_definition)
raise
else:
registered_trigger_types_db.append(trigger_type_db)
return registered_trigger_types_db
示例12: test_create_dup_name_fail
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def test_create_dup_name_fail(self):
"""
This test will attempt to create two targets with identitical names,
and it will fail as Mongo should throw a not unique exception.
"""
with self.assertRaises(NotUniqueError):
target1 = Database.create_target(self.TEST_NAME)
target2 = Database.create_target(self.TEST_NAME)
self.assertEqual(target1.name, target2.name)
示例13: map_ids_from_line
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def map_ids_from_line(self, line: CrisprLine, mode, wges_dict=None):
crispr: Crispr = self.create_crispr_from_crispr_line(line)
try:
crispr.extract_and_save_wge(mode, wges_dict)
except NoWGEException as ex:
logger.error(ex.msg())
pass
except (DuplicateKeyError, NotUniqueError) as ex:
logger.warning("Skipping duplicate wge_id")
pass
except Exception as e:
traceback.print_exc()
logger.error(f"A problem happened for {self.filename}, oligo_id {crispr.crispr_line.get_oligo_id}")
pass
示例14: _create_new_variant_set
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def _create_new_variant_set(self):
variant_set_name = os.path.basename(
self.f)
if VariantSet.objects(
name=variant_set_name,
reference_set=self.reference_set):
if not self.force:
raise NotUniqueError(
"VariantSet %s already exists. Rerun with -f to recreate." %
variant_set_name)
else:
self._remove_variant_set(variant_set_name)
self.vcf_variant_set = VariantSet.create_and_save(
name=variant_set_name,
reference_set=self.reference_set)
示例15: api
# 需要导入模块: import mongoengine [as 别名]
# 或者: from mongoengine import NotUniqueError [as 别名]
def api(uri, login=False, **kwargs):
def decorator(f):
@wraps(f)
def wrapped_f(*func_args, **func_kwargs):
if login:
user_token = request.cookies.get('user-token')
if user_token is not None:
try:
func_kwargs['user'] = users.validate_token(user_token)
except utils.AuthenticationError:
return JSONResponse(status=httplib.UNAUTHORIZED)
else:
return JSONResponse(status=httplib.UNAUTHORIZED)
try:
results, status_code = f(*func_args, **func_kwargs)
if 'count' in request.args:
if isinstance(results, BaseQuerySet):
results = results.count()
else:
results = len(results)
except mongoengine.ValidationError:
traceback.print_exc()
status_code = httplib.BAD_REQUEST
results = {"error": "invalid input"}
except mongoengine.NotUniqueError:
traceback.print_exc()
status_code = httplib.BAD_REQUEST
results = {"error": "not unique"}
except Exception as e:
traceback.print_exc()
status_code = httplib.INTERNAL_SERVER_ERROR
results = None
if request.args.get('format', 'json') == 'bson':
output = bsonify(results) # .json_util.dumps(results, indent=2, sort_keys=True)
return Response(output, status=status_code, content_type='application/json')
else:
return JSONResponse(results, status=status_code)
endpoint = kwargs.pop('endpoint', f.__name__) # + str(len(api_endpoints)))
app.add_url_rule(uri, endpoint, wrapped_f, **kwargs)
assert endpoint not in api_endpoints
api_endpoints[endpoint] = f, uri, kwargs.get('methods', ['GET']), rest_doc(f)
return f
return decorator