initial commit

This commit is contained in:
Lance Edgar 2012-03-07 06:04:18 -06:00
commit 7aa259c009
13 changed files with 1492 additions and 0 deletions

34
edbob/__init__.py Normal file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob`` -- Namespace Root
"""
from edbob._version import __version__
from edbob.core import *
from edbob.files import *
from edbob.modules import *
from edbob.times import *

1
edbob/_version.py Normal file
View file

@ -0,0 +1 @@
__version__ = '0.1a1'

72
edbob/core.py Normal file
View file

@ -0,0 +1,72 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.core`` -- Core Stuff
"""
import logging
import uuid
__all__ = ['Object', 'basic_logging', 'get_uuid']
class Object(object):
"""
Generic base class which provides a common ancestor, and some constructor
convenience.
"""
def __init__(self, **kwargs):
"""
Constructor. All keyword arguments are assumed to be attribute names
and are assigned directly to the new ``Object`` instance.
"""
for key in kwargs:
setattr(self, key, kwargs[key])
def basic_logging():
"""
Does some basic configuration on the ``edbob`` logger. This only enables
console output at this point; it is assumed that if you intend to configure
logging that you will be using a proper config file and calling
:func:`edbob.init()`.
"""
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(name)s: %(levelname)s: %(message)s'))
logging.getLogger('edbob').addHandler(handler)
def get_uuid():
"""
Generates a universally-unique identifier and returns its 32-character hex
value.
"""
return uuid.uuid1().hex

57
edbob/exceptions.py Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.exceptions`` -- Exceptions
"""
class LoadSpecError(Exception):
"""
Raised when something obvious goes wrong with :func:`edbob.load_spec()`.
"""
def __init__(self, spec):
self.spec = spec
def __str__(self):
msg = 'Failed to load spec: %s' % self.spec
specifics = self.specifics()
if specifics:
msg += " (%s)" % specifics
return msg
def specifics(self):
return None
class ModuleMissingAttribute(LoadSpecError):
"""
Raised during :func:`edbob.load_spec()` when the module imported okay but
the attribute could not be found.
"""
def specifics(self):
mod, attr = self.spec.split(':')
return "module '%s' was loaded but '%s' attribute not found" % (mod, attr)

58
edbob/files.py Normal file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.files`` -- Files & Folders
"""
import os
import tempfile
__all__ = ['count_lines', 'temp_path']
def count_lines(path):
"""
Convenience function to count the number of lines in a text file. Some
attempt is made to ensure cross-platform compatibility.
"""
f = open(path, 'rb')
lines = f.read().count('\n') + 1
f.close()
return lines
def temp_path(suffix='.tmp', prefix='edbob.'):
"""
Convenience function to return a temporary file path. The arguments'
meanings are the same as for ``tempfile.mkstemp()``.
"""
fd, path = tempfile.mkstemp(suffix=suffix, prefix=prefix)
os.close(fd)
os.remove(path)
return path

93
edbob/modules.py Normal file
View file

@ -0,0 +1,93 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.modules`` -- Module Tools
"""
import sys
from edbob import exceptions
__all__ = ['load_spec']
def last_module(module, module_path):
"""
Returns the "last" module represented by ``module_path``, by walking
``module`` until the desired module is found.
For example, passing a reference to the ``rattail`` module and a module
path of ``"rattail.sw.ishida.slpv"``, this function will ultimately return
a reference to the actual ``rattail.sw.ishida.slpv`` module.
This function is primarily used by :func:`import_module_path()`, since
Python's ``__import__()`` function will typically return the top-most
("first") module in the dotted path.
"""
parts = module_path.split('.')
parts.pop(0)
child = getattr(module, parts[0])
if len(parts) == 1:
return child
return last_module(child, '.'.join(parts))
def import_module_path(module_path):
"""
Imports and returns an arbitrary Python module, given its "dotted" path
(i.e. not its file path).
"""
if module_path in sys.modules:
return sys.modules[module_path]
module = __import__(module_path)
return last_module(module, module_path)
def load_spec(spec):
"""
.. highlight:: none
Returns an object as found in a module namespace. ``spec`` should be of
the same form which setuptools uses for its entry points, e.g.::
rattail.ce:collect_batch
The above would return a reference to the ``collect_batch`` function found
in the ``rattail.ce`` module namespace. The module is loaded (imported) if
necessary.
"""
module_path, obj = spec.split(':')
module = import_module_path(module_path)
try:
obj = getattr(module, obj)
except AttributeError:
raise exceptions.ModuleMissingAttribute(spec)
return obj

84
edbob/times.py Normal file
View file

@ -0,0 +1,84 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
#
# edbob -- Pythonic software framework
# Copyright © 2010,2011,2012 Lance Edgar
#
# This file is part of edbob.
#
# edbob is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# edbob is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# edbob. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
``edbob.times`` -- Date & Time Utilities
"""
import datetime
import pytz
__all__ = ['local_time', 'set_timezone']
_timezone = None
def local_time(timestamp=None):
"""
Tries to return a "localized" version of ``timestamp``, which should be a
UTC-based ``datetime.datetime`` instance.
If a local timezone has been configured, then
``datetime.datetime.utcnow()`` will be called to obtain a value for
``timestamp`` if one is not specified. Then ``timestamp`` will be modified
in such a way that its ``tzinfo`` member contains the local timezone, but
the effective UTC value for the timestamp remains accurate.
If a local timezone has *not* been configured, then
``datetime.datetime.now()`` will be called instead to obtain the value
should none be specified. ``timestamp`` will be returned unchanged.
"""
if _timezone:
if timestamp is None:
timestamp = datetime.datetime.utcnow()
timestamp = pytz.utc.localize(timestamp)
return timestamp.astimezone(_timezone)
if timestamp is None:
timestamp = datetime.datetime.now()
return timestamp
def set_timezone(tz):
"""
Sets edbob's notion of the "local" timezone. ``tz`` should be an Olson
name.
.. highlight:: ini
You usually don't need to call this yourself, since it's called by
:func:`edbob.init()` whenever ``edbob.conf`` includes a timezone::
[edbob]
timezone = US/Central
"""
global _timezone
if tz is None:
_timezone = None
else:
_timezone = pytz.timezone(tz)