"""Common services code"""


from datetime import datetime
import importlib

AVAILABLE_SERVICES = ['habit_list', 'idonethis', 'nikeplus']
SERVICES_PKG = 'dayonetools.services'

def get_service_module(service_name):
    """Import given service from dayonetools.services package"""

    module = '%s.%s' % (SERVICES_PKG, service_name)
    return importlib.import_module(module)


def convert_to_dayone_date_string(date):
    """
    Convert given date in 'yyyy-mm-dd' format into dayone accepted format of
    iso8601

    The timestamp will match the current time but year, month, and day will
    be replaced with given arguments.
    """

    year, month, day = date.split('-')
    now = datetime.utcnow()

    # Dayone doesn't read entries correctly when date has a ms component
    ms = 0

    date = now.replace(year=int(year),
                       month=int(month),
                       day=int(day),
                       microsecond=ms)

    iso_string = date.isoformat()

    # Very specific format for dayone, if the 'Z' is not in the
    # correct positions the entries will not show up in dayone at all.
    return iso_string + 'Z'


# Make all services available from this level
for service_name in AVAILABLE_SERVICES:
    service = get_service_module(service_name)

@@ -25,57 +25,8 @@
 %22%22%0A%0A
-%0Afrom datetime import datetime%0Aimport importlib%0A%0A
 AVAI
@@ -86,46 +86,9 @@
 s'%5D%0A
-SERVICES_PKG = 'dayonetools.services'
 %0A
+
 %0Adef
@@ -184,24 +184,88 @@
 package%22%22%22%0A%0A
+    import importlib%0A    services_pkg = 'dayonetools.services'%0A%0A
     module =
@@ -280,20 +280,20 @@
  %25 (
-SERVICES_PKG
+services_pkg
 , se
@@ -649,16 +649,51 @@
 lit('-')
+%0A%0A    from datetime import datetime
 %0A    now

Move some imports internal so importing services package is not so cluttered