pynedm.utils

Utility modules

class pynedm.utils.ProcessObject(uri=None, username=None, password=None, adb=None, verbose=False, **kw)[source]

Process object to listen for commands as well as interacting with the nedm databases.

Parameters:
  • adb (str) – name of database
  • username (str) – username
  • password (str) – password
  • uri (str) – address of server
  • verbose (bool) – vebosity
delete_file(docid, attachment_name, db=None)[source]

delete file associated with docid.

Parameters:
  • docid (str) – document id
  • attachment_name (str) – name of attachment
  • db (str) – name of database
Returns:

json response from server

download_file(docid, attachment_name, db=None, chunk_size=102400, headers=None)[source]

download file associated with docid, yields the data in chunks first data yielded is the total expected size, the rest is the data from the file.

Parameters:
  • docid (str) – document id
  • db (str) – database name
  • chunk_size (int) – size of chunks to yield
  • headers (dict) – HTTP headers forwarded to requests
Returns:

dict – response from the server

Following code example:

from clint.textui.progress import Bar as ProgressBar
total_size = None
x = process_object.download_file("docid", "attachment", "mydb")
bar = ProgressBar(expected_size=x.next(), filled_char='=')
total = 0
with open("temp_file.out", "wb") as o:
    for ch in x:
        total += len(ch)
        bar.show(total)
        o.write(ch)
        o.flush()
open_file(docid, attachment_name, db=None)[source]

open file for reading, allows reading ranges of data.

Parameters:
  • docid (str) – document id
  • attachment_name (str) – name of attachment
  • db (str) – name of database
Returns:

pynedm.fileutils.AttachmentFile – file-like object

Following code example:

o = ProcessObject(...)
_fn = "temp.out"
_doc = "no_exist"
_db = "nedm%2Fhg_laser"
x = o.open_file(_doc, _fn, db=_db)
y = x.read(4)
print len(y), y # should be equal

print x.read()
x.seek(1)
for i in x.iterate(10):
    print i
send_command(cmd_name, *args, **kwargs)[source]

Send command, raises exception if timeout or if an exception occurs in remotely-called function.

Parameters:
  • cmd_name (str) – Name of command
  • args – arguments to command
  • db (str) – (optional) name of database
  • timeout (int) – (optional) how much time to wait, default 10000 (10 seconds)
Returns:

return of remotely-called function

Raises:

pynedm.exception.CommandError

Following code example:

o = ProcessObject(...)
# Gives by IP
print o.send_command("temp-control.1.nedm1_d.ip_get")

# Choosing another database, timeout.
print o.send_command("getvoltage", 1, db="nedm%2Finternal_coils", timeout=4000)

try:
  # Will raise error (not enough arguments)
  print o.send_command("getvoltage", db="nedm%2Finternal_coils", timeout=4000)
except:
  traceback.print_exc()

try:
  # Will raise timeout (command doesn't exist)
  print o.send_command("get_voltage",
    db="nedm%2Finternal_coils",
    timeout=4000)
except:
  traceback.print_exc()
upload_file(file_or_name, docid, db=None, attachment_name=None, callback=None)[source]

Upload file associated with a particular doc id

Parameters:
  • file_or_name (str or file) – full path to file or file-like object
  • docid (str) – id of document
  • db (str) – name of database
  • callback (func(size_read, total_size)) – upload callback, should be of form: func(size_read, total_size)
  • attachment_name (str) – name of attachment, otherwise name will be taken from file
wait()[source]

Wait until the current changes feed execution is complete. Execution can be stopped also by calling stop_listening()

write_document_to_db(adoc, db=None, ignoreErrors=True)[source]

Write a document to the database.

Parameters:
  • adoc (dict) – dictionary to be return to the DB.
  • db (str) – database name
  • ignoreErrors (bool) – if True, do not reraise errors
Returns:

dict – response from the server

Raises:

pynedm.exception.PynEDMException

pynedm.utils.stop_listening(stop=True)[source]

Request the listening to stop. Code blocked on ProcessObject.wait() will proceed.

pynedm.utils.should_stop()[source]

Returns whether or not stop has been requested.

Return type:bool
pynedm.utils.listen(function_dict, database, username=None, password=None, uri='http://localhost:5984', verbose=False, **kw)[source]

Listen to database changes feed and execute commands when certain documents arrive.

Parameters:
  • function_dict (dict) – dictionary of functions (values) with names (keys)
  • database (str) – name of database
  • username (str) – username
  • password (str) – password
  • uri (str) – address of server
  • verbose (bool) – vebosity
Return type:

ProcessObject

function_dict should look like the following:

adict = {
   "func_name1" : func1,
   "func_name2" : func2,
}

or, if explicitly passing in documentation strings:

adict = {
   "func_name1" : (func1, "my doc string"),
   "func_name2" : (func2, "my doc string for func2")
}

where of course the names can be more creative and func1/2 should be actually references to functions.