Robot Framework SSH Library
config.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from .utils import is_bytes, secs_to_timestr, timestr_to_secs
17 
18 
19 
22  pass
23 
24 
25 
40 class Configuration():
41  def __init__(self, **entries):
42  self._config_config = entries
43 
44  def __str__(self):
45  return '\n'.join('%s=%s' % (k, v) for k, v in self._config_config.items())
46 
47 
55  def update(self, **entries):
56  for name, value in entries.items():
57  if value is not None:
58  self._config_config[name].set(value)
59 
60 
61  def get(self, name):
62  return self._config_config[name]
63 
64  def __getattr__(self, name):
65  if name in self._config_config:
66  return self._config_config[name].value
67  msg = "Configuration parameter '%s' is not defined." % name
68  raise ConfigurationException(msg)
69 
70 
71 
75 class Entry():
76 
77  def __init__(self, initial=None):
78  self._value_value = self._create_value_create_value(initial)
79 
80  def __str__(self):
81  return str(self._value_value)
82 
83  @property
84  value = property
85 
86  def value(self):
87  return self._value_value
88 
89  def set(self, value):
90  self._value_value = self._parse_value_parse_value(value)
91 
92  def _parse_value(self, value):
93  raise NotImplementedError
94 
95  def _create_value(self, value):
96  if value is None:
97  return None
98  return self._parse_value_parse_value(value)
99 
100 
101 
103 
104  def _parse_value(self, value):
105  return str(value)
106 
107 
108 
114  def _parse_value(self, value):
115  return int(value)
116 
117 
118 
125  def _parse_value(self, value):
126  value = str(value)
127  return timestr_to_secs(value) if value else None
128 
129  def __str__(self):
130  return secs_to_timestr(self._value_value)
131 
132 
133 
139  LEVELS = ('TRACE', 'DEBUG', 'INFO', 'WARN', 'NONE')
140 
141  def _parse_value(self, value):
142  value = str(value).upper()
143  if value not in self.LEVELSLEVELS:
144  raise ConfigurationException("Invalid log level '%s'." % value)
145  return value
146 
147 
148 
155 
156  def _parse_value(self, value):
157  if is_bytes(value):
158  value = value.decode('ASCII')
159  value = value.upper()
160  return value.replace('LF', '\n').replace('CR', '\r')
161 
Raised when creating, updating or accessing a Configuration entry fails.
Definition: config.py:21
A simple configuration class.
Definition: config.py:40
def update(self, **entries)
Update configuration entries.
Definition: config.py:55
def get(self, name)
Return entry corresponding to name.
Definition: config.py:61
def __init__(self, **entries)
Definition: config.py:41
def __getattr__(self, name)
Definition: config.py:64
A base class for values stored in :py:class:Configuration.
Definition: config.py:75
def _parse_value(self, value)
Definition: config.py:92
def set(self, value)
Definition: config.py:89
def __str__(self)
Definition: config.py:80
def __init__(self, initial=None)
Definition: config.py:77
def _create_value(self, value)
Definition: config.py:95
Integer value to be stored in stored in :py:class:Configuration.
Definition: config.py:113
def _parse_value(self, value)
Definition: config.py:114
Log level to be stored in :py:class:Configuration.
Definition: config.py:138
def _parse_value(self, value)
Definition: config.py:141
New line sequence to be stored in :py:class:Configuration.
Definition: config.py:154
def _parse_value(self, value)
Definition: config.py:156
String value to be stored in :py:class:Configuration.
Definition: config.py:102
def _parse_value(self, value)
Definition: config.py:104
Time string to be stored in :py:class:Configuration.
Definition: config.py:124
def _parse_value(self, value)
Definition: config.py:125