Skip to content

Commit

Permalink
run yapf over StaSh src
Browse files Browse the repository at this point in the history
  • Loading branch information
bennr01 committed Jun 7, 2019
1 parent c68a672 commit fc47afe
Show file tree
Hide file tree
Showing 162 changed files with 7,728 additions and 7,276 deletions.
8 changes: 5 additions & 3 deletions bin/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import sys
import argparse


def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('expr', nargs='?', help='name=value')

ns = ap.parse_args(args)

app = globals()['_stash']
""":type : StaSh"""

Expand All @@ -21,7 +22,7 @@ def main(args):
if ns.expr is None:
for k, v in current_state.aliases.items():
print('{}={}'.format(k, v[0]))

else:
if "=" in ns.expr:
name, value = ns.expr.split("=", 1)
Expand All @@ -40,5 +41,6 @@ def main(args):
except KeyError as err:
raise KeyError('alias: {} not found'.format(err.message))


if __name__ == "__main__":
main(sys.argv[1:])
7 changes: 2 additions & 5 deletions bin/cat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Print the contents of the given files.
"""

Expand All @@ -18,16 +17,14 @@ def filter_non_printable(s):

def main(args):
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("files", action="store", nargs="*",
help="files to print")
p.add_argument("files", action="store", nargs="*", help="files to print")
ns = p.parse_args(args)

status = 0

fileinput.close() # in case it is not closed
try:
for line in fileinput.input(ns.files,
openhook=fileinput.hook_encoded("utf-8")):
for line in fileinput.input(ns.files, openhook=fileinput.hook_encoded("utf-8")):
print(filter_non_printable(line), end='')
except Exception as e:
print('cat: %s' % str(e))
Expand Down
7 changes: 2 additions & 5 deletions bin/cd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Change the current working directory.
"""

Expand All @@ -13,9 +12,7 @@

def main(args):
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("dir", action="store", nargs="?",
default=os.environ["HOME2"],
help="the new working directory")
p.add_argument("dir", action="store", nargs="?", default=os.environ["HOME2"], help="the new working directory")
ns = p.parse_args(args)

status = 0
Expand All @@ -31,7 +28,7 @@ def main(args):
else:
print('cd: %s: Not a directory' % ns.dir)
else:
print ('cd: %s: No such file or directory' % ns.dir)
print('cd: %s: No such file or directory' % ns.dir)
except Exception as err:
print("cd: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
status = 1
Expand Down
94 changes: 47 additions & 47 deletions bin/cowsay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,62 @@
import sys
import argparse

COW = """ \ ^__^
COW = """ \ ^__^
\ (oo)\_______
(__)\ )\/\\
||----w |
|| ||
"""


def get_cow(text):
"""create a string of a cow saying things."""
lines = text.split("\n")
nlines = len(lines)
longest_line = max([len(l) for l in lines])
lenght_of_lines = longest_line + 2
ret = (' ' + '_' * lenght_of_lines + "\n")
if nlines == 1:
formated = text.center(longest_line + 2)
ret += formated.join('<>') + "\n"
else:
t = ""
for i in range(nlines):
line = lines[i].center(longest_line + 2)
if i == 0:
t += ("/" + line + "\\\n")
elif i == (nlines - 1):
t += ("\\" + line + "/\n")
else:
t += ("|" + line + "|\n")
ret += t
ret += (' ' + '-' * lenght_of_lines + "\n")
ret += COW
return ret
def get_cow(text):
"""create a string of a cow saying things."""
lines = text.split("\n")
nlines = len(lines)
longest_line = max([len(l) for l in lines])
lenght_of_lines = longest_line + 2
ret = (' ' + '_' * lenght_of_lines + "\n")
if nlines == 1:
formated = text.center(longest_line + 2)
ret += formated.join('<>') + "\n"
else:
t = ""
for i in range(nlines):
line = lines[i].center(longest_line + 2)
if i == 0:
t += ("/" + line + "\\\n")
elif i == (nlines - 1):
t += ("\\" + line + "/\n")
else:
t += ("|" + line + "|\n")
ret += t
ret += (' ' + '-' * lenght_of_lines + "\n")
ret += COW
return ret


def main():
"""main function"""
# todo: lookuo real description
parser = argparse.ArgumentParser(description="Let a cow speak for you")
parser.add_argument("text", nargs="*", default=None, help="text to say")
ns = parser.parse_args()
if (ns.text is None) or (len(ns.text) == 0):
text = ""
while True:
inp = sys.stdin.read(4096)
if inp.endswith("\n"):
inp = inp[:-1]
if not inp:
break
text += inp
else:
text = " ".join(ns.text)
cow = get_cow(text)
print(cow)
"""main function"""
# todo: lookuo real description
parser = argparse.ArgumentParser(description="Let a cow speak for you")
parser.add_argument("text", nargs="*", default=None, help="text to say")
ns = parser.parse_args()

if (ns.text is None) or (len(ns.text) == 0):
text = ""
while True:
inp = sys.stdin.read(4096)
if inp.endswith("\n"):
inp = inp[:-1]
if not inp:
break
text += inp
else:
text = " ".join(ns.text)

cow = get_cow(text)
print(cow)


if __name__ == "__main__":
main()
main()
12 changes: 7 additions & 5 deletions bin/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def pprint(path):
return '~' + path.split(os.environ['HOME'], 1)[-1]
return path


def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('source', nargs='+', help='one or more files or directories to be copied')
ap.add_argument('dest', help='destination file or folder')
ns = ap.parse_args(args)

files = ns.source
dest = ns.dest

Expand All @@ -32,8 +33,8 @@ def main(args):
for filef in files:
full_file = os.path.abspath(filef)
file_name = os.path.basename(full_file)
new_name = os.path.join(full_dest, file_name)
new_name = os.path.join(full_dest, file_name)

try:
if os.path.isdir(full_file):
shutil.copytree(full_file, new_name)
Expand Down Expand Up @@ -67,14 +68,15 @@ def main(args):
# Destination does not yet exist
if os.path.isdir(full_file):
# Source is a directory, destination should become a directory
shutil.copytree(full_file,full_dest)
shutil.copytree(full_file, full_dest)
else:
# Source is a file, destination should become a file
shutil.copy(full_file,full_dest)
shutil.copy(full_file, full_dest)
except Exception as err:
print("cp: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
else:
print("cp: {}: No such file".format(pprint(filef)), file=sys.stderr)


if __name__ == "__main__":
main(sys.argv[1:])
101 changes: 54 additions & 47 deletions bin/crypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,61 @@

_stash = globals()['_stash']
try:
import pyaes
import pyaes
except ImportError:
print('Installing Required packages...')
_stash('pip install pyaes')
import pyaes
print('Installing Required packages...')
_stash('pip install pyaes')
import pyaes


class Crypt(object):
def __init__(self, in_filename, out_filename=None):
self.in_filename = in_filename
self.out_filename = out_filename
def aes_encrypt(self, key=None, chunksize=64*1024):
self.out_filename = self.out_filename or self.in_filename + '.enc'
if key is None:
key = base64.b64encode(os.urandom(32))[:32]
aes = pyaes.AESModeOfOperationCTR(key)
with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
pyaes.encrypt_stream(aes, infile, outfile)
return key
def aes_decrypt(self, key, chunksize=64*1024):
self.out_filename = self.out_filename or os.path.splitext(self.in_filename)[0]
aes = pyaes.AESModeOfOperationCTR(key)
with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
pyaes.decrypt_stream(aes, infile, outfile)
def __init__(self, in_filename, out_filename=None):
self.in_filename = in_filename
self.out_filename = out_filename

def aes_encrypt(self, key=None, chunksize=64 * 1024):
self.out_filename = self.out_filename or self.in_filename + '.enc'
if key is None:
key = base64.b64encode(os.urandom(32))[:32]
aes = pyaes.AESModeOfOperationCTR(key)
with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
pyaes.encrypt_stream(aes, infile, outfile)
return key

def aes_decrypt(self, key, chunksize=64 * 1024):
self.out_filename = self.out_filename or os.path.splitext(self.in_filename)[0]
aes = pyaes.AESModeOfOperationCTR(key)

with open(self.in_filename, 'rb') as infile:
with open(self.out_filename, 'wb') as outfile:
pyaes.decrypt_stream(aes, infile, outfile)


if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument(
'-k', '--key', action='store', default=None,
help='Encrypt/Decrypt Key.',
)
ap.add_argument(
'-d', '--decrypt', action='store_true', default=False,
help='Flag to decrypt.',
)
#ap.add_argument('-t','--type',action='store',choices={'aes','rsa'},default='aes')
ap.add_argument('infile', action='store', help='File to encrypt/decrypt.')
ap.add_argument('outfile', action='store', nargs='?', help='Output file.')
args = ap.parse_args()
crypt = Crypt(args.infile, args.outfile)
if args.decrypt:
crypt.aes_decrypt(args.key)
else:
nk = crypt.aes_encrypt(args.key)
if args.key is None:
print("Key: %s" % nk)
ap = argparse.ArgumentParser()
ap.add_argument(
'-k',
'--key',
action='store',
default=None,
help='Encrypt/Decrypt Key.',
)
ap.add_argument(
'-d',
'--decrypt',
action='store_true',
default=False,
help='Flag to decrypt.',
)
#ap.add_argument('-t','--type',action='store',choices={'aes','rsa'},default='aes')
ap.add_argument('infile', action='store', help='File to encrypt/decrypt.')
ap.add_argument('outfile', action='store', nargs='?', help='Output file.')
args = ap.parse_args()
crypt = Crypt(args.infile, args.outfile)
if args.decrypt:
crypt.aes_decrypt(args.key)
else:
nk = crypt.aes_encrypt(args.key)
if args.key is None:
print("Key: %s" % nk)
14 changes: 11 additions & 3 deletions bin/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
except ImportError:
pass


def main(args):
ap = argparse.ArgumentParser()
ap.add_argument('url', nargs='?', help='the url to read (default to clipboard')
ap.add_argument('-o', '--output-file', help='write output to file instead of stdout')
ap.add_argument('-X', '--request-method', default='GET', choices=['GET', 'POST', 'HEAD'],
help='specify request method to use (default to GET)')
ap.add_argument(
'-X',
'--request-method',
default='GET',
choices=['GET',
'POST',
'HEAD'],
help='specify request method to use (default to GET)'
)
ap.add_argument('-H', '--header', help='Custom header to pass to server (H)')
ap.add_argument('-d', '--data', help='HTTP POST data (H)')

Expand Down Expand Up @@ -47,4 +55,4 @@ def main(args):


if __name__ == '__main__':
main(sys.argv[1:])
main(sys.argv[1:])
Loading

0 comments on commit fc47afe

Please sign in to comment.