/usr/lib/python3/dist-packages/backup2swift/utils.py is in python3-backup2swift 0.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | # -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Kouhei Maeda <mkouhei@palmtb.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import syslog
import multiprocessing
import sys
import socket
import backup2swift
FQDN = socket.getfqdn()
def logging(priority, message):
"""
Arguments:
priority: syslog priority
message: log message
"""
syslog.openlog(backup2swift.__name__, syslog.LOG_PID, syslog.LOG_LOCAL0)
syslog.syslog(priority, str(message))
syslog.closelog()
print(message)
exit(1)
def list_data(data):
"""
Argument:
data: list of data
"""
for i in data:
if sys.version_info < (3, 0):
if isinstance(i, unicode):
print(i)
elif isinstance(i, dict):
pretty_print(list(i.keys()), data)
break
else:
if isinstance(i, str):
print(i)
elif isinstance(i, dict):
pretty_print(list(i.keys()), data)
break
def pretty_print(header, rows):
"""
Arguments:
header: list of header data
rows: list of data
"""
# formatting header
header.sort()
header.reverse()
# retrieve column width from header
col_width_l = [len(i) for i in header]
# retrieve column width from data
col_width_l = get_columns_width(col_width_l, header, rows)
# print formatly
print_header(col_width_l, header)
for row in rows:
print(generate_row_s(row, col_width_l, header))
print_footer(col_width_l)
def get_columns_width(columns_width, header, rows):
"""
Arguments:
columns_width: list of columns string length
header: list of header data
rows: list of data
"""
for row in rows:
for i, key in enumerate(header):
value = str(row.get(key))
if columns_width[i] <= len(value):
columns_width[i] = len(value)
else:
columns_width[i] = columns_width[i]
return columns_width
def print_header(columns_width, header):
"""
Arguments:
columns_width: list of columns string length
header: list of header data
"""
border = ''
for col_width in columns_width:
border += "-" * col_width + '-'
print("%s" % border)
print(generate_row_s(header, columns_width))
print("%s" % border)
def print_footer(columns_width):
"""
Argument:
columns_width: list of columns string length
"""
border = ''
for i, col_width in enumerate(columns_width):
border += "-" * col_width + '-'
print("%s" % border)
def generate_row_s(row, columns_width, header=None):
"""
Arguments:
row: data of row
columns_width: list of columns string length
header: list of header data
"""
row_s = ''
if header:
for i, key in enumerate(header):
column = str(row.get(key))
row_s += (column + ' ' *
(columns_width[i] - len(column) + 1))
row_s += ' '
else:
for i, value in enumerate(row):
column = str(value)
row_s += (column + ' ' *
(columns_width[i] - len(column) + 1))
row_s += ' '
return row_s
def multiprocess(func, *args, **kwargs):
proc = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
proc.start()
|