本文整理汇总了Python中tools.stderr函数的典型用法代码示例。如果您正苦于以下问题:Python stderr函数的具体用法?Python stderr怎么用?Python stderr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了stderr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_error
def handle_error(self):
''' Handle any uncaptured error in the core. Overrides asyncore's
handle_error '''
trace = traceback.format_exc()
stderr(trace)
self.debug("core", 'Fatal error in core, please review exception log',
'always')
logfile = codecs.open(os.path.join(self.config.logdir, 'exceptions.log'),
'a', encoding='utf-8') # TODO: make not
# hardcoded
logfile.write('Fatal error in core, handle_error() was called\n')
logfile.write('last raw line was %s' % self.raw)
logfile.write(trace)
logfile.write('Buffer:\n')
logfile.write(self.buffer)
logfile.write('----------------------------------------\n\n')
logfile.close()
if self.error_count > 10:
if (datetime.now() - self.last_error_timestamp).seconds < 5:
print >> sys.stderr, "Too many errors, can't continue"
os._exit(1)
self.last_error_timestamp = datetime.now()
self.error_count = self.error_count + 1
if self.config.exit_on_error:
os._exit(1)
示例2: found_terminator
def found_terminator(self):
line = self.buffer
if line.endswith('\r'):
line = line[:-1]
self.buffer = u''
self.raw = line
if line.startswith(':'):
source, line = line[1:].split(' ', 1)
else:
source = None
if ' :' in line:
argstr, text = line.split(' :', 1)
args = argstr.split()
args.append(text)
else:
args = line.split()
text = args[-1]
self.last_ping_time = datetime.now()
if args[0] == 'PING':
self.write(('PONG', text))
elif args[0] == 'ERROR':
self.debug(__file__, text, 'always')
if self.hasquit:
self.close_when_done()
elif args[0] == '433':
stderr('Nickname already in use!')
self.handle_close()
origin = Origin(self, source, args)
self.dispatch(origin, text, args)
示例3: found_terminator
def found_terminator(self):
line = self.buffer
if line.endswith("\r"):
line = line[:-1]
self.buffer = u""
self.raw = line
if line.startswith(":"):
source, line = line[1:].split(" ", 1)
else:
source = None
if " :" in line:
argstr, text = line.split(" :", 1)
args = argstr.split()
args.append(text)
else:
args = line.split()
text = args[-1]
self.last_ping_time = datetime.now()
if args[0] == "PING":
self.write(("PONG", text))
elif args[0] == "ERROR":
self.debug("IRC Server Error", text, "always")
elif args[0] == "433":
stderr("Nickname already in use!")
self.hasquit = True
self.handle_close()
origin = Origin(self, source, args)
self.dispatch(origin, text, args)
示例4: unregister
def unregister(self, variables):
"""Unregister all willie callables in variables, and their bindings.
When unloading a module, this ensures that the unloaded modules will
not get called and that the objects can be garbage collected. Objects
that have not been registered are ignored.
Args:
variables -- A list of callable objects from a willie module.
"""
def remove_func(func, commands):
"""Remove all traces of func from commands."""
for func_list in commands.itervalues():
if func in func_list:
func_list.remove(func)
hostmask = "%s!%[email protected]%s" % (self.nick, self.user, socket.gethostname())
willie = self.WillieWrapper(self, irc.Origin(self, hostmask, [], {}))
for obj in variables.itervalues():
if obj in self.callables:
self.callables.remove(obj)
for commands in self.commands.itervalues():
remove_func(obj, commands)
if obj in self.shutdown_methods:
try:
obj(willie)
except Exception as e:
stderr(
"Error calling shutdown method for module %s:%s" %
(obj.__module__, e)
)
self.shutdown_methods.remove(obj)
示例5: found_terminator
def found_terminator(self):
line = self.buffer
if line.endswith('\r'):
line = line[:-1]
self.buffer = u''
self.raw = line
if line.startswith(':'):
source, line = line[1:].split(' ', 1)
else:
source = None
if ' :' in line:
argstr, text = line.split(' :', 1)
args = argstr.split()
args.append(text)
else:
args = line.split()
text = args[-1]
if args[0] == 'PING':
self.write(('PONG', text))
elif args[0] == 'ERROR':
self.debug('IRC Server Error', text, 'always')
elif args[0] == '433':
stderr('Nickname already in use!')
self.hasquit = True
self.handle_close()
origin = Origin(self, source, args)
self.dispatch(origin, text, args)
示例6: handle_close
def handle_close(self):
self._shutdown()
stderr('Closed!')
# This will eventually call asyncore dispatchers close method, which
# will release the main thread. This should be called last to avoid
# race conditions.
asynchat.async_chat.handle_close(self)
示例7: _timeout_check
def _timeout_check(self):
while True:
if (datetime.now() - self.last_ping_time).seconds > int(self.config.timeout):
stderr('Ping timeout reached after %s seconds, closing connection' % self.config.timeout)
self.handle_close()
break
else:
time.sleep(int(self.config.timeout))
示例8: error
def error(self, origin=None, trigger=None):
''' Called internally when a module causes an error '''
try:
trace = traceback.format_exc()
trace = trace.decode('utf-8', errors='xmlcharrefreplace')
stderr(trace)
try:
lines = list(reversed(trace.splitlines()))
report = [lines[0].strip()]
for line in lines:
line = line.strip()
if line.startswith('File "/'):
report.append(line[0].lower() + line[1:])
break
else:
report.append('source unknown')
signature = '%s (%s)' % (report[0], report[1])
# TODO: make not hardcoded
log_filename = os.path.join(
self.config.logdir, 'exceptions.log'
)
with codecs.open(
log_filename, 'a', encoding='utf-8'
) as logfile:
logfile.write(u'Signature: %s\n' % signature)
if origin:
logfile.write(
u'from %s at %s:\n' % (
origin.sender, str(datetime.now())
)
)
if trigger:
logfile.write(
u'Message was: <%s> %s\n' % (
trigger.nick, trigger.group(0)
)
)
logfile.write(trace)
logfile.write(
'----------------------------------------\n\n'
)
except Exception as e:
stderr("Could not save full traceback!")
self.debug(__file__, "(From: " + origin.sender +
"), can't save traceback: " + str(e), 'always')
if origin:
self.msg(origin.sender, signature)
except Exception as e:
if origin:
self.msg(origin.sender, "Got an error.")
self.debug(
__file__,
"(From: " + origin.sender + ") " + str(e),
'always'
)
示例9: error
def error(self, origin, trigger):
''' Called internally when a module causes an error '''
try:
trace = traceback.format_exc()
try:
trace = trace.decode('utf-8')
except:
pass # Can't do much about it
stderr(trace)
try:
lines = list(reversed(trace.splitlines()))
report = [lines[0].strip()]
for line in lines:
line = line.strip()
if line.startswith('File "/'):
report.append(line[0].lower() + line[1:])
break
else:
report.append('source unknown')
signature = '%s (%s)' % (report[0], report[1])
logfile = codecs.open(os.path.join(self.config.logdir,
'exceptions.log'),
'a', encoding='utf-8') # TODO: make not
# hardcoded
logfile.write(u'Signature: %s\n' % signature)
logfile.write(u'from %s at %s:\n' % (origin.sender,
str(datetime.now())))
logfile.write(u'Message was: <%s> %s\n' % (trigger.nick,
trigger.group(0)))
try:
logfile.write(trace.encode('utf-8'))
except:
logfile.write(trace)
logfile.write('----------------------------------------\n\n')
logfile.close()
except Exception as e:
stderr("Could not save full traceback!")
self.debug("core: error reporting", "(From: " + origin.sender +
"), can't save traceback: " + str(e), 'always')
if getattr(self.config, 'errors_to_sender', 'True') == 'True':
self.msg(origin.sender, signature)
except Exception as e:
if getattr(self.config, 'errors_to_sender', 'True') == 'True':
self.msg(origin.sender, "Got an error.")
self.debug("core: error reporting", "(From: " + origin.sender +
") " + str(e), 'always')
示例10: initiate_connect
def initiate_connect(self, host, port):
stderr('Connecting to %s:%s...' % (host, port))
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
if self.config.core.bind_host is not None:
self.socket.bind((self.config.core.bind_host, 0))
if self.config.core.use_ssl and has_ssl:
self.send = self._ssl_send
self.recv = self._ssl_recv
elif not has_ssl and self.config.core.use_ssl:
stderr('SSL is not avilable on your system, attempting connection '
'without it')
self.connect((host, port))
try:
asyncore.loop()
except KeyboardInterrupt:
print 'KeyboardInterrupt'
self.quit('KeyboardInterrupt')
示例11: initiate_connect
def initiate_connect(self, host, port):
stderr('Connecting to %s:%s...' % (host, port))
source_address = ((self.config.core.bind_host, 0)
if self.config.core.bind_address else None)
self.set_socket(socket.create_connection((host, port),
source_address=source_address))
if self.config.core.use_ssl and has_ssl:
self.send = self._ssl_send
self.recv = self._ssl_recv
elif not has_ssl and self.config.core.use_ssl:
stderr('SSL is not avilable on your system, attempting connection '
'without it')
self.connect((host, port))
try:
asyncore.loop()
except KeyboardInterrupt:
print 'KeyboardInterrupt'
self.quit('KeyboardInterrupt')
示例12: initiate_connect
def initiate_connect(self, host, port):
if self.verbose:
message = "Connecting to %s:%s..." % (host, port)
stderr(message)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
if self.config.core.bind_host is not None:
self.socket.bind((self.config.core.bind_host, 0))
if self.config.core.use_ssl and has_ssl:
self.send = self._ssl_send
self.recv = self._ssl_recv
elif not has_ssl and self.config.core.use_ssl:
stderr("SSL is not avilable on your system, attempting connection " "without it")
self.connect((host, port))
try:
asyncore.loop()
except KeyboardInterrupt:
print "KeyboardInterrupt"
self.quit("KeyboardInterrupt")
示例13: found_terminator
def found_terminator(self):
line = self.buffer
if line.endswith('\r'):
line = line[:-1]
self.buffer = u''
self.raw = line
# Break off IRCv3 message tags, if present
tags = {}
if line.startswith('@'):
tagstring, line = line.split(' ', 1)
for tag in tagstring[1:].split(';'):
tag = tag.split('=', 1)
if len(tag) > 1:
tags[tag[0]] = tag[1]
else:
tags[tag[0]] = None
if line.startswith(':'):
source, line = line[1:].split(' ', 1)
else:
source = None
if ' :' in line:
argstr, text = line.split(' :', 1)
args = argstr.split(' ')
args.append(text)
else:
args = line.split(' ')
text = args[-1]
self.last_ping_time = datetime.now()
if args[0] == 'PING':
self.write(('PONG', text))
elif args[0] == 'ERROR':
self.debug(__file__, text, 'always')
if self.hasquit:
self.close_when_done()
elif args[0] == '433':
stderr('Nickname already in use!')
self.handle_close()
origin = Origin(self, source, args, tags)
self.dispatch(origin, text, args)
示例14: run
def run(config):
if config.core.delay is not None:
delay = config.core.delay
else:
delay = 20
def signal_handler(sig, frame):
if sig == signal.SIGUSR1 or sig == signal.SIGTERM:
stderr('Got quit signal, shutting down.')
p.quit('Closing')
while True:
try:
p = bot.Skrizz(config)
if hasattr(signal, 'SIGUSR1'):
signal.signal(signal.SIGUSR1, signal_handler)
if hasattr(signal, 'SIGTERM'):
signal.signal(signal.SIGTERM, signal_handler)
p.run(config.core.host, int(config.core.port))
except KeyboardInterrupt:
break
except Exception, e:
trace = traceback.format_exc()
try:
stderr(trace)
except:
pass
logfile = open(os.path.join(config.logdir, 'exceptions.log'), 'a')
logfile.write('Critical exception in core')
logfile.write(trace)
logfile.write('----------------------------------------\n\n')
logfile.close()
os.unlink(config.pid_file_path)
os._exit(1)
if not isinstance(delay, int):
break
if p.hasquit or config.exit_on_error:
break
stderr('Warning: Disconnected. Reconnecting in %s seconds...' % delay)
time.sleep(delay)
示例15: setup
def setup(self):
stderr("\nWelcome to Willie. Loading modules...\n\n")
self.callables = set()
self.shutdown_methods = set()
filenames = self.config.enumerate_modules()
# Coretasks is special. No custom user coretasks.
this_dir = os.path.dirname(os.path.abspath(__file__))
filenames["coretasks"] = os.path.join(this_dir, "coretasks.py")
modules = []
error_count = 0
for name, filename in filenames.iteritems():
try:
module = imp.load_source(name, filename)
except Exception, e:
error_count = error_count + 1
filename, lineno = tools.get_raising_file_and_line()
rel_path = os.path.relpath(filename, os.path.dirname(__file__))
raising_stmt = "%s:%d" % (rel_path, lineno)
stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
else:
try:
if hasattr(module, "setup"):
module.setup(self)
self.register(vars(module))
modules.append(name)
except Exception, e:
error_count = error_count + 1
filename, lineno = tools.get_raising_file_and_line()
rel_path = os.path.relpath(filename, os.path.dirname(__file__))
raising_stmt = "%s:%d" % (rel_path, lineno)
stderr("Error in %s setup procedure: %s (%s)" % (name, e, raising_stmt))