python 从https://github.com/github/gitignore下载指定语言/ IDE的.gitignore文件内容的Python脚本。的名字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从https://github.com/github/gitignore下载指定语言/ IDE的.gitignore文件内容的Python脚本。的名字相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python

""" 
gitignore.py: 

This program searches for specified module on GitHub/gitignore repository.
If the file is found on github, it is downloaded and created in the current working directory.
This program creates .gitignore if it does not exist or overwrites if it does exist.
"""

__author__      = "Harshad Kale"
__license__   	= "MIT"

import sys
import urllib2

subject = ''
location = ''
gitignore = ''

class Color:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

def file_exists(subject, searchGlobal=False):
	gitignore_url = 'https://raw.githubusercontent.com/github/gitignore/master/'
	
	global location

	if not searchGlobal:
		location = gitignore_url + subject + '.gitignore'
	else:
		location = gitignore_url + 'Global/' + subject + '.gitignore'

	print
	print 'Checking at ' + Color.OKBLUE + location + Color.ENDC

	request = urllib2.Request(location)
	request.get_method = lambda : 'HEAD'
	try:
		response = urllib2.urlopen(request)
		return True
	except urllib2.HTTPError:
		return False

def create_gitignore(mode='w+'):
	print
	print Color.OKGREEN + 'File Found at ' + Color.OKBLUE + location + Color.ENDC
	print

	global gitignore

	for line in urllib2.urlopen(location):
		gitignore += line
	pass

	file = open('.gitignore', mode)
	file.write(gitignore)
	file.close()

	print
	print Color.OKGREEN + 'Your .gitignore is created successfully' + Color.ENDC

def main(argv):
	print argv
	subject = argv[1]
	print Color.HEADER + 'Searching gitignore for - ' + subject + Color.ENDC

	if (file_exists(subject) or file_exists(subject.capitalize()) or
		file_exists(subject, True) or file_exists(subject.capitalize(), True)):
		if len(argv) > 2:
			mode = argv[2]
			if (mode == 'a' or mode == '-a' or
				mode == 'append' or mode == '-append'):
				create_gitignore('a')
			else:
				print 'Wrong argument.'
				print '[-a] to append to existing .gitignore file'
		else:
			create_gitignore('w+')
	else:
		print
		print
		print Color.FAIL
		print 'ERROR! File not found'
		print
		print 'NOTE - '
		print 'GitHub URLs are ' + Color.WARNING + 'case sensitive.' + Color.FAIL + ' If not found, this program tries to match for it by capitalizing.'
		print 'There is a chance that the gitignore file that you are looking for has camalized name for exmaple JetBrains, CakePHP'
		print 'Please check at ' + Color.OKBLUE + 'https://github.com/github/gitignore' + Color.FAIL + ' for exact location and name of your file'
		print Color.ENDC
		print
		print

if __name__ == '__main__':
	if sys.argv[0].startswith('python'):
		main(sys.argv[1])
	else:
		main(sys.argv)

以上是关于python 从https://github.com/github/gitignore下载指定语言/ IDE的.gitignore文件内容的Python脚本。的名字的主要内容,如果未能解决你的问题,请参考以下文章

python流程图绘制?

Python3网络爬虫实战-13部署相关库ScrapydClientScrapydAPI

Mycat快速入门教程

centos7下yourcompleteme安装

使用Docker镜像安装saltshaker

之前博客中的代码都放到github上