[gnome-games] tools: Add gameinfo/merge.py
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] tools: Add gameinfo/merge.py
- Date: Fri, 2 Sep 2016 22:50:47 +0000 (UTC)
commit d5b5d075bf6daa843ad05d4bf0193fe0a1bc3d3f
Author: Adrien Plazas <kekun plazas laposte net>
Date: Fri Sep 2 19:46:00 2016 +0200
tools: Add gameinfo/merge.py
Allow to merge Gameinfo documents.
This helps merging existing Gameinfo documents which is necessary to
update an existing one with the data from a newly generated one.
https://bugzilla.gnome.org/show_bug.cgi?id=770192
tools/gameinfo/gameinfo/__init__.py | 34 +++++++++++++++
tools/gameinfo/merge.py | 76 +++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)
---
diff --git a/tools/gameinfo/gameinfo/__init__.py b/tools/gameinfo/gameinfo/__init__.py
index 1402de5..1cb5145 100644
--- a/tools/gameinfo/gameinfo/__init__.py
+++ b/tools/gameinfo/gameinfo/__init__.py
@@ -89,6 +89,29 @@ class Gameinfo:
return game
+ def get_game_for_disc_id(self, disc_id):
+ return self.find('games/game/discs/disc[@id="' + disc_id + '"]/../..')
+
+ def get_game_title(self, game):
+ if not game in self.games:
+ return
+
+ title = game.find('_title')
+ if title is None:
+ return
+
+ return title.text
+
+ def get_game_disc_set_id(self, game):
+ if not game in self.games:
+ return
+
+ disc = game.find('discs/disc[1]')
+ if disc is None:
+ return
+
+ return disc.get('id')
+
def set_game_title(self, game, title):
if not game in self.games:
return
@@ -99,6 +122,17 @@ class Gameinfo:
titleE = ET.SubElement(game, '_title')
titleE.text = title
+ def set_disc_title_for_disc_id(self, disc_id, title):
+ disc = self.find('games/game/discs/disc[@id="' + disc_id + '"]')
+ if disc is None:
+ return
+
+ for t in disc.findall('_title'):
+ disc.remove(t)
+
+ titleE = ET.SubElement(disc, '_title')
+ titleE.text = title
+
def _sort(self):
games = self.gameinfo.find('games')
diff --git a/tools/gameinfo/merge.py b/tools/gameinfo/merge.py
new file mode 100755
index 0000000..dc1cd4c
--- /dev/null
+++ b/tools/gameinfo/merge.py
@@ -0,0 +1,76 @@
+#!/bin/env python3
+
+from gameinfo import Gameinfo
+from sys import argv
+
+def merge_missing_games(gameinfo, reference):
+ games = gameinfo.find('games')
+ reference_games = reference.find('games')
+ for reference_game in reference.findall('games/game'):
+ disc_id = reference.get_game_disc_set_id(reference_game)
+ game = gameinfo.get_game_for_disc_id(disc_id)
+
+ # If the game already exists in gameinfo, continue
+ if game is not None:
+ continue
+
+ print('new game')
+
+ reference_games.remove(reference_game)
+ games.append(reference_game)
+
+def merge_game_titles(gameinfo, reference):
+ for game in gameinfo.findall('games/game'):
+ disc_id = gameinfo.get_game_disc_set_id(game)
+ reference_game = reference.get_game_for_disc_id(disc_id)
+
+ # If the game is new, continue
+ if reference_game is None:
+ continue
+
+ # If we didn't extract the info correctly in the reference,
+ # there is nothing interesting to retrieve from it.
+ disc_set = game.find('discs[@info]')
+ reference_disc_set = reference_game.find('discs[@info]')
+ if disc_set is None and reference_disc_set is not None:
+ continue
+
+ reference_title = reference.get_game_title(reference_game)
+ if not reference_title:
+ continue
+
+ title = gameinfo.get_game_title(game)
+ if title == reference_title:
+ continue
+
+ if disc_set is not None:
+ del disc_set.attrib['info']
+
+ gameinfo.set_game_title(game, reference_title)
+
+def merge_disc_titles(gameinfo, reference):
+ for reference_disc in reference.findall('games/game/discs/disc[_title]'):
+ disc_id = reference_disc.get('id')
+ title = reference_disc.find('_title').text
+ gameinfo.set_disc_title_for_disc_id(disc_id, title)
+ disc_set = gameinfo.find('games/game/discs/disc[@id="' + disc_id + '"]/..')
+ attribs = disc_set.attrib
+ if 'includes' in attribs.keys():
+ del attribs['includes']
+
+if __name__ == '__main__':
+ if len(argv) < 4:
+ exit(1)
+
+ source_path = argv[1]
+ reference_path = argv[2]
+ out_path = argv[3]
+
+ gameinfo = Gameinfo(source_path)
+ reference = Gameinfo(reference_path)
+
+ merge_missing_games(gameinfo, reference)
+ merge_game_titles(gameinfo, reference)
+ merge_disc_titles(gameinfo, reference)
+
+ gameinfo.save(out_path)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]