Skip to content

Commit

Permalink
Trying to package for pip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Oct 23, 2013
1 parent 2163037 commit 56834bf
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.py[cod]

# C extensions
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.md LICENSE
recursive-include localizable *.py
Empty file added localizable/__init__.py
Empty file.
103 changes: 103 additions & 0 deletions localizable/localizable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Adapted from Transifex: https://github.com/transifex/transifex/blob/master/transifex/resources/formats/strings.py
# -*- coding: utf-8 -*-
# GPLv2

"""
Apple strings file handler/compiler
"""

from __future__ import absolute_import
import codecs, re, chardet


"""
Handler for Apple STRINGS translation files.
Apple strings files *must* be encoded in cls.ENCODING encoding.
"""

format_encoding = 'UTF-16'

def _unescape_key(s):
return s.replace('\\\n', '')

def _unescape(s):
s = s.replace('\\\n', '')
return s.replace('\\"', '"').replace(r'\n', '\n').replace(r'\r', '\r')

def _get_content(filename=None, content=None):
if content is not None:
if chardet.detect(content)['encoding'].startswith(format_encoding):
encoding = format_encoding
else:
encoding = 'UTF-8'
if isinstance(content, str):
content.decode(encoding)
else:
return content
if filename is None:
return None
return _get_content_from_file(filename, format_encoding)

def _get_content_from_file(filename, encoding):
f = open(filename, 'r')
try:
content = f.read()
if chardet.detect(content)['encoding'].startswith(format_encoding):
#f = f.decode(format_encoding)
encoding = format_encoding
else:
#f = f.decode(default_encoding)
encoding = 'utf-8'
f.close()
f = codecs.open(filename, 'r', encoding=encoding)
return f.read()
except IOError, e:
print "Error opening file %s with encoding %s: %s" %\
(filename, format_encoding, e.message)
except Exception, e:
print "Unhandled exception: %s" % e.message
finally:
f.close()

def parse_strings(content="", filename=None):
"""Parse an apple .strings file and create a stringset with
all entries in the file.
See
http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPInternational/Articles/StringsFiles.html
for details.
"""
if filename is not None:
content = _get_content(filename=filename)

stringset = []
f = content
if f.startswith(u'\ufeff'):
f = f.lstrip(u'\ufeff')
#regex for finding all comments in a file
cp = r'(?:/\*(?P<comment>(?:[^*]|(?:\*+[^*/]))*\**)\*/)'
p = re.compile(r'(?:%s[ \t]*[\n]|[\r\n]|[\r]){0,1}(?P<line>(("(?P<key>[^"\\]*(?:\\.[^"\\]*)*)")|(?P<property>\w+))\s*=\s*"(?P<value>[^"\\]*(?:\\.[^"\\]*)*)"\s*;)'%cp, re.DOTALL|re.U)
#c = re.compile(r'\s*/\*(.|\s)*?\*/\s*', re.U)
c = re.compile(r'//[^\n]*\n|/\*(?:.|[\r\n])*?\*/', re.U)
ws = re.compile(r'\s+', re.U)
end=0
start = 0
for i in p.finditer(f):
start = i.start('line')
end_ = i.end()
key = i.group('key')
comment = i.group('comment') or ''
if not key:
key = i.group('property')
value = i.group('value')
while end < start:
m = c.match(f, end, start) or ws.match(f, end, start)
if not m or m.start() != end:
print "Invalid syntax: %s" %\
f[end:start]
end = m.end()
end = end_
key = _unescape_key(key)
stringset.append({'key': key, 'value': _unescape(value), 'comment': comment})
return stringset
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chardet==2.1.1
44 changes: 44 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python
import os, re
from distutils.core import setup

def get_requirements(filename):
"""
Read requirements and dependency links from a file passed by parameter
and return them as two lists in a tuple.
"""
def add_dependency_link(line):
link = re.sub(r'\s*-[ef]\s+', '', line)
filename = os.path.basename(link.split('://')[1])
url = link.split(filename)[0]
if url not in dependency_links:
dependency_links.append(url)

requirements = []
dependency_links = []
for line in open(filename, 'r').read().split('\n'):
if re.match(r'(\s*#)|(\s*$)', line):
continue
if re.match(r'\s*-e\s+', line):
# TODO support version numbers
requirements.append(re.sub(r'\s*-e\s+.*#egg=(.*)$', r'\1', line))
add_dependency_link(line)
elif re.match(r'\s*-f\s+', line):
add_dependency_link(line)
else:
requirements.append(line)
return requirements, dependency_links

requirements, dependency_links = get_requirements('requirements.txt')

setup(name='localizable',
version='0.1',
description='Localizable.strings parser for Apple-specific localization files',
author='Chris Ballinger',
author_email='[email protected]',
url='https://github.com/chrisballinger/python-localizable',
license="GPLv2",
long_description=open('README.md').read(),
packages=['localizable'],
install_requires=requirements,
)

0 comments on commit 56834bf

Please sign in to comment.