Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.09 KB | None | 0 0
  1. import time
  2. from time import time
  3.  
  4. class OperationClass:
  5.  
  6.      def __init__(self):
  7.           pass
  8.  
  9.      def connectDB(self):
  10.           pass
  11.  
  12.      def disconnectDB(self):
  13.           pass
  14.  
  15.      def insertJsonFromDirPath(self, path_source, type_db):
  16.           import json
  17.           import glob
  18.           json_data = []
  19.           path_files = glob.glob(path_source+'\\*')
  20.           try:
  21.                if type_db == 'PostgreSQL':
  22.                     for path_file in path_files:
  23.                          with open(path_file,'r') as json_file:
  24.                               json_data.append(json.load(json_file))
  25.                     self.jsonIn(json_data)
  26.                
  27.           except:
  28.                return(0)
  29.           return(True)
  30.  
  31.      def insertImagesFromDirPath(self, path_source, path_target):
  32.           import ntpath
  33.           from ntpath import basename
  34.           import glob
  35.          
  36.           path_files = glob.glob(path_source+'\\*')
  37.           self.__deleteFilesTarget(path_target, glob.glob)
  38.           count = 0
  39.           try:
  40.                for path_file in path_files:
  41.                     count = count + 1
  42.                     self.doInsert(path_file, ntpath.basename(path_file), count)
  43.           except:
  44.                     return(0)
  45.           return(True)
  46.  
  47.      def jsonIn(self, json_list):
  48.           pass
  49.      
  50.      def doInsert(self, path_file, file_name, count):
  51.           pass
  52.  
  53.      def extractAllImages(self):
  54.           pass
  55.  
  56.      def clearTable(self):
  57.           pass
  58.  
  59.      def getTime(self):
  60.           pass
  61.  
  62.      def __deleteFilesTarget(self, path_target, glob):
  63.           import os
  64.           from os import unlink
  65.           files = glob(path_target+'\\*')
  66.           for fl in files:
  67.                unlink(fl)
  68.  
  69. class RunDB:
  70.  
  71.      def __init__(self, path_source, path_target, type_db):
  72.           self.__path_source = path_source
  73.           self.__path_target = path_target
  74.           self.__type_db = type_db
  75.  
  76.      def defineDB(self):
  77.           if self.__type_db == 'MySQL':
  78.                self.__objDB = MySQL(self.__path_source, self.__path_target)
  79.           elif self.__type_db == 'Cassandra':
  80.                self.__objDB = Cassandra(self.__path_source, self.__path_target)
  81.           elif self.__type_db == 'PostgreSQL':
  82.                self.__objDB = PostgreSQL(self.__path_source, self.__path_target)
  83.           elif self.__type_db == 'MongoDB':
  84.                self.__objDB = MongoDB(self.__path_source, self.__path_target)
  85.           else:
  86.                return(1)
  87.  
  88.           if self.__objDB is None:
  89.                return(2)
  90.           else:
  91.                try:
  92.                     self.__objDB.connectDB()
  93.                except:
  94.                     return(3)
  95.           return(True)
  96.  
  97.      def insertImageInDB(self):
  98.           self.__objDB.clearTable()
  99.           return(self.__objDB.insertImagesFromDirPath(self.__path_source, self.__path_target))
  100.  
  101.      def insertJsonInDB(self):
  102.           return(self.__objDB.insertJsonFromDirPath(self.__path_source,self.__type_db))
  103.  
  104.      def extractImageFromDB(self):
  105.           return(self.__objDB.extractAllImages())
  106.  
  107.      def disconnectDB(self):
  108.           self.__objDB.disconnectDB()
  109.  
  110.      def getTime(self):
  111.           return(self.__objDB.getTime()
  112.  
  113. class PostgreSQL(OperationClass):
  114.      
  115.      def __init__(self, path_source, path_target):
  116.           self.__path_source = path_source
  117.           self.__path_target = path_target
  118.           self.__ins_query = 'INSERT INTO tab_data(data_file,name_file) VALUES(%s,%s)'
  119.           self.__ins_js_query = 'INSERT INTO tab_json(color) VALUES(%s)'
  120.           self.__clr_tab = 'TRUNCATE TABLE tab_data'
  121.           self.__extract_all = 'SELECT data_file,name_file FROM tab_data'
  122.           self.__timeSum = {'timeInsert':0,'timeExtract':0}
  123.  
  124.      def connectDB(self):
  125.           sql = self.__importPostgre()
  126.           self.__conn = sql.connect(host = 'localhost', port = '5433', dbname = 'postgres', user = 'postgres', password = 'ale89')
  127.           self.__cur = self.__conn.cursor()
  128.  
  129.      def disconnectDB(self):
  130.           self.__conn.close()
  131.  
  132.      def doInsert(self,path_file, file_name, count):
  133.           self.__insertBlobImage(path_file, file_name)
  134.  
  135.      def jsonIn(self,json_list):
  136.           self.__insertJson(json_list)
  137.  
  138.      def __insertBlobImage(self, path_file, file_name):
  139.           blob = open(path_file,'rb').read()
  140.           args = (blob,file_name,)
  141.           begin_time = time()
  142.           self.__cur.execute(self.__ins_query,args)
  143.           self.__conn.commit()
  144.           end_time = time()
  145.           self.__timeSum['timeInsert'] += (end_time - begin_time)
  146.  
  147.      def __insertJson(self, json_list):
  148.           js = {}
  149.           for indx,x in enumerate(json_list):
  150.                js[indx] = x
  151.  
  152.           elements = js.get(0)
  153.           for element in elements:
  154.                args(element['color'])
  155.                begin_time = time()
  156.                self.__cur.execute(self.__ins_js_query,args)
  157.                self.__conn.commit()
  158.                end_time = time()
  159.                self.__timeSum['timeInsert'] += (end_time - begin_time)
  160.          
  161.      def clearTable(self):
  162.           self.__cur.execute(self.__clr_tab)
  163.           self.__conn.commit()
  164.  
  165.      def extractAllImages(self):
  166.           try:
  167.                import io
  168.                from io import BytesIO
  169.                import PIL
  170.                from PIL import Image
  171.  
  172.                begin_time = time()              
  173.                self.__cur.execute(self.__extract_all)
  174.                end_time = time()
  175.                self.__timeSum['timeExtract'] += (end_time - begin_time)
  176.                res = self.__cur.fetchall()
  177.                for x in res:
  178.                     data = BytesIO(x[0])
  179.                     img = Image.open(data)
  180.                     path_file = self.__path_target + '\\' + x[1]
  181.                     img.save(path_file)
  182.           except:
  183.                self.disconnectDB()
  184.                return(4)
  185.           return(True)
  186.  
  187.      def getTime(self):
  188.           return(self.__timeSum)
  189.  
  190.      def __importPostgre(self):
  191.           import psycopg2
  192.           return (psycopg2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement