Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade for Python 3 #61

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ tags
build/
dist/
*.vim
Pipfile
Pipfile.lock
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: python
cache: pip
python:
- "2.6"
- "2.7"
- "3.7"
- "3.8"
install:
- pip install coveralls
script:
coverage run --source=psdash setup.py test
after_success:
coveralls

2 changes: 1 addition & 1 deletion psdash/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6.2'
__version__ = "0.7.0"
8 changes: 5 additions & 3 deletions psdash/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


def socket_constants(prefix):
return dict((getattr(socket, n), n) for n in dir(socket) if n.startswith(prefix))
return dict(
(getattr(socket, n), n) for n in dir(socket) if n.startswith(prefix)
)


socket_families = socket_constants('AF_')
socket_types = socket_constants('SOCK_')
socket_families = socket_constants("AF_")
socket_types = socket_constants("SOCK_")
34 changes: 19 additions & 15 deletions psdash/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import logging

logger = logging.getLogger('psdash.log')
logger = logging.getLogger("psdash.log")


class LogError(Exception):
Expand Down Expand Up @@ -33,7 +33,7 @@ def reset(self):
def __iter__(self):
return self

def next(self):
def __next__(self):
pos = self.find()
if pos < 0:
raise StopIteration
Expand Down Expand Up @@ -71,7 +71,7 @@ def find(self):
return filepos

# for it to work when the needle is split between chunks.
lastbuf = buf[:len(self._needle)]
lastbuf = buf[: len(self._needle)]

return -1

Expand All @@ -91,19 +91,21 @@ class LogReader(object):

def __init__(self, filename, buffer_size=BUFFER_SIZE):
self.filename = filename
self.fp = open(filename, 'r')
self.fp = open(filename, "r")
self.buffer_size = buffer_size
self._searchers = {}

def __repr__(self):
return '<LogReader filename=%s, file-pos=%d>' % (
self.filename, self.fp.tell()
return "<LogReader filename=%s, file-pos=%d>" % (
self.filename,
self.fp.tell(),
)

def set_tail_position(self):
stat = os.fstat(self.fp.fileno())
if stat.st_size >= self.buffer_size:
self.fp.seek(-self.buffer_size, os.SEEK_END)
self.fp.seek(0, os.SEEK_END)
self.fp.seek(self.fp.tell() - self.buffer_size, os.SEEK_SET)
else:
self.fp.seek(0)

Expand All @@ -130,7 +132,7 @@ def search(self, text):
if position < 0:
# reset the searcher to start from the tail again.
searcher.reset()
return -1, -1, ''
return -1, -1, ""

# try to get some content from before and after the result's position
read_before = self.buffer_size / 2
Expand All @@ -151,13 +153,12 @@ def __init__(self):
def add_available(self, filename):
# quick verification that it exists and can be read
try:
filename = filename.decode('utf-8')
f = open(filename)
f.close()
except IOError as e:
raise LogError('Could not read log file "%s" (%s)' % (filename, e))

logger.debug('Adding log file %s', filename)
logger.debug("Adding log file %s", filename)

return self.available.add(filename)

Expand All @@ -173,11 +174,14 @@ def get_available(self):
log = self.get(filename)
available.append(log)
except IOError:
logger.info('Failed to get "%s", removing from available logs', filename)
logger.info(
'Failed to get "%s", removing from available logs',
filename,
)
to_remove.append(filename)

if to_remove:
map(self.remove_available, to_remove)
list(map(self.remove_available, to_remove))

return available

Expand All @@ -196,16 +200,16 @@ def add_patterns(self, patterns):
except LogError as e:
logger.warning(e)

logger.info('Added %d log file(s)', i)
logger.info("Added %d log file(s)", i)
return i

def clear(self):
for r in self.readers.itervalues():
for r in self.readers.values():
r.close()
self.readers = {}

def remove(self, filename):
for reader_key, r in self.readers.items():
for reader_key, r in list(self.readers.items()):
if reader_key[0] == filename:
r.close()
del self.readers[reader_key]
Expand Down
26 changes: 15 additions & 11 deletions psdash/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def _get_net_io_counters(self):
counters = psutil.net_io_counters(pernic=self.pernic)

res = {}
for name, io in counters.iteritems():
for name, io in counters.items():
res[name] = io._asdict()
res[name].update({'tx_per_sec': 0, 'rx_per_sec': 0})
res[name].update({"tx_per_sec": 0, "rx_per_sec": 0})

return res

Expand All @@ -43,15 +43,19 @@ def update(self):
if not time_delta:
return counters

for name, io in counters.iteritems():
for name, io in counters.items():
last_io = self.last_req.get(name)
if not last_io:
continue

counters[name].update({
'rx_per_sec': (io['bytes_recv'] - last_io['bytes_recv']) / time_delta,
'tx_per_sec': (io['bytes_sent'] - last_io['bytes_sent']) / time_delta
})
counters[name].update(
{
"rx_per_sec": (io["bytes_recv"] - last_io["bytes_recv"])
/ time_delta,
"tx_per_sec": (io["bytes_sent"] - last_io["bytes_sent"])
/ time_delta,
}
)

self._set_last_request(counters)

Expand All @@ -70,7 +74,7 @@ def get_interface_addresses():
ifaces = netifaces.interfaces()
for iface in ifaces:
addrs = netifaces.ifaddresses(iface)
families = addrs.keys()
families = list(addrs.keys())

# put IPv4 to the end so it lists as the main iface address
if netifaces.AF_INET in families:
Expand All @@ -80,9 +84,9 @@ def get_interface_addresses():
for family in families:
for addr in addrs[family]:
address = {
'name': iface,
'family': family,
'ip': addr['addr'],
"name": iface,
"family": family,
"ip": addr["addr"],
}
addresses.append(address)

Expand Down
Loading