コード例 #1
0
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']))
コード例 #2
0
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']))
コード例 #3
0
ファイル: actions.py プロジェクト: eliheady/gists.cli
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'])
コード例 #4
0
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.')
コード例 #5
0
ファイル: actions.py プロジェクト: khilnani/gists.cli
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'])
コード例 #6
0
ファイル: actions.py プロジェクト: khilnani/gists.cli
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'])
コード例 #7
0
def get(id, path, fileName=''):
    log.debug(
        "Downloading Gist with Id '{0}' (fileName: {1}) to '{2}'.".format(
            id, fileName, path))

    if id[0] in _cmds['#']:
        id = _get_id_for_index(id)

    if id:
        gist = _get_gist(id)
        target = os.path.join(path, id)

        print(('Gist \'{0}\' has {1} file(s)'.format(id, len(gist['files']))))
        for file in gist['files']:
            print(('  ' + file))
        dmsg = 'file(s)' if fileName == '' else "'" + fileName + "'"
        confirm = util.readConsole(
            prompt="Download {0} to (1) '{1}/' or (2) '{2}/'?: ".format(
                dmsg, path, target))
        if confirm in ('1', '2'):
            filesDownloaded = 0
            try:
                if not os.path.isdir(path):
                    os.makedirs(path)
                if confirm == '1':
                    target = path
                else:
                    os.makedirs(target)
                for (file, data) in list(gist['files'].items()):
                    downLoadFile = False
                    if fileName != '':
                        if fileName.strip().lower() == file.strip().lower():
                            downLoadFile = True
                    else:
                        downLoadFile = True
                    if downLoadFile == True:
                        content = data['content']
                        filepath = os.path.join(target, file)
                        file = open(filepath, 'w')
                        file.write(content)
                        file.close()
                        filesDownloaded += 1
                        log.debug('Saved file:' + filepath)
                print(('{0} File(s) downloaded.'.format(filesDownloaded)))
            except Exception as e:
                print("Insufficient privilages to write to %s." % target)
                print("Error message: " + str(e))
        else:
            print('Ok. I won\'t download the Gist.')
コード例 #8
0
ファイル: actions.py プロジェクト: khilnani/gists.cli
def get (id, path, fileName=''):
  log.debug ("Downloading Gist with Id '{0}' (fileName: {1}) to '{2}'.".format (id, fileName, path))

  if id[0] in _cmds['#']:
    id = _get_id_for_index(id)

  if id:
    gist = _get_gist(id)
    target = os.path.join(path,id)

    print ('Gist \'{0}\' has {1} file(s)'.format(id, len(gist['files'])))
    for file in gist['files']:
      print ('  ' + file)
    dmsg = 'file(s)' if fileName == '' else "'" + fileName + "'"
    confirm = util.readConsole(prompt="Download {0} to (1) '{1}/' or (2) '{2}/'?: ".format(dmsg, path, target))
    if confirm in ('1', '2'):
      filesDownloaded = 0
      try:
        if not os.path.isdir(path):
          os.makedirs(path)
        if confirm == '1':
          target = path
        else:
          os.makedirs(target)
        for (file, data) in gist['files'].items():
          downLoadFile = False
          if fileName != '':
            if fileName.strip().lower() == file.strip().lower():
              downLoadFile = True
          else:
            downLoadFile = True
          if downLoadFile == True:
            content = data['content']
            filepath = os.path.join(target,file)
            file = open( filepath , 'w')
            file.write(content)
            file.close()
            filesDownloaded += 1
            log.debug( 'Saved file:' + filepath )
        print ('{0} File(s) downloaded.'.format(filesDownloaded))
      except Exception as e:
        print "Insufficient privilages to write to %s." % target
        print "Error message: " + str(e)
    else:
      print 'Ok. I won\'t download the Gist.'
コード例 #9
0
ファイル: actions.py プロジェクト: khilnani/gists.cli
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.'