ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Gromulus/Gromulus_Lib.py
Revision: 805
Committed: Thu Dec 14 02:42:16 2023 UTC (2 years, 3 months ago) by nino.borges
Content type: text/x-python
File size: 5837 byte(s)
Log Message:
Added the system table and added methods for importing roms.

File Contents

# User Rev Content
1 nino.borges 795 """
2     This creates the main library of methods for Gromulus.
3    
4     """
5    
6 nino.borges 805 import sqlite3, configparser, shutil
7 nino.borges 795 import xml.etree.ElementTree as ET
8     import Tool_Box.NinoGenTools, os
9    
10    
11 nino.borges 805 config = configparser.ConfigParser()
12     config.read("/home/nino/.gromulus/gromulus_cfg.ini")
13 nino.borges 795
14 nino.borges 805 conn = sqlite3.connect(config.get('RootConfig','databasePath')) #"GromulusDatabase.db")
15 nino.borges 795
16    
17     def ImportNewNoIntroDat(datPath):
18     """Deletes all of the data in the two No_Intro tables and load the info from the new dat."""
19     tree = ET.parse(datPath)
20     root = tree.getroot()
21    
22     ## First gather information from header
23     for elm in root.findall('header'):
24     headerID = elm.find('id').text
25     headerName = elm.find('name').text
26     headerDesc = elm.find('description').text
27     headerVer = elm.find('version').text
28    
29     #print(headerID)
30     #print (headerName)
31     #print(headerDesc)
32     #print(headerVer)
33     conn.execute("""DELETE FROM no_intro_system""" )
34     conn.execute(f'INSERT INTO no_intro_system VALUES ({headerID}, "{headerName}", "{headerDesc}", "{headerVer}")')
35    
36     conn.execute("""DELETE FROM no_intro_main""" )
37     for elm in root.findall('game'):
38     gameName = elm.get('name')
39     gameID = elm.get('id')
40     gameCloneOfID = elm.get('cloneofid')
41     gameDesc = elm.find('description').text
42    
43     romName = elm.find('rom').get('name')
44     romSize = elm.find('rom').get('size')
45     romCRC = elm.find('rom').get('crc')
46     romMD5 = elm.find('rom').get('md5')
47     romSHA1 = elm.find('rom').get('sha1')
48     romSHA256 = elm.find('rom').get('sha256')
49     romStatus = elm.find('rom').get('status')
50     conn.execute(f'INSERT INTO no_intro_main VALUES ("{gameName}", "{gameID}", "{gameCloneOfID}", "{gameDesc}", "{romName}", "{romCRC}", "{romMD5}", "{romSHA1}", "{romSHA256}", "{romStatus}", {headerID})')
51    
52     conn.commit()
53     conn.close()
54    
55    
56 nino.borges 805 def AddNewSystem(systemName, shortName, relativePath):
57     """Adding a single system, with path where the ROMs will live."""
58     conn.execute(f'INSERT INTO main_app_system("name", "short_name", "relative_file_path")VALUES ("{systemName}", "{shortName}", "{relativePath}")')
59     conn.commit()
60     conn.close()
61    
62 nino.borges 795 ## Next gather game information
63 nino.borges 805 def AddNewRoms(pathToRoms, systemID, testOnly = False):
64 nino.borges 795 """Adding additional roms."""
65 nino.borges 805 importedCount = 0
66     alreadyExistsCount = 0
67     errImportCount = 0
68    
69     sysResult = conn.execute(f'SELECT relative_file_path FROM main_app_system WHERE id={systemID}')
70     for s in sysResult:
71     systemRomPath = s[0]
72     fullRomPath = os.path.join(config.get('RootConfig','softwareBackupStartDir'),systemRomPath)
73    
74 nino.borges 795 res = conn.execute('SELECT md5 FROM main_app')
75     currentHashList = []
76     for h in res:
77 nino.borges 805 #print(h[0])
78 nino.borges 795 currentHashList.append(h[0])
79     #print(currentHashList)
80     print(len(currentHashList))
81 nino.borges 805
82 nino.borges 795 hshr = Tool_Box.NinoGenTools.HashFileContents('md5')
83     for testFile in os.listdir(pathToRoms):
84 nino.borges 805 print(f"\n\nNow analyzing {testFile}...")
85 nino.borges 795 hashVal = hshr.HashFile(os.path.join(pathToRoms,testFile))
86     if hashVal in currentHashList:
87     print("This file is already in your collection, skipping.")
88 nino.borges 805 alreadyExistsCount +=1
89 nino.borges 795 else:
90     print("This file is unique. Adding to collection.")
91 nino.borges 805 if os.path.isfile(os.path.join(fullRomPath,testFile)):
92     print("ERROR: This filename already exists! Cannot copy!")
93     errImportCount +=1
94     else:
95     if testOnly:
96     pass
97     else:
98     shutil.copyfile(os.path.join(pathToRoms,testFile),os.path.join(fullRomPath,testFile))
99     conn.execute(f'INSERT INTO main_app("file_name", "system_console", "md5")VALUES("{testFile}","{systemID}","{hashVal}")')
100     conn.commit()
101     importedCount +=1
102 nino.borges 795 conn.close()
103 nino.borges 805 return importedCount, alreadyExistsCount, errImportCount
104 nino.borges 795
105 nino.borges 805 def GetSystemList():
106     res = conn.execute("SELECT id, name, short_name, relative_file_path FROM main_app_system ")
107     systemNamesMatrix = {}
108     for h in res:
109     systemNamesMatrix[h[1]] = [h[0],h[2],h[3]]
110     return systemNamesMatrix
111    
112 nino.borges 795 def GetGameListBySystem(systemName):
113     res = conn.execute("SELECT main_app.id, main_app.game_name, no_intro_main.game_name, main_app.file_name FROM main_app LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5")
114     gameNamesMatrix = {}
115     for h in res:
116     if h[1]:
117     gameNamesMatrix[h[1]] = h[0]
118     elif h[2]:
119     gameNamesMatrix[h[2]] = h[0]
120     else:
121     gameNamesMatrix[h[3]] = h[0]
122    
123     #for g in gameNamesList:
124     # print(g)
125     return gameNamesMatrix
126    
127     def GetSingleGameById(id):
128     res = conn.execute(f"""SELECT main_app.game_name, main_app.md5, main_app.file_name, main_app.relative_file_path, no_intro_main.game_name, no_intro_system.name FROM main_app
129     LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5
130     LEFT JOIN no_intro_system ON no_intro_main.no_intro_system_id = no_intro_system.id
131     WHERE main_app.id = {id}""")
132     for h in res:
133     gameTuple = h
134     print(h)
135     return gameTuple
136    
137    
138    
139     if __name__ == '__main__':
140 nino.borges 805
141     #test = GetSystemList()
142     #print(test.keys())
143     #AddNewSystem("Super Nintendo", "SNES", r"Roms/Snes/Gromulus")
144 nino.borges 795 #ImportNewNoIntroDat("/home/nino/MyCode/Python/Active_prgs/Gromulus/_data/_dats/Nintendo - Super Nintendo Entertainment System (20230516-033614).dat")
145 nino.borges 805 AddNewRoms('/mnt/smb/HomeData/ninoborges/Emulation/Roms/Snes/Random2',"1",testOnly=True)
146 nino.borges 795 #res = conn.execute("SELECT * FROM main_app LEFT JOIN no_intro_main ON main_app.md5 = no_intro_main.md5")
147     #for h in res:
148     # print(h)
149     # print("\n")
150     #GetGameListBySystem('snes')
151 nino.borges 805 #GetSingleGameById('224')
152 nino.borges 795