def list(): api.getCredentials() log.debug("Command: List.") url = "/gists" gists = api.get(url) public_count = 0 private_count = 0 table = Texttable(max_width=defaults.max_width) table.set_deco(Texttable.HEADER | Texttable.HLINES) table.set_cols_align(["l", "l", "l", "l", "l"]) table.set_cols_width([4, 30, 6, 20, 30]) table.header(["", "Files", "Public", "Gist ID", "Description"]) for (i, gist) in enumerate(gists): private = False file_list = '' for (file, data) in gist['files'].items(): file_list += "'" + file + "' " if gist['public']: public_count += 1 else: private_count += 1 table.add_row([ i + 1, file_list, str(gist['public']), gist['id'], gist['description'] ]) print(table.draw()) print('') print("You have %i Gists. (%i Private)" % (len(gists), private_count))
def _get_gist(id): api.getCredentials() log.debug ("Internal: _get_gist: " + id) url = "/gists/" + id gist = api.get(url) return gist
def delete(id): api.getCredentials() log.debug("Command: Delete: " + id) if id[0] in _cmds['#']: id = _get_id_for_index(id) confirm = defaults.forceDelete if _supress == False: gist = _get_gist(id) print(('Gist \'{0}\' Description: {1}'.format(id, gist['description']))) for file in gist['files']: print((' ' + file)) confirm = util.parseBool( util.readConsole(prompt='Delete Gist? (y/n):', required=False, bool=True)) if confirm: url = '/gists/' + id api.delete(url) print('Gist deleted: {0}'.format(id)) else: print('I did not delete the Gist.')
def list (): api.getCredentials() log.debug ("Command: List.") url = "/gists" gists = api.get(url) public_count = 0 private_count = 0 table = Texttable(max_width=defaults.max_width) table.set_deco(Texttable.HEADER | Texttable.HLINES) table.set_cols_align(["l", "l", "l", "l", "l"]) table.set_cols_width([4, 30, 6, 20, 30]) table.header( ["","Files","Public", "Gist ID", "Description"] ) for (i, gist) in enumerate(gists): private = False file_list = '' for (file, data) in gist['files'].items(): file_list += "'" + file + "' " if gist['public']: public_count += 1 else: private_count += 1 table.add_row( [i+1, file_list, str(gist['public']), gist['id'], gist['description']] ) print table.draw() print '' print "You have %i Gists. (%i Private)" % (len(gists), private_count)
def _get_gist(id): api.getCredentials() log.debug("Internal: _get_gist: " + id) url = "/gists/" + id gist = api.get(url) return gist
def new(public=None, description=None, content=None, filename=None): api.getCredentials() log.debug( "Command: New: public: '{0}' description: '{1}' filename: '{2}' content: '{3}'." .format(str(public), str(description), str(filename), str(content))) if public == None: if _supress: public = defaults.public else: public = util.parseBool( util.readConsole(prompt='Public Gist? (y/n):', bool=True)) if description == None: if _supress: description = defaults.description else: description = util.readConsole(prompt='Description:', required=False) if content == None and filename != None: if os.path.isfile(filename): content = util.readFile(filename) else: print("Sorry, filename '{0}' is actually a Directory.".format( filename)) sys.exit(0) if content == None: if _supress: content = defaults.content else: content = util.readConsole() if filename == None: filename = defaults.file log.debug("Creating Gist using content: \n" + content) url = '/gists' data = { 'public': str(public).lower(), 'description': description, 'files': { os.path.basename(filename): { 'content': content } } } log.debug('Data: ' + str(data)) gist = api.post(url, data=data) pub_str = 'Public' if gist['public'] else 'Private' print("{0} Gist created:Id '{1}' and Url: {2}".format( pub_str, gist['id'], gist['html_url']))
def open(id): log.debug("open Gist with ID: {0} ".format(id)) if id[0] in _cmds['#']: api.getCredentials() id = _get_id_for_index(id) if id: gist = _get_gist(id) html_url = gist['html_url'] log.debug("open Gist html_url : {0}".format(html_url)) call(["open", html_url])
def append(id, description=None, content=None, filename=None): api.getCredentials() log.debug( "Command: Append: id: '{0}' description: '{1}' filename: '{2}' content: '{3}'." .format(id, str(description), str(filename), str(content))) if id[0] in _cmds['#']: id = _get_id_for_index(id) if description == None: if _supress: description = defaults.description else: description = util.readConsole(prompt='Description:', required=False) if content == None and filename != None: if os.path.isfile(filename): content = util.readFile(filename) else: print("Sorry, filename '{0}' is actually a Directory.".format( filename)) sys.exit(0) if content == None: if _supress: content = defaults.content else: content = util.readConsole(required=False) if filename == None: filename = defaults.file log.debug("Appending Gist " + id + " with content: \n" + content) oldgist = _get_gist(id) if description and description != '?': oldgist['description'] = description if content and content != '?': for (file, data) in list(oldgist['files'].items()): oldgist['files'][file][ 'content'] = data['content'] + '\n' + content log.debug('Data: ' + str(oldgist)) url = '/gists/' + id gist = api.patch(url, data=oldgist) pub_str = 'Public' if gist['public'] else 'Private' print("{0} Gist appended: Id '{1}' and Url: {2}".format( pub_str, gist['id'], gist['html_url']))
def update(id, description=None, content=None, filename=None): api.getCredentials() log.debug( "Command: Update: id: '{0}' description: '{1}' filename: '{2}' content: '{3}'." .format(id, str(description), str(filename), str(content))) if id[0] in _cmds['#']: id = _get_id_for_index(id) if description == None: if _supress: description = defaults.description else: description = util.readConsole(prompt='Description:', required=False) if content == None and filename != None: if os.path.isfile(filename): content = util.readFile(filename) else: print "Sorry, filename '{0}' is actually a Directory.".format( filename) sys.exit(0) if content == None: if _supress: content = defaults.content else: content = util.readConsole(required=False) if filename == None: filename = defaults.file log.debug("Updating Gist " + id + " with content: \n" + content) url = '/gists/' + id data = {} if description and description != '?': data['description'] = description if content and content != '?': data['files'] = {os.path.basename(filename): {'content': content}} log.debug('Data: ' + str(data)) gist = api.patch(url, data=data) pub_str = 'Public' if gist['public'] else 'Private' print "{0} Gist updated: Id '{1}' and Url: {2}".format( pub_str, gist['id'], gist['html_url'])
def append (id, description=None,content=None,filename=None): api.getCredentials() log.debug ("Command: Append: id: '{0}' description: '{1}' filename: '{2}' content: '{3}'.".format(id, str(description), str(filename), str(content))) if id[0] in _cmds['#']: id = _get_id_for_index(id) if description == None: if _supress: description = defaults.description else: description = util.readConsole(prompt='Description:', required=False) if content == None and filename != None: if os.path.isfile( filename ): content = util.readFile(filename) else: print "Sorry, filename '{0}' is actually a Directory.".format(filename) sys.exit(0) if content == None: if _supress: content = defaults.content else: content = util.readConsole(required=False) if filename == None: filename = defaults.file log.debug ("Appending Gist " + id + " with content: \n" + content) oldgist = _get_gist(id) if description and description != '?': oldgist['description'] = description if content and content != '?': for (file, data) in oldgist['files'].items(): oldgist['files'][file]['content'] = data['content'] + '\n' + content log.debug ('Data: ' + str(oldgist)) url = '/gists/' + id gist = api.patch(url, data=oldgist) pub_str = 'Public' if gist['public'] else 'Private' print "{0} Gist appended: Id '{1}' and Url: {2}".format(pub_str, gist['id'], gist['html_url'])
def new (public=None,description=None,content=None,filename=None): api.getCredentials() log.debug ("Command: New: public: '{0}' description: '{1}' filename: '{2}' content: '{3}'.".format(str(public), str(description), str(filename), str(content))) if public == None: if _supress: public = defaults.public else: public = util.parseBool( util.readConsole(prompt='Public Gist? (y/n):', bool=True) ) if description == None: if _supress: description = defaults.description else: description = util.readConsole(prompt='Description:', required=False) if content == None and filename != None: if os.path.isfile( filename ): content = util.readFile(filename) else: print "Sorry, filename '{0}' is actually a Directory.".format(filename) sys.exit(0) if content == None: if _supress: content = defaults.content else: content = util.readConsole() if filename == None: filename = defaults.file log.debug ("Creating Gist using content: \n" + content) url = '/gists' data = {'public': str(public).lower(), 'description': description, 'files': { os.path.basename(filename): { 'content': content } } } log.debug ('Data: ' + str(data)) gist = api.post(url, data=data) pub_str = 'Public' if gist['public'] else 'Private' print "{0} Gist created:Id '{1}' and Url: {2}".format(pub_str, gist['id'], gist['html_url'])
def delete (id): api.getCredentials() log.debug ("Command: Delete: " + id) if id[0] in _cmds['#']: id = _get_id_for_index(id) confirm = defaults.forceDelete if _supress == False: gist = _get_gist(id) print ('Gist \'{0}\' Description: {1}'.format(id, gist['description'])) for file in gist['files']: print (' ' + file) confirm = util.parseBool( util.readConsole(prompt='Delete Gist? (y/n):', required=False, bool=True) ) if confirm: url = '/gists/' + id api.delete(url) print 'Gist deleted: {0}'.format(id) else: print 'I did not delete the Gist.'
def _get_id_for_index(id): log.debug("_get_id_for_index: " + str(id)) _id = '' if id[0] in _cmds['#']: index = -1 try: index = int(id[1:]) except ValueError: log.error('Please use a valid number as the index.') return _id log.debug("Using index: " + str(index)) api.getCredentials() url = "/gists" gists = api.get(url) for (i, gist) in enumerate(gists): log.debug("Checking... gist {0} at index: {1} == {2} ?".format(gist['id'], (i+1), index)) if i+1 == index: _id = gist['id'] log.debug("Found Gist: {0} at index: {1}".format(_id, index)) break return _id
def _get_id_for_index(id): log.debug("_get_id_for_index: " + str(id)) _id = '' if id[0] in _cmds['#']: index = -1 try: index = int(id[1:]) except ValueError: log.error('Please use a valid number as the index.') return _id log.debug("Using index: " + str(index)) api.getCredentials() url = "/gists" gists = api.get(url) for (i, gist) in enumerate(gists): log.debug("Checking... gist {0} at index: {1} == {2} ?".format( gist['id'], (i + 1), index)) if i + 1 == index: _id = gist['id'] log.debug("Found Gist: {0} at index: {1}".format(_id, index)) break return _id
def search(): api.getCredentials() log.debug("Command: Search.")
def backup(): api.getCredentials() log.debug("Command: Backup.")
def search (): api.getCredentials() log.debug ("Command: Search.")
def backup (): api.getCredentials() log.debug ("Command: Backup.")