本文整理汇总了Python中message.Messager类的典型用法代码示例。如果您正苦于以下问题:Python Messager类的具体用法?Python Messager怎么用?Python Messager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Messager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_match_regex
def _get_match_regex(text, text_match="word", match_case=False,
whole_string=False):
"""
Helper for the various search_anns_for_ functions.
"""
if match_case:
regex_flags = 0
else:
regex_flags = re.IGNORECASE
if text is None:
text = ''
if text_match == "word":
# full word match: require word boundaries or, optionally,
# whole string boundaries
if whole_string:
return re.compile(r'^'+re.escape(text)+r'$', regex_flags)
else:
return re.compile(r'\b'+re.escape(text)+r'\b', regex_flags)
elif text_match == "substring":
# any substring match, as text (nonoverlapping matches)
return re.compile(re.escape(text), regex_flags)
elif text_match == "regex":
try:
return re.compile(text, regex_flags)
except: # whatever (sre_constants.error, other?)
Messager.warning('Given string "%s" is not a valid regular expression.' % text)
return None
else:
Messager.error('Unrecognized search match specification "%s"' % text_match)
return None
示例2: _report_timings
def _report_timings(dbname, start, msg=None):
delta = datetime.now() - start
strdelta = str(delta).replace('0:00:0', '') # take out zero min & hour
queries = normdb.get_query_count(dbname)
normdb.reset_query_count(dbname)
Messager.info("Processed " + str(queries) + " queries in " + strdelta +
(msg if msg is not None else ""))
示例3: _server_crash
def _server_crash(cookie_hdrs, e):
from config import ADMIN_CONTACT_EMAIL, DEBUG
from jsonwrap import dumps
from message import Messager
stack_trace = _get_stack_trace()
if DEBUG:
# Send back the stack-trace as json
error_msg = '\n'.join(('Server Python crash, stack-trace is:\n',
stack_trace))
Messager.error(error_msg, duration=-1)
else:
# Give the user an error message
# Use the current time since epoch as an id for later log look-up
error_msg = ('The server encountered a serious error, '
'please contact the administrators at %s '
'and give the id #%d'
) % (ADMIN_CONTACT_EMAIL, int(time()))
Messager.error(error_msg, duration=-1)
# Print to stderr so that the exception is logged by the webserver
print(stack_trace, file=stderr)
json_dic = {
'exception': 'serverCrash',
}
return (cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json(json_dic))))
示例4: ann_logger
def ann_logger(directory):
"""
Lazy initializer for the annotation logger. Returns None if
annotation logging is not configured for the given directory and a
logger otherwise.
"""
if ann_logger.__logger == False:
# not initialized
annlogfile = options_get_annlogfile(directory)
if annlogfile == '<NONE>':
# not configured
ann_logger.__logger = None
else:
# initialize
try:
l = logging.getLogger('annotation')
l.setLevel(logging.INFO)
handler = logging.FileHandler(annlogfile)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s\t%(message)s')
handler.setFormatter(formatter)
l.addHandler(handler)
ann_logger.__logger = l
except IOError, e:
Messager.error("""Error: failed to initialize annotation log %s: %s.
Edit action not logged.
Please check the Annotation-log logfile setting in tools.conf""" % (annlogfile, e))
logging.error("Failed to initialize annotation log %s: %s" %
(annlogfile, e))
ann_logger.__logger = None
示例5: ssdb_build
def ssdb_build(strs, dbname, ngram_length=DEFAULT_NGRAM_LENGTH, include_marks=DEFAULT_INCLUDE_MARKS):
"""
Given a list of strings, a DB name, and simstring options, builds
a simstring DB for the strings.
"""
try:
import simstring
except ImportError:
Messager.error(SIMSTRING_MISSING_ERROR, duration=-1)
raise NoSimStringError
dbfn = __ssdb_path(dbname)
try:
# only library defaults (n=3, no marks) supported just now (TODO)
assert ngram_length == 3, "Error: unsupported n-gram length"
assert include_marks == False, "Error: begin/end marks not supported"
db = simstring.writer(dbfn)
for s in strs:
db.insert(s)
db.close()
except:
print >> sys.stderr, "Error building simstring DB"
raise
return dbfn
示例6: norm_get_name
def norm_get_name(database, key, collection=None):
if NORM_LOOKUP_DEBUG:
_check_DB_version(database)
if REPORT_LOOKUP_TIMINGS:
lookup_start = datetime.now()
dbpath = _get_db_path(database, collection)
if dbpath is None:
# full path not configured, fall back on name as default
dbpath = database
try:
data = normdb.data_by_id(dbpath, key)
except normdb.dbNotFoundError as e:
Messager.warning(str(e))
data = None
# just grab the first one (sorry, this is a bit opaque)
if data is not None:
value = data[0][0][1]
else:
value = None
if REPORT_LOOKUP_TIMINGS:
_report_timings(database, lookup_start)
# echo request for sync
json_dic = {
'database': database,
'key': key,
'value': value
}
return json_dic
示例7: _config_check
def _config_check():
from message import Messager
from sys import path
from copy import deepcopy
from os.path import dirname
# Reset the path to force config.py to be in the root (could be hacked
# using __init__.py, but we can be monkey-patched anyway)
orig_path = deepcopy(path)
# Can't you empty in O(1) instead of O(N)?
while path:
path.pop()
path.append(path_join(abspath(dirname(__file__)), '../..'))
# Check if we have a config, otherwise whine
try:
import config
del config
except ImportError, e:
path.extend(orig_path)
# "Prettiest" way to check specific failure
if e.message == 'No module named config':
Messager.error(_miss_config_msg(), duration=-1)
else:
Messager.error(_get_stack_trace(), duration=-1)
raise ConfigurationError
示例8: ssdb_supstring_exists
def ssdb_supstring_exists(s, dbname, threshold=DEFAULT_THRESHOLD):
"""Given a string s and a DB name, returns whether at least one string in
the associated simstring DB likely contains s as an (approximate)
substring."""
try:
import simstring
except ImportError:
Messager.error(SIMSTRING_MISSING_ERROR, duration=-1)
raise NoSimStringError
if threshold == 1.0:
# optimized (not hugely, though) for this common case
db = ssdb_open(dbname.encode('UTF-8'))
__set_db_measure(db, 'overlap')
db.threshold = threshold
result = db.retrieve(s)
db.close()
# assume simstring DBs always contain UTF-8 - encoded strings
result = [r.decode('UTF-8') for r in result]
for r in result:
if s in r:
return True
return False
else:
# naive implementation for everything else
return len(ssdb_supstring_lookup(s, dbname, threshold)) != 0
示例9: retrieve_stored
def retrieve_stored(document, suffix):
stored_path = _stored_path()+'.'+suffix
if not isfile(stored_path):
# @ninjin: not sure what 'version' was supposed to be returned
# here, but none was defined, so returning that
# raise NoSVGError(version)
raise NoSVGError('None')
filename = document+'.'+suffix
# sorry, quick hack to get the content-type right
# TODO: send this with initial 'stored' response instead of
# guessing on suffix
if suffix == SVG_SUFFIX:
content_type = 'image/svg+xml'
elif suffix == PNG_SUFFIX:
content_type = 'image/png'
elif suffix == PDF_SUFFIX:
content_type = 'application/pdf'
elif suffix == EPS_SUFFIX:
content_type = 'application/postscript'
else:
Messager.error('Unknown suffix "%s"; cannot determine Content-Type' % suffix)
# TODO: reasonable backoff value
content_type = None
# Bail out with a hack since we violated the protocol
hdrs = [('Content-Type', content_type),
('Content-Disposition', 'inline; filename=' + filename)]
with open(stored_path, 'rb') as stored_file:
data = stored_file.read()
raise NoPrintJSONError(hdrs, data)
示例10: possible_arc_types
def possible_arc_types(collection, origin_type, target_type):
directory = collection
real_dir = real_directory(directory)
projectconf = ProjectConfiguration(real_dir)
response = {}
try:
possible = projectconf.arc_types_from_to(origin_type, target_type)
# TODO: proper error handling
if possible is None:
Messager.error('Error selecting arc types!', -1)
elif possible == []:
# nothing to select
response['html'] = generate_empty_fieldset()
response['keymap'] = {}
response['empty'] = True
else:
# XXX TODO: intentionally breaking this; KB shortcuts
# should no longer be sent here. Remove 'keymap' and
# 'html' args once clientside generation done.
arc_kb_shortcuts = {} #select_keyboard_shortcuts(possible)
response['keymap'] = {}
for k, p in arc_kb_shortcuts.items():
response['keymap'][k] = "arc_"+p
response['html'] = generate_arc_type_html(projectconf, possible, arc_kb_shortcuts)
except:
Messager.error('Error selecting arc types!', -1)
raise
return response
示例11: norm_get_data
def norm_get_data(database, key, collection=None):
if NORM_LOOKUP_DEBUG:
_check_DB_version(database)
if REPORT_LOOKUP_TIMINGS:
lookup_start = datetime.now()
dbpath = _get_db_path(database, collection)
if dbpath is None:
# full path not configured, fall back on name as default
dbpath = database
try:
data = normdb.data_by_id(dbpath, key)
except normdb.dbNotFoundError as e:
Messager.warning(str(e))
data = None
if data is None:
Messager.warning("Failed to get data for " + database + ":" + key)
if REPORT_LOOKUP_TIMINGS:
_report_timings(database, lookup_start)
# echo request for sync
json_dic = {
'database': database,
'key': key,
'value': data
}
return json_dic
示例12: _parse_relation_annotation
def _parse_relation_annotation(self, id, data, data_tail, input_file_path):
try:
type_delim = data.index(' ')
type, type_tail = (data[:type_delim], data[type_delim:])
except ValueError:
# cannot have a relation with just a type (contra event)
raise IdedAnnotationLineSyntaxError(id, self.ann_line, self.ann_line_num+1, input_file_path)
try:
args = [tuple(arg.split(':')) for arg in type_tail.split()]
except ValueError:
raise IdedAnnotationLineSyntaxError(id, self.ann_line, self.ann_line_num+1, input_file_path)
if len(args) != 2:
Messager.error('Error parsing relation: must have exactly two arguments')
raise IdedAnnotationLineSyntaxError(id, self.ann_line, self.ann_line_num+1, input_file_path)
args.sort()
if args[0][0] == args[1][0]:
Messager.error('Error parsing relation: arguments must not be identical')
raise IdedAnnotationLineSyntaxError(id, self.ann_line, self.ann_line_num+1, input_file_path)
return BinaryRelationAnnotation(id, type,
args[0][0], args[0][1],
args[1][0], args[1][1],
data_tail, source_id=input_file_path)
示例13: get_drawing_config_by_storage_form
def get_drawing_config_by_storage_form(directory, term):
cache = get_drawing_config_by_storage_form.__cache
if directory not in cache:
d = {}
for n in get_drawing_config(directory):
t = n.storage_form()
if t in d:
Messager.warning("Project configuration: term %s appears multiple times, only using last. Configuration may be wrong." % t, 5)
d[t] = {}
for a in n.arguments:
if len(n.arguments[a]) != 1:
Messager.warning("Project configuration: expected single value for %s argument %s, got '%s'. Configuration may be wrong." % (t, a, "|".join(n.arguments[a])))
else:
d[t][a] = n.arguments[a][0]
# TODO: hack to get around inability to have commas in values;
# fix original issue instead
for t in d:
for k in d[t]:
d[t][k] = d[t][k].replace("-", ",")
# propagate defaults (TODO: get rid of magic "DEFAULT" values)
default_keys = [VISUAL_SPAN_DEFAULT, VISUAL_ARC_DEFAULT]
for default_dict in [d.get(dk, {}) for dk in default_keys]:
for k in default_dict:
for t in d:
d[t][k] = d[t].get(k, default_dict[k])
cache[directory] = d
return cache[directory].get(term, None)
示例14: arc_types_from_to
def arc_types_from_to(self, from_ann, to_ann="<ANY>", include_special=False):
"""
Returns the possible arc types that can connect an annotation
of type from_ann to an annotation of type to_ann.
If to_ann has the value \"<ANY>\", returns all possible arc types.
"""
from_node = get_node_by_storage_form(self.directory, from_ann)
if from_node is None:
Messager.warning("Project configuration: unknown textbound/event type %s. Configuration may be wrong." % from_ann)
return []
if to_ann == "<ANY>":
relations_from = get_relations_by_arg1(self.directory, from_ann, include_special)
# TODO: consider using from_node.arg_list instead of .arguments for order
return unique_preserve_order([role for role in from_node.arguments] + [r.storage_form() for r in relations_from])
# specific hits
types = from_node.keys_by_type.get(to_ann, [])
if "<ANY>" in from_node.keys_by_type:
types += from_node.keys_by_type["<ANY>"]
# generic arguments
if self.is_event_type(to_ann) and '<EVENT>' in from_node.keys_by_type:
types += from_node.keys_by_type['<EVENT>']
if self.is_physical_entity_type(to_ann) and '<ENTITY>' in from_node.keys_by_type:
types += from_node.keys_by_type['<ENTITY>']
# relations
types.extend(self.relation_types_from_to(from_ann, to_ann))
return unique_preserve_order(types)
示例15: ann_logger
def ann_logger():
"""
Lazy initializer for the annotation logger. Returns None if
annotation logging is not configured and a logger otherwise.
"""
if ann_logger.__logger == False:
# not initialized
if ANNOTATION_LOG is None:
# not configured
ann_logger.__logger = None
else:
# initialize
try:
l = logging.getLogger('annotation')
l.setLevel(logging.INFO)
handler = logging.FileHandler(ANNOTATION_LOG)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s\t%(message)s')
handler.setFormatter(formatter)
l.addHandler(handler)
ann_logger.__logger = l
except IOError, e:
Messager.error("""Error: failed to initialize annotation log %s: %s.
Edit action not logged.
Please check ANNOTATION_LOG setting in config.py""" % (ANNOTATION_LOG, e))
logging.error("Failed to initialize annotation log %s: %s" %
(ANNOTATION_LOG, e))
ann_logger.__logger = None