MyHost / couchdb / fill_couch.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

"""
Python source code - replace this with a description of the code and write
the code below this text.
"""
__author__ = "Frank Becker <fb@alien8.de>"
__version__ = "$Revision: 0.0 $"
__date__ = "$Date: YDATE $"
__copyright__ = "Copyright (c) 2010 Frank Becker"
__license__ = "Python"

from datetime import datetime
import couchdbkit

class Host(couchdbkit.Document):
    """CouchDB doc for Host"""
    name = couchdbkit.StringProperty()
    date_updated = couchdbkit.DateTimeProperty()
    date_created = couchdbkit.DateTimeProperty()


def db_connect():
    """connect to db"""

    try:
        server = couchdbkit.Server(uri="http://alzey.osrc.amd.com:5984")
    except:
        return False

    db = server.get_or_create_db("myhost_hosts")

    return db

def db_register(doc_class, db):
    """Register Document class to DB and return it

    FIXME (a8): This doesn't work, I got no fucking clue why
    """

    try:
        setattr(doc_class, 'set_db', db)
    except:
        return False

    return doc_class

def save_or_create_host(cls, name=None, **kwargs):
    """

    Args:
        cls registered Host() class
        name=None, **kwargs

    Returns:

        host obj

    """
    if not name:
        return False

    host = cls(name=name, date_created=datetime.utcnow())
    for key, value in kwargs.items():
        setattr(host, key, value)
    try:
        host.save()
    except:
        return False

    return host

def send_view(path):
    """
    send design document to db
    """
    couchdbkit.designer.push(path, db_connect())

def get_all_hosts():
    """

    Args:


    Returns:

    """
    return Host.view("hosts/all")

def get_host_by_name(host):
    """

    Args:


    Returns:

    """
    return Host.view("hosts/one", key=host)


def main():
    """ main
    """
    db_connect()

if __name__ == '__main__':
    main()

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8 :
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.