· 9 years ago · Oct 26, 2016, 10:34 AM
1#config.py
2definition = 'C:\\Users\\dlaevskiy\\PycharmProjects\\train\\new\\'
3databaseName = 'database.db'
4tableFileCount = 'filecount'
5tableFileProcessing = 'fileProcessing'
6tableRegistredUsers = 'users'
7in_directory = 'in'
8out_directory = 'out'
9
10#main file processing script
11import shutil
12import os
13import config
14import uuid
15import codecs
16from datetime import datetime
17
18from DatabaseProcessing import DatabaseProcess
19database = DatabaseProcess()
20
21class FileProcess(object):
22 def __init__(self):
23 pass
24
25 def createInFile(self, filename):
26 uniqid = str(uuid.uuid4())[:8]
27 processedName = filename + '.' + uniqid + '.txt'
28 whereCreateFile = (processedName, config.in_directory)
29 print 'Trying to create new empty file "%s" in directory "%s"' % whereCreateFile
30 if not os.path.isfile(config.definition + config.in_directory + processedName):
31 open(config.definition + config.in_directory + '\\' + processedName, 'w')
32 database.updateFileProcessedAndAllFiles()
33 print 'File "%s" created in directory "%s"' % whereCreateFile
34 else:
35 print 'File "%s" already exists in directory "%s"' % whereCreateFile
36
37 def writeInfoInFile(self, fileName, *args):
38 if os.path.isfile(config.definition + config.in_directory + fileName):
39 for record in args:
40 file = open(config.definition + config.in_directory + '\\' + fileName, 'a')
41 file.write(record + '\n')
42 file.close()
43 print 'New info has been added to the file: %s' % fileName
44 else:
45 print 'Try to add new info in file... No such file %s. You should create it first' % fileName
46
47 def deleteFile(self, directory, fileName):
48 if os.path.isfile(config.definition + directory + '\\' + fileName):
49 os.remove(config.definition + directory + '\\' + fileName)
50 database.updateFileDeletedAndAllFiles()
51 print 'File was deleted: %s' % fileName
52 else:
53 print 'Try to delete file... No such file "%s" in directory "%s"' % (fileName, directory)
54
55 def moveFile(self, fileName, directory1, directory2):
56 try:
57 shutil.move(config.definition + directory1 + '\\' + fileName, config.definition + directory2)
58 print 'File "%s" was moved from directory "%s" to directory "%s"' % (fileName, directory1, directory2)
59 except:
60 print 'Trying to move file "%s"... Nothing to move!' % fileName
61
62 def zipFile(self, file):
63 shutil.make_archive(config.definition + config.out_directory + '//' + os.path.basename(file), 'zip',
64 config.definition + config.out_directory + '//', os.path.basename(file))
65 self.deleteFile(config.out_directory, os.path.basename(file))
66 print 'File "%s" zipped' % os.path.basename(file)
67
68 def encodetoUTF8File(self, file):
69 filepath = config.definition + config.out_directory + '//' + os.path.basename(file)
70 BLOCKSIZE = 1048576
71 with codecs.open(filepath, "r", "Windows-1251") as sourceFile:
72 with codecs.open(filepath, "w", "utf-8") as targetFile:
73 while True:
74 contents = sourceFile.read(BLOCKSIZE)
75 if not contents:
76 break
77 targetFile.write(contents)
78 sourceFile.close()
79 targetFile.close()
80 print 'File "%s" encoded' % os.path.basename(file)
81
82 def processInFiles(self):
83 for file in os.listdir(config.definition + config.in_directory):
84 filename = os.path.basename(file)
85 splittedname = filename.split('.')
86 user = splittedname[0]
87 method = splittedname[1]
88 id = splittedname[2]
89 process_date = str(datetime.now().strftime('%Y-%m-%d'))
90 if database.isUserRegistered(user):
91 self.moveFile(os.path.basename(file), config.in_directory, config.out_directory)
92 database.updateProcessTable(user, method, id, process_date)
93 if method == 'ZIP':
94 self.zipFile(filename)
95 elif method == 'ENCODE':
96 self.encodetoUTF8File(filename)
97 else:
98 print "Incorrect method. Allowed types: ENCODE, ZIP. File was moved without processing"
99 else:
100 self.deleteFile(config.in_directory, filename)
101
102#database processing
103import sqlite3
104import config
105
106class DatabaseProcess(object):
107 def __init__(self):
108 self.connection = sqlite3.connect(config.definition + config.databaseName)
109 self.c = self.connection.cursor()
110
111 def createTableFileCount(self):
112 try:
113 self.c.execute('CREATE TABLE %s(FilesProcessed int, FilesDeleted int, AllFiles int)' % config.tableFileCount)
114 self.c.execute('INSERT INTO %s(FilesProcessed, FilesDeleted, AllFiles) VALUES (0,0,0)' % config.tableFileCount)
115 self.connection.commit()
116 print 'Table "%s" created.' % config.tableFileCount
117 except:
118 print 'Table "%s" is already created. Try to another one if it need.' % config.databaseName
119
120 def createTableUsers(self):
121 try:
122 self.c.execute('CREATE TABLE %s(RegistredUsers varchar(255))' % config.tableRegistredUsers)
123 self.connection.commit()
124 print 'Table "%s" created.' % config.tableRegistredUsers
125 except:
126 print 'Table "%s" is already created. Try to another one if it need.' % config.tableRegistredUsers
127
128 def createTableFileProcessing(self):
129 try:
130 self.c.execute('CREATE TABLE %s(User varchar(50), Method varchar(10), ID varchar(8), Date varchar(10))' % config.tableFileProcessing)
131 self.connection.commit()
132 print 'Table "%s" created.' % config.tableFileProcessing
133 except:
134 print 'Table "%s" is already created. Try to another one if it need.' % config.tableFileProcessing
135
136 def isUserRegistered(self, user):
137 currentusers = []
138 self.c.execute('SELECT RegistredUsers from users')
139 for element in self.c.fetchall():
140 currentusers.append(element[0])
141 return True if user in currentusers else False
142
143 def addRegistredUsers(self, *users):
144 for user in users:
145 if not self.isUserRegistered(user):
146 self.c.execute('INSERT INTO %s(RegistredUsers) VALUES ("%s")' % (config.tableRegistredUsers, user))
147 print 'User "%s" added.' % user
148 else:
149 print 'User "%s" already registered.' % user
150 self.connection.commit()
151
152 def deleteRegistredUsers(self, *users):
153 for user in users:
154 if self.isUserRegistered(user):
155 self.c.execute('DELETE FROM %s WHERE RegistredUsers="%s"' % (config.tableRegistredUsers, user))
156 print 'User "%s" deleted.' % user
157 else:
158 print 'There is no user "%s" in database.' % user
159 self.connection.commit()
160
161 def updateFileProcessedAndAllFiles(self):
162 self.c.execute('UPDATE filecount SET FilesProcessed = FilesProcessed + 1')
163 self.c.execute('UPDATE filecount SET AllFiles = AllFiles + 1')
164 self.connection.commit()
165
166 def updateFileDeletedAndAllFiles(self):
167 self.c.execute('UPDATE filecount SET FilesDeleted = FilesDeleted + 1')
168 self.c.execute('UPDATE filecount SET AllFiles = AllFiles - 1')
169 self.connection.commit()
170
171 def updateProcessTable(self, user, method, id, process_date):
172 self.c.execute('INSERT INTO %s(User, Method, ID, Date) VALUES ("%s", "%s", "%s", "%s")' % (config.tableFileProcessing, user, method, id, process_date))
173 self.connection.commit()
174
175 def cleanHistory(self, table):
176 self.c.execute('DELETE FROM %s' % table)
177 self.connection.commit()
178 print 'Table "%s" cleaned.' % table
179
180 def displayTable(self, tableName):
181 self.c.execute('SELECT * FROM %s' % tableName)
182 field_names = [i[0] for i in self.c.description]
183 print field_names
184 for element in self.c.fetchall():
185 print element
186
187 def connectToDatabase(self, databaseName):
188 self.connection = sqlite3.connect(config.definition + databaseName)
189 self.c = self.connection.cursor()
190
191 def closeConnection(self):
192 self.connection.close()