Skip to content

Commit

Permalink
retab Python scripts according to PEP8
Browse files Browse the repository at this point in the history
Recent Vim sets sts=4 for Python files by default. Might as well follow
it already, I guess. Use `git show -w` for this one.
  • Loading branch information
grawity committed Nov 21, 2013
1 parent 781ecaa commit 992046f
Show file tree
Hide file tree
Showing 46 changed files with 5,536 additions and 5,559 deletions.
8 changes: 4 additions & 4 deletions lib/python/nullroute/__init__.py
@@ -1,11 +1,11 @@
import sys

def warn(*args):
print("\033[1;33mwarning:\033[m", *args, file=sys.stderr)
print("\033[1;33mwarning:\033[m", *args, file=sys.stderr)

def err(*args):
print("\033[1;31merror:\033[m", *args, file=sys.stderr)
print("\033[1;31merror:\033[m", *args, file=sys.stderr)

def die(*args):
err(*args)
sys.exit(1)
err(*args)
sys.exit(1)
304 changes: 150 additions & 154 deletions lib/python/nullroute/authorized_keys.py
@@ -1,9 +1,5 @@
#!/usr/bin/env python
# parser for OpenSSH authorized_keys files
# vim: ft=python

# State-machine-based parser for OpenSSH authorized_keys files.
# (c) 2011 Mantas M. <grawity@gmail.com>
# Released under WTFPL v2 <http://sam.zoy.org/wtfpl/>
#
# for line in open("authorized_keys"):
# if line and not line.startswith("#"):
Expand All @@ -13,156 +9,156 @@
import hashlib

class PublicKeyOptions(list):
def __str__(self):
o = []
for k, v in self:
if v is True:
o.append(k)
else:
o.append("%s=%s" % (k, v))
return ",".join(o)
@classmethod
def parse(klass, text):
keys = []
values = []
current = ""
state = "key"

for char in text:
if state == "key":
if char == ",":
keys.append(current)
values.append(True)
current = ""
elif char == "=":
keys.append(current)
current = ""
state = "value"
else:
current += char
elif state == "value":
if char == ",":
values.append(current)
current = ""
state = "key"
elif char == "\"":
current += char
state = "value dquote"
else:
current += char
elif state == "value dquote":
if char == "\"":
current += char
state = "value"
elif char == "\\":
current += char
state = "value dquote escape"
else:
current += char
elif state == "value dquote escape":
current += char
state = "value dquote"

if current:
if state == "key":
keys.append(current)
values.append(True)
else:
values.append(current)

return klass(zip(keys, values))
def __str__(self):
o = []
for k, v in self:
if v is True:
o.append(k)
else:
o.append("%s=%s" % (k, v))
return ",".join(o)

@classmethod
def parse(klass, text):
keys = []
values = []
current = ""
state = "key"

for char in text:
if state == "key":
if char == ",":
keys.append(current)
values.append(True)
current = ""
elif char == "=":
keys.append(current)
current = ""
state = "value"
else:
current += char
elif state == "value":
if char == ",":
values.append(current)
current = ""
state = "key"
elif char == "\"":
current += char
state = "value dquote"
else:
current += char
elif state == "value dquote":
if char == "\"":
current += char
state = "value"
elif char == "\\":
current += char
state = "value dquote escape"
else:
current += char
elif state == "value dquote escape":
current += char
state = "value dquote"

if current:
if state == "key":
keys.append(current)
values.append(True)
else:
values.append(current)

return klass(zip(keys, values))

class PublicKey(object):
def __init__(self, line=None):
if line:
tokens = self.parse(line)
else:
tokens = ["", None, None, None]

self.prefix, self.algo, self.blob, self.comment = tokens

self.options = PublicKeyOptions.parse(self.prefix)
def __repr__(self):
return "<PublicKey prefix=%r algo=%r comment=%r>" % \
(self.prefix, self.algo, self.comment)
def __str__(self):
options = self.options
blob = base64.b64encode(self.blob).decode("utf-8")
comment = self.comment
k = [self.algo, blob]
if len(options):
k.insert(0, str(options))
if len(comment):
k.append(comment)
return " ".join(k)

def fingerprint(self, alg=None, hex=False):
if alg is None:
alg = hashlib.md5
m = alg()
m.update(self.blob)
return m.hexdigest() if hex else m.digest()

@classmethod
def parse(self, line):
tokens = []
current = ""
state = "normal"

for char in line:
old = state
if state == "normal":
if char in " \t":
tokens.append(current)
current = ""
elif char == "\"":
current += char
state = "dquote"
else:
current += char
elif state == "dquote":
if char == "\"":
current += char
state = "normal"
elif char == "\\":
current += char
state = "dquote escape"
else:
current += char
elif state == "dquote escape":
current += char
state = "dquote"

if current:
tokens.append(current)
if tokens[0] in {"ssh-rsa", "ssh-dss", "ecdsa-sha2-nistp256",
"ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521"}:
prefix = ""
else:
prefix = tokens.pop(0)
algo = tokens[0]
blob = tokens[1]
blob = base64.b64decode(blob.encode("utf-8"))
comment = " ".join(tokens[2:])

return prefix, algo, blob, comment
def __init__(self, line=None):
if line:
tokens = self.parse(line)
else:
tokens = ["", None, None, None]

self.prefix, self.algo, self.blob, self.comment = tokens

self.options = PublicKeyOptions.parse(self.prefix)

def __repr__(self):
return "<PublicKey prefix=%r algo=%r comment=%r>" % \
(self.prefix, self.algo, self.comment)

def __str__(self):
options = self.options
blob = base64.b64encode(self.blob).decode("utf-8")
comment = self.comment
k = [self.algo, blob]
if len(options):
k.insert(0, str(options))
if len(comment):
k.append(comment)
return " ".join(k)

def fingerprint(self, alg=None, hex=False):
if alg is None:
alg = hashlib.md5
m = alg()
m.update(self.blob)
return m.hexdigest() if hex else m.digest()

@classmethod
def parse(self, line):
tokens = []
current = ""
state = "normal"

for char in line:
old = state
if state == "normal":
if char in " \t":
tokens.append(current)
current = ""
elif char == "\"":
current += char
state = "dquote"
else:
current += char
elif state == "dquote":
if char == "\"":
current += char
state = "normal"
elif char == "\\":
current += char
state = "dquote escape"
else:
current += char
elif state == "dquote escape":
current += char
state = "dquote"

if current:
tokens.append(current)

if tokens[0] in {"ssh-rsa", "ssh-dss", "ecdsa-sha2-nistp256",
"ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521"}:
prefix = ""
else:
prefix = tokens.pop(0)
algo = tokens[0]
blob = tokens[1]
blob = base64.b64decode(blob.encode("utf-8"))
comment = " ".join(tokens[2:])

return prefix, algo, blob, comment

if __name__ == "__main__":
import os

path = os.path.expanduser("~/.ssh/authorized_keys")

for line in open(path, "r"):
line = line.strip()
if line and not line.startswith("#"):
key = PublicKey(line)
print("key = %r" % key)
print("prefix = %r" % key.prefix)
print("algo = %r" % key.algo)
print("comment = %r" % key.comment)
print("options = %r" % key.options)
print()
import os

path = os.path.expanduser("~/.ssh/authorized_keys")

for line in open(path, "r"):
line = line.strip()
if line and not line.startswith("#"):
key = PublicKey(line)
print("key = %r" % key)
print("prefix = %r" % key.prefix)
print("algo = %r" % key.algo)
print("comment = %r" % key.comment)
print("options = %r" % key.options)
print()
50 changes: 25 additions & 25 deletions lib/python/nullroute/clipboard.py
@@ -1,30 +1,30 @@
import sys

def get():
if sys.platform == "win32":
import win32clipboard as clip
clip.OpenClipboard()
# TODO: what type does this return?
data = clip.GetClipboardData(clip.CF_UNICODETEXT)
#print("clipboard.get =", repr(data))
clip.CloseClipboard()
return data
else:
raise RuntimeError("Unsupported platform")
if sys.platform == "win32":
import win32clipboard as clip
clip.OpenClipboard()
# TODO: what type does this return?
data = clip.GetClipboardData(clip.CF_UNICODETEXT)
#print("clipboard.get =", repr(data))
clip.CloseClipboard()
return data
else:
raise RuntimeError("Unsupported platform")

def put(data):
if sys.platform == "win32":
import win32clipboard as clip
clip.OpenClipboard()
clip.EmptyClipboard()
clip.SetClipboardText(data, clip.CF_UNICODETEXT)
clip.CloseClipboard()
elif sys.platform.startswith("linux"):
import subprocess
proc = subprocess.Popen(("xsel", "-i", "-b", "-l", "/dev/null"),
stdin=subprocess.PIPE)
proc.stdin.write(data.encode("utf-8"))
proc.stdin.close()
proc.wait()
else:
raise RuntimeError("Unsupported platform")
if sys.platform == "win32":
import win32clipboard as clip
clip.OpenClipboard()
clip.EmptyClipboard()
clip.SetClipboardText(data, clip.CF_UNICODETEXT)
clip.CloseClipboard()
elif sys.platform.startswith("linux"):
import subprocess
proc = subprocess.Popen(("xsel", "-i", "-b", "-l", "/dev/null"),
stdin=subprocess.PIPE)
proc.stdin.write(data.encode("utf-8"))
proc.stdin.close()
proc.wait()
else:
raise RuntimeError("Unsupported platform")

0 comments on commit 992046f

Please sign in to comment.