-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Localization #57
base: master
Are you sure you want to change the base?
Localization #57
Changes from all commits
63f1191
6316dca
8f7f015
281edc3
c7a0db3
d515efd
ca93e29
1299fd3
b866ca1
36ace73
1cfcbd5
5dd5a45
909734d
6c6ed45
bd0087f
8ae6f77
91d2776
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ build | |
*.swo | ||
*.pyc | ||
.repos | ||
|
||
.mypy_cache | ||
mypy.ini |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Component(object): | ||
|
||
def __init__(self): | ||
raise NotImplementedError | ||
|
||
|
||
class File_C(Component): | ||
|
||
def __init__(self, | ||
filename: str = "") -> None: | ||
self.filename = filename | ||
|
||
|
||
class Line_C(Component): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
|
||
def __init__(self, | ||
filename: str = "", | ||
lineno: int = 0) -> None: | ||
self.filename = filename | ||
self.lineno = lineno | ||
|
||
|
||
class Function_C(Component): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
|
||
def __init__(self, | ||
funcname: str = "", | ||
filename: File_C = None, | ||
line_start: int = 0, | ||
line_end: int = 0) -> None: | ||
self.funcname = funcname | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer hidden variables (e.g., |
||
self.filename = filename | ||
self.line_start = line_start | ||
self.line_end = line_end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import Dict, List, Type, TypeVar | ||
from blameandshame.components import Component | ||
import yaml | ||
|
||
VERSION = "1.0" | ||
G = TypeVar('G', bound=Component) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. G and S are the same? |
||
S = TypeVar('S', bound=Component) | ||
|
||
|
||
class Localization (object): | ||
|
||
@staticmethod | ||
def from_file(filename: str) -> 'Localization': | ||
""" | ||
Builds a Localization object from a file | ||
""" | ||
with open(filename) as f: | ||
try: | ||
loc = yaml.load(f) | ||
except KeyError as err: | ||
print('Error when importing', filename, '.', err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work? Prefer |
||
raise IOError | ||
return loc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the type of |
||
|
||
def to_file(self, filename: str) -> None: | ||
""" | ||
Stores its internal data in a file | ||
""" | ||
with open(filename, "w") as f: | ||
yaml.dump(self, f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer to dump relevant information to a dictionary rather than dumping the entire object, which may contain irrelevant and/or ephemeral information. |
||
|
||
def __init__(self, | ||
mapping: Dict, | ||
scope: List[S], | ||
granularity: Type[G], | ||
version: str = VERSION) -> None: | ||
|
||
self.mapping = mapping | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use hidden variables and properties. |
||
self.scope = scope | ||
self.__version = version | ||
self.__granularity = granularity | ||
|
||
@property | ||
def version(self) -> str: | ||
""" | ||
The version number of this localization object. | ||
""" | ||
return self.__version | ||
|
||
@property | ||
def granularity(self) -> Type[G]: | ||
""" | ||
The granularity of this object | ||
""" | ||
return self.__granularity |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env python3 | ||
import tempfile | ||
import textwrap | ||
from typing import Dict, List, Type | ||
import unittest | ||
from blameandshame.localization import Localization | ||
from blameandshame.components import Component, File_C, Function_C, Line_C | ||
|
||
|
||
class LocalizationTestCase(unittest.TestCase): | ||
|
||
def test_from_file(self): | ||
""" | ||
Tests the from_file function by creating a YAML file from a string | ||
and using from_file to import it. | ||
""" | ||
def check_one(yaml_string: str, | ||
granularity: Type[Component], | ||
version: str, | ||
mapping: Dict, | ||
scope: List[Component]) -> None: | ||
f = tempfile.NamedTemporaryFile() | ||
f.write(yaml_string.encode()) | ||
f.seek(0) | ||
l = Localization.from_file(f.name) | ||
f.close() | ||
self.assertEqual(l.granularity, granularity) | ||
self.assertEqual(l.version, version) | ||
self.assertEqual(l.mapping, mapping) | ||
self.assertEqual(l.scope, scope) | ||
|
||
simple_localization = textwrap.dedent( | ||
""" | ||
!!python/object:blameandshame.localization.Localization | ||
_Localization__granularity: !!python/name:blameandshame.components.File_C | ||
_Localization__version: '0.0' | ||
mapping: {} | ||
scope: [] | ||
""" | ||
) | ||
check_one(simple_localization, File_C, "0.0", {}, []) | ||
simple_localization = textwrap.dedent( | ||
""" | ||
!!python/object:blameandshame.localization.Localization | ||
_Localization__granularity: !!python/name:blameandshame.components.Function_C | ||
_Localization__version: '0.0' | ||
mapping: {} | ||
scope: [] | ||
""" | ||
) | ||
check_one(simple_localization, Function_C, "0.0", {}, []) | ||
simple_localization = textwrap.dedent( | ||
""" | ||
!!python/object:blameandshame.localization.Localization | ||
_Localization__granularity: !!python/name:blameandshame.components.Line_C | ||
_Localization__version: '0.0' | ||
mapping: {} | ||
scope: [] | ||
""" | ||
) | ||
check_one(simple_localization, Line_C, "0.0", {}, []) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please can we change this to
FileComponent
?