-
Notifications
You must be signed in to change notification settings - Fork 814
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
Common modularization, custom emitters #112
Closed
charles-dyfis-net
wants to merge
2
commits into
DataDog:master
from
charles-dyfis-net:custom-emitter-support
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" Tools for loading Python modules from arbitrary locations. | ||
""" | ||
|
||
import os | ||
import imp | ||
import sys | ||
|
||
def imp_type_for_filename(filename): | ||
"""Given the name of a Python module, return a type description suitable to | ||
be passed to imp.load_module()""" | ||
for type_data in imp.get_suffixes(): | ||
extension = type_data[0] | ||
if filename.endswith(extension): | ||
return type_data | ||
return None | ||
|
||
def load_qualified_module(full_module_name, path=None): | ||
"""Load a module which may be within a package""" | ||
remaining_pieces = full_module_name.split('.') | ||
done_pieces = [] | ||
file_obj = None | ||
while remaining_pieces: | ||
try: | ||
done_pieces.append(remaining_pieces.pop(0)) | ||
curr_module_name = '.'.join(done_pieces) | ||
(file_obj, filename, description) = imp.find_module( | ||
done_pieces[-1], path) | ||
package_module = imp.load_module( | ||
curr_module_name, file_obj, filename, description) | ||
path = getattr(package_module, '__path__', None) or [filename] | ||
finally: | ||
if file_obj: | ||
file_obj.close() | ||
return package_module | ||
|
||
def module_name_for_filename(filename): | ||
"""Given the name of a Python file, find an appropropriate module name. | ||
|
||
This involves determining whether the file is within a package, and | ||
determining the name of same.""" | ||
all_segments = filename.split(os.sep) | ||
path_elements = all_segments[:-1] | ||
module_elements = [all_segments[-1].rsplit('.', 1)[0]] | ||
while os.path.exists('/'.join(path_elements + ['__init__.py'])): | ||
module_elements.insert(0, path_elements.pop()) | ||
modulename = '.'.join(module_elements) | ||
basename = '/'.join(path_elements) | ||
return (basename, modulename) | ||
|
||
def get_module(name): | ||
"""Given either an absolute path to a Python file or a module name, load | ||
and return a Python module. | ||
|
||
If the module is already loaded, takes no action.""" | ||
if name.startswith('/'): | ||
basename, modulename = module_name_for_filename(name) | ||
path = [basename] | ||
else: | ||
modulename = name | ||
path = None | ||
if modulename in sys.modules: | ||
return sys.modules[modulename] | ||
return load_qualified_module(modulename, path) | ||
|
||
def load(config_string, default_name=None): | ||
"""Given a module name and an object expected to be contained within, | ||
return said object""" | ||
(module_name, object_name) = \ | ||
(config_string.rsplit(':', 1) + [default_name])[:2] | ||
module = get_module(module_name) | ||
return getattr(module, object_name) if object_name else module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
default_target = 'DEFAULT' | ||
specified_target = 'SPECIFIED' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import os | ||
import logging | ||
import unittest | ||
|
||
import modules | ||
|
||
log = logging.getLogger('datadog.test') | ||
|
||
default_target = 'DEFAULT' | ||
specified_target = 'SPECIFIED' | ||
has_been_mutated = False | ||
|
||
class TestModuleLoad(unittest.TestCase): | ||
def setUp(self): | ||
sys.modules[__name__].has_been_mutated = True | ||
if 'tests.target_module' in sys.modules: | ||
del sys.modules['tests.target_module'] | ||
def tearDown(self): | ||
sys.modules[__name__].has_been_mutated = False | ||
def test_cached_module(self): | ||
"""Modules already in the cache should be reused""" | ||
self.assertTrue(modules.load('%s:has_been_mutated' % __name__)) | ||
def test_cache_population(self): | ||
"""Python module cache should be populated""" | ||
self.assertTrue(not 'tests.target_module' in sys.modules) | ||
modules.load('tests.target_module') | ||
self.assertTrue('tests.target_module' in sys.modules) | ||
def test_modname_load_default(self): | ||
"""When the specifier contains no module name, any provided default | ||
should be used""" | ||
self.assertEquals( | ||
modules.load( | ||
'tests.target_module', | ||
'default_target'), | ||
'DEFAULT' | ||
) | ||
def test_modname_load_specified(self): | ||
"""When the specifier contains a module name, any provided default | ||
should be overridden""" | ||
self.assertEquals( | ||
modules.load( | ||
'tests.target_module:specified_target', | ||
'default_target'), | ||
'SPECIFIED' | ||
) | ||
def test_pathname_load_finds_package(self): | ||
""""Loading modules by absolute path should correctly set the name of | ||
the loaded module to include any package containing it.""" | ||
m = modules.load(os.getcwd() + '/tests/target_module.py') | ||
self.assertEquals(m.__name__, 'tests.target_module') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@charles-dyfis-net not sure I get this line. Did you mean?
My little test
gets me
[1, '2', '3', '4']
whereas withlen(s) == 0
I only get[1]
.Did I get this right?
Interestingly enough:
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.
We might want to go listcomp all the way and replace the for loop + if test.
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.
Good catch, thanks!
As for the condensed version, that's what I would have done if it didn't mean calling
strip()
twice.