Skip to content

Commit 7da8372

Browse files
committed
Refactoring
Make FluentBundle module optional
1 parent 6130643 commit 7da8372

19 files changed

+94
-93
lines changed

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Once you have some FTL files, you can generate translations using the
2727

2828
.. code-block:: python
2929
30-
>>> from fluent_compiler import FluentBundle
30+
>>> from fluent_compiler.bundle import FluentBundle
3131
3232
You pass a list of locales to the constructor - the first being the
3333
desired locale, with fallbacks after that:

src/fluent_compiler/__init__.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1 @@
11
from __future__ import absolute_import, unicode_literals
2-
3-
import attr
4-
5-
from .compiler import compile_messages
6-
from .utils import ATTRIBUTE_SEPARATOR, TERM_SIGIL
7-
8-
9-
class FluentBundle(object):
10-
"""
11-
A FluentBundle is a single-language store of translations. It can
12-
format translation units (entities) to strings.
13-
14-
Use `FluentBundle.format` to retrieve translation units from a context.
15-
Translations can contain references to other entities or external arguments,
16-
conditional logic in form of select expressions, traits which describe their
17-
grammatical features, and can use Fluent builtins. See the documentation of
18-
the Fluent syntax for more information.
19-
20-
"""
21-
def __init__(self, locale, resources, functions=None, use_isolating=True, escapers=None):
22-
self.locale = locale
23-
compiled_ftl = compile_messages(
24-
resources,
25-
locale,
26-
use_isolating=use_isolating,
27-
functions=functions,
28-
escapers=escapers)
29-
self._compiled_messages = compiled_ftl.message_functions
30-
self._compilation_errors = compiled_ftl.errors
31-
32-
@classmethod
33-
def from_string(cls, locale, text, functions=None, use_isolating=True, escapers=None):
34-
return cls(
35-
locale,
36-
[FtlResource(text)],
37-
use_isolating=use_isolating,
38-
functions=functions,
39-
escapers=escapers
40-
)
41-
42-
@classmethod
43-
def from_files(cls, locale, filenames, functions=None, use_isolating=True, escapers=None):
44-
return cls(
45-
locale,
46-
[FtlResource.from_file(f) for f in filenames],
47-
use_isolating=use_isolating,
48-
functions=functions,
49-
escapers=escapers
50-
)
51-
52-
def has_message(self, message_id):
53-
if message_id.startswith(TERM_SIGIL) or ATTRIBUTE_SEPARATOR in message_id:
54-
return False
55-
return message_id in self._compiled_messages
56-
57-
def format(self, message_id, args=None):
58-
errors = []
59-
return self._compiled_messages[message_id](args, errors), errors
60-
61-
def check_messages(self):
62-
return self._compilation_errors
63-
64-
65-
@attr.s
66-
class FtlResource(object):
67-
'''
68-
Represents an (unparsed) FTL file (contents and optional filename)
69-
'''
70-
text = attr.ib()
71-
filename = attr.ib(default=None)
72-
73-
@classmethod
74-
def from_file(cls, filename):
75-
return cls(text=open(filename).read(), filename=filename)

src/fluent_compiler/bundle.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from __future__ import absolute_import, unicode_literals
2+
3+
from .compiler import compile_messages, FtlResource
4+
from .utils import ATTRIBUTE_SEPARATOR, TERM_SIGIL
5+
6+
7+
class FluentBundle(object):
8+
"""
9+
A FluentBundle is a single-language store of translations. It can
10+
format translation units (entities) to strings.
11+
12+
Use `FluentBundle.format` to retrieve translation units from a context.
13+
Translations can contain references to other entities or external arguments,
14+
conditional logic in form of select expressions, traits which describe their
15+
grammatical features, and can use Fluent builtins. See the documentation of
16+
the Fluent syntax for more information.
17+
18+
"""
19+
def __init__(self, locale, resources, functions=None, use_isolating=True, escapers=None):
20+
self.locale = locale
21+
compiled_ftl = compile_messages(
22+
resources,
23+
locale,
24+
use_isolating=use_isolating,
25+
functions=functions,
26+
escapers=escapers)
27+
self._compiled_messages = compiled_ftl.message_functions
28+
self._compilation_errors = compiled_ftl.errors
29+
30+
@classmethod
31+
def from_string(cls, locale, text, functions=None, use_isolating=True, escapers=None):
32+
return cls(
33+
locale,
34+
[FtlResource.from_string(text)],
35+
use_isolating=use_isolating,
36+
functions=functions,
37+
escapers=escapers
38+
)
39+
40+
@classmethod
41+
def from_files(cls, locale, filenames, functions=None, use_isolating=True, escapers=None):
42+
return cls(
43+
locale,
44+
[FtlResource.from_file(f) for f in filenames],
45+
use_isolating=use_isolating,
46+
functions=functions,
47+
escapers=escapers
48+
)
49+
50+
def has_message(self, message_id):
51+
if message_id.startswith(TERM_SIGIL) or ATTRIBUTE_SEPARATOR in message_id:
52+
return False
53+
return message_id in self._compiled_messages
54+
55+
def format(self, message_id, args=None):
56+
errors = []
57+
return self._compiled_messages[message_id](args, errors), errors
58+
59+
def check_messages(self):
60+
return self._compilation_errors

src/fluent_compiler/compiler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@
5757
PROPERTY_EXTERNAL_ARG = 'PROPERTY_EXTERNAL_ARG'
5858

5959

60+
@attr.s
61+
class FtlResource(object):
62+
'''
63+
Represents an (unparsed) FTL file (contents and optional filename)
64+
'''
65+
text = attr.ib()
66+
filename = attr.ib(default=None)
67+
68+
@classmethod
69+
def from_string(cls, text):
70+
return cls(text)
71+
72+
@classmethod
73+
def from_file(cls, filename):
74+
return cls(text=open(filename).read(), filename=filename)
75+
76+
6077
@attr.s
6178
class CurrentEnvironment(object):
6279
# The parts of CompilerEnvironment that we want to mutate (and restore)

tests/format/test_arguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66
from fluent_compiler.errors import FluentReferenceError
77

88
from ..utils import dedent_ftl

tests/format/test_attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66
from fluent_compiler.errors import FluentReferenceError
77

88
from ..utils import dedent_ftl

tests/format/test_builtins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import date, datetime
55
from decimal import Decimal
66

7-
from fluent_compiler import FluentBundle
7+
from fluent_compiler.bundle import FluentBundle
88
from fluent_compiler.errors import FluentReferenceError
99
from fluent_compiler.types import fluent_date, fluent_number
1010

tests/format/test_escapers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from markdown import markdown
99
from markupsafe import Markup, escape
1010

11-
from fluent_compiler import FluentBundle
11+
from fluent_compiler.bundle import FluentBundle
1212

1313
from ..utils import dedent_ftl
1414

tests/format/test_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import six
66

7-
from fluent_compiler import FluentBundle
7+
from fluent_compiler.bundle import FluentBundle
88
from fluent_compiler.errors import FluentReferenceError
99
from fluent_compiler.types import FluentNone, fluent_number
1010

tests/format/test_isolating.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66

77
from ..utils import dedent_ftl
88

tests/format/test_parameterized_terms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66
from fluent_compiler.errors import FluentFormatError, FluentReferenceError
77

88
from ..utils import dedent_ftl

tests/format/test_placeables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66
from fluent_compiler.errors import FluentCyclicReferenceError, FluentReferenceError
77

88
from ..utils import dedent_ftl

tests/format/test_primitives.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import six
77

8-
from fluent_compiler import FluentBundle
8+
from fluent_compiler.bundle import FluentBundle
99

1010
from ..utils import dedent_ftl
1111

tests/format/test_select_expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44

5-
from fluent_compiler import FluentBundle
5+
from fluent_compiler.bundle import FluentBundle
66
from fluent_compiler.errors import FluentReferenceError
77

88
from ..utils import dedent_ftl

tests/test_bundle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import traceback
55
import unittest
66

7-
from fluent_compiler import FluentBundle, FtlResource
7+
from fluent_compiler.bundle import FluentBundle, FtlResource
88
from fluent_compiler.errors import FluentDuplicateMessageId, FluentJunkFound, FluentReferenceError
99
from fluent_compiler.types import FluentNumber
1010

tests/test_compiler.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from markupsafe import Markup, escape
66

7-
from fluent_compiler import FtlResource
8-
from fluent_compiler.compiler import compile_messages
7+
from fluent_compiler.compiler import compile_messages, FtlResource
98
from fluent_compiler.errors import FluentCyclicReferenceError, FluentFormatError, FluentReferenceError
109
from fluent_compiler.utils import SimpleNamespace
1110

tools/benchmarks/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import subprocess
88
import sys
99

10-
from fluent_compiler import FluentBundle
10+
from fluent_compiler.bundle import FluentBundle
1111

1212
this_file = os.path.abspath(__file__)
1313
this_dir = os.path.dirname(this_file)

tools/benchmarks/fluent_benchmark.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import sys
77

88
import pytest
9-
import six
109

11-
from fluent_compiler import FluentBundle
10+
from fluent_compiler.bundle import FluentBundle
1211

1312
FTL_CONTENT = """
1413
one = One
@@ -66,5 +65,5 @@ def test_imports():
6665
]
6766
for k in fluent_deps:
6867
del sys.modules[k]
69-
from fluent_compiler import FluentBundle # noqa
68+
from fluent_compiler.bundle import FluentBundle # noqa
7069
benchmark(test_imports)

tools/benchmarks/gettext_comparisons.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import six
1313
from fluent.runtime import FluentBundle as InterpretingFluentBundle
1414

15-
from fluent_compiler import FluentBundle as CompilingFluentBundle
15+
from fluent_compiler.bundle import FluentBundle as CompilingFluentBundle
1616

1717
this_file = os.path.abspath(__file__)
1818
this_dir = os.path.dirname(this_file)

0 commit comments

Comments
 (0)