9
9
from pathlib import Path
10
10
from subprocess import PIPE
11
11
12
+ class Logger :
13
+
14
+ def __init__ (self ):
15
+ self .CRED = '\033 [91m'
16
+ self .CGREEN = '\033 [92m'
17
+ self .CEND = '\033 [0m'
18
+
19
+ def error (self , msg ):
20
+ print ("{cred}{msg}{cend}" .format (cred = self .CRED , msg = msg , cend = self .CEND ))
21
+
22
+ def info (self , msg ):
23
+ print (msg )
24
+
25
+ def success (self , msg ):
26
+ print ("{cgreen}{msg}{cend}" .format (cgreen = self .CGREEN , msg = msg , cend = self .CEND ))
27
+
12
28
class Platform :
13
29
14
30
def __init__ (self , name , manufacturer_id ):
@@ -25,9 +41,10 @@ def __init__(self, name, md5):
25
41
26
42
class Game :
27
43
28
- def __init__ (self , display_name , serial , rom , developer_id , franchise_id , release_year , release_month , region_id , genre_id , description , platform_id ):
44
+ def __init__ (self , display_name , full_name , serial , rom , developer_id , franchise_id , release_year , release_month , region_id , genre_id , platform_id ):
29
45
self .id = None
30
46
self .display_name = display_name
47
+ self .full_name = full_name
31
48
self .serial = serial
32
49
self .rom = rom
33
50
self .developer_id = developer_id
@@ -36,7 +53,6 @@ def __init__(self, display_name, serial, rom, developer_id, franchise_id, releas
36
53
self .release_month = release_month
37
54
self .region_id = region_id
38
55
self .genre_id = genre_id
39
- self .description = description
40
56
self .platform_id = platform_id
41
57
42
58
"""
@@ -46,6 +62,8 @@ def join(self, other):
46
62
# Join the top-level fields
47
63
if self .display_name is None and other .display_name is not None :
48
64
self .display_name = other .display_name
65
+ if self .full_name is None and other .full_name is not None :
66
+ self .full_name = other .full_name
49
67
if self .serial is None and other .serial is not None :
50
68
self .serial = other .serial
51
69
if self .developer_id is None and other .developer_id is not None :
@@ -60,8 +78,6 @@ def join(self, other):
60
78
self .region_id = other .region_id
61
79
if self .genre_id is None and other .genre_id is not None :
62
80
self .genre_id = other .genre_id
63
- if self .description is None and other .description is not None :
64
- self .description = other .description
65
81
if self .platform_id is None and other .platform_id is not None :
66
82
self .platform_id = other .platform_id
67
83
# Join the ROM
@@ -75,6 +91,7 @@ def join(self, other):
75
91
class Converter :
76
92
77
93
def __init__ (self , rdb_dir , output_file , libretrodb_tool ):
94
+ self .logger = Logger ()
78
95
self .rdb_dir = self ._validate_rdb_dir (rdb_dir )
79
96
self .output_file = self ._validate_output_file (output_file )
80
97
self .libretrodb_tool = self ._validate_libretrodb_tool (libretrodb_tool )
@@ -92,7 +109,7 @@ def __init__(self, rdb_dir, output_file, libretrodb_tool):
92
109
"""
93
110
def _validate_rdb_dir (self , rdb_dir ):
94
111
if not os .path .isdir (rdb_dir ):
95
- print ("{} is not a directory" .format (rdb_dir ))
112
+ self . logger . error ("{} is not a directory" .format (rdb_dir ))
96
113
exit (1 )
97
114
return rdb_dir
98
115
@@ -101,7 +118,7 @@ def _validate_rdb_dir(self, rdb_dir):
101
118
"""
102
119
def _validate_output_file (self , output_file ):
103
120
if os .path .isfile (output_file ):
104
- print ("{} already exists" .format (output_file ))
121
+ self . logger . error ("{} already exists" .format (output_file ))
105
122
exit (1 )
106
123
return output_file
107
124
@@ -123,7 +140,7 @@ def _validate_output_file(self, output_file):
123
140
"""
124
141
def _validate_libretrodb_tool (self , libretrodb_tool ):
125
142
if not os .path .isfile (libretrodb_tool ):
126
- print ("{} not found" .format (libretrodb_tool ))
143
+ self . logger . error ("{} not found" .format (libretrodb_tool ))
127
144
exit (1 )
128
145
return libretrodb_tool
129
146
@@ -143,7 +160,7 @@ def run(self):
143
160
# Iterate over the .rdb files in the input directory and parse all data
144
161
for file in [f for f in os .listdir (self .rdb_dir ) if os .path .isfile (os .path .join (self .rdb_dir , f ))]:
145
162
if not file .endswith ('.rdb' ):
146
- print ("Skipping non-RDB file: {}" .format (file ))
163
+ self . logger . info ("Skipping non-RDB file: {}" .format (file ))
147
164
continue
148
165
self ._parse_platform_file (os .path .join (self .rdb_dir , file ))
149
166
# break # TODO: Remove this
@@ -174,7 +191,7 @@ def run(self):
174
191
Parses a single platform .rdb file.
175
192
"""
176
193
def _parse_platform_file (self , rdb_file ):
177
- print ( " > Parsing: {}" .format (rdb_file ))
194
+ self . logger . info ( " Parsing {}" .format (rdb_file ))
178
195
179
196
# Try to parse out the manufacturer and platfrom from the .rdb filename
180
197
system_fullname = Path (rdb_file ).stem
@@ -209,8 +226,8 @@ def _parse_line(self, json_str, platform_id):
209
226
try :
210
227
json_obj = json .loads (json_str )
211
228
except json .decoder .JSONDecodeError as e :
212
- print ("Error while parsing JSON: {}" .format (e . msg ))
213
- print ("Original JSON string: {}" .format (json_str ))
229
+ self . logger . error ("Error while parsing JSON: {}" .format (str ( e ) ))
230
+ self . logger . error ("Original JSON string: {}" .format (json_str ))
214
231
return
215
232
216
233
# Extract the fields from the JSON
@@ -224,8 +241,14 @@ def _parse_line(self, json_str, platform_id):
224
241
rom_name = self ._get_json_value (json_obj , 'rom_name' )
225
242
region = self ._get_json_value (json_obj , 'region' )
226
243
genre = self ._get_json_value (json_obj , 'genre' )
227
- description = self ._get_json_value (json_obj , 'description' )
228
- display_name = self ._get_json_value (json_obj , 'name' )
244
+ # description = self._get_json_value(json_obj, 'description') # This field in the dataset doesn't currently provide any added value
245
+ full_name = self ._get_json_value (json_obj , 'name' )
246
+
247
+ # Build the display name from the full name, but ignore all the trailing parenthesis-wrapped meta-tags
248
+ if full_name is None :
249
+ display_name = None
250
+ else :
251
+ display_name = full_name .split ("(" )[0 ].strip () if "(" in full_name else full_name
229
252
230
253
# Save potentially common references to developers, franchises, regions and genres, and assign an ID
231
254
if developer is not None and developer not in self .developers :
@@ -244,7 +267,7 @@ def _parse_line(self, json_str, platform_id):
244
267
245
268
# Build the ROM and Game objects. Note that ROMs and games should be 1:1.
246
269
rom = ROM (rom_name , md5 )
247
- game = Game (display_name , serial , rom , developer_id , franchise_id , release_year , release_month , region_id , genre_id , description , platform_id )
270
+ game = Game (display_name , full_name , serial , rom , developer_id , franchise_id , release_year , release_month , region_id , genre_id , platform_id )
248
271
if md5 in self .games :
249
272
self .games [md5 ].join (game )
250
273
else :
@@ -257,60 +280,59 @@ def _parse_line(self, json_str, platform_id):
257
280
Insert the manufacturers into the database.
258
281
"""
259
282
def _insert_manufacturers (self , cursor ):
260
- print (" > Inserting {} manufacturers into database…" .format (len (self .manufacturers )))
261
283
for key ,value in self .manufacturers .items ():
262
284
(id , name ) = (value , key )
263
285
cursor .execute (self ._load_sql ("./sql/insert_manufacturer.sql" ), (id , name ))
286
+ self .logger .success ("Inserted {} manufacturers into database" .format (len (self .manufacturers )))
264
287
265
288
"""
266
289
Insert the platforms into the database.
267
290
"""
268
291
def _insert_platforms (self , cursor ):
269
- print (" > Inserting {} platforms into database…" .format (len (self .platforms )))
270
292
for key ,value in self .platforms .items ():
271
293
cursor .execute (self ._load_sql ("./sql/insert_platform.sql" ), (value .id , value .name , value .manufacturer_id ))
294
+ self .logger .success ("Inserted {} platforms into database" .format (len (self .platforms )))
272
295
273
296
"""
274
297
Insert the developers into the database.
275
298
"""
276
299
def _insert_developers (self , cursor ):
277
- print (" > Inserting {} developers into database…" .format (len (self .developers )))
278
300
for key ,value in self .developers .items ():
279
301
(id , name ) = (value , key )
280
302
cursor .execute (self ._load_sql ("./sql/insert_developer.sql" ), (id , name ))
303
+ self .logger .success ("Inserted {} developers into database" .format (len (self .developers )))
281
304
282
305
"""
283
306
Insert the franchises into the database.
284
307
"""
285
308
def _insert_franchises (self , cursor ):
286
- print (" > Inserting {} franchises into database…" .format (len (self .franchises )))
287
309
for key ,value in self .franchises .items ():
288
310
(id , name ) = (value , key )
289
311
cursor .execute (self ._load_sql ("./sql/insert_franchise.sql" ), (id , name ))
312
+ self .logger .success ("Inserted {} franchises into database" .format (len (self .franchises )))
290
313
291
314
"""
292
315
Insert the regions into the database.
293
316
"""
294
317
def _insert_regions (self , cursor ):
295
- print (" > Inserting {} regions into database…" .format (len (self .regions )))
296
318
for key ,value in self .regions .items ():
297
319
(id , name ) = (value , key )
298
320
cursor .execute (self ._load_sql ("./sql/insert_region.sql" ), (id , name ))
321
+ self .logger .success ("Inserted {} regions into database" .format (len (self .regions )))
299
322
300
323
"""
301
324
Insert the genres into the database.
302
325
"""
303
326
def _insert_genres (self , cursor ):
304
- print (" > Inserting {} genres into database…" .format (len (self .genres )))
305
327
for key ,value in self .genres .items ():
306
328
(id , name ) = (value , key )
307
329
cursor .execute (self ._load_sql ("./sql/insert_genre.sql" ), (id , name ))
330
+ self .logger .success ("Inserted {} genres into database" .format (len (self .genres )))
308
331
309
332
"""
310
333
Insert the ROMs and games into the database.
311
334
"""
312
335
def _insert_games (self , cursor ):
313
- print (" > Inserting {} games into database…" .format (len (self .games )))
314
336
for key ,value in self .games .items ():
315
337
game = value
316
338
cursor .execute (self ._load_sql ("./sql/insert_rom.sql" ), (game .rom .id , game .rom .name , game .rom .md5 ))
@@ -324,9 +346,10 @@ def _insert_games(self, cursor):
324
346
game .release_month ,
325
347
game .region_id ,
326
348
game .genre_id ,
327
- game .description ,
328
349
game .display_name ,
350
+ game .full_name ,
329
351
game .platform_id ))
352
+ self .logger .success ("Inserted {} games into database" .format (len (self .games )))
330
353
331
354
def _get_json_value (self , json_obj , key ):
332
355
return json_obj [key ] if key in json_obj else None
0 commit comments