File: extract_strings_from_sqlite.py

package info (click to toggle)
unknown-horizons 2019.1-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 347,924 kB
  • sloc: python: 46,805; xml: 3,137; sql: 1,189; sh: 736; makefile: 39; perl: 35
file content (134 lines) | stat: -rwxr-xr-x 3,600 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3

# ###################################################
# Copyright (C) 2008-2017 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# ###################################################

###############################################################################
#
# == I18N DEV USE CASES: CHEATSHEET ==
#
# ** Refer to  development/create_pot.sh  for help with building or updating
#    the translation files for Unknown Horizons.
#
###############################################################################
#
# THIS SCRIPT IS A HELPER SCRIPT. DO NOT INVOKE MANUALLY!
#
###############################################################################

from __future__ import print_function
import os
import sqlalchemy
import sqlalchemy.orm
import sqlalchemy.ext.declarative
import sqlite3
import sys
import tempfile
from collections import defaultdict

sys.path.append(".")
sys.path.append("./horizons")

from horizons.constants import PATHS


# sqlalchemy doesn't support importing sql files,
# therefore we work around this by using sqlite3

filename = tempfile.mkstemp(text=True)[1]
conn = sqlite3.connect(filename)

for db_file in PATHS.DB_FILES:
	conn.executescript(open(db_file, "r").read())

conn.commit()

engine = sqlalchemy.create_engine('sqlite:///' + filename) # must be 4 slashes total, sqlalchemy breaks the unixoid conventions here

Session = sqlalchemy.orm.sessionmaker(bind=engine)
db_session = Session()

Base = sqlalchemy.ext.declarative.declarative_base()


#
# Classes
#
class Message(Base):
	__tablename__ = 'message_text'

	text = sqlalchemy.Column(sqlalchemy.String, primary_key=True)


class Resource(Base):
	__tablename__ = 'resource'

	name = sqlalchemy.Column(sqlalchemy.String, primary_key=True)


class Tier(Base):
	__tablename__ = 'tier'

	name = sqlalchemy.Column(sqlalchemy.String, primary_key=True)

#
# print it
#


class MSGID_collect:
	msgids = defaultdict(list)

	def __init__(self):
		pass

	def add_to_collection(self, msgid, place):
		self.msgids[msgid].append(place)

	def __str__(self):
		s = []
		for text, locations in self.msgids.items():
			comment = '#. This is a database entry: {}\n'.format(','.join(locations))
			s += [comment + build_msgid(text)]
		return '\n'.join(s).strip()


def build_msgid(msgid):
	return 'msgid "{}"\nmsgstr ""\n'.format(msgid.replace('"', '\\"'))


def collect_all():
	collector = MSGID_collect()

	for message in db_session.query(Message):
		collector.add_to_collection(message.text, 'a messagewidget message (left part of the screen)')

	for resource in db_session.query(Resource):
		collector.add_to_collection(resource.name, 'the name of a resource')

	for tier in db_session.query(Tier):
		collector.add_to_collection(tier.name, 'the name of an inhabitant tier (level)')

	return collector


print(collect_all())
os.unlink(filename)