def __init__(self, lazy_connection=False):
"""Constructor. Initialize the client.
Parameters:
lazy_connection: avoid testing if the connection is working while
initializing it.
"""
if self.address is None:
self.address = "mongodb://%s/" % FLAGS.rawdata_cache_server_address
for _ in range(FLAGS.mongodb_connection_retry):
self.client = self._connect(self.address, lazy_connection)
if self.client is not None:
break
else:
logging.critical("Unable to reach the MongoDB server.")
python类critical()的实例源码
def main():
"""
Called as a console_scripts entry point in setup.py. This function defines
the manubot command line script.
"""
# Track if message gets logged with severity of error or greater
# See https://stackoverflow.com/a/45446664/4651668
error_handler = errorhandler.ErrorHandler()
# Log to stderr
logger = logging.getLogger()
stream_handler = logging.StreamHandler(stream=sys.stderr)
stream_handler.setFormatter(logging.Formatter('## {levelname}\n{message}', style='{'))
logger.addHandler(stream_handler)
args = parse_arguments()
logger.setLevel(getattr(logging, args.log_level))
prepare_manuscript(args)
if error_handler.fired:
logging.critical('Failure: exiting with code 1 due to logged errors')
raise SystemExit(1)
def run(self):
'''
Called by twisted
'''
# load initial config
self.refresh_config()
if self.config is None:
logging.critical("cannot start due to error in config file")
return
# refresh and check status every event_period seconds
self.refresh_task = task.LoopingCall(self.refresh_loop)
refresh_deferred = self.refresh_task.start(self.config['event_period'], now=False)
refresh_deferred.addErrback(errorCallback)
# setup server for receiving blinded counts from the DC nodes and key shares from the SK nodes
listen_port = self.config['listen_port']
key_path = self.config['key']
cert_path = self.config['cert']
ssl_context = ssl.DefaultOpenSSLContextFactory(key_path, cert_path)
logging.info("Tally Server listening on port {}".format(listen_port))
reactor.listenSSL(listen_port, self, ssl_context)
reactor.run()
def execute_action(self, agent, action):
'''Have the agent carry out this action. If a move in a compass
direction, it may work, or may not, depending on whether there's an
obstacle there. The next percept (2nd element of tuple) will tell the
agent whether this happened.'''
agent._bump = False
if action in ['Left','Up','Right','Down']:
agent._bump = self.try_to_move_in_dir(agent, action)
elif action == 'Forward':
agent._bump = self.try_to_move_in_dir(agent,
agent._facing_direction)
elif action == 'TurnLeft':
directions = [ 'Up','Left','Down','Right','Up' ]
agent._facing_direction = directions[
directions.index(agent._facing_direction) + 1]
elif action == 'TurnRight':
directions = [ 'Up','Right','Down','Left','Up' ]
agent._facing_direction = directions[
directions.index(agent._facing_direction) + 1]
elif action == 'NoOp':
pass
else:
logging.critical("UNKNOWN action {}!!".format(action))
self.notify_observers(agent)
def run(self, edit):
self.label_ids = []
self.labels = []
region = self.view.sel()[0]
if region.begin() == region.end():
logging.critical("Please select the complete label")
return False
word = self.view.substr(region)
for po_file in INFOS.get_po_files():
for entry in po_file:
if entry.msgid.lower() == word.lower() and entry.msgctxt not in self.label_ids:
self.label_ids.append(entry.msgctxt)
self.labels.append(["%s (%s)" % (entry.msgid, entry.msgctxt), entry.comment])
self.labels.append("Create new label")
sublime.active_window().show_quick_panel(items=self.labels,
on_select=lambda s: self.on_done(s, region),
selected_index=0)
def check_busy(func):
"""
Decorator to check for self.is_busy
Only one of the decorated functions may run simultaniously
"""
@wraps(func)
def decorator(self, *args, **kwargs):
if self.is_busy:
logging.critical("Already busy. Please wait.")
return None
self.is_busy = True
try:
func(self, *args, **kwargs)
except Exception as e:
logging.critical(e)
self.is_busy = False
return decorator
gui.py 文件源码
项目:Grating_Advanced_Simulation_Platform
作者: GratingLaboratories
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def sel_handler(self):
if self.event.key == K_q:
if self.platform.focus in self.current_selection:
pos = self.current_selection.index(self.platform.focus)
index = (pos + 1) % len(self.current_selection)
self.platform.focus = self.current_selection[index]
elif len(self.current_selection) > 0:
self.platform.focus = self.current_selection[0]
else:
self.platform.focus = None
elif self.event.key == K_e:
if self.current_selection == self.platform.operands:
self.current_selection = self.platform.operators
elif self.current_selection == self.platform.operators:
self.current_selection = self.platform.operands
else:
logging.critical("Selection error!")
else:
pass
def InstallKVM():
"""Installs KVM packages."""
rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
if rc:
logging.critical('ERROR: Did not install KVM. Make sure hardware '
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
'AMD SVM).')
# TODO(navabi): Use modprobe kvm-amd on AMD processors.
rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
if rc:
logging.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure '
'hardware virtualization is enabled in BIOS.')
# Now check to ensure KVM acceleration can be used.
if not RunKvmOk():
logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
'AMD SVM).')
def SuppressLogging(level=logging.ERROR):
"""Momentarilly suppress logging events from all loggers.
TODO(jbudorick): This is not thread safe. Log events from other threads might
also inadvertently disappear.
Example:
with logging_utils.SuppressLogging():
# all but CRITICAL logging messages are suppressed
logging.info('just doing some thing') # not shown
logging.critical('something really bad happened') # still shown
Args:
level: logging events with this or lower levels are suppressed.
"""
logging.disable(level)
yield
logging.disable(logging.NOTSET)
def __init__(self, name='', desc='', config=None):
assert config is not None, logging.critical(
"Board.__init__: config is None")
# Configuration object used to read / write config values.
self.config = config
self.c = Colors()
self._name = name.lower()
self._desc = desc
self.thread = 0 # The thread that the user is viewing.
self.path = os.path.join(self.config.root, "boards", self._name)
self.index_path = os.path.join(self.path, "index")
self.boardlist_path = self.config.boardlist_path
if self.add_board():
logging.info(
'Board "/%s/ - %s" added succesfully.', self._name, self._desc)
def run(self, remoteName, remoteHost):
self.connect(remoteName, remoteHost)
self.__remoteOps = RemoteOperations(self.__smbConnection, self.__doKerberos, self.__kdcHost)
try:
self.__remoteOps.enableRegistry()
except Exception, e:
logging.debug(str(e))
logging.warning('Cannot check RemoteRegistry status. Hoping it is started...')
self.__remoteOps.connectWinReg()
try:
dce = self.__remoteOps.getRRP()
if self.__action == 'QUERY':
self.query(dce, self.__options.keyName)
else:
logging.error('Method %s not implemented yet!' % self.__action)
except (Exception, KeyboardInterrupt), e:
# import traceback
# traceback.print_exc()
logging.critical(str(e))
finally:
if self.__remoteOps:
self.__remoteOps.finish()
def do_put(self, s):
try:
params = s.split(' ')
if len(params) > 1:
src_path = params[0]
dst_path = params[1]
elif len(params) == 1:
src_path = params[0]
dst_path = ''
src_file = os.path.basename(src_path)
fh = open(src_path, 'rb')
dst_path = string.replace(dst_path, '/','\\')
import ntpath
pathname = ntpath.join(ntpath.join(self.__pwd,dst_path), src_file)
drive, tail = ntpath.splitdrive(pathname)
logging.info("Uploading %s to %s" % (src_file, pathname))
self.__transferClient.putFile(drive[:-1]+'$', tail, fh.read)
fh.close()
except Exception, e:
logging.critical(str(e))
pass
def openPipe(self, s, tid, pipe, accessMask):
pipeReady = False
tries = 50
while pipeReady is False and tries > 0:
try:
s.waitNamedPipe(tid,pipe)
pipeReady = True
except:
tries -= 1
time.sleep(2)
pass
if tries == 0:
logging.critical('Pipe not ready, aborting')
raise
fid = s.openFile(tid,pipe,accessMask, creationOption = 0x40, fileAttributes = 0x80)
return fid
def connectPipe(self):
try:
lock.acquire()
global dialect
self.server = SMBConnection('*SMBSERVER', self.transport.get_smb_connection().getRemoteHost(),
sess_port=self.port, preferredDialect=dialect)
user, passwd, domain, lm, nt, aesKey, TGT, TGS = self.credentials
self.server.login(user, passwd, domain, lm, nt)
lock.release()
self.tid = self.server.connectTree('IPC$')
self.server.waitNamedPipe(self.tid, self.pipe)
self.fid = self.server.openFile(self.tid,self.pipe,self.permissions, creationOption = 0x40, fileAttributes = 0x80)
self.server.setTimeout(1000000)
except Exception, e:
logging.critical("Something wen't wrong connecting the pipes(%s), try again" % self.__class__)
def do_put(self, s):
try:
params = s.split(' ')
if len(params) > 1:
src_path = params[0]
dst_path = params[1]
elif len(params) == 1:
src_path = params[0]
dst_path = ''
src_file = os.path.basename(src_path)
fh = open(src_path, 'rb')
dst_path = string.replace(dst_path, '/','\\')
import ntpath
pathname = ntpath.join(ntpath.join(self.__pwd,dst_path), src_file)
drive, tail = ntpath.splitdrive(pathname)
logging.info("Uploading %s to %s" % (src_file, pathname))
self.__transferClient.putFile(drive[:-1]+'$', tail, fh.read)
fh.close()
except Exception, e:
logging.critical(str(e))
pass
def openPipe(self, s, tid, pipe, accessMask):
pipeReady = False
tries = 50
while pipeReady is False and tries > 0:
try:
s.waitNamedPipe(tid,pipe)
pipeReady = True
except:
tries -= 1
time.sleep(2)
pass
if tries == 0:
logging.critical('Pipe not ready, aborting')
raise
fid = s.openFile(tid,pipe,accessMask, creationOption = 0x40, fileAttributes = 0x80)
return fid
def connectPipe(self):
try:
lock.acquire()
global dialect
self.server = SMBConnection('*SMBSERVER', self.transport.get_smb_connection().getRemoteHost(),
sess_port=self.port, preferredDialect=dialect)
user, passwd, domain, lm, nt, aesKey, TGT, TGS = self.credentials
self.server.login(user, passwd, domain, lm, nt)
lock.release()
self.tid = self.server.connectTree('IPC$')
self.server.waitNamedPipe(self.tid, self.pipe)
self.fid = self.server.openFile(self.tid,self.pipe,self.permissions, creationOption = 0x40, fileAttributes = 0x80)
self.server.setTimeout(1000000)
except:
logging.critical("Something wen't wrong connecting the pipes(%s), try again" % self.__class__)
def openPipe(self, s, tid, pipe, accessMask):
pipeReady = False
tries = 50
while pipeReady is False and tries > 0:
try:
s.waitNamedPipe(tid,pipe)
pipeReady = True
except:
tries -= 1
time.sleep(2)
pass
if tries == 0:
logging.critical('Pipe not ready, aborting')
raise
fid = s.openFile(tid,pipe,accessMask, creationOption = 0x40, fileAttributes = 0x80)
return fid
def do_get(self, src_path):
try:
if self.transferClient is None:
self.connect_transferClient()
import ntpath
filename = ntpath.basename(src_path)
fh = open(filename,'wb')
logging.info("Downloading %s\%s" % (self.share, src_path))
self.transferClient.getFile(self.share, src_path, fh.write)
fh.close()
except Exception, e:
logging.critical(str(e))
pass
self.send_data('\r\n')
def fetch_descriptors():
"""
Fetch and return relay descriptors.
"""
downloader = DescriptorDownloader(use_mirrors=True, timeout=20)
query = downloader.get_server_descriptors(validate=False)
descs = {}
try:
for desc in query.run():
descs[desc.fingerprint] = desc
log.info("Query took %0.2f seconds." % query.runtime)
except Exception as exc:
log.critical("Unable to retrieve server descriptors: %s" % exc)
log.info("Downloaded %d descs." % len(descs))
return descs
def build_syscall_name_map(self):
# Its a bit difficult to know where the system call table ends, here we
# do something kind of risky and read as long as translate_v2ksym
# returns something that looks like a system call handler.
mapping = {}
for i in range(0, MAX_SYSTEM_CALL_COUNT):
p_addr = self.sys_call_table_addr + (i * VOID_P_SIZE)
try:
addr = self.libvmi.read_addr_va(p_addr, 0)
symbol = self.libvmi.translate_v2ksym(addr)
except LibvmiError as error:
logging.critical("Failed to build syscall name map")
raise error
else:
if symbol is not None:
mapping[symbol] = i
else:
break
return mapping
def find_qemu_pid(vm_name):
"""
Find QEMU's PID that is associated with a given virtual machine
:param str vm_name: libvirt domain name
:rtype: int
"""
logging.info('Finding QEMU pid for domain %s', vm_name)
libvirt_vm_pid_file = '/var/run/libvirt/qemu/{}.pid'.format(vm_name)
try:
with open(libvirt_vm_pid_file, 'r') as f:
content = f.read()
pid = int(content)
return pid
except IOError:
for proc in psutil.process_iter():
cmdline = proc.cmdline()[1:]
if proc.name() == "qemu-system-x86_64" and \
next((True for k, v in zip(cmdline, cmdline[1:]) if k == "-name" and vm_name in v), False):
return proc.pid
logging.critical('Cannot find QEMU')
raise QEMUNotFoundError('Cannot find QEMU')
def post(self):
try:
url = self.get_argument("url", None)
if (not url): # take a default image
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Serpent_roi_bandes_grises_01.JPG/300px-Serpent_roi_bandes_grises_01.JPG"
call = dualprocessing.AsyncCall("predict", url=url)
response = yield computationBroker.submit_call_async(call)
if (response.Success):
self.write(response.Result)
else:
raise response.Error
except:
def lastExceptionString():
exc_type, ex, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
return "{0} in {1}:{2}".format(ex, fname, exc_tb.tb_lineno)
exmsg = lastExceptionString()
logging.critical(exmsg)
self.write(exmsg)
def start(self):
"""Application start point"""
try:
self._swd = swd.Swd(swd_frequency=self._swd_frequency)
self.print_device_info()
self.process_actions()
except swd.stlinkcom.StlinkComNotFound:
logging.error("ST-Link not connected.")
except PyswdException as err:
logging.error("pyswd error: %s.", err)
except swd.stlink.StlinkException as err:
logging.critical("Stlink error: %s.", err)
except swd.stlinkcom.StlinkComException as err:
logging.critical("StlinkCom error: %s.", err)
else:
return 0
return 1
def InstallKVM():
"""Installs KVM packages."""
rc = cmd_helper.RunCmd(['sudo', 'apt-get', 'install', 'kvm'])
if rc:
logging.critical('ERROR: Did not install KVM. Make sure hardware '
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
'AMD SVM).')
# TODO(navabi): Use modprobe kvm-amd on AMD processors.
rc = cmd_helper.RunCmd(['sudo', 'modprobe', 'kvm-intel'])
if rc:
logging.critical('ERROR: Did not add KVM module to Linux Kernel. Make sure '
'hardware virtualization is enabled in BIOS.')
# Now check to ensure KVM acceleration can be used.
if not RunKvmOk():
logging.critical('ERROR: Can not use KVM acceleration. Make sure hardware '
'virtualization is enabled in BIOS (i.e. Intel VT-x or '
'AMD SVM).')
def SuppressLogging(level=logging.ERROR):
"""Momentarilly suppress logging events from all loggers.
TODO(jbudorick): This is not thread safe. Log events from other threads might
also inadvertently dissapear.
Example:
with logging_utils.SuppressLogging():
# all but CRITICAL logging messages are suppressed
logging.info('just doing some thing') # not shown
logging.critical('something really bad happened') # still shown
Args:
level: logging events with this or lower levels are suppressed.
"""
logging.disable(level)
yield
logging.disable(logging.NOTSET)
def check_config(path):
if not os.path.exists(path):
logging.warning('Config file %s doesn\'t exist. Trying to create'
% (path))
if create_config(path):
logging.warning('Created new config file %s. Edit it first to '
'configure your settings correctly & then run '
'program again' % (path))
return False
else:
global cfg
try:
with open(path, "r") as config_file:
cfg = yaml.load(config_file)
except IOError as e:
logging.critical('Error while reading config_file %s: %s'
% (path, str(e)))
logging.critical('Check the config file path and try again')
return False
if not cfg or 'filters' not in cfg or not cfg['filters']:
logging.critical('Empty or malformed config_file %s' % (path))
logging.critical('Check the config file path and try again')
return False
return True
def main():
# Configure the logging system
logging.basicConfig(
filename='app.log',
level=logging.ERROR
)
# Variables (to make the calls that follow work)
hostname = 'www.python.org'
item = 'spam'
filename = 'data.csv'
mode = 'r'
# Example logging calls (insert into your program)
logging.critical('Host %s unknown', hostname)
logging.error("Couldn't find %r", item)
logging.warning('Feature is deprecated')
logging.info('Opening file %r, mode=%r', filename, mode)
logging.debug('Got here')
def main():
# Configure the logging system
logging.config.fileConfig('logconfig.ini')
# Variables (to make the calls that follow work)
hostname = 'www.python.org'
item = 'spam'
filename = 'data.csv'
mode = 'r'
# Example logging calls (insert into your program)
logging.critical('Host %s unknown', hostname)
logging.error("Couldn't find %r", item)
logging.warning('Feature is deprecated')
logging.info('Opening file %r, mode=%r', filename, mode)
logging.debug('Got here')
def __init__(self):
self.AsciiOP = False # change to False for unicode output
InvertSymmetricalComponents = True
self.SymAng = math.pi * (2. / 3.)
if InvertSymmetricalComponents == True:
self.SymAng = -self.SymAng
logging.basicConfig(filename = 'PMU2CSV_logfile.log', level = logging.INFO, filemode='w', format='%(asctime)s %(message)s')
logging.critical('-------------------------------------')
logging.critical('Script started at ' + time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) + 'GMT')
threading.Thread.__init__(self)
self.PMUip = "192.168.0.10"
self.PMUport = 4712
self.PMUnumber = 20
self.CSVlabel = "PMUtoCSV_scriptDefault_"
self.WriteEvery = 5
self.CloseFileAfter = 3600
self.timeSF = 13
self.dataSF = 7