# TODO: replace dflat with hg
from osrf.app import Application
from dflat import dflat


class Versioning(Application):

    Application.register_method(
        api_name='psu.stewardship.versioning.init',
        method='init',
        argc=1,
        stream=False
        )
    def init(self, request, obj_dir):
        dflat.init(obj_dir)
        request.respond(dflat.status(obj_dir))
        

Application.register_app(Versioning())
import os
from osrf.app import Application
import osrf.log
import pairtree


class Storage(Application):

    Application.register_method(
        api_name='psu.stewardship.storage.store',
        method='store',
        argc=3,
        stream=False
        )
    def store(self, request, base_location, identifier, source):
        f = pairtree.PairtreeStorageFactory()
        tree_location = os.path.join(base_location, "stewardship")
        pairstore = f.get_store(store_dir=tree_location, uri_base="ark://")
        pairobj = pairstore.create_object(identifier)
        pairobj.add_directory(source)
        request.respond(pairobj.location)


Application.register_app(Storage())
from osrf.app import Application


class Test(Application):

    Application.register_method(
        api_name='opensrf.test.reverse',
        method='reverse',
        argc=1,
        stream=True
        )
    def reverse(self, request, message=''):
        request.respond(message[::-1])


Application.register_app(Test())
Example #4
0
class Example(Application):
    ''' Example OpenSRF application. '''

    # ---------------------------------------------------------
    # Register a new method for this application
    # ---------------------------------------------------------
    Application.register_method(
        api_name = 'opensrf.py-example.reverse', # published API name for the method
        method = 'reverse', # name of def that implements this method
        argc = 1, # expects a single argument
        stream = True # returns a stream of results.  can be called atomic-ly
    )

    # ---------------------------------------------------------
    # This method implements the API call registered above
    # ---------------------------------------------------------
    def reverse(self, request, message=''):
        ''' Returns the given string in reverse order one character at a time
            @param type:string Message to reverse 
            @return type:string The reversed message, one character at a time.  '''
        idx = len(message) - 1
        while idx >= 0:
            request.respond(message[idx])
            idx -= 1

    # ---------------------------------------------------------
    # Session data test
    # ---------------------------------------------------------

    Application.register_method(
        api_name = 'opensrf.stateful_session_test',
        method = 'session_test',
        argc = 0
    )

    def session_test(self, request):
        c = request.session.session_data.get('counter', 0) + 1
        request.session.session_data['counter'] = c
        return c

    # ---------------------------------------------------------
    # Session callbacks test
    # ---------------------------------------------------------
    Application.register_method(
        api_name = 'opensrf.session_callback_test',
        method = 'callback_test',
        argc = 0
    )

    def callback_test(self, request):
        
        def pre_req_cb(ses):
            osrf.log.log_info("running pre_request callback")

        def post_req_cb(ses):
            osrf.log.log_info("running post_request callback")

        def disconnect_cb(ses):
            osrf.log.log_info("running disconnect callback")

        def death_cb(ses):
            osrf.log.log_info("running death callback")

        ses = request.session

        ses.register_callback('pre_request', pre_req_cb)
        ses.register_callback('post_request', post_req_cb)
        ses.register_callback('disconnect', disconnect_cb)
        ses.register_callback('death', death_cb)

        c = ses.session_data.get('counter', 0) + 1
        ses.session_data['counter'] = c
        return c


    # ---------------------------------------------------------
    # These example methods override methods from 
    # osrf.app.Application.  They are not required.
    # ---------------------------------------------------------
    def global_init(self):
        osrf.log.log_debug("Running global init handler for %s" % __name__)

    def child_init(self):
        osrf.log.log_debug("Running child init handler for process %d" % os.getpid())

    def child_exit(self):
        osrf.log.log_debug("Running child exit handler for process %d" % os.getpid())
Example #5
0
        ses.register_callback('pre_request', pre_req_cb)
        ses.register_callback('post_request', post_req_cb)
        ses.register_callback('disconnect', disconnect_cb)
        ses.register_callback('death', death_cb)

        c = ses.session_data.get('counter', 0) + 1
        ses.session_data['counter'] = c
        return c


    # ---------------------------------------------------------
    # These example methods override methods from 
    # osrf.app.Application.  They are not required.
    # ---------------------------------------------------------
    def global_init(self):
        osrf.log.log_debug("Running global init handler for %s" % __name__)

    def child_init(self):
        osrf.log.log_debug("Running child init handler for process %d" % os.getpid())

    def child_exit(self):
        osrf.log.log_debug("Running child exit handler for process %d" % os.getpid())


# ---------------------------------------------------------
# Now register an instance of this class as an application
# ---------------------------------------------------------
Application.register_app(Example())


Example #6
0
import hashlib
from osrf.app import Application


class Fixity(Application):

    Application.register_method(
        api_name='psu.stewardship.fixity.generate',
        method='generate',
        argc=2,
        stream=False
        )
    def generate(self, request, file_location, algorithm):
        h = hashlib.new(algorithm)
        h.update(open(file_location).read())
        request.respond(h.hexdigest())


Application.register_app(Fixity())
from osrf.app import Application
from arkpy import arkpy


class Identity(Application):

    Application.register_method(
        api_name='psu.stewardship.identity.mint',
        method='mint',
        argc=0,
        stream=False
        )
    def mint(self, request):
        ark = arkpy.mint(authority='42409', template='eeddeeddk')
        request.respond(ark)

    Application.register_method(
        api_name='psu.stewardship.identity.validate',
        method='validate',
        argc=1,
        stream=False
        )
    def validate(self, request, ark=''):
        is_valid = arkpy.validate(ark)
        request.respond(is_valid)

    
Application.register_app(Identity())