#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Taken by Kristen on 1/21/16."""
"""pathinfo.py:  Object to represent a snapshot of a file's status.

  For documentation in "literate programming" style, see:
    http://www.nmt.edu/help/lang/python/examples/pathinfo/
"""
import os, stat, time, sys
from itertools import chain


TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
class PathInfo:
	def __init__(self, path):
		self.path = path
		self.status = os.lstat(path)
		self.size = self.status[stat.ST_SIZE]
		self.createEpoch = self.status[stat.ST_CTIME]
		self.modEpoch = self.status[stat.ST_MTIME]
		self.mode = self.status[stat.ST_MODE]

	def is_dir(self):
		return stat.S_ISDIR(self.mode)

	def is_link(self):
		return stat.S_ISLNK(self.mode)

	def abs_path(self):
		return os.path.abspath(self.path)

	def real_path(self):
		return os.path.realpath(self.path)

	def mod_time(self):
		return time.strftime(TIME_FORMAT, time.localtime(self.modEpoch))

	def __str__(self):
		return ("%s%s %s %8d %s" % (self.__fileType(), self.__permFlags(), self.modTime(), self.size, self.path))

	def __file_type(self):
		if self.is_link():
			return 'l'
		if self.is_dir():
			return 'd'
		if self.is_file():
			return '-'
		else:
			return '?'

	def __permission_flags(self):
		return ("%s%s%s" % (self.__rwx(self.mode & stat.S_IRUSR, self.mode & stat.S_IWUSR, self.mode & stat.S_IXUSR),
		                    self.__rwx(self.mode & stat.S_IRGRP, self.mode & stat.S_IWGRP, self.mode & stat.S_IXGRP),
		                    self.__rwx(self.mode & stat.S_IROTH, self.mode & stat.S_IWOTH, self.mode & stat.S_IXOTH)))

	def __rwx(self, r, w, x):
		return ("%s%s%s" % (self.__dasher(r, "r"), self.__dasher(w, "w"), self.__dasher(x, "x")))

	def __dasher(self, bit, flag):
		if bit:
			return flag
		else:
			return "-"

	def __cmp__(self, other):
		return cmp(self.path, other.path)

	def owner_can_read(self):
		return self.mode & stat.S_IRUSR

	def owner_can_write(self):
		return self.mode & stat.S_IWUSR

	def owner_can_exec(self):
		return self.mode & stat.S_IXUSR

	def group_can_read(self):
		return self.mode & stat.S_IRGRP

	def group_can_write(self):
		return self.mode & stat.S_IWGRP

	def group_can_exec(self):
		return self.mode & stat.S_IXGRP

	def world_can_read(self):
		return self.mode & stat.S_IROTH

	def world_can_write(self):
		return self.mode & stat.S_IWOTH

	def world_can_exec(self):
		return self.mode & stat.S_IXOTH

def paths(path): return PathInfo(path)

class Pig(PathInfo):
	def __cmp__(self, other):
		compare = - cmp(self.size(), other.size())
		if compare != 0:
			# print(compare)
			return compare
		else:
			# print(self.path, other.path)
			return cmp(self.path, other.path)

	def __str__(self):
		return "%s %10s %s" % (self.modTime(), self.size(), self.path)

def Visitor(file):
	megafile = []
	files = []
	dirName = os.path.abspath(file)
	# print(dirName)
	nameList = os.listdir(file)
	# print(nameList)
	for fileName in nameList:
		info = os.path.join(dirName, fileName)
		files.append(info)  # Append directory entry
	return files

files = []
try:
	root = sys.argv[1]  # First command line argument = directory name
except:
	root = "."  # Default is current directory

os.walk(root, Visitor, None)
files.sort()


pathfiles= ['/Users/Kristen/Library/Python/3.4/lib/python/site-packages',
            '/Users/Kristen/Downloads/conceptnet5-master',
            '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
            '/Users/Kristen/PycharmProjects/MyProject',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/aeosa',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/readline-6.2.4.1-py3.4-macosx-10.6-intel.egg',
            '/Users/Kristen/.conceptnet5', '/Users/Kristen/PycharmProjects/Git',
            '/Applications/PyCharm.app/Contents/helpers/pydev', '/Users/Kristen/MyPython',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip', '/Users/Kristen/git/sigmakee',
            '/Users/Kristen/PycharmProjects/Other',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages',
            '/Applications/PyCharm.app/Contents/helpers/pycharm',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/bottle-0.12.9-py3.4.egg',
            '/anaconda/lib/python3.4/site-packages', '/Users/Kristen/PycharmProjects/proj/iReveal/mysite',
            '/Users/Kristen/Library/Preferences/PyCharm50/scratches',
            '/Users/Kristen/Library/Python/3.4/lib/python/site-packages/quickgui',
            '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload',
            '/Users/Kristen/PycharmProjects/Formal-Concept-Analysis-master', '/Users/Kristen/PycharmProjects/proj']

def main():
	def paths(path): return PathInfo(path)
	import argparse
	parser = argparse.ArgumentParser()
	parser.add_argument('input', help="The list of major paths on which to search")
	parser.add_argument('output', help="The file to save the info")
	args = parser.parse_args()

if __name__ == '__main__':
	main()

