definitions.py
Go to the documentation of this file.
1 """ Client methods relating to definitions """
2 
3 from samweb_client import json, convert_from_unicode
4 from samweb_client.client import samweb_method
5 from samweb_client.http_client import escape_url_path, escape_url_component
6 from samweb_client.exceptions import *
7 
8 from itertools import ifilter
9 
10 @samweb_method
11 def listDefinitions(samweb, stream=False, **queryCriteria):
12  """ List definitions matching given query parameters:
13  arguments:
14  one or more key=string value pairs to pass to server
15  stream: boolean: if True, the results will be returned progressively
16  """
17  params = dict(queryCriteria)
18  params['format'] = 'plain'
19  result = samweb.getURL('/definitions/list', params, stream=True)
20  output = ifilter( None, (l.strip() for l in result.iter_lines()) )
21  if stream: return output
22  else: return list(output)
23 
24 def _descDefinitionURL(defname):
25  return '/definitions/name/' + escape_url_component(defname) + '/describe'
26 
27 @samweb_method
28 def descDefinitionDict(samweb, defname):
29  """ Describe a dataset definition
30  arguments:
31  definition name
32  """
33  result = samweb.getURL(_descDefinitionURL(defname))
34  return convert_from_unicode(result.json())
35 
36 @samweb_method
37 def descDefinition(samweb, defname):
38  """ Describe a dataset definition
39  arguments:
40  definition name
41  """
42  result = samweb.getURL(_descDefinitionURL(defname), {'format':'plain'})
43  return result.text.rstrip()
44 
45 @samweb_method
46 def createDefinition(samweb, defname, dims, user=None, group=None, description=None):
47  """ Create a new dataset definition
48  arguments:
49  definition name
50  dimensions string
51  user: username (default None)
52  group: group name (default None)
53  description: description of new definition (default None)
54  """
55 
56  params = { "defname": defname,
57  "dims": dims,
58  "user": user or samweb.user,
59  "group": group or samweb.group,
60  }
61  if description:
62  params["description"] = description
63 
64  result = samweb.postURL('/definitions/create', params, secure=True)
65  return result.json()
66 
67 @samweb_method
68 def modifyDefinition(samweb, existing_defname, defname=None, description=None):
69  """ Modify a dataset definition
70  arguments:
71  existing definition name
72  defname: new name for definition
73  description: new description
74  """
75  params = {}
76  if defname:
77  params["defname"] = defname
78  if description:
79  params["description"] = description
80  samweb.putURL('/definitions/name/%s' % escape_url_component(existing_defname), params, secure=True, role='*')
81 
82 @samweb_method
83 def deleteDefinition(samweb, defname):
84  """ Delete a dataset definition
85  arguments:
86  definition name
87 
88  (Definitions that have already been used cannot be deleted)
89  """
90  result = samweb.deleteURL('/definitions/name/%s' % escape_url_component(defname), {}, secure=True)
91  return result.text.rstrip()
92 
93 @samweb_method
94 def takeSnapshot(samweb, defname, group=None):
95  """ Create a snapshot for a existing definition
96  arguments:
97  definition name
98  group: group (default experiment name)
99  """
100  if not group: group = samweb.group
101  result = samweb.postURL('/definitions/name/%s/snapshot?format=plain' % escape_url_component(defname), {"group":group}, secure=True)
102  return int(result.text.rstrip())
103 
def listDefinitions(samweb, stream=False, queryCriteria)
Definition: definitions.py:11
def modifyDefinition(samweb, existing_defname, defname=None, description=None)
Definition: definitions.py:68
def descDefinition(samweb, defname)
Definition: definitions.py:37
def descDefinitionDict(samweb, defname)
Definition: definitions.py:28
def deleteDefinition(samweb, defname)
Definition: definitions.py:83
def _descDefinitionURL(defname)
Definition: definitions.py:24
def createDefinition(samweb, defname, dims, user=None, group=None, description=None)
Definition: definitions.py:46
def convert_from_unicode(input)
Definition: __init__.py:14
def takeSnapshot(samweb, defname, group=None)
Definition: definitions.py:94