本文整理汇总了Python中systematic.log.Logger类的典型用法代码示例。如果您正苦于以下问题:Python Logger类的具体用法?Python Logger怎么用?Python Logger使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Logger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AuthorizedKeys
class AuthorizedKeys(list):
"""
Parser for OpenSSH authorized_keys file contents
"""
def __init__(self, path=DEFAULT_AUTHORIZED_KEYS):
self.log = Logger().default_stream
self.path = path
self.load()
def load(self):
"""
Load authorized keys file, discarding earlier data in the object.
SSH v1 authorized_keys line: options, bits, exponent, modulus, comment)
SSH v2 authorized_keys line: options, keytype, base64-encoded key, comment)'
See man sshd for details.
"""
self.__delslice__(0, len(self))
if not os.path.isfile(self.path):
self.log.debug('No such file: {0}'.format(self.path))
return
for line in [l.rstrip() for l in open(self.path, 'r').readlines()]:
if line.startswith('#') or line.strip() == '':
continue
try:
self.append(OpenSSHPublicKey(line))
except SSHKeyError:
pass
示例2: KnownHosts
class KnownHosts(list):
"""
Parser for OpenSSH known_hosts file contents
"""
def __init__(self, path=DEFAULT_KNOWN_HOSTS, fingerprint_hash=None):
self.log = Logger().default_stream
self.path = path
self.fingerprint_hash = fingerprint_hash
self.load()
def load(self):
"""
Load known hosts file, discarding earlier data in the object.
"""
del self[0:len(self)]
if not os.path.isfile(self.path):
self.log.debug('No such file: {}'.format(self.path))
return
for line in [l.rstrip() for l in open(self.path, 'r').readlines()]:
if line.startswith('#') or line.strip() == '':
continue
# Strip list of hosts from line
hosts, key = line.split(None, 1)
hosts = hosts.split(',')
try:
key = KnownHostsEntry(key)
if key not in self:
self.append(key)
else:
key = self[self.index(key)]
key.add_hosts(hosts)
except SSHKeyError:
pass
def save(self, path=None):
"""Save known hosts
Saves known hosts file, merging same keys to single line
"""
if path is None:
path = self.path
try:
with open(path, 'w') as fd:
for entry in self:
fd.write('{}\n'.format(entry))
except Exception as e:
raise SSHKeyError('Error writing {}: {}'.format(path, e))
def find_host_key(self, value):
"""Find key for hostname
"""
for key in self:
if value in key.hosts:
return key
return None
示例3: __init__
def __init__(self, name=None, description=None, epilog=None, debug_flag=True):
self.name = os.path.basename(sys.argv[0])
setproctitle('%s %s' % (self.name, ' '.join(sys.argv[1:])))
signal.signal(signal.SIGINT, self.SIGINT)
reload(sys)
sys.setdefaultencoding('utf-8')
if name is None:
name = self.name
# Set to True to avoid any messages from self.message to be printed
self.silent = False
self.logger = Logger(self.name)
self.log = self.logger.default_stream
self.subcommand_parser = None
self.parser = argparse.ArgumentParser(
prog=name,
description=description,
formatter_class=argparse.RawTextHelpFormatter,
epilog=epilog,
add_help=True,
conflict_handler='resolve',
)
if debug_flag:
self.parser.add_argument('--debug', action='store_true', help='Show debug messages')
self.parser.add_argument('--insecure', action='store_false', help='No HTTPS certificate validation')
self.parser.add_argument('-B', '--browser',
choices=('chrome','chromium','firefox'),
help='Browser for cookie stealing'
)
示例4: __init__
def __init__(self, oid):
self.log = Logger('snmp').default_stream
self.oid = self.__format_oid__(oid)
self.oid_string = self.__format_oid_string__(oid)
self._parent = None
self._next = None
示例5: __init__
def __init__(self, config, name, **kwargs):
self.log = Logger().default_stream
self.name = name
self.description = name
self.connect_command = DEFAULT_CONNECT_COMMAND
self.command_separator = DEFAULT_COMMAND_SEPARATOR
self.update_commands = []
self.servers = []
for k in ( 'description', 'command_separator', ):
if k in kwargs:
setattr(self, k, kwargs[k])
if 'connect' in kwargs:
self.connect_command = kwargs['connect']
if 'commands' in kwargs:
self.update_commands = kwargs['commands']
if 'servers' in kwargs:
names = kwargs['servers']
if not isinstance(names, list):
names = [names]
for name in names:
self.servers.append(Server(self, name))
self.modified = False
示例6: __init__
def __init__(self, path):
self.operating_systems = []
self.servers = []
self.path = path
self.log = Logger().default_stream
if os.path.isfile(self.path):
self.load()
示例7: __init__
def __init__(self,src,dst,flags=DEFAULT_RSYNC_FLAGS,output_format=DEFAULT_OUTPUT_FORMAT):
self.log = Logger('rsync').default_stream
self.src = src
self.dst = dst
self.flags = flags
self.output_format = output_format
cmd = CommandPathCache().which('rsync')
if cmd is None:
raise RsyncError('No such command: rsync')
self.command = [cmd] + flags + [ self.output_format, '{0}'.format(src),'{0}'.format(dst) ]
示例8: SSHConfig
class SSHConfig(dict):
def __init__(self, path=None):
self.defaults = {}
self.patterns = []
self.log = Logger().default_stream
self.path = path is not None and path or os.path.expanduser('~/.ssh/config')
self.reload()
def reload(self):
self.clear()
if not os.path.isfile(self.path):
self.log.debug('No such file: {}'.format(self.path))
return
with open(self.path, 'r') as fd:
host = None
for line in [x.strip() for x in fd.readlines()]:
if line == '' or line.startswith('#'):
continue
if line[:5] == 'Host ':
host = SSHConfigHostPattern(self, line[5:])
for pattern in host.patterns:
self[pattern] = host
self.patterns.append(host)
else:
host.parse(line)
if '*' in self.keys():
self.defaults.update(self.pop('*').items())
def keys(self):
return [k for k in sorted(super(SSHConfig, self).keys())]
def items(self):
return [(k, self[k]) for k in self.keys()]
def values(self):
return [self[k] for k in self.keys()]
示例9: SSHConfig
class SSHConfig(dict):
def __init__(self, path=None):
self.defaults = {}
self.log = Logger().default_stream
self.path = path is not None and path or os.path.expanduser('~/.ssh/config')
self.reload()
def reload(self):
self.clear()
if not os.path.isfile(self.path):
self.log.debug('No such file: %s' % self.path)
return
with open(self.path, 'r') as fd:
host = None
for l in [x.strip() for x in fd.readlines()]:
if l=='' or l.startswith('#'):
continue
if l[:5]=='Host ':
host = SSHConfigHost(self, l[5:])
self[host.name] = host
else:
host.parse(l)
if '*' in self.keys():
self.defaults.update(self.pop('*').items())
def keys(self):
return [k for k in sorted(dict.keys(self))]
def items(self):
return [(k, self[k]) for k in self.keys()]
def values(self):
return [self[k] for k in self.keys()]
示例10: UserSSHKeys
class UserSSHKeys(dict):
"""
List of user configured SSH keys to process
"""
def __init__(self, authorized_keys=DEFAULT_AUTHORIZED_KEYS):
self.log = Logger().default_stream
self.__parse_user_keyfiles()
self.authorized_keys = AuthorizedKeys(authorized_keys)
def __parse_user_keyfiles(self):
"""
Parses any normal user keyfiles in ~/.ssh directory automatically.
These keys are marked by default not to be automatically loaded:
to enable, add key path to ~/.ssh/sshkeys.conf
"""
user_sshdir = os.path.expanduser('~/.ssh')
if not os.path.isdir(user_sshdir):
return
paths = filter(lambda x:
os.path.isfile(x),
[os.path.join(user_sshdir, x) for x in filter(lambda x:
x not in SSH_CONFIG_FILES and os.path.splitext(x)[1]!='.pub',
os.listdir(user_sshdir)
)]
)
for path in paths:
try:
sshkey = SSHKeyFile(self, path)
except SSHKeyError, emsg:
self.log.debug(emsg)
continue
self[sshkey.path] = sshkey
示例11: RsyncCommand
class RsyncCommand(object):
"""
Wrapper to execute rsync to target nicely from python
"""
def __init__(self, src, dst,
flags=DEFAULT_RSYNC_FLAGS,
output_format=DEFAULT_OUTPUT_FORMAT):
self.log = Logger('rsync').default_stream
self.src = src
self.dst = dst
self.flags = flags
self.output_format = output_format
cmd = CommandPathCache().which('rsync')
if cmd is None:
raise RsyncError('No such command: rsync')
self.command = [cmd] + flags + [self.output_format, '{0}'.format(src), '{0}'.format(dst)]
def __str__(self):
return ' '.join(self.command)
def run(self, verbose=False):
"""
Run the rsync command specific in __init__()
"""
if not verbose and self.log.level == logging.DEBUG:
verbose = True
try:
p = Popen(self.command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
self.log.debug('Running: {0}'.format(self))
rval = None
while rval is None:
if verbose:
while True:
line = p.stdout.readline()
if line == '':
break
sys.stdout.write('{0}\n'.format(line.rstrip()))
time.sleep(0.2)
rval = p.poll()
self.log.debug('Return code: {0}'.format(rval))
if rval != 0:
raise RsyncError('Error running command {0}: {1}'.format(self, p.stderr.read()))
except KeyboardInterrupt:
self.log.debug('Rsync interrupted')
raise KeyboardInterrupt
示例12: __init__
def __init__(self, authorized_keys=DEFAULT_AUTHORIZED_KEYS):
self.log = Logger().default_stream
self.__parse_user_keyfiles()
self.authorized_keys = AuthorizedKeys(authorized_keys)
示例13: OperatingSystemGroup
class OperatingSystemGroup(object):
"""
Group of operating systems in configuration file
"""
def __init__(self, config, name, **kwargs):
self.log = Logger().default_stream
self.name = name
self.description = name
self.connect_command = DEFAULT_CONNECT_COMMAND
self.command_separator = DEFAULT_COMMAND_SEPARATOR
self.update_commands = []
self.servers = []
for k in ( 'description', 'command_separator', ):
if k in kwargs:
setattr(self, k, kwargs[k])
if 'connect' in kwargs:
self.connect_command = kwargs['connect']
if 'commands' in kwargs:
self.update_commands = kwargs['commands']
if 'servers' in kwargs:
names = kwargs['servers']
if not isinstance(names, list):
names = [names]
for name in names:
self.servers.append(Server(self, name))
self.modified = False
def __repr__(self):
return '%s: %s (%d servers)' % (self.name,self.description,len(self.servers))
@property
def command_separator(self):
return self._command_separator
@command_separator.setter
def command_separator(self, value):
self._command_separator = value
self.modified = True
@property
def connect_command(self):
return self._connect_command
@connect_command.setter
def connect_command(self, value):
if isinstance(value, basestring):
value = value.split()
self._connect_command = value
self.modified = True
@property
def update_command(self):
return self._update_commands
@update_command.setter
def update_commands(self, value):
if isinstance(value, basestring):
value = [value]
self._update_commands = value
self.modified = True
@property
def descripion(self):
return self._descripion
@descripion.setter
def descripion(self, value):
self._descripion = value
self.modified = True
def add_server(self, name):
if name in [s.name for s in self.servers]:
self.log.debug('Error adding: server already in group: %s' % name)
return
self.servers.append(Server(self, name))
self.modified = True
def remove_server(self,name):
try:
server = [s for s in self.servers if s.name==name][0]
self.servers.remove(server)
self.modified = True
except IndexError:
self.log.debug('Error removing: server not in group: %s' % name)
return
示例14: Script
class Script(object):
"""
Class for common CLI tool script
"""
def __init__(self, name=None, description=None, epilog=None, debug_flag=True):
self.name = os.path.basename(sys.argv[0])
signal.signal(signal.SIGINT, self.SIGINT)
if has_setproctitle:
setproctitle('{0} {1}'.format(self.name, ' '.join(sys.argv[1:])))
if sys.version_info.major < 3:
reload(sys) # noqa
sys.setdefaultencoding('utf-8')
if name is None:
name = self.name
# Set to True to avoid any messages from self.message to be output
self.silent = False
self.logger = Logger(self.name)
self.log = self.logger.default_stream
self.parser = argparse.ArgumentParser(
prog=name,
description=description,
formatter_class=argparse.RawTextHelpFormatter,
epilog=epilog,
add_help=True,
conflict_handler='resolve',
)
if debug_flag:
self.parser.add_argument('--debug', action='store_true', help='Show debug messages')
self.subcommand_parser = None
def SIGINT(self, signum, frame):
"""
Parse SIGINT signal by quitting the program cleanly with exit code 1
"""
for t in [t for t in threading.enumerate() if t.name != 'MainThread']:
if hasattr(t, 'stop') and callable(t.stop):
t.stop()
for t in [t for t in threading.enumerate() if t.name != 'MainThread']:
t.join()
self.exit(1)
def wait(self, poll_interval=1):
"""
Wait for running threads to finish.
Poll interval is time to wait between checks for threads
"""
while True:
active = [t for t in threading.enumerate() if t.name != 'MainThread']
if not len(active):
break
self.log.debug('Waiting for {0:d} threads'.format(len(active)))
time.sleep(poll_interval)
def exit(self, value=0, message=None):
"""
Exit the script with given exit value.
If message is not None, it is output to stdout
"""
if isinstance(value, bool):
if value:
value = 0
else:
value = 1
else:
try:
value = int(value)
if value < 0 or value > 255:
raise ValueError
except ValueError:
value = 1
if message is not None:
self.message(message)
for t in [t for t in threading.enumerate() if t.name != 'MainThread']:
if hasattr(t, 'stop') and callable(t.stop):
t.stop()
while True:
active = [t for t in threading.enumerate() if t.name != 'MainThread']
if not len(active):
break
time.sleep(0.1)
sys.exit(value)
def message(self, message):
if self.silent:
return
sys.stdout.write('{0}\n'.format(message))
#.........这里部分代码省略.........
示例15: SNMPAgent
class SNMPAgent(Script):
"""SNMP Agent implementation
SNMP agent daemon for net-snmpd pass_persist mode
Example usage:
agent = SNMPAgent('.1.2.3.4')
tree = agent.register_tree('1.2.3.4.1')
tree.register('1.2.3.4.1.1', 'integer', 1)
tree.register('1.2.3.4.1.2', 'integer', 2)
agent.register_tree('1.2.3.4.2')
agent.register('1.2.3.4.2.1', 'integer', 1)
agent.register('1.2.3.4.2.4', 'integer', 4)
agent.register('1.2.3.4.2.3', 'integer', 3)
agent.register('1.2.3.4.2.2', 'integer', 2)
tree = agent.register_tree('1.2.3.4.3')
tree.register('1.2.3.4.3.1.1', 'integer', 1)
tree.add_values('string', [x for x in string.digits[2:]])
tree = agent.register_tree('1.2.3.4.4')
tree.add_prefix_map([
{ 'oid': '1.2.3.4.4.1', 'key': 'string', 'values': [x for x in string.letters[:10]]},
{ 'oid': '1.2.3.4.4.2', 'key': 'integer', 'values': [x for x in string.digits[:10]]},
])
agent.run()
"""
def __init__(self, oid, reload_interval=60):
super(SNMPAgent, self).__init__()
self.log = Logger('snmp').default_stream
self.reload_interval = reload_interval
self.last_reload = None
self.args = None
self.add_argument('-g', '--get', help='SNMP GET request')
self.add_argument('-n', '--next', help='SNMP GET request')
self.add_argument('-t', '--tree', action='store_true', help='Show OID tree')
self.tree = Tree(oid)
def __SIGHUP__(self, signum, frame):
"""
Signal handler to reload configuration. Note this requires also the
IOError processing in main input loop below
"""
self.log.debug('Reloading from signal')
self.reload()
def parse_args(self):
if self.args is None:
self.args = super(SNMPAgent, self).parse_args()
return self.args
def register_tree(self, oid):
return self.tree.add(Tree(oid))
def register(self, oid, key, value):
return self.tree.add(Item(oid, key, value))
def GET(self, oid):
"""GET from agent
Return given OID from agent tree or None
"""
try:
return self.tree.GET(oid)
except SNMPError as e:
self.log.debug(e)
return None
def NEXT(self, oid):
"""NEXT from agent
Return given NEXT from agent tree or None
"""
try:
return self.tree.NEXT(oid)
except SNMPError as e:
self.log.debug(e)
return None
def SET(self, oid, value):
"""SET registered OID
Set value for registered OID in agent tree.
Note: By default items registered are readonly.
"""
return self.tree.SET(oid, value)
def clear(self):
return self.tree.clear()
#.........这里部分代码省略.........