0

util: fix code style

This commit is contained in:
Nicolas F
2019-03-07 15:49:37 +01:00
parent fc88572aa3
commit ec63aa55b6

View File

@@ -17,14 +17,16 @@
Misc utility routines used by multiple files that don't belong anywhere else Misc utility routines used by multiple files that don't belong anywhere else
""" """
import errno
import imp import imp
import os.path import os.path
import sys
import platform import platform
from string import hexdigits import sys
from subprocess import Popen, PIPE
from itertools import cycle, islice, product from itertools import cycle, islice, product
import errno from string import hexdigits
from subprocess import PIPE, Popen
def get_program_path(): def get_program_path():
if hasattr(sys, "frozen") or imp.is_frozen("__main__"): if hasattr(sys, "frozen") or imp.is_frozen("__main__"):
return os.path.dirname(sys.executable) return os.path.dirname(sys.executable)
@@ -36,6 +38,7 @@ def get_program_path():
except NameError: except NameError:
return os.path.dirname(sys.argv[0]) return os.path.dirname(sys.argv[0])
def findGitHash(): def findGitHash():
try: try:
p = Popen('git rev-parse HEAD', stdout=PIPE, stderr=PIPE, shell=True) p = Popen('git rev-parse HEAD', stdout=PIPE, stderr=PIPE, shell=True)
@@ -51,6 +54,7 @@ def findGitHash():
pass pass
return "unknown" return "unknown"
def findGitVersion(): def findGitVersion():
try: try:
p = Popen('git describe --tags --match "v*.*.*"', stdout=PIPE, stderr=PIPE, shell=True) p = Popen('git describe --tags --match "v*.*.*"', stdout=PIPE, stderr=PIPE, shell=True)
@@ -78,6 +82,7 @@ def findGitVersion():
except Exception: except Exception:
return "unknown" return "unknown"
def is_bare_console(): def is_bare_console():
"""Returns true if Overviewer is running in a bare console in """Returns true if Overviewer is running in a bare console in
Windows, that is, if overviewer wasn't started in a cmd.exe Windows, that is, if overviewer wasn't started in a cmd.exe
@@ -90,21 +95,23 @@ def is_bare_console():
num = GetConsoleProcessList(ctypes.byref(ctypes.c_int(0)), ctypes.c_int(1)) num = GetConsoleProcessList(ctypes.byref(ctypes.c_int(0)), ctypes.c_int(1))
if (num == 1): if (num == 1):
return True return True
except Exception: except Exception:
pass pass
return False return False
def nice_exit(ret=0): def nice_exit(ret=0):
"""Drop-in replacement for sys.exit that will automatically detect """Drop-in replacement for sys.exit that will automatically detect
bare consoles and wait for user input before closing. bare consoles and wait for user input before closing.
""" """
if ret and is_bare_console(): if ret and is_bare_console():
print print("")
print "Press [Enter] to close this window." print("Press [Enter] to close this window.")
raw_input() raw_input()
sys.exit(ret) sys.exit(ret)
# http://docs.python.org/library/itertools.html # http://docs.python.org/library/itertools.html
def roundrobin(iterables): def roundrobin(iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C" "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
@@ -119,6 +126,7 @@ def roundrobin(iterables):
pending -= 1 pending -= 1
nexts = cycle(islice(nexts, pending)) nexts = cycle(islice(nexts, pending))
def dict_subset(d, keys): def dict_subset(d, keys):
"Return a new dictionary that is built from copying select keys from d" "Return a new dictionary that is built from copying select keys from d"
n = dict() n = dict()
@@ -127,10 +135,10 @@ def dict_subset(d, keys):
n[key] = d[key] n[key] = d[key]
return n return n
## (from http://code.activestate.com/recipes/576693/ [r9])
# (from http://code.activestate.com/recipes/576693/ [r9])
# Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy. # Backport of OrderedDict() class that runs on Python 2.4, 2.5, 2.6, 2.7 and pypy.
# Passes Python2.7's test suite and incorporates all the latest updates. # Passes Python2.7's test suite and incorporates all the latest updates.
try: try:
from thread import get_ident as _get_ident from thread import get_ident as _get_ident
except ImportError: except ImportError:
@@ -141,6 +149,7 @@ try:
except ImportError: except ImportError:
pass pass
class OrderedDict(dict): class OrderedDict(dict):
'Dictionary that remembers insertion order' 'Dictionary that remembers insertion order'
# An inherited dict maps keys to values. # An inherited dict maps keys to values.
@@ -366,7 +375,7 @@ class OrderedDict(dict):
''' '''
if isinstance(other, OrderedDict): if isinstance(other, OrderedDict):
return len(self)==len(other) and self.items() == other.items() return len(self) == len(other) and self.items() == other.items()
return dict.__eq__(self, other) return dict.__eq__(self, other)
def __ne__(self, other): def __ne__(self, other):
@@ -386,6 +395,7 @@ class OrderedDict(dict):
"od.viewitems() -> a set-like object providing a view on od's items" "od.viewitems() -> a set-like object providing a view on od's items"
return ItemsView(self) return ItemsView(self)
# now replace all that with the official version, if available # now replace all that with the official version, if available
try: try:
import collections import collections
@@ -393,7 +403,8 @@ try:
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass pass
def pid_exists(pid): # http://stackoverflow.com/a/6940314/1318435
def pid_exists(pid): # http://stackoverflow.com/a/6940314/1318435
"""Check whether pid exists in the current process table.""" """Check whether pid exists in the current process table."""
if pid < 0: if pid < 0:
return False return False
@@ -402,4 +413,4 @@ def pid_exists(pid): # http://stackoverflow.com/a/6940314/1318435
except OSError, e: except OSError, e:
return e.errno != errno.ESRCH return e.errno != errno.ESRCH
else: else:
return True return True