Coverage for GuiBulkImport.py: 0%

78 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-28 16:41 +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() 

23# Standard Library modules 

24import os 

25import sys 

26from time import time 

27 

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

29 

30# fpdb/FreePokerTools modules 

31 

32 

33import Importer 

34 

35import Configuration 

36 

37 

38import logging 

39if __name__ == "__main__": 

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

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

42log = logging.getLogger("importer") 

43 

44class GuiBulkImport(QWidget): 

45 # CONFIGURATION - update these as preferred: 

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

47 

48 def load_clicked(self): 

49 stored = None 

50 dups = None 

51 partial = None 

52 skipped = None 

53 errs = None 

54 ttime = None 

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

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

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

58 #try: 

59 # get the dir to import from the chooser 

60 selected = self.importDir.text() 

61 

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

63 

64 self.importer.setHandsInDB(self.n_hands_in_db) 

65 self.importer.setMode('bulk') 

66 

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

68 self.importer.setCallHud(False) 

69 

70 starttime = time() 

71 

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

73 

74 ttime = time() - starttime 

75 if ttime == 0: 

76 ttime = 1 

77 

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

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

80 print(completionMessage) 

81 log.info(completionMessage) 

82 

83 self.importer.clearFileList() 

84 

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

86 else: 

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

88 

89 def get_vbox(self): 

90 """returns the vbox of this thread""" 

91 return self.layout() 

92 

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

94 QWidget.__init__(self, parent) 

95 self.settings = settings 

96 self.config = config 

97 

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

99 

100 

101 self.setLayout(QVBoxLayout()) 

102 

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

104 hbox = QHBoxLayout() 

105 hbox.addWidget(self.importDir) 

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

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

108 hbox.addWidget(self.chooseButton) 

109 self.layout().addLayout(hbox) 

110 

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

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

113 self.layout().addWidget(self.load_button) 

114 

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

116 tcursor = self.importer.database.cursor 

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

118 row = tcursor.fetchone() 

119 tcursor.close() 

120 self.importer.database.rollback() 

121 self.n_hands_in_db = row[0] 

122 

123 def browseClicked(self): 

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

125 directory=self.importDir.text()) 

126 if newdir: 

127 self.importDir.setText(newdir) 

128 

129if __name__ == '__main__': 

130 config = Configuration.Config() 

131 settings = {} 

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

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

134 

135 settings.update(config.get_db_parameters()) 

136 settings.update(config.get_import_parameters()) 

137 settings.update(config.get_default_paths()) 

138 import interlocks 

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

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

141 

142 # from PyQt5.QtWidgets import QApplication, QMainWindow 

143 # app = QApplication(sys.argv) 

144 # main_window = QMainWindow() 

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

146 # main_window.show() 

147 # main_window.resize(600, 100) 

148 # app.exec_()