#!/usr/bin/env python
#coding: utf-8
#
# keycut キーカットプログラム（Open usp Tukubai版）
# 
# designed by Nobuaki Tounaka
# written by Yoshio Katayama
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "keycut [options] <filename> <file>"
_option = "-d : キーの削除"
_option1 = "-a : ファイル追記"
_option2 = "-z : 圧縮"
_version = "Wed Oct 26 03:01:58 JST 2011"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"
_keypat = r'([^%]*)%(\d+)((\.\d{1,3})?)((\.\d{1,3})?)((\b|\D).*)'
		# 1: string before '%'
		# 2: field number
		# 3: '.' + offset
		# 5: '.' + length
		# 7: string after field specifier

import re
import os
import sys
import stat
import gzip
from getopt import getopt

def error(msg, *arg):
	print >> sys.stderr, 'Error[keycut] :', msg % arg
	sys.exit(1)

def usage():
	print >> sys.stderr, "Usage   :", _usage
	print >> sys.stderr, "Option  :", _option
	print >> sys.stderr, "        :", _option1
	print >> sys.stderr, "        :", _option2
	print >> sys.stderr, "Version :", _version
	sys.exit(1)

class FieldLine:
	def __init__(self, line, allow_z = False):
		self.__allow_zero = allow_z
		line = line.rstrip('\n')
		self.__fields = [ line ]
		self.__fields += [ x for x in line.split(' ') if x ]

	def size(self):
		return len(self.__fields) - 1

	def getFieldNum(self, key):
		if type(key) == type(0):
			return key
		if re.match(r'\d+$', key):
			key = int(key)
		elif key == 'NF':
			key = self.size()
		else:
			key = self.size() - int(key[3:])
			if key <= 0:
				error("NF-x の x が大きすぎます。")
		if key < 0:
			error("フィールド番号が負です。")
		if key == 0 and not self.__allow_zero:
			error("フィールド番号が０です。")
		if key > self.size():
			error("フィールド番号が大きすぎます。")
		return key

	def getField(self, s, e = None):
		s = self.getFieldNum(s)
		if e == None:
			e = s
		else:
			e = self.getFieldNum(e)
		if s <= e:
			return ' '.join(self.__fields[s : e + 1])
		else:
			t = self.__fields[e : s + 1]
			t.reverse()
			return ' '.join(t)

class FileName:
	def __init__(self, str):
		self.__fname = []
		self.__del = set([])
		while '%' in str:
			r = re.match(_keypat, str);
			if not r:
				error("%% のみの引数は認められません。")
			self.__fname += [ r.group(1) ]
			fld, off, end = int(r.group(2)), 0, None
			self.__del |= set([ fld ])
			if r.group(3):
				off = max(int(r.group(3)[1:]) - 1, 0)
			if r.group(5):
				end = off + int(r.group(5)[1:])
			self.__fname += [ (fld, off, end) ]
			str = r.group(7)
		self.__fname += [ str ]

	def generate(self, line):
		str = ''
		for x in self.__fname:
			if type(x) == type(''):
				str += x
			else:
				f = line.getField(x[0])
				if x[1] >= len(f):
					error("部分文字列のオフセットが大きすぎます。")
				str += f[x[1]:x[2]]
		return str

	def delset(self):
		return self.__del

#
# キーフィールドの削除
#
def delkey(line, delset):
	fld = list(set(range(1, line.size() + 1)) - delset)
	fld.sort()
	return ' '.join([ line.getField(i) for i in fld ])

#
# 入力ファイルオープン
#
def open_file(n, mode = 'r'):
	if n >= len(sys.argv) or sys.argv[n] == '-':
		file = sys.stdin
	else:
		try:
			file = open(sys.argv[n], mode)
		except:
			error("ファイル %s をオープンできません。", sys.argv[n])
	return file

#
# パスの分解
#
def path_dir(path):
	p = path.split('/')
	return map(lambda x: x + '/', p[:-1])

#
# ディレクトリの判定
#
def isdir(path):
	try:
		return stat.S_ISDIR(os.stat(path).st_mode)
	except:
		return False

#
# 出力ファイルオープン
#
def create_file(fname, mode, zip):
	path = path_dir(fname)
	fn = ''
	for p in path:
		fn += p
		if not isdir(fn):
			try:
				os.mkdir(fn)
			except:
				error("ディレクトリ %s を作れません。", fn)
	try:
		if zip:
			file = gzip.open(fname, mode, 6)
		else:
			file = open(fname, mode)
	except:
		error("ファイル %s を作れません。", fname)
	return file

#
#メイン関数
#
if __name__ == '__main__':

	if len(sys.argv) < 2 \
	  or sys.argv[1] == '--help' \
	  or sys.argv[1] == '--version':
		usage()

	#
	# オプションの取得
	#
	mode, delete, zip = 'w', False, False
	try:
		opts = getopt(sys.argv[1:], "adz")
		for opt in opts[0]:
			if opt[0] == '-a':
				mode = 'a'
			elif opt[0] == '-d':
				delete = True
			else:
				zip = True
		if not opts[1]:
			usage()
		sys.argv = sys.argv[0:1] + opts[1]
	except:
		usage()

	#
	# ファイル名文字列の解析
	#
	fname = FileName(sys.argv[1])

	#
	# 入力ファイルオープン
	#
	file = open_file(2)

	#
	# メインループ
	#
	ofn = ''
	for line in file:
		line = FieldLine(line)
		fn = fname.generate(line)
		if fn != ofn:
			if ofn:
				ofile = create_file(ofn, mode, zip)
				ofile.write(lines)
				ofile.flush()
				ofile.close()
			ofn = fn
			lines = ''
		if delete:
			lines += delkey(line, fname.delset()) + '\n'
		else:
			lines += line.getField(0) + '\n'

	if ofn:
		ofile = create_file(ofn, mode, zip)
		ofile.write(lines)
		ofile.flush()
		ofile.close()

	sys.exit(0)
