标签:
命令行生成各种文件模板
为python2.7制造 构件包括 命令行参数处理 文件读写 文件夹创建 网络读取
#!/usr/bin/env python # -*- coding: utf-8 -*- # <one line to give the program‘s name and a brief idea of what it does.> <project url> # Copyright (C) <2015> simplicity <simplicity@vip.sina.com> # # This program 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. # # This program 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 this program. If not, see <http://www.gnu.org/licenses/>. list_template = { ‘cmake‘ : ‘CMakeLists.txt.tpl‘, ‘default‘ : ‘DEFAULT.tpl‘, ‘makefile‘ : ‘Makefile.tpl‘, ‘readme‘ : ‘README.tpl‘, ‘c‘ : ‘TEMPLATE.c.tpl‘, ‘cls‘ : ‘TEMPLATE.cls.tpl‘, ‘cpp‘ : ‘TEMPLATE.cpp.tpl‘, ‘el‘ : ‘TEMPLATE.el.tpl‘, ‘go‘ : ‘TEMPLATE.go.tpl‘, ‘gyp‘ : ‘TEMPLATE.gyp.tpl‘, ‘h‘ : ‘TEMPLATE.h.tpl‘, ‘html‘ : ‘TEMPLATE.html.tpl‘, ‘java‘ : ‘TEMPLATE.java.tpl‘, ‘js‘ : ‘TEMPLATE.js.tpl‘, ‘pp‘ : ‘TEMPLATE.pp.tpl‘, ‘proto‘ : ‘TEMPLATE.proto.tpl‘, ‘py‘ : ‘TEMPLATE.py.tpl‘, ‘rst‘ : ‘TEMPLATE.rst.tpl‘, ‘sh‘ : ‘TEMPLATE.sh.tpl‘, ‘sty‘ : ‘TEMPLATE.sty.tpl‘, ‘tex‘ : ‘TEMPLATE.tex.tpl‘, } import sys import argparse # prepare arguments 准备参数 parser = argparse.ArgumentParser(description=‘Create file by template‘) parser.add_argument("name", nargs=‘?‘, default=‘‘, help="init file‘s name") parser.add_argument("-t", "--type", nargs=‘?‘, default=‘‘, dest=‘t‘, help="init file‘s type") parser.add_argument("-l", "--list", nargs=‘?‘, default=False, const=True, dest="islist", help="list all file‘s type") args = parser.parse_args() def print_type_list():
print ‘available template:‘
a = list_template.keys()
a.sort()
for n in a:
print ‘ %s‘ % n
if args.islist:
print_type_list()
exit() # check arguments 检查参数 file_name = args.name file_type_name = list_template.get(args.t, ‘‘) if file_type_name == ‘‘: print ‘unknown template [%s]‘ % args.t print_type_list() exit(2) if file_name == ‘‘: file_name = list_template[args.t].replace(‘.tpl‘, ‘‘) import os
# 准备缓存目录
if os.sys.platform == ‘win32‘:
home_path = os.getenv(‘HOMEDRIVE‘) + os.getenv(‘HOMEPATH‘)
else:
home_path = os.getenv(‘HOME‘)
local_template_path=os.path.join(home_path, ‘.cfinit‘)
if not os.path.exists(local_template_path): os.makedirs(local_template_path) local_template_filename = os.path.join(local_template_path, file_type_name) # get template 获取模板文件 if os.path.exists(local_template_filename): # local 本地缓存 print ‘get emplate for [%s]:[%s] from local‘ % (file_name, args.t) with open(local_template_filename, ‘rb‘) as f: with open(file_name, ‘wb‘) as f2: f2.write(f.read()); else: # remote 远程拉取 try:
import requests
except ImportError:
print ‘please run \‘pip install requests\‘‘
exit(3)
template_path = ‘https://raw.githubusercontent.com/xiaogaozi/princess-alist/master/home/xiaogaozi/.templates/%s‘ % file_type_name print ‘get template for [%s]:[%s] from remote‘ % (file_name, args.t) print ‘ %s‘ % file_type_name try: r = requests.get(template_path, stream=True) if r.status_code == 200: with open(local_template_filename, ‘wb‘) as f: with open(file_name, ‘wb‘) as f2: for chunk in r.iter_content(1024): f.write(chunk) f2.write(chunk) else: print ‘can\‘t find template for [%]‘ % file_type except: print sys.exc_info()
cf.init.bat (win32)
1. 保存以上文件在同一位置 如果你是直接拷贝网页中的代码 请自行规避 indent 缩进故障 不管你信不信 反正我踩到了
2. 加上权限 (linux)
3. 设定PATH路径
4. 为 python 安装 requests
$ pip install requests
5. 开始
$ cf.init -l
available template:
c
cls
cmake
cpp
default
el
go
gyp
h
html
java
js
makefile
pp
proto
py
readme
rst
sh
sty
tex
$ cf.init -t c a.c
$ ls
感谢xiaogaozi在github上提供的模板 虽然我和他完全不熟
标签:
原文地址:http://www.cnblogs.com/simplicity/p/4637068.html