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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
#!/usr/bin/env python
#
from __future__ import print_function
# We can't use unicode_literals because there seems to be
# a bug in setuptools:
# https://stackoverflow.com/a/23175194/2015768
# from __future__ import unicode_literals
import codecs
import glob
import logging
from setuptools import setup
from setuptools.command.install import install
from setuptools.command.test import test as TestCommand
from distutils.command.build import build
#import py2exe
import os
import sys
logging.basicConfig(level=logging.WARN)
# Just in case we're attempting to execute this setup.py
# when cwd != thisdir...
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open(os.path.join('keysign', '_version.py')) as f:
# This should define __version__
exec(f.read())
def translate_desktop_file(infile, outfile, localedir):
infp = codecs.open(infile, 'rb', encoding='utf-8')
outfp = codecs.open(outfile, 'wb', encoding='utf-8')
catalogs = get_catalogs(localedir)
for line in (x.strip() for x in infp):
logging.debug('Found in original (%s): %r', type(line), line)
# We intend to ignore the first line
if line.startswith('[Desktop'):
additional_lines = []
else:
additional_lines = []
# This is a rather primitive approach to generating the translated
# desktop file. For example we don't really care about all the
# keys in the file. But its simplicity is a feature and we
# ignore the runtime overhead, because it should only run centrally
# once.
key, value = line.split('=', 1)
logging.debug("Found key: %r", key)
for locale, catalog in catalogs.items():
translated = catalog.get(value)
logging.debug("Translated %r[%r]=%r: %r (%r)",
key, locale, value, translated,
translated.string if translated else '')
if translated and translated.string \
and translated.string != value:
additional_line = u'{keyword}[{locale}]={translated}'.format(
keyword=key,
locale=locale,
translated=translated.string,
)
additional_lines.append(additional_line)
logging.debug("Writing more lines: %s", additional_lines)
# Write the new file.
# First the original line found it in the file, then the translations.
outfp.writelines((outline+'\n' for outline in ([line] + additional_lines)))
def translate_appdata_file(infile, outfile, localedir):
from lxml import etree
catalogs = get_catalogs(localedir)
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(infile, parser)
root = tree.getroot()
for elem in root.iter():
# We remove any possible tailing whitespaces to allow lxml to format the output
elem.tail = None
if elem.get("translatable") == "yes":
elem.attrib.pop("translatable", None)
elem.attrib.pop("comments", None) # Are comments allowed?
message = elem.text
parent = elem.getparent()
pos = parent.getchildren().index(elem) + 1
for locale, catalog in catalogs.items():
translated = catalog.get(message)
if translated and translated.string \
and translated.string != message:
logging.debug("Translated [%s]%r: %r (%r)",
locale, message, translated, translated.string)
tr = etree.Element(elem.tag)
attrib = tr.attrib
attrib["{http://www.w3.org/XML/1998/namespace}lang"] = str(locale)
tr.text = translated.string
parent.insert(pos, tr)
tree.write(outfile, encoding='utf-8', xml_declaration=True, pretty_print=True)
def get_catalogs(localedir):
# We import it here rather than globally because
# we don't have a guarantee for babel to be available
# globally. The setup_requires can only be evaluated after
# this file has been loaded. And it can't load if the import
# cannot be resolved.
from babel.messages.pofile import read_po
# glob in Python 3.5 takes ** syntax
# pofiles = glob.glob(os.path.join(localedir, '**.po', recursive=True))
pofiles = sorted([os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(localedir)
for f in files if f.endswith('.po')])
logging.debug('Loading %r', pofiles)
catalogs = {}
for pofile in pofiles:
catalog = read_po(codecs.open(pofile, 'r', encoding="utf-8"))
catalogs[catalog.locale] = catalog
logging.info("Found %d strings for %s", len(catalog), catalog.locale)
# logging.debug("Strings for %r", catalog, catalog.values())
if not catalogs:
logging.warning("Could not find pofiles in %r", pofiles)
return catalogs
class BuildWithCompile(build):
sub_commands = [('compile_catalog', None)] + build.sub_commands
def run(self):
translate_desktop_file('data/org.gnome.Keysign.raw.desktop', 'data/org.gnome.Keysign.desktop', 'keysign/locale')
translate_appdata_file('data/org.gnome.Keysign.raw.appdata.xml', 'data/org.gnome.Keysign.appdata.xml', 'keysign/locale')
build.run(self)
# Pretty much from http://stackoverflow.com/a/41120180/2015768
class InstallWithCompile(install):
def run(self):
try:
from babel.messages.frontend import compile_catalog
compiler = compile_catalog(self.distribution)
option_dict = self.distribution.get_option_dict('compile_catalog')
compiler.domain = [option_dict['domain'][1]]
compiler.directory = option_dict['directory'][1]
compiler.run()
except Exception as e:
print ("Error compiling message catalogs: {}".format(e),
file=sys.stderr)
print ("Do you have Babel (python-babel) installed?",
file=sys.stderr)
#super(InstallWithCompile, self).run()
install.run(self)
# Inspired by the example at https://pytest.org/latest/goodpractises.html
class NoseTestCommand(TestCommand):
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
# Run nose ensuring that argv simulates running nosetests directly
import nose
nose.run_exit(argv=['nosetests' , 'tests/test_bluetooth.py', 'tests/test_gpgmeh.py', 'tests/test_latin1.py', 'tests/test_uids.py'])
setup(
name = 'gnome-keysign',
version = __version__,
description = 'OpenPGP key signing helper',
author = 'Tobias Mueller',
author_email = 'tobiasmue@gnome.org',
url = 'https://wiki.gnome.org/Apps/Keysign',
packages = [
'keysign',
'keysign.compat',
'keysign.network',
],
package_dir={
'keysign': 'keysign',
},
package_data={
'keysign': [
'*.ui',
'locale/*/*/*.mo',
# The PO files are added in the MANIFEST, because they
# should be part of the source distribution.
# 'locale/*/*/*.po'
]
},
include_package_data = True,
data_files=[
( 'share/applications',
['data/org.gnome.Keysign.desktop']),
( 'share/metainfo',
['data/org.gnome.Keysign.appdata.xml']),
( 'share/icons/hicolor/scalable/apps',
['data/org.gnome.Keysign.svg']),
#( 'share/locale/',
# # We cannot use the glob below, because it only copies the file
# # into the directory mentioned above, i.e. keysign.po, rather
# # than de/LC_MESSAGES/keysign.po including the de/... directories.
# ([f for f in glob.glob('keysign/locale/*/*/*.po')] +
# [f for f in glob.glob('keysign/locale/*')])
# ),
],
#scripts = ['gnome-keysign.py'],
install_requires=[
# Note that the dependency on <= 2.2 is only
# to not confuse Ubuntu 14.04's pip as that
# seems incompatible with a newer requests library.
# https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1306991
# 'requests<=2.2',
# But this version seems to be requiring an old pyopenssl
# with SSLv3 support which doesn't work with Ubuntu's 16.04.
# So let's require a more modern requests.
'requests>=2.6',
'qrcode',
'twisted[tls]>=17.5.0',
'future',
'magic-wormhole>=0.10.2',
# avahi # Also no entry in the cheeseshop
# dbus # dbus-python is in the cheeseshop but not pip-able
],
extras_require={
'bluetooth': ['pybluez>=0.22'],
},
setup_requires=[
"babel",
"lxml",
],
tests_require=[
"nose",
"tox",
"pycodestyle",
"pylint",
],
license='GPLv3+',
long_description=open('README.rst').read(),
entry_points = {
'console_scripts': [
'gnome-keysign-sign-key = keysign.SignKey:main'
],
'gui_scripts': [
'gnome-keysign = keysign:main',
],
},
classifiers = [
# Maybe not yet...
#'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Information Technology',
'Intended Audience :: Legal Industry',
'Intended Audience :: Telecommunications Industry',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
# I think we are only 2.7 compatible
'Programming Language :: Python :: 2.7',
# We're still lacking support for 3
'Programming Language :: Python :: 3',
'License :: OSI Approved :: GNU General Public License (GPL)',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Operating System :: POSIX :: Linux',
'Environment :: X11 Applications :: GTK',
'Topic :: Desktop Environment',
'Natural Language :: English',
'Topic :: Communications :: Email',
'Topic :: Multimedia :: Video :: Capture',
'Topic :: Security :: Cryptography',
'Topic :: Software Development :: Libraries :: Python Modules',
],
message_extractors = {
'': [
('**.raw.desktop', 'babelglade:extract_desktop', None),
('**.raw.appdata.xml', 'babelglade:extract_glade', None),
],
'keysign': [
('**.py', 'python', None),
('**.ui', 'babelglade:extract_glade', None),
],
},
cmdclass={
'build': BuildWithCompile,
#'install': InstallWithCompile,
'test': NoseTestCommand,
},
# test_suite = 'nose.collector',
)
|