jkong.py ( File view )

  • By 1111 2013-11-24
  • View(s):25
  • Download(s):2
  • Point(s): 1
			001	#coding=utf-8
002	import sys
003	import os
004	import datetime
005	import time
006	 
007	class ArgsDealwith:
008	     
009	    def arg_environment(self, args):
010	        filepath = ('PYTHON_PATH', 'path')
011	        for i in filepath:
012	            filename = os.environ.get(i,"%s is null." % i)
013	            print '%s:' % i,' ', filename
014	 
015	    def arg_en(self, args):
016	        filepath = ('PYTHON_PATH', 'path')
017	        for i in filepath:
018	            filename = os.environ.get(i,"%s is null." % i)
019	            print '%s:' % i, ' ', ' '.join(filename.split(';'))
020	 
021	    def arg_file(self, args):
022	        if not args:
023	            print 'Error: file name is null.'
024	        else:
025	            if len(args) == 1 and args[0].lower() == 'python_path':
026	                filename = os.environ.get(args[0], None)
027	                if filename:
028	                    args = filename.split(';')
029	                else:
030	                    print 'Error: "%s" is null' % args[0]
031	                    exit()
032	                 
033	            for i in args:
034	                if os.path.isfile(i):
035	                    try:
036	                        execfile(i)
037	                    except:
038	                        print 'Error: "%s" run failed.' % i
039	                else:
040	                    print 'Error: Not found "%s" file.' % i
041	             
042	    def arg_filetime(self, args):
043	        if not args:
044	            print 'Error: file name is null.'
045	        elif len(args) % 2 != 0:
046	            print 'Error: args isn't validate'
047	        else:
048	            fileTimes = [i for i in args if args.index(i) % 2 != 0 ]
049	            for i in fileTimes:
050	                try:
051	                    fileTimes[fileTimes.index(i)] = int(i)
052	                except Exception:
053	                    print 'Error: time args isn't validate'
054	                    exit()
055	                if i <= 0:
056	                    print 'Error: time<=0'
057	                    exit()                
058	                 
059	            minTime = fileTimes[0]
060	            for i in fileTimes:
061	                if i < minTime:
062	                    minTime = i
063	             
064	            strTime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
065	            tupleTime = tuple([int(i) for i in strTime.split('-')])
066	            begin = datetime.datetime(tupleTime[0], tupleTime[1], tupleTime[2], tupleTime[3], tupleTime[4], tupleTime[5])
067	             
068	             
069	            fileTimes = [[i, 0] for i in fileTimes]
070	            seconds = 0
071	            while True:
072	                strTime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
073	                tupleTime = tuple([int(i) for i in strTime.split('-')])
074	                end = datetime.datetime(tupleTime[0], tupleTime[1],tupleTime[2], tupleTime[3], tupleTime[4], tupleTime[5])
075	                 
076	                time_sub = end - begin
077	                seconds = time_sub.seconds
078	                begin = end
079	                for i in fileTimes:
080	                    i[1] += seconds
081	                 
082	                for i in fileTimes:
083	                    if i[1] == 0:
084	                        self.arg_file( [args[args.index(str(i[0])) - 1] ] )
085	                    elif i[1] >= i[0]:
086	                        i[1] %= i[0]
087	                        self.arg_file( [args[args.index(str(i[0])) - 1] ] )
088	                        if i[0] - i[1] < minTime:
089	                            minTime = i[0] - i[1]
090	                 
091	                time.sleep(minTime)
092	 
093	    def arg_help(self, args):
094	        strHelp = "Usage: ps [-options] [args...] where option include:"
095	        strHelp += """
096	        -? -help            print this help message
097	        -e -environment     print environment path
098	        -en                 print envrionment path per row
099	        -f -file:<file> [file2 file3...]
100	                            execute file(.py)     
101	        -ft -filetime:<file time> [file2 time2 file3 time3...]
102	                            execute file(.py) per time,
103	                            this run not stop,
104	                            but this command hasn't validate. 
105	                            time(seconds) must is interger and
106	                            not less than zero"""
107	        print strHelp
108	     
109	def arg_args():
110	    args_dic = {
'arg_help' : ['-?', '-help'], 'arg_environment' : ['-e', '-environment'],
111	                'arg_en' : ['-en'], 'arg_file' : ['-f', '-file'], 'arg_filetime' : ['-ft', 'filetime']
}
112	    argsCls = ArgsDealwith()
113	    if len(sys.argv) <= 1:
114	        argsCls.arg_help(sys.argv)
115	    else:
116	        argsFun = ''
117	        for i in sys.argv[1:]:
118	            bMath = False
119	            for j in args_dic.items():
120	                if i in j[1]:
121	                    argsFun = j[0]
122	                    bMath = True
123	                    break
124	            if bMath:
125	                break
126	        if argsFun:
127	            try:
128	                getattr(argsCls, argsFun)(sys.argv[2:])   
129	            except Exception, error:
130	                print error
131	                exit()
132	        else:
133	            print 'Error: "%s" isn't validate arg.' % ' '.join(sys.argv[1:])
134	            del argsCls                   
135	 
136	if __name__ == '__main__':
137	    arg_args()			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
jkong.py5.64 kB21-11-13 15:58
...
Sponsored links

jkong.py (1.70 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D