Coverage for GuiBulkImport.py: 0%

83 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-27 18:50 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3 

4#Copyright 2008-2011 Steffen Schaumburg 

5#This program is free software: you can redistribute it and/or modify 

6#it under the terms of the GNU Affero General Public License as published by 

7#the Free Software Foundation, version 3 of the License. 

8# 

9#This program is distributed in the hope that it will be useful, 

10#but WITHOUT ANY WARRANTY; without even the implied warranty of 

11#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

12#GNU General Public License for more details. 

13# 

14#You should have received a copy of the GNU Affero General Public License 

15#along with this program. If not, see <http://www.gnu.org/licenses/>. 

16#In the "official" distribution you can find the license in agpl-3.0.txt. 

17 

18from __future__ import print_function 

19from __future__ import division 

20from past.utils import old_div 

21#import L10n 

22#_ = L10n.get_translation() 

23from L10n import set_locale_translation 

24# Standard Library modules 

25import os 

26import sys 

27from time import time 

28from optparse import OptionParser 

29import traceback 

30 

31from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QFileDialog 

32 

33# fpdb/FreePokerTools modules 

34 

35import Options 

36 

37import Importer 

38 

39import Configuration 

40 

41import Exceptions 

42 

43import logging 

44if __name__ == "__main__": 

45 Configuration.set_logfile("fpdb-log.txt") 

46# logging has been set up in fpdb.py or HUD_main.py, use their settings: 

47log = logging.getLogger("importer") 

48 

49class GuiBulkImport(QWidget): 

50 # CONFIGURATION - update these as preferred: 

51 allowThreads = False # set to True to try out the threads field 

52 

53 def load_clicked(self): 

54 stored = None 

55 dups = None 

56 partial = None 

57 skipped = None 

58 errs = None 

59 ttime = None 

60 # Does the lock acquisition need to be more sophisticated for multiple dirs? 

61 # (see comment above about what to do if pipe already open) 

62 if self.settings['global_lock'].acquire(wait=False, source="GuiBulkImport"): # returns false immediately if lock not acquired 

63 #try: 

64 # get the dir to import from the chooser 

65 selected = self.importDir.text() 

66 

67 # get the import settings from the gui and save in the importer 

68 

69 self.importer.setHandsInDB(self.n_hands_in_db) 

70 self.importer.setMode('bulk') 

71 

72 self.importer.addBulkImportImportFileOrDir(selected, site = 'auto') 

73 self.importer.setCallHud(False) 

74 

75 starttime = time() 

76 

77 (stored, dups, partial, skipped, errs, ttime) = self.importer.runImport() 

78 

79 ttime = time() - starttime 

80 if ttime == 0: 

81 ttime = 1 

82 

83 completionMessage = ('Bulk import done: Stored: %d, Duplicates: %d, Partial: %d, Skipped: %d, Errors: %d, Time: %s seconds, Stored/second: %.0f')\ 

84 % (stored, dups, partial, skipped, errs, ttime, old_div((stored+0.0), ttime)) 

85 print(completionMessage) 

86 log.info(completionMessage) 

87 

88 self.importer.clearFileList() 

89 

90 self.settings['global_lock'].release() 

91 else: 

92 print(("bulk import aborted - global lock not available")) 

93 

94 def get_vbox(self): 

95 """returns the vbox of this thread""" 

96 return self.layout() 

97 

98 def __init__(self, settings, config, sql = None, parent = None): 

99 QWidget.__init__(self, parent) 

100 self.settings = settings 

101 self.config = config 

102 

103 self.importer = Importer.Importer(self, self.settings, config, sql, self, zmq_port="5557") 

104 

105 

106 self.setLayout(QVBoxLayout()) 

107 

108 self.importDir = QLineEdit(self.settings['bulkImport-defaultPath']) 

109 hbox = QHBoxLayout() 

110 hbox.addWidget(self.importDir) 

111 self.chooseButton = QPushButton('Browse...') 

112 self.chooseButton.clicked.connect(self.browseClicked) 

113 hbox.addWidget(self.chooseButton) 

114 self.layout().addLayout(hbox) 

115 

116 self.load_button = QPushButton(('Bulk Import')) 

117 self.load_button.clicked.connect(self.load_clicked) 

118 self.layout().addWidget(self.load_button) 

119 

120# see how many hands are in the db and adjust accordingly 

121 tcursor = self.importer.database.cursor 

122 tcursor.execute("Select count(1) from Hands") 

123 row = tcursor.fetchone() 

124 tcursor.close() 

125 self.importer.database.rollback() 

126 self.n_hands_in_db = row[0] 

127 

128 def browseClicked(self): 

129 newdir = QFileDialog.getExistingDirectory(self, caption=("Please choose the path that you want to Auto Import"), 

130 directory=self.importDir.text()) 

131 if newdir: 

132 self.importDir.setText(newdir) 

133 

134if __name__ == '__main__': 

135 config = Configuration.Config() 

136 settings = {} 

137 if os.name == 'nt': settings['os'] = 'windows' 

138 else: settings['os'] = 'linuxmac' 

139 

140 settings.update(config.get_db_parameters()) 

141 settings.update(config.get_import_parameters()) 

142 settings.update(config.get_default_paths()) 

143 import interlocks 

144 settings['global_lock'] = interlocks.InterProcessLock(name="fpdb_global_lock") 

145 settings['cl_options'] = '.'.join(sys.argv[1:]) 

146 

147 # from PyQt5.QtWidgets import QApplication, QMainWindow 

148 # app = QApplication(sys.argv) 

149 # main_window = QMainWindow() 

150 # main_window.setCentralWidget(GuiBulkImport(settings, config)) 

151 # main_window.show() 

152 # main_window.resize(600, 100) 

153 # app.exec_()