#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) <2015> simplicity # # 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 . 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) 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()