本文整理汇总了Python中mysql.utilities.common.format.print_list函数的典型用法代码示例。如果您正苦于以下问题:Python print_list函数的具体用法?Python print_list怎么用?Python print_list使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了print_list函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: show_file_statistics
def show_file_statistics(file_name, wild=False, out_format="GRID"):
"""Show file statistics for file name specified
file_name[in] target file name and path
wild[in] if True, get file statistics for all files with prefix of
file_name. Default is False
out_format[in] output format to print file statistics. Default is GRID.
"""
def _get_file_stats(path, file_name):
"""Return file stats
"""
stats = os.stat(os.path.join(path, file_name))
return ((file_name, stats.st_size, time.ctime(stats.st_ctime),
time.ctime(stats.st_mtime)))
columns = ["File", "Size", "Created", "Last Modified"]
rows = []
path, filename = os.path.split(file_name)
if wild:
for _, _, files in os.walk(path):
for f in files:
if f.startswith(filename):
rows.append(_get_file_stats(path, f))
else:
rows.append(_get_file_stats(path, filename))
print_list(sys.stdout, out_format, columns, rows)
示例2: show_users
def show_users(src_val, verbosity, fmt, dump=False):
"""Show all users except root and anonymous users on the server.
src_val[in] a dictionary containing connection information for the
source including:
(user, password, host, port, socket)
verbosty[in] level of information to display
fmt[in] format of output
dump[in] if True, dump the grants for all users
default = False
"""
conn_options = {"version": "5.1.0"}
servers = connect_servers(src_val, None, conn_options)
source = servers[0]
if verbosity <= 1:
_QUERY = """
SELECT user, host FROM mysql.user
WHERE user.user != ''
"""
cols = ("user", "host")
else:
_QUERY = """
SELECT user.user, user.host, db FROM mysql.user LEFT JOIN mysql.db
ON user.user = db.user AND user.host = db.host
WHERE user.user != ''
"""
cols = ("user", "host", "database")
users = source.exec_query(_QUERY)
print "# All Users:"
print_list(sys.stdout, fmt, cols, users)
if dump:
for user in users:
_show_user_grants(source, None, "'%s'@'%s'" % user[0:2], verbosity)
示例3: execute
def execute(self, connections, **kwrds):
"""Execute the search for processes, queries, or connections
This method searches for processes, queriers, or connections to
either kill or display the matches for one or more servers.
connections[in] list of connection parameters
kwrds[in] dictionary of options
output file stream to display information
default = sys.stdout
connector connector to use
default = mysql.connector
format format for display
default = GRID
"""
output = kwrds.get('output', sys.stdout)
connector = kwrds.get('connector', mysql.connector)
fmt = kwrds.get('format', "grid")
charset = kwrds.get('charset', None)
headers = ("Connection", "Id", "User", "Host", "Db",
"Command", "Time", "State", "Info")
entries = []
# Build SQL statement
for info in connections:
conn = parse_connection(info)
if not conn:
msg = "'%s' is not a valid connection specifier" % (info,)
raise FormatError(msg)
if charset:
conn['charset'] = charset
info = conn
connection = connector.connect(**info)
if not charset:
# If no charset provided, get it from the
# "character_set_client" server variable.
cursor = connection.cursor()
cursor.execute("SHOW VARIABLES LIKE 'character_set_client'")
res = cursor.fetchall()
connection.set_charset_collation(charset=str(res[0][1]))
cursor.close()
cursor = connection.cursor()
cursor.execute(self.__select)
for row in cursor:
if KILL_QUERY in self.__actions:
cursor.execute("KILL {0}".format(row[0]))
if KILL_CONNECTION in self.__actions:
cursor.execute("KILL {0}".format(row[0]))
if PRINT_PROCESS in self.__actions:
entries.append(tuple([_spec(info)] + list(row)))
# If output is None, nothing is printed
if len(entries) > 0 and output:
entries.sort(key=lambda fifth: fifth[5])
print_list(output, fmt, headers, entries)
elif PRINT_PROCESS in self.__actions:
raise EmptyResultError("No matches found")
示例4: _get_formatted_rows
def _get_formatted_rows(rows, table, format="GRID"):
"""Get a printable representation of the data rows
This method generates a formatted view of the rows from a table. The output
format can be in one of GRID, CSV, TAB, or VERTICAL. This output is
returned as a list of strings for use in storing the output for later
presentation.
rows[in] missing rows
table[in] a Table instance of the table
obj1_str[in] full table name for base table
obj2_str[in] full table name for other table
format[in] format to print
Returns list of formatted rows
"""
import os
import tempfile
from mysql.utilities.common.format import print_list
result_rows = []
outfile = tempfile.TemporaryFile()
print_list(outfile, format, table.get_col_names(), rows)
outfile.seek(0)
for line in outfile.readlines():
result_rows.append(line.strip("\n"))
return result_rows
示例5: show_database_usage
def show_database_usage(server, datadir, dblist, options):
"""Show database usage.
Display a list of databases and their disk space usage. The method
accepts a list of databases to list or None or [] for all databases.
server[in] Connected server to operate against
datadir[in] The datadir for the server
dblist[in] List of databases
options[in] Required options for operation: format, no_headers,
verbosity, have_read, include_empty
returns True or exception on error
"""
from mysql.utilities.common.format import print_list
format = options.get("format", "grid")
no_headers = options.get("no_headers", False)
verbosity = options.get("verbosity", 0)
have_read = options.get("have_read", False)
include_empty = options.get("do_empty", True)
do_all = options.get("do_all", True)
quiet = options.get("quiet", False)
if verbosity is None:
verbosity = 0
locale.setlocale(locale.LC_ALL, '')
# Check to see if we're doing all databases.
if len(dblist) > 0:
include_list = "("
stop = len(dblist)
for i in range(0,stop):
include_list += "'%s'" % dblist[i]
if i < stop-1:
include_list += ", "
include_list += ")"
where_clause = "WHERE table_schema IN %s" % include_list
where_clause += " AND table_schema != 'INFORMATION_SCHEMA'"
else:
where_clause = "WHERE table_schema != 'INFORMATION_SCHEMA'"
res = server.exec_query(_QUERY_DBSIZE % where_clause)
# Get list of databases with sizes and formatted when necessary
columns, rows, db_total = _build_db_list(server, res, dblist, datadir,
format == "grid",
have_read, verbosity,
include_empty or do_all)
if not quiet:
print "# Database totals:"
print_list(sys.stdout, format, columns, rows, no_headers)
if not quiet:
_print_size("\nTotal database disk usage = ", db_total)
print
return True
示例6: show_logfile_usage
def show_logfile_usage(server, options):
"""Show log file disk space usage.
Display log file information if logs are turned on.
server[in] Connected server to operate against
datadir[in] The datadir for the server
options[in] Required options for operation: format, no_headers
return True or raise exception on error
"""
from mysql.utilities.common.format import print_list
format = options.get("format", "grid")
no_headers = options.get("no_headers", False)
verbosity = options.get("verbosity", 0)
have_read = options.get("have_read", False)
quiet = options.get("quiet", False)
if not quiet:
print "# Log information."
total = 0
_LOG_NAMES = [
('general_log', '_file'), ('slow_query_log', '_file'),
('log_error', '')
]
logs = []
for log_name in _LOG_NAMES:
log, size = _get_log_information(server, log_name[0], log_name[1])
if log is not None:
logs.append((log, size))
total += size
fmt_logs = []
columns = ['log_name', 'size']
if len(logs) > 0:
if format == 'grid':
max_col = _get_formatted_max_width(logs, columns, 1)
if max_col < len('size'):
max_col = len('size')
size = "{0:>{1}}".format('size', max_col)
columns = ['log_name',size]
for row in logs:
# Add commas
size = locale.format("%d", row[1], grouping=True)
# Make justified strings
size = "{0:>{1}}".format(size, max_col)
fmt_logs.append((row[0], size))
else:
fmt_logs = logs
print_list(sys.stdout, format, columns, fmt_logs, no_headers)
if not quiet:
_print_size("\nTotal size of logs = ", total)
print
return True
示例7: print_charsets
def print_charsets(self):
"""Print the character set list
"""
print_list(sys.stdout, self.format,
["id", "character_set_name", "collation_name",
"maxlen", "is_default"],
self.charset_map)
print len(self.charset_map), "rows in set."
示例8: execute
def execute(self, connections, output=sys.stdout,
connector=mysql.connector, **kwrds):
"""Execute the search for objects
This method searches for objects that match a search criteria for
one or more servers.
connections[in] list of connection parameters
output[in] file stream to display information
default = sys.stdout
connector[in] connector to use
default = mysql.connector
kwrds[in] dictionary of options
format format for display
default = GRID
"""
fmt = kwrds.get('format', "grid")
charset = kwrds.get('charset', None)
ssl_opts = kwrds.get('ssl_opts', {})
entries = []
for info in connections:
conn = parse_connection(info)
if not conn:
msg = "'%s' is not a valid connection specifier" % (info,)
raise FormatError(msg)
if charset:
conn['charset'] = charset
info = conn
conn['host'] = conn['host'].replace("[", "")
conn['host'] = conn['host'].replace("]", "")
if connector == mysql.connector:
set_ssl_opts_in_connection_info(ssl_opts, info)
connection = connector.connect(**info)
if not charset:
# If no charset provided, get it from the
# "character_set_client" server variable.
cursor = connection.cursor()
cursor.execute("SHOW VARIABLES LIKE 'character_set_client'")
res = cursor.fetchall()
connection.set_charset_collation(charset=str(res[0][1]))
cursor.close()
cursor = connection.cursor()
cursor.execute(self.__sql)
entries.extend([tuple([_spec(info)] + list(row))
for row in cursor])
headers = ["Connection"]
headers.extend(col[0].title() for col in cursor.description)
if len(entries) > 0 and output:
print_list(output, fmt, headers, entries)
else:
msg = "Nothing matches '%s' in any %s" % \
(self.__pattern, _join_words(self.__types, conjunction="or"))
raise EmptyResultError(msg)
示例9: show_logfile_usage
def show_logfile_usage(server, options):
"""Show log file disk space usage.
Display log file information if logs are turned on.
server[in] Connected server to operate against
datadir[in] The datadir for the server
options[in] Required options for operation: format, no_headers
return True or raise exception on error
"""
fmt = options.get("format", "grid")
no_headers = options.get("no_headers", False)
is_remote = options.get("is_remote", False)
quiet = options.get("quiet", False)
if not quiet:
print "# Log information."
total = 0
_LOG_NAMES = [("general_log", "_file"), ("slow_query_log", "_file"), ("log_error", "")]
logs = []
for log_name in _LOG_NAMES:
(log, size) = _get_log_information(server, log_name[0], log_name[1], is_remote)
if log is not None:
logs.append((log, size))
total += size
fmt_logs = []
columns = ["log_name", "size"]
if len(logs) > 0:
if fmt == "grid":
max_col = _get_formatted_max_width(logs, columns, 1)
if max_col < len("size"):
max_col = len("size")
size = "{0:>{1}}".format("size", max_col)
columns = ["log_name", size]
for row in logs:
# Add commas
size = locale.format("%d", row[1], grouping=True)
# Make justified strings
size = "{0:>{1}}".format(size, max_col)
fmt_logs.append((row[0], size))
else:
fmt_logs = logs
print_list(sys.stdout, fmt, columns, fmt_logs, no_headers)
if not quiet:
_print_size("\nTotal size of logs = ", total)
print
return True
示例10: show_topology
def show_topology(master_vals, options=None):
"""Show the slaves/topology map for a master.
This method find the slaves attached to a server if it is a master. It
can also discover the replication topology if the recurse option is
True (default = False).
It prints a tabular list of the master(s) and slaves found. If the
show_list option is True, it will also print a list of the output
(default = False).
master_vals[in] Master connection in form user:[email protected]:port:socket
or login-path:port:socket.
options[in] dictionary of options
recurse If True, check each slave found for additional slaves
Default = False
prompt_user If True, prompt user if slave connection fails with
master connection parameters
Default = False
num_retries Number of times to retry a failed connection attempt
Default = 0
quiet if True, print only the data
Default = False
format Format of list
Default = Grid
width width of report
Default = 75
max_depth maximum depth of recursive search
Default = None
"""
if options is None:
options = {}
topo = TopologyMap(master_vals, options)
topo.generate_topology_map(options.get('max_depth', None))
if not options.get("quiet", False) and topo.depth():
print "\n# Replication Topology Graph"
if not topo.slaves_found():
print "No slaves found."
topo.print_graph()
print
if options.get("show_list", False):
from mysql.utilities.common.format import print_list
# make a list from the topology
topology_list = topo.get_topology_map()
print_list(sys.stdout, options.get("format", "GRID"),
["Master", "Slave"], topology_list, False, True)
示例11: _log_master_status
def _log_master_status(self, master):
"""Logs the master information.
master[in] Master server instance.
This method logs the master information from SHOW MASTER STATUS.
"""
# If no master present, don't print anything.
if master is None:
return
print("#")
self._report("# {0}:".format("Current Master Information"),
logging.INFO)
try:
status = master.get_status()[0]
except UtilError:
msg = "Cannot get master status"
self._report(msg, logging.ERROR, False)
raise UtilRplError(msg)
cols = ("Binary Log File", "Position", "Binlog_Do_DB",
"Binlog_Ignore_DB")
rows = (status[0] or "N/A", status[1] or "N/A", status[2] or "N/A",
status[3] or "N/A")
print_list(sys.stdout, self.format, cols, [rows])
self._report("# {0}".format(
", ".join(["{0}: {1}".format(*item) for item in zip(cols, rows)]),
), logging.INFO, False)
# Display gtid executed set
master_gtids = []
for gtid in status[4].split("\n"):
if gtid:
# Add each GTID to a tuple to match the required format to
# print the full GRID list correctly.
master_gtids.append((gtid.strip(","),))
try:
if len(master_gtids) > 1:
gtid_executed = "{0}[...]".format(master_gtids[0][0])
else:
gtid_executed = master_gtids[0][0]
except IndexError:
gtid_executed = "None"
self._report("# GTID Executed Set: {0}".format(gtid_executed),
logging.INFO)
示例12: show_options
def show_options(self):
""" Show all audit log variables.
"""
server = Server({'conn_info': self.options.get("server_vals", None)})
server.connect()
rows = server.show_server_variable("audit%")
server.disconnect()
if rows:
print "#\n# Audit Log Variables and Options\n#"
print_list(sys.stdout, "GRID", ['Variable_name', 'Value'],
rows)
print
else:
raise UtilError("No audit log variables found.")
示例13: _log_data
def _log_data(self, title, labels, data, print_format=True):
"""Helper method to log data.
title[in] Title to log.
labels[in] List of labels.
data[in] List of data rows.
"""
self._report("# {0}".format(title), logging.INFO)
for row in data:
msg = ", ".join(
["{0}: {1}".format(*col) for col in zip(labels, row)]
)
self._report("# {0}".format(msg), logging.INFO, False)
if print_format:
print_list(sys.stdout, self.format, labels, data)
示例14: __print_index_list
def __print_index_list(indexes, fmt, no_header=False):
"""Print the list of indexes
indexes[in] list of indexes to print
fmt[in] format out output = sql, table, tab, csv
no_header[in] (optional) if True, do not print the header
"""
if fmt == "sql":
for index in indexes:
index.print_index_sql()
else:
cols = ("database", "table", "name", "type", "columns")
rows = []
for index in indexes:
rows.append(index.get_row())
print_list(sys.stdout, fmt, cols, rows, no_header)
示例15: _print_list
def _print_list(self, refresh=True, comment=None):
"""Display the list information
This method displays the list information using the start_list and
end_list member variables to control the view of the data. This
permits users to scroll through the data should it be longer than
the space permitted on the screen.
"""
# If no data to print, exit
if self.list_data is None:
return
if refresh:
self.clear()
self._print_header()
self._print_master_status()
# Print list name
if comment is None:
comment = self.comment
print comment
self.rows_printed += 1
# Print the list in the remaining space
footer_len = 2
remaining_rows = self.max_rows - self.rows_printed - 4 - footer_len
if len(self.list_data[1][self.start_list:self.end_list]) > \
remaining_rows:
rows = self.list_data[1][self.start_list:self.start_list +
remaining_rows]
self.end_list = self.start_list + remaining_rows
self.scroll_on = True
else:
if len(self.list_data[1]) == self.end_list and \
self.start_list == 0:
self.scroll_on = False
rows = self.list_data[1][self.start_list:self.end_list]
if len(rows) > 0:
self.scroll_size = len(rows)
print_list(sys.stdout, 'GRID', self.list_data[0], rows)
self.rows_printed += self.scroll_size + 4
else:
print "0 Rows Found."
self.rows_printed += 1
if refresh:
self._print_footer(self.scroll_on)