Coverage for GuiBulkImport.py: 0%

79 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-07 02:19 +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 

22# import L10n 

23# _ = L10n.get_translation() 

24# Standard Library modules 

25import os 

26import sys 

27from time import time 

28 

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

30 

31# fpdb/FreePokerTools modules 

32 

33 

34import Importer 

35 

36import Configuration 

37 

38 

39import logging 

40 

41if __name__ == "__main__": 

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

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

44log = logging.getLogger("importer") 

45 

46 

47class GuiBulkImport(QWidget): 

48 # CONFIGURATION - update these as preferred: 

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

50 

51 def load_clicked(self): 

52 stored = None 

53 dups = None 

54 partial = None 

55 skipped = None 

56 errs = None 

57 ttime = None 

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

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

60 if self.settings["global_lock"].acquire( 

61 wait=False, source="GuiBulkImport" 

62 ): # 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 = ( 

84 "Bulk import done: Stored: %d, Duplicates: %d, Partial: %d, Skipped: %d, Errors: %d, Time: %s seconds, Stored/second: %.0f" 

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

86 print(completionMessage) 

87 log.info(completionMessage) 

88 

89 self.importer.clearFileList() 

90 

91 self.settings["global_lock"].release() 

92 else: 

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

94 

95 def get_vbox(self): 

96 """returns the vbox of this thread""" 

97 return self.layout() 

98 

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

100 QWidget.__init__(self, parent) 

101 self.settings = settings 

102 self.config = config 

103 

104 self.importer = Importer.Importer(self, self.settings, config, sql, self) 

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( 

130 self, caption=("Please choose the path that you want to Auto Import"), directory=self.importDir.text() 

131 ) 

132 if newdir: 

133 self.importDir.setText(newdir) 

134 

135 

136if __name__ == "__main__": 

137 config = Configuration.Config() 

138 settings = {} 

139 if os.name == "nt": 

140 settings["os"] = "windows" 

141 else: 

142 settings["os"] = "linuxmac" 

143 

144 settings.update(config.get_db_parameters()) 

145 settings.update(config.get_import_parameters()) 

146 settings.update(config.get_default_paths()) 

147 import interlocks 

148 

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

150 settings["cl_options"] = ".".join(sys.argv[1:]) 

151 

152 # from PyQt5.QtWidgets import QApplication, QMainWindow 

153 # app = QApplication(sys.argv) 

154 # main_window = QMainWindow() 

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

156 # main_window.show() 

157 # main_window.resize(600, 100) 

158 # app.exec_()