client.py
Go to the documentation of this file.
1 
2 import samweb_client
3 import http_client
4 
5 import os, pwd
6 
7 class ExperimentNotDefined(samweb_client.Error): pass
8 
9 _version = None
11  """ Get the version somehow """
12  global _version
13  if _version is None:
14 
15  # first try the baked in value
16  try:
17  from _version import version
18  _version = version
19  except ImportError:
20  _version = 'unknown'
21 
22  # if running from a git checkout, try to obtain the version
23  gitdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../.git")
24  if os.path.exists(gitdir):
25  import subprocess
26  try:
27  p = subprocess.Popen(["git", "--work-tree=%s" % os.path.join(gitdir,".."), "--git-dir=%s" % gitdir, "describe", "--tags", "--dirty"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28  if p.wait() == 0:
29  _version = p.stdout.read().strip()
30  except: pass
31  return _version
32 
34  _experiment = os.environ.get('SAM_EXPERIMENT')
35  _host = os.environ.get('SAM_WEB_HOST') or 'samweb.fnal.gov'
36  _port = os.environ.get('SAM_WEB_PORT') or '8480'
37  _sslport = os.environ.get('SAM_WEB_SSL_PORT') or '8483'
38  _baseurl = os.environ.get('SAM_WEB_BASE_URL')
39  _basesslurl = os.environ.get('SAM_WEB_BASE_SSL_URL')
40  if _basesslurl is None and _baseurl and _baseurl.startswith('https:'): _basesslurl = _baseurl
41  _group = os.environ.get('SAM_GROUP')
42  _station = os.environ.get('SAM_STATION')
43  _timezone = os.environ.get('SAM_TZ')
44 
45  # SAM role to use
46  _default_role = 'default'
47 
48  def __init__(self, experiment=None, secure=False, cert=None, key=None, devel=None, timezone=None):
49  self.devel = False
50  if experiment is not None: self.experiment = experiment
51  self.secure = secure
52  if devel is not None: self.devel = devel
53  self.http_client = http_client.get_client()
54  self.set_client_certificate(cert, key)
55  self.role = None
56  timezone = timezone or self._timezone
57  if timezone: self.timezone = timezone
58 
59  def get_role(self):
60  return self._role
61  def set_role(self, newval):
62  if newval is None:
63  self._role = self._default_role
64  else:
65  self._role = newval
66 
67  role = property(get_role, set_role)
68 
69  def get_experiment(self):
70  if self._experiment is None:
71  raise ExperimentNotDefined("Experiment is not defined")
72  return self._experiment
73 
74  def set_experiment(self, experiment):
75  self._experiment = experiment
76  if self._experiment.endswith('/dev'):
77  self.devel = True
78  self._experiment = self._experiment[:-4]
79 
80  experiment = property(get_experiment, set_experiment)
81 
82  def set_client_certificate(self, cert, key=None):
83  self.http_client.use_client_certificate(cert, key)
84 
85  def set_host(host):
86  self._host = host
87 
88  def set_ports(port, sslport):
89  if port: self._port = port
90  if sslport: self._sslport = sslport
91 
92  def get_baseurl(self, secure=None):
93  """ Return the base url. If secure is set this will be an
94  https url, if not it may not be """
95  if not secure and self._baseurl is not None:
96  return self._baseurl
97  if secure and self._basesslurl is not None:
98  return self._basesslurl
99 
100  if self.devel:
101  path = "/sam/%s/dev/api" % self.experiment
102  else:
103  path = "/sam/%s/api" % self.experiment
104  if secure:
105  if self._baseurl:
106  import sys
107  sys.stderr.write('Warning: BASEURL is set, but using default SSL URL')
108  return "https://%s:%s%s" % (self._host, self._sslport, path)
109  else:
110  return "http://%s:%s%s" % (self._host, self._port, path)
111 
112  baseurl = property(get_baseurl)
113 
114  def get_group(self):
115  if not self._group:
116  # if the group isn't set then get it from the experiment name
117  # knocking off anything after a trailing slash (ie hypot/dev -> hypot)
118  self._group = self.get_experiment().split('/', 1)[0]
119  return self._group
120 
121  def set_group(self,group):
122  self._group = group
123 
124  group = property(get_group, set_group)
125 
126  def get_station(self):
127  return self._station or self.get_experiment()
128 
129  def set_station(self,station):
130  self._station = station
131 
132  station = property(get_station, set_station)
133 
134  def get_user(self):
135  return pwd.getpwuid(os.getuid()).pw_name
136 
137  user = property(get_user)
138 
139  def get_timezone(self):
140  return self.http_client.timezone
141 
142  def set_timezone(self, new_tz):
143  self.http_client.timezone = new_tz
144 
145  timezone = property(get_timezone, set_timezone)
146 
147  def _prepareURL(self, url, secure=None):
148  # if provided with a relative url, add the baseurl
149  if '://' not in url:
150  url = self.get_baseurl(secure) + url
151  return url
152 
153  def getURL(self, url, params=None, *args, **kwargs):
154  return self._doURL(self.http_client.getURL, url, params=params, *args, **kwargs)
155 
156  def postURL(self, url, data=None, *args, **kwargs):
157  return self._doURL(self.http_client.postURL, url, data=data, *args, **kwargs)
158 
159  def putURL(self, url, data=None, *args, **kwargs):
160  return self._doURL(self.http_client.putURL, url, data=data, *args, **kwargs)
161 
162  def deleteURL(self, url, params=None, *args, **kwargs):
163  return self._doURL(self.http_client.deleteURL, url, params=params, *args, **kwargs)
164 
165  def _doURL(self, method, url, params=None, data=None, secure=None, role=None, *args, **kwargs):
166  secure = secure or self.secure
167  url = self._prepareURL(url, secure)
168  if secure: kwargs['role'] = role or self.role
169  return method(url, params=params, data=data, *args, **kwargs)
170 
171  def get_verbose(self):
172  return self.http_client.verbose
173  def set_verbose(self, verbose):
174  self.http_client.verbose = verbose
175 
176  verbose = property(get_verbose, set_verbose)
177 
178  def get_socket_timeout(self): return self.http_client.socket_timeout
179  def set_socket_timeout(self, timeout): self.http_client.socket_timeout = timeout
180  socket_timeout = property(get_socket_timeout, set_socket_timeout)
181 
183  """ Attach this function as a method of the SAMWebClient class """
184  setattr(SAMWebClient, m.func_name, m)
185  return m
void split(double tt, double *fr)
def _prepareURL(self, url, secure=None)
Definition: client.py:147
def get_baseurl(self, secure=None)
Definition: client.py:92
def set_station(self, station)
Definition: client.py:129
def set_timezone(self, new_tz)
Definition: client.py:142
def __init__(self, experiment=None, secure=False, cert=None, key=None, devel=None, timezone=None)
Definition: client.py:48
def getURL(self, url, params=None, args, kwargs)
Definition: client.py:153
def putURL(self, url, data=None, args, kwargs)
Definition: client.py:159
def set_ports(port, sslport)
Definition: client.py:88
def set_verbose(self, verbose)
Definition: client.py:173
Definition: novas.h:112
def set_client_certificate(self, cert, key=None)
Definition: client.py:82
def _doURL(self, method, url, params=None, data=None, secure=None, role=None, args, kwargs)
Definition: client.py:165
def set_group(self, group)
Definition: client.py:121
def samweb_method(m)
Definition: client.py:182
def postURL(self, url, data=None, args, kwargs)
Definition: client.py:156
def deleteURL(self, url, params=None, args, kwargs)
Definition: client.py:162
def set_experiment(self, experiment)
Definition: client.py:74
def set_role(self, newval)
Definition: client.py:61
def set_socket_timeout(self, timeout)
Definition: client.py:179