diff options
author | Scott Bahling <sbahling@suse.de> | 2014-08-18 17:02:53 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2014-08-18 17:40:15 +0200 |
commit | a307b3870cac382c1d97c60f0a2a22b6e5ee6876 (patch) | |
tree | 442e85cadec7b8b4dce58198c38b9ed176696831 | |
parent | 3d14328637bc4a64dbe69f98b7a7fa0e67a62f8f (diff) | |
download | cpaste-a307b3870cac382c1d97c60f0a2a22b6e5ee6876.tar.gz cpaste-a307b3870cac382c1d97c60f0a2a22b6e5ee6876.tar.xz cpaste-a307b3870cac382c1d97c60f0a2a22b6e5ee6876.zip |
Add checks for valid language option
- Query api for list of valid languages.
- Fail if user specified language not in valid list
- Fall back default if guessed language on in valid list.
- Add --list-lang option to output list of valid languages.
-rwxr-xr-x | cpaste | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -36,6 +36,17 @@ try: except ImportError: guess_lang = False +def tabulate(words, termwidth=79, pad=3, indent=0): + width = len(max(words, key=len)) + pad + ncols = max(1, (termwidth - indent) // width) + nrows = (len(words) - 1) // ncols + 1 + table = [] + for i in xrange(nrows): + row = words[i::nrows] + format_str = ' ' * indent + ('%%-%ds' % width) * len(row) + table.append(format_str % tuple(row)) + return '\n'.join(table) + default_lang = 'text' # Map numpy to python because the numpy lexer gives false positives @@ -44,12 +55,22 @@ lexer_lang_map = {'numpy': 'python'} paste_url = 'https://cpaste.org' +valid_langs = [] + +try: + r = requests.get(paste_url + '/api/json/parameter/language') + valid_langs = json.loads(r.text)['result']['values'] +except: + print "Unable to retrieve list of valid languages from api." + parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="Read from a file instead of stdin", metavar="FILE") parser.add_option("-l", "--lang", action="store", type="string", dest="lang", help="Specifiy the language.", metavar="LANG") +parser.add_option("--list-lang", action="store_true", dest="list_lang", + help="List valid language options.") parser.add_option("-p", "--private", action="store_true", dest="private", default=False, help="Create a private post") @@ -58,6 +79,11 @@ parser.add_option("-e", "--expire", help="Expire the paste in X minutes (default: 1 day (1440), valid: 0, 30, 360, 1440, 43200)", metavar="X") (options, args) = parser.parse_args() +if options.list_lang: + print "Valid language options are:" + print tabulate(valid_langs, indent=3) + sys.exit(0) + if options.filename: f = open(options.filename) if not f: @@ -71,6 +97,16 @@ if not paste_data: print "Oops, we have no data" sys.exit(1) +# API expects lower case language option +options.lang = options.lang.lower() + +# Exit if user specified language not valid for the API +if valid_langs and options.lang not in valid_langs: + print "Specified language '%s' is not a valid option." % options.lang + print "Valid language options are:" + print tabulate(valid_langs, indent=3) + sys.exit(0) + if not options.lang and guess_lang: # If we have a filename, try guessing using the more reliable # guess_lexer_for_filename function. @@ -92,6 +128,10 @@ if not options.lang and guess_lang: # Use default if no language specified or guessed options.lang = options.lang or default_lang +if valid_langs and options.lang not in valid_langs: + options.lang = default_lang + + if int(options.expire) != 86400: options.expire = int(options.expire) * 60 |