Skip to content

Instantly share code, notes, and snippets.

@dheaney
Last active December 14, 2015 03:29
Show Gist options
  • Save dheaney/5021104 to your computer and use it in GitHub Desktop.
Save dheaney/5021104 to your computer and use it in GitHub Desktop.
Trying to handle user input for whit
#!/usr/bin/python
# whit-path-tests.py
# This file is part of whit.
#
# Copyright (C) 2013 - dejay
#
# Whit 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.
#
# Whit 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 whit. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import sys, os
import argparse
def arguments():
parser = argparse.ArgumentParser(prog='whit path tests', description='testing the ability of whit to handle path arguments, etc.')
parser.add_argument("-p", "--path", nargs=1, dest="path", metavar="path", help="output path", action="store", default=False)
parser.add_argument("-f", dest="fetch", help="fetch the name from whitsend.org", action="store_true", default=False)
return parser.parse_args()
def expand_path(path):
return os.path.abspath(os.path.expanduser(os.path.normpath(path)))
def main():
args = arguments()
if args.path:
print("received a path argument")
if list(args.path[0])[-1] == '/':
if os.path.isdir(os.path.abspath(os.path.expanduser(os.path.normpath(args.path[0])))):
print("path is an existing directory")
if args.fetch != False:
print("using name from whitsend.org")
print("writing to the provided directory")
else:
print("no fetch argument was provided")
print("will split the url to acquire a filename")
else:
print("the user asked for a file to be written to a nonexistent directory")
else:
path = expand_path(args.path[0])
if args.fetch:
print("using name from whitsend.org")
if os.path.isdir(path):
print("path is an existing directory")
else:
print("path was not an existing directory")
else:
if os.path.isdir(path):
print("path was a directory")
print("will split the url to acquire a filename")
else:
print("path was not a directory")
if os.path.isdir(os.path.dirname(path)):
print("will write to the folder \"%s\" with the the filename \"%s\"" % (os.path.dirname(path), os.path.basename(path)))
else:
print("the user asked for a file to be written to a nonexistent directory")
elif args.fetch:
print("using name fron whitsend.org")
print("will write to the current directory")
else:
print("did not receive any arguments")
print("will split the url to acquire a filename")
print("will write to the current directory")
if __name__ == '__main__':
main()
#!/usr/bin/python
# whit-test-paths.py
# This file is part of whit.
#
# Copyright (C) 2013 - dejay
#
# Whit 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.
#
# Whit 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 whit. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import subprocess
def testWhitPath(path=None, fetch=None):
if path == None:
path = ""
else:
path = "-p " + path
if fetch == None:
fetch = ""
else:
fetch = "-f"
command = "python whit-path-tests.py %s %s" % (path, fetch)
print('\033[92m' + 'Command: ' + command)
print('Output: \033[0m\n')
subprocess.call(command, shell=True)
print('')
def main():
print('Testing path implementation...\n')
testWhitPath('../')
testWhitPath('~/Desktop/foo')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment