-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
152 lines (142 loc) · 4.87 KB
/
console.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# console rom management functions
"""
functions used to:
- find unique game name
- sort out by country
- sort out by revision
"""
import os
import msg
import remote_host
# expunge roms by keyword
def purge(romlist,banned,debug):
"""remove roms using word from banned list in their filename"""
clean = []
for rom in romlist:
found = False
for banword in banned:
if banword in rom:
found = True
break
if found:
msg.debug(f"SKIPPED:\tgame {rom}, keyword {banword}",debug) # pylint: disable=undefined-loop-variable
else:
clean.append(rom)
return clean
# extract base name to create sets of similar game
def extract_basename(rom,debug):
"""extract base rom name from file name"""
if '(' in rom:
base_name = rom.split('(')[0] + '('
msg.debug(f"FOUND:\tbase name is {base_name}",debug)
return base_name
else:
base_name = rom.split('.')[0]
msg.debug(f"FOUND:\tno country specified, basename is {base_name}",debug)
return base_name
def create_sets(romlist,debug):
"""create sets per unique names"""
fullset = []
previous_basename = ''
for rom in romlist:
basename = extract_basename(rom,debug)
if basename.lower() != previous_basename.lower():
gameset = []
for game in romlist:
if game.startswith(basename):
msg.debug(f"FOUND:\tadding rom {game}",debug)
gameset.append(game)
previous_basename = basename
fullset.append(gameset)
return fullset
# select rom by revision
def find_revision(game,allow_translations,debug):
"""find most recent revision of game"""
msg.debug(f"Looking at {game}",debug)
game.sort()
revisions = []
multidisc = []
if allow_translations:
translations = []
adds = []
for rom in game:
# find revisions or versions
if ('(Rev' or '(v.') in rom:
revisions.append(rom)
if allow_translations:
# find translations of Japanese games
if ('Japan' and '[T-En') in rom:
translations.append(rom)
# find addendums to translations
if ('Japan' and '[T-En' and '[Add') in rom:
adds.append(rom)
if allow_translations:
if len(adds)>0:
adds.sort()
# use the latest add to translation
return adds[-1]
if len(translations)>0:
translations.sort()
# use the latest translation
return translations[-1]
if len(revisions)>0:
revisions.sort()
# use the latest revision
return revisions[-1]
return game[-1]
# select rom by country
def find_country(game,country_list,country_index):
"""find most appropriate country based off list in settings"""
game_country = []
for rom in game:
try:
if country_list[country_index] in rom:
game_country.append(rom)
except IndexError:
game_country.append(rom)
if len(game_country)>0:
return game_country
# we test every country before pushing following a specific order
while country_index < len(country_list):
return find_country(game,country_list,country_index+1)
# select rom by country and revision
def select_unique(gamelist,country_list,allow_translations,debug):
"""find the most appropriate dump for each game"""
dumps = []
for game in gamelist:
# start with the first country in the list
country = find_country(game,country_list,0)
if ('(Disc' or '(Disk') in country[0]:
msg.debug(f"Multi-disk game found : {country}",debug)
for disc in country:
dumps.append(disc)
else:
best = find_revision(country,allow_translations,debug)
dumps.append(best)
return dumps
def trim_path(folder):
"""trim folder path and keep last"""
if folder[-1] == '/':
folder = folder.rstrip(folder[-1])
return folder.split('/')[-1]
# main sync functions
def sync(folder,remote,banned_words,country_list,allow_translations,debug,noop):
"""sync selected console roms to remote folder"""
local_folder = os.path.abspath(folder)
# list all roms in folder and remove some based on keywords
romlist = purge(os.listdir(local_folder),banned_words,debug)
# sort list
romlist.sort()
# create sets by game and select unique one
romset = select_unique(create_sets(romlist,debug),country_list,allow_translations,debug)
if noop:
for rom in romset:
print(rom)
else:
# push romsets once they have been curated
remote_host.pushromset(
romset,
local_folder,
remote['rom_path'] + '/' + trim_path(local_folder),
remote,debug
)