-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added library ifcfg to support FreeBSD and OSX
- Loading branch information
Showing
11 changed files
with
403 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
|
||
import platform | ||
from . import meta | ||
from . import parser | ||
from . import tools | ||
from . import exc | ||
|
||
Log = tools.minimal_logger(__name__) | ||
|
||
def get_parser(**kw): | ||
""" | ||
Detect the proper parser class, and return it instantiated. | ||
Optional Arguments: | ||
parser | ||
The parser class to use instead of detecting the proper one. | ||
distro | ||
The distro to parse for (used for testing). | ||
kernel | ||
The kernel to parse for (used for testing). | ||
ifconfig | ||
The ifconfig (stdout) to pass to the parser (used for testing). | ||
""" | ||
parser = kw.get('parser', None) | ||
ifconfig = kw.get('ifconfig', None) | ||
if not parser: | ||
distro = kw.get('distro', platform.system()) | ||
full_kernel = kw.get('kernel', platform.uname()[2]) | ||
kernel = '.'.join(full_kernel.split('.')[0:2]) | ||
|
||
if distro == 'Linux': | ||
if float(kernel) < 3.3: | ||
from .parser import Linux2Parser as LinuxParser | ||
else: | ||
from .parser import LinuxParser | ||
parser = LinuxParser(ifconfig=ifconfig) | ||
elif distro in ['Darwin', 'MacOSX']: | ||
from .parser import MacOSXParser | ||
parser = MacOSXParser(ifconfig=ifconfig) | ||
elif distro in ['FreeBSD']: | ||
from .parser import FreeBSDParser | ||
parser = FreeBSDParser(ifconfig=ifconfig) | ||
else: | ||
raise exc.IfcfgParserError("Unknown distro type '%s'." % distro) | ||
|
||
Log.debug("Distro detected as '%s'" % distro) | ||
Log.debug("Using '%s'" % parser) | ||
return parser | ||
|
||
def interfaces(): | ||
""" | ||
Return just the parsed interfaces dictionary from the proper parser. | ||
""" | ||
parser = get_parser() | ||
return parser.interfaces | ||
|
||
def default_interface(): | ||
""" | ||
Return just the default interface device dictionary. | ||
""" | ||
parser = get_parser() | ||
return parser.default_interface | ||
|
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,10 @@ | ||
|
||
class IfcfgError(Exception): | ||
"""Generic Ifcfg Errors.""" | ||
def __init__(self, msg): | ||
self.msg = msg | ||
|
||
class IfcfgParserError(IfcfgError): | ||
"""Ifcfg Parsing Errors.""" | ||
def __init__(self, *args, **kw): | ||
super(IfcfgParserError, self).__init__(*args, **kw) |
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,44 @@ | ||
"""Ifcfg core meta functionality. Barrowed from http://slumber.in/.""" | ||
|
||
class Meta(object): | ||
""" | ||
Model that acts as a container class for a meta attributes for a larger | ||
class. It stuffs any kwarg it gets in it's init as an attribute of itself. | ||
""" | ||
|
||
def __init__(self, **kw): | ||
self._merge(kw) | ||
|
||
def _merge(self, dict_obj): | ||
for key, value in dict_obj.items(): | ||
setattr(self, key, value) | ||
|
||
class MetaMixin(object): | ||
""" | ||
Mixin that provides the Meta class support to add settings to instances | ||
of slumber objects. Meta settings cannot start with a _. | ||
""" | ||
|
||
def __init__(self, *args, **kw): | ||
# Get a List of all the Classes we in our MRO, find any attribute named | ||
# Meta on them, and then merge them together in order of MRO | ||
metas = reversed([x.Meta for x in self.__class__.mro() \ | ||
if hasattr(x, "Meta")]) | ||
final_meta = {} | ||
|
||
# Merge the Meta classes into one dict | ||
for meta in metas: | ||
final_meta.update(dict([x for x in list(meta.__dict__.items()) \ | ||
if not x[0].startswith("_")])) | ||
|
||
# Update the final Meta with any kw passed in | ||
for key in list(final_meta.keys()): | ||
if key in kw: | ||
final_meta[key] = kw.pop(key) | ||
|
||
self._meta = Meta(**final_meta) | ||
|
||
# FIX ME: object.__init__() doesn't take params without exception | ||
super(MetaMixin, self).__init__() |
Oops, something went wrong.