Пример #1
0
def update_olog_uid(uid, text, attachments):
    '''Update olog book  logid entry cotaining uid string with text and attachments files
    uid: string, the uid of a scan or a specficial string (only gives one log entry)
    text: the text to update, will add this text to the old text
    attachments: add new attachment files    
    An example:
    
    filename1 = '/XF11ID/analysis/2016_2/yuzhang/Results/August/af8f66/Report_uid=af8f66.pdf'
    atch=[  Attachment(open(filename1, 'rb')) ] 
    update_olog_uid( uid='af8f66', text='Add xpcs pdf report', attachments= atch )    
    
    '''

    olog_client = SimpleOlogClient(
        url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog',
        username='******',
        password='******')

    client = OlogClient(url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog',
                        username='******',
                        password='******')

    logid = olog_client.find(search=uid)[0]['id']
    #print(attachments)
    update_olog_id(logid, text, attachments)
Пример #2
0
def update_olog_uid( uid, text, attachments):  
    '''Update olog book  logid entry cotaining uid string with text and attachments files
    uid: string, the uid of a scan or a specficial string (only gives one log entry)
    text: the text to update, will add this text to the old text
    attachments: add new attachment files    
    An example:
    
    filename1 = '/XF11ID/analysis/2016_2/yuzhang/Results/August/af8f66/Report_uid=af8f66.pdf'
    atch=[  Attachment(open(filename1, 'rb')) ] 
    update_olog_uid( uid='af8f66', text='Add xpcs pdf report', attachments= atch )    
    
    '''
    
    olog_client=SimpleOlogClient( url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog', 
                                    username= '******', password= '******' )
    
    client = OlogClient( url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog', 
                                    username= '******', password= '******' )
    
    logid = olog_client.find( search= uid )[0]['id']
    #print(attachments)
    update_olog_id( logid, text, attachments)    
    
    
    
    
    
Пример #3
0
def update_olog_id(logid, text, attachments):
    '''Update olog book  logid entry with text and attachments files
    logid: integer, the log entry id
    text: the text to update, will add this text to the old text
    attachments: add new attachment files
    An example:
    
    filename1 = '/XF11ID/analysis/2016_2/yuzhang/Results/August/af8f66/Report_uid=af8f66.pdf'
    atch=[  Attachment(open(filename1, 'rb')) ] 
    
    update_olog_id( logid=29327, text='add_test_atch', attachmenents= atch )    
    
    '''
    url = 'https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog'
    olog_client = SimpleOlogClient(url=url,
                                   username='******',
                                   password='******')

    client = OlogClient(url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog',
                        username='******',
                        password='******')

    old_text = olog_client.find(id=logid)[0]['text']
    upd = LogEntry(
        text=old_text + '\n' + text,
        attachments=attachments,
        logbooks=[Logbook(name='Operations', owner=None, active=True)])
    upL = client.updateLog(logid, upd)
    print(
        'The url=%s was successfully updated with %s and with the attachments'
        % (url, text))
Пример #4
0
def update_olog_id( logid, text, attachments):   
    '''Update olog book  logid entry with text and attachments files
    logid: integer, the log entry id
    text: the text to update, will add this text to the old text
    attachments: add new attachment files
    An example:
    
    filename1 = '/XF11ID/analysis/2016_2/yuzhang/Results/August/af8f66/Report_uid=af8f66.pdf'
    atch=[  Attachment(open(filename1, 'rb')) ] 
    
    update_olog_id( logid=29327, text='add_test_atch', attachmenents= atch )    
    
    '''
    url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog'
    olog_client=SimpleOlogClient(  url= url, 
                                    username= '******', password= '******'   )
    
    client = OlogClient( url='https://logbook.nsls2.bnl.gov/Olog-11-ID/Olog', 
                                    username= '******', password= '******' )
    
    old_text =  olog_client.find( id = logid )[0]['text']    
    upd = LogEntry( text= old_text + '\n'+text,   attachments=  attachments,
                      logbooks= [Logbook( name = 'Operations', owner=None, active=True)]
                  )  
    upL = client.updateLog( logid, upd )    
    print( 'The url=%s was successfully updated with %s and with the attachments'%(url, text))
Пример #5
0
class OlogCallback(CallbackBase):
    """Example callback to customize the logbook.

    This callback publishes the most recent IPython command (which of course
    is not guaranteed to be the one that initiated the run in question) and
    the full RunStart Document.

    Example
    -------
    # add this callback to the run engine
    >>> gs.RE.subscribe(OlogCallback(), 'start')
    # turn off the default logger
    >>> gs.RE.logbook = None
    """
    def __init__(self, logbook):
        self.logbook = logbook
        from pyOlog import SimpleOlogClient
        self.client = SimpleOlogClient()
        # Check at init time we are in an IPython session.
        from IPython import get_ipython  # noqa: F401

    def start(self, doc):
        from IPython import get_ipython
        commands = list(get_ipython().history_manager.get_range())
        document_content = ('%s: %s\n\n'
                            'RunStart Document\n'
                            '-----------------\n'
                            '%s' %
                            (doc['scan_id'], commands[-1][2], pformat(doc)))
        olog_status = self.client.log(document_content, logbooks=self.logbook)
        logger.debug('client.log returned %s' % olog_status)
        super().start(doc)
Пример #6
0
class OlogCallback(CallbackBase):
    """Example callback to customize the logbook.

    This callback publishes the most recent IPython command (which of course
    is not guaranteed to be the one that initiated the run in question) and
    the full RunStart Document.

    Example
    -------
    # add this callback to the run engine
    >>> gs.RE.subscribe(OlogCallback(), 'start')
    # turn off the default logger
    >>> gs.RE.logbook = None
    """
    def __init__(self, logbook):
        self.logbook = logbook
        from pyOlog import SimpleOlogClient
        self.client = SimpleOlogClient()
        # Check at init time we are in an IPython session.
        from IPython import get_ipython

    def start(self, doc):
        from IPython import get_ipython
        commands = list(get_ipython().history_manager.get_range())
        document_content = ('%s: %s\n\n'
                            'RunStart Document\n'
                            '-----------------\n'
                            '%s' % (doc['scan_id'],
                                    commands[-1][2],
                                    pformat(doc)))
        olog_status = self.client.log(document_content, logbooks=self.logbook)
        logger.debug('client.log returned %s' % olog_status)
        super().start(doc)
Пример #7
0
Call:
    {{ start.signature }}
{% endif %}
Metadata
--------
{% for k, v in start.items() -%}
{%- if k not in ['plan_name', 'plan_args'] -%}{{ k }} : {{ v }}
{% endif -%}
{%- endfor -%}"""

bluesky.callbacks.olog.TEMPLATES['desc'] = """
{{- start.plan_name }} ['{{ start.uid[:6] }}'] (scan num: {{ start.scan_id }})"""

bluesky.callbacks.olog.TEMPLATES['call'] = """RE({{ start.plan_name }}(
{%- for k, v in start.plan_args.items() %}{%- if not loop.first %}   {% endif %}{{ k }}={{ v }}
{%- if not loop.last %},
{% endif %}{% endfor %}))
"""

LOGBOOKS = ['Comissioning']  # list of logbook names to publish to
simple_olog_client = SimpleOlogClient()
generic_logbook_func = simple_olog_client.log
configured_logbook_func = partial(generic_logbook_func, logbooks=LOGBOOKS)

cb = logbook_cb_factory(configured_logbook_func)
RE.subscribe('start', cb)
loogbook = simple_olog_client


import esm # general ESM routines: e2g(), g2e(), angles()
Пример #8
0
 def __init__(self, logbook):
     self.logbook = logbook
     from pyOlog import SimpleOlogClient
     self.client = SimpleOlogClient()
     # Check at init time we are in an IPython session.
     from IPython import get_ipython  # noqa: F401
Пример #9
0
 def __init__(self, logbook):
     self.logbook = logbook
     from pyOlog import SimpleOlogClient
     self.client = SimpleOlogClient()
     # Check at init time we are in an IPython session.
     from IPython import get_ipython
Пример #10
0
def configure_olog(user_ns, *, callback=None, subscribe=True):
    """
    Setup a callback that publishes some metadata from the RunEngine to Olog.

    Also, add the public contents of pyOlog.ophyd_tools to the namespace.

    This is expected to be run after :func:`configure_base`. It expects to find
    an instance of RunEngine named ``RE`` in the user namespace. Additionally,
    if the user namespace contains the name ``logbook``, that is expected to be
    an instance ``pyOlog.SimpleOlogClient``.

    Parameters
    ----------
    user_ns: dict
        a namespace --- for example, ``get_ipython().user_ns``
    callback : callable, optional
        a hook for customizing the logbook_cb_factory; if None a default is
        used
    subscribe : boolean, optional
        True by default. Set to False to skip the subscription. (You still get
        pyOlog.ophyd_tools.)

    Returns
    -------
    names : list
        list of names added to the namespace

    Examples
    --------
    Configure the Olog.

    >>>> configure_olog(get_ipython().user_ns);
    """
    # Conceptually our task is simple: add a subscription to the RunEngine that
    # publishes to the Olog using the Python wrapper of its REST API, pyOlog.
    # In practice this is messy because we have deal with the many-layered API
    # of pyOlog and, more importantly, ensure that slowness or errors from the
    # Olog do not affect the run. Historically the Olog deployment has not been
    # reliable, so it is important to be robust against these issues. Of
    # course, by ignoring Olog errors, we leave gaps in the log, which is not
    # great, but since all data is saved to a databroker anyway, we can always
    # re-generate them later.

    ns = {}  # We will update user_ns with this at the end.

    from bluesky.callbacks.olog import logbook_cb_factory
    from functools import partial
    from pyOlog import SimpleOlogClient
    import queue
    import threading
    from warnings import warn

    # This is for pyOlog.ophyd_tools.get_logbook, which simply looks for
    # a variable called 'logbook' in the global IPython namespace.
    if "logbook" in user_ns:
        simple_olog_client = user_ns["logbook"]
    else:
        simple_olog_client = SimpleOlogClient()
        ns["logbook"] = simple_olog_client

    if subscribe:
        if callback is None:
            # list of logbook names to publish to
            LOGBOOKS = ("Data Acquisition", )
            generic_logbook_func = simple_olog_client.log
            configured_logbook_func = partial(generic_logbook_func,
                                              logbooks=LOGBOOKS)
            callback = logbook_cb_factory(configured_logbook_func)

        def submit_to_olog(queue, cb):
            while True:
                name, doc = queue.get()  # waits until document is available
                try:
                    cb(name, doc)
                except Exception as exc:
                    warn("This olog is giving errors. This will not be logged."
                         "Error:" + str(exc))

        olog_queue = queue.Queue(maxsize=100)
        olog_thread = threading.Thread(target=submit_to_olog,
                                       args=(olog_queue, callback),
                                       daemon=True)

        olog_thread.start()

        def send_to_olog_queue(name, doc):
            try:
                olog_queue.put((name, doc), block=False)
            except queue.Full:
                warn("The olog queue is full. This will not be logged.")

        RE = user_ns["RE"]
        RE.subscribe(send_to_olog_queue, "start")

    import pyOlog.ophyd_tools

    import_star(pyOlog.ophyd_tools, ns)

    user_ns.update(ns)
    return list(ns)