onetick-py 1.162.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. locator_parser/__init__.py +0 -0
  2. locator_parser/acl.py +73 -0
  3. locator_parser/actions.py +266 -0
  4. locator_parser/common.py +365 -0
  5. locator_parser/io.py +41 -0
  6. locator_parser/locator.py +150 -0
  7. onetick/__init__.py +101 -0
  8. onetick/doc_utilities/__init__.py +3 -0
  9. onetick/doc_utilities/napoleon.py +40 -0
  10. onetick/doc_utilities/ot_doctest.py +140 -0
  11. onetick/doc_utilities/snippets.py +280 -0
  12. onetick/lib/__init__.py +4 -0
  13. onetick/lib/instance.py +138 -0
  14. onetick/py/__init__.py +290 -0
  15. onetick/py/_stack_info.py +89 -0
  16. onetick/py/_version.py +2 -0
  17. onetick/py/aggregations/__init__.py +11 -0
  18. onetick/py/aggregations/_base.py +645 -0
  19. onetick/py/aggregations/_docs.py +912 -0
  20. onetick/py/aggregations/compute.py +286 -0
  21. onetick/py/aggregations/functions.py +2216 -0
  22. onetick/py/aggregations/generic.py +104 -0
  23. onetick/py/aggregations/high_low.py +80 -0
  24. onetick/py/aggregations/num_distinct.py +83 -0
  25. onetick/py/aggregations/order_book.py +427 -0
  26. onetick/py/aggregations/other.py +1014 -0
  27. onetick/py/backports.py +26 -0
  28. onetick/py/cache.py +373 -0
  29. onetick/py/callback/__init__.py +5 -0
  30. onetick/py/callback/callback.py +275 -0
  31. onetick/py/callback/callbacks.py +131 -0
  32. onetick/py/compatibility.py +752 -0
  33. onetick/py/configuration.py +736 -0
  34. onetick/py/core/__init__.py +0 -0
  35. onetick/py/core/_csv_inspector.py +93 -0
  36. onetick/py/core/_internal/__init__.py +0 -0
  37. onetick/py/core/_internal/_manually_bound_value.py +6 -0
  38. onetick/py/core/_internal/_nodes_history.py +250 -0
  39. onetick/py/core/_internal/_op_utils/__init__.py +0 -0
  40. onetick/py/core/_internal/_op_utils/every_operand.py +9 -0
  41. onetick/py/core/_internal/_op_utils/is_const.py +10 -0
  42. onetick/py/core/_internal/_per_tick_scripts/tick_list_sort_template.script +121 -0
  43. onetick/py/core/_internal/_proxy_node.py +140 -0
  44. onetick/py/core/_internal/_state_objects.py +2307 -0
  45. onetick/py/core/_internal/_state_vars.py +87 -0
  46. onetick/py/core/_source/__init__.py +0 -0
  47. onetick/py/core/_source/_symbol_param.py +95 -0
  48. onetick/py/core/_source/schema.py +97 -0
  49. onetick/py/core/_source/source_methods/__init__.py +0 -0
  50. onetick/py/core/_source/source_methods/aggregations.py +810 -0
  51. onetick/py/core/_source/source_methods/applyers.py +296 -0
  52. onetick/py/core/_source/source_methods/columns.py +141 -0
  53. onetick/py/core/_source/source_methods/data_quality.py +301 -0
  54. onetick/py/core/_source/source_methods/debugs.py +270 -0
  55. onetick/py/core/_source/source_methods/drops.py +120 -0
  56. onetick/py/core/_source/source_methods/fields.py +619 -0
  57. onetick/py/core/_source/source_methods/filters.py +1001 -0
  58. onetick/py/core/_source/source_methods/joins.py +1393 -0
  59. onetick/py/core/_source/source_methods/merges.py +566 -0
  60. onetick/py/core/_source/source_methods/misc.py +1325 -0
  61. onetick/py/core/_source/source_methods/pandases.py +155 -0
  62. onetick/py/core/_source/source_methods/renames.py +356 -0
  63. onetick/py/core/_source/source_methods/sorts.py +183 -0
  64. onetick/py/core/_source/source_methods/switches.py +142 -0
  65. onetick/py/core/_source/source_methods/symbols.py +117 -0
  66. onetick/py/core/_source/source_methods/times.py +627 -0
  67. onetick/py/core/_source/source_methods/writes.py +702 -0
  68. onetick/py/core/_source/symbol.py +202 -0
  69. onetick/py/core/_source/tmp_otq.py +222 -0
  70. onetick/py/core/column.py +209 -0
  71. onetick/py/core/column_operations/__init__.py +0 -0
  72. onetick/py/core/column_operations/_methods/__init__.py +4 -0
  73. onetick/py/core/column_operations/_methods/_internal.py +28 -0
  74. onetick/py/core/column_operations/_methods/conversions.py +215 -0
  75. onetick/py/core/column_operations/_methods/methods.py +294 -0
  76. onetick/py/core/column_operations/_methods/op_types.py +150 -0
  77. onetick/py/core/column_operations/accessors/__init__.py +0 -0
  78. onetick/py/core/column_operations/accessors/_accessor.py +30 -0
  79. onetick/py/core/column_operations/accessors/decimal_accessor.py +92 -0
  80. onetick/py/core/column_operations/accessors/dt_accessor.py +464 -0
  81. onetick/py/core/column_operations/accessors/float_accessor.py +160 -0
  82. onetick/py/core/column_operations/accessors/str_accessor.py +1374 -0
  83. onetick/py/core/column_operations/base.py +1061 -0
  84. onetick/py/core/cut_builder.py +149 -0
  85. onetick/py/core/db_constants.py +20 -0
  86. onetick/py/core/eval_query.py +244 -0
  87. onetick/py/core/lambda_object.py +442 -0
  88. onetick/py/core/multi_output_source.py +193 -0
  89. onetick/py/core/per_tick_script.py +2253 -0
  90. onetick/py/core/query_inspector.py +465 -0
  91. onetick/py/core/source.py +1663 -0
  92. onetick/py/db/__init__.py +2 -0
  93. onetick/py/db/_inspection.py +1042 -0
  94. onetick/py/db/db.py +1423 -0
  95. onetick/py/db/utils.py +64 -0
  96. onetick/py/docs/__init__.py +0 -0
  97. onetick/py/docs/docstring_parser.py +112 -0
  98. onetick/py/docs/utils.py +81 -0
  99. onetick/py/functions.py +2354 -0
  100. onetick/py/license.py +188 -0
  101. onetick/py/log.py +88 -0
  102. onetick/py/math.py +947 -0
  103. onetick/py/misc.py +437 -0
  104. onetick/py/oqd/__init__.py +22 -0
  105. onetick/py/oqd/eps.py +1195 -0
  106. onetick/py/oqd/sources.py +325 -0
  107. onetick/py/otq.py +211 -0
  108. onetick/py/pyomd_mock.py +47 -0
  109. onetick/py/run.py +841 -0
  110. onetick/py/servers.py +173 -0
  111. onetick/py/session.py +1342 -0
  112. onetick/py/sources/__init__.py +19 -0
  113. onetick/py/sources/cache.py +167 -0
  114. onetick/py/sources/common.py +126 -0
  115. onetick/py/sources/csv.py +642 -0
  116. onetick/py/sources/custom.py +85 -0
  117. onetick/py/sources/data_file.py +305 -0
  118. onetick/py/sources/data_source.py +1049 -0
  119. onetick/py/sources/empty.py +94 -0
  120. onetick/py/sources/odbc.py +337 -0
  121. onetick/py/sources/order_book.py +238 -0
  122. onetick/py/sources/parquet.py +168 -0
  123. onetick/py/sources/pit.py +191 -0
  124. onetick/py/sources/query.py +495 -0
  125. onetick/py/sources/snapshots.py +419 -0
  126. onetick/py/sources/split_query_output_by_symbol.py +198 -0
  127. onetick/py/sources/symbology_mapping.py +123 -0
  128. onetick/py/sources/symbols.py +357 -0
  129. onetick/py/sources/ticks.py +825 -0
  130. onetick/py/sql.py +70 -0
  131. onetick/py/state.py +256 -0
  132. onetick/py/types.py +2056 -0
  133. onetick/py/utils/__init__.py +70 -0
  134. onetick/py/utils/acl.py +93 -0
  135. onetick/py/utils/config.py +186 -0
  136. onetick/py/utils/default.py +49 -0
  137. onetick/py/utils/file.py +38 -0
  138. onetick/py/utils/helpers.py +76 -0
  139. onetick/py/utils/locator.py +94 -0
  140. onetick/py/utils/perf.py +499 -0
  141. onetick/py/utils/query.py +49 -0
  142. onetick/py/utils/render.py +1139 -0
  143. onetick/py/utils/script.py +244 -0
  144. onetick/py/utils/temp.py +471 -0
  145. onetick/py/utils/types.py +118 -0
  146. onetick/py/utils/tz.py +82 -0
  147. onetick_py-1.162.2.dist-info/METADATA +148 -0
  148. onetick_py-1.162.2.dist-info/RECORD +152 -0
  149. onetick_py-1.162.2.dist-info/WHEEL +5 -0
  150. onetick_py-1.162.2.dist-info/entry_points.txt +2 -0
  151. onetick_py-1.162.2.dist-info/licenses/LICENSE +21 -0
  152. onetick_py-1.162.2.dist-info/top_level.txt +2 -0
onetick/py/session.py ADDED
@@ -0,0 +1,1342 @@
1
+ import os
2
+ import re
3
+ import warnings
4
+ import shutil
5
+ from datetime import datetime
6
+ from typing import List
7
+
8
+ from locator_parser.io import FileReader, FileWriter, PrintWriter
9
+ from locator_parser.actions import Add, GetAll, Delete
10
+ from locator_parser.common import apply_actions
11
+ from locator_parser import locator as _locator
12
+ from locator_parser import acl as _acl
13
+ from abc import ABC, abstractmethod
14
+ from onetick.py.otq import otli
15
+ from . import utils
16
+ from . import license as _license
17
+ from . import db as _db
18
+ from . import servers as _servers
19
+ from .configuration import config
20
+ from .db._inspection import databases as _databases
21
+
22
+
23
+ class EntityOperationFailed(Exception):
24
+ """
25
+ Raise when operation with an entity for a config
26
+ in this module failed
27
+ """
28
+
29
+
30
+ class MultipleSessionsException(Exception):
31
+ """
32
+ Raises when user tries to initiate a new one Session
33
+ when another one is already used
34
+ """
35
+
36
+
37
+ def _check_entities(entities):
38
+ if not entities:
39
+ raise ValueError("At least one argument in parameter 'entities' is expected.")
40
+
41
+
42
+ def _apply_to_entities(cfg, operations):
43
+ """
44
+ Function generalizes operations for locators and ACLs:
45
+ tries to apply ``operations``, and does rollback in case
46
+ OneTick config is invalid.
47
+ """
48
+ if not operations:
49
+ return
50
+
51
+ for entities, func, roll_back_func in operations:
52
+ _check_entities(entities)
53
+ result = func(entities)
54
+ if not result:
55
+ entity_name = entities[0].__class__.__name__
56
+ entities = list(map(str, entities))
57
+ raise EntityOperationFailed(
58
+ f'Operation {func.__name__} for {entity_name}s {entities}'
59
+ f' for {cfg.__class__.__name__} "{cfg.path}" has failed'
60
+ )
61
+ try:
62
+ cfg.reload()
63
+ except Exception:
64
+ for entities, func, roll_back_func in reversed(operations):
65
+ roll_back_func(entities)
66
+ raise
67
+
68
+
69
+ class _FileHandler(ABC):
70
+ def __init__(self, file_h=None, clean_up=utils.default, copied=True, session_ref=None):
71
+ self._file = file_h
72
+ self._clean_up = clean_up
73
+ # flag to understand whether we work with externally passed files;
74
+ # it is set and affects logic, when copy=False
75
+ self._copied = copied
76
+ self._session_ref = session_ref
77
+
78
+ @property
79
+ def path(self):
80
+ return self._file.path
81
+
82
+ @property
83
+ def file(self):
84
+ return self._file
85
+
86
+ def copy(self, clean_up=utils.default, copy=True, session_ref=None):
87
+ return self.__class__(self.path, clean_up=clean_up, copy=copy, session_ref=session_ref) # pylint: disable=E1123
88
+
89
+ @abstractmethod
90
+ def cleanup(self):
91
+ pass
92
+
93
+ @staticmethod
94
+ def _db_in_dbs_case_insensitive(db_id: str, databases: List[str]):
95
+ for db_name in databases:
96
+ if db_id.upper() == db_name.upper():
97
+ return True
98
+ return False
99
+
100
+
101
+ class _CommonBuilder(ABC):
102
+ def __init__(self, src=None, clean_up=utils.default, copy=True, session_ref=None):
103
+ self.src = src
104
+ self.clean_up = clean_up
105
+ self.copy = copy
106
+ self.session_ref = session_ref
107
+
108
+ @abstractmethod
109
+ def build(self):
110
+ pass
111
+
112
+
113
+ class ACL(_FileHandler):
114
+ class User(str):
115
+ """
116
+ Subclass represents an ACL user
117
+ """
118
+
119
+ pass
120
+
121
+ def __init__(self, path=None, clean_up=utils.default, copy=True, session_ref=None):
122
+ """
123
+ Class representing OneTick database access list file.
124
+ ACL is the file that describes the list of the users
125
+ that are allowed to access the database and what permissions do they have.
126
+
127
+ Parameters
128
+ ----------
129
+ path: str
130
+ A path to custom acl file. Default is `None`, that means to generate a temporary acl file.
131
+ clean_up: bool
132
+ If `True`, then temporary acl file will be removed when ACL object will be destroyed. It is
133
+ helpful for debug purpose.
134
+
135
+ By default,
136
+ :py:attr:`otp.config.clean_up_tmp_files<onetick.py.configuration.Config.clean_up_tmp_files>` is used.
137
+ copy: bool
138
+ If `True`, then the passed custom acl file by the ``path`` parameter will be copied first before
139
+ usage. It might be used when you want to work with a custom acl file, but don't want to change
140
+ the original file; in that case a custom acl file will be copied into a temporary file and
141
+ every request for modification will be executed for that temporary file. Default is `True`.
142
+ """
143
+
144
+ copied = True
145
+
146
+ # TODO: implement this logic later
147
+ # if copy is None and path is not None:
148
+ # # if copy rule is not specified, but path is specified
149
+ # # then we set copy to True with safety goal, otherwise
150
+ # # we would might change a permanent file without user
151
+ # # acknowledgment
152
+ # copy = True
153
+ # raise Warning("You set the ACL file, but have not specify a copy rule. "
154
+ # "We copy it with safety goal, and it means you will work "
155
+ # "with copied file instead of passed. If you want to work "
156
+ # "with passed file directly, then you could set the 'copy' "
157
+ # "parameter to True.")
158
+
159
+ # if path is set, then copy file, we should not work directly
160
+ # with externally passed files
161
+ if copy:
162
+ if path:
163
+ file_h = utils.TmpFile(suffix=".acl", clean_up=clean_up)
164
+ shutil.copyfile(path, file_h.path)
165
+ else:
166
+ file_h = utils.tmp_acl(clean_up=clean_up)
167
+ else:
168
+ if path:
169
+ file_h = utils.PermanentFile(path)
170
+ copied = False
171
+ else:
172
+ file_h = utils.tmp_acl(clean_up=clean_up)
173
+
174
+ assert file_h is not None
175
+
176
+ super().__init__(file_h, clean_up=clean_up, copied=copied, session_ref=session_ref)
177
+
178
+ self._added_dbs = []
179
+
180
+ def cleanup(self):
181
+ self._remove_db(self._added_dbs)
182
+ self._added_dbs = []
183
+ self.reload()
184
+
185
+ def _apply_actions(self, actions, print_writer=False):
186
+ writer = PrintWriter() if print_writer else FileWriter(self.path)
187
+ flush = False if print_writer else True
188
+ return apply_actions(_acl.parse_acl, FileReader(self.path), writer, actions, flush=flush)
189
+
190
+ def _add_db(self, dbs):
191
+ actions = []
192
+ for db in dbs:
193
+ actions.append(Add(_acl.DB(id=db.id, read_access="true")))
194
+
195
+ permissions = {}
196
+ if db._write:
197
+ permissions["write_access"] = "true"
198
+ if hasattr(db, "_destroy_access") and db._destroy_access:
199
+ permissions["destroy_access"] = "true"
200
+ if db._minimum_start_date:
201
+ permissions['minimum_start_date'] = db._minimum_start_date.strftime('%Y%m%d')
202
+ if db._maximum_end_date:
203
+ permissions['maximum_end_date'] = db._maximum_end_date.strftime('%Y%m%d')
204
+
205
+ if permissions:
206
+ action = Add(_acl.Allow(role="Admin", **permissions))
207
+ action.add_where(_acl.DB, id=db.id)
208
+ actions.append(action)
209
+
210
+ return self._apply_actions(actions)
211
+
212
+ def _remove_db(self, dbs):
213
+ actions = []
214
+ for db in dbs:
215
+ action = Delete()
216
+ action.add_where(_acl.DB, id=db.id)
217
+ actions.append(action)
218
+
219
+ return self._apply_actions(actions)
220
+
221
+ def _add_user(self, users):
222
+ actions = []
223
+ for user in users:
224
+ action = Add(_acl.User(name=user))
225
+ action.add_where(_acl.Role, name="Admin")
226
+ actions.append(action)
227
+
228
+ return self._apply_actions(actions)
229
+
230
+ def _remove_user(self, users):
231
+ actions = []
232
+ for user in users:
233
+ action = Delete()
234
+ action.add_where(_acl.Role, name="Admin")
235
+ action.add_where(_acl.User, name=user)
236
+ actions.append(action)
237
+
238
+ return self._apply_actions(actions)
239
+
240
+ def add(self, *entities):
241
+ """
242
+ Add entities to the ACL and reload it.
243
+ If it fails, then tries to roll back to the original state.
244
+
245
+ Parameters
246
+ ----------
247
+ entities: DB or ACL.User
248
+
249
+ Raises
250
+ ------
251
+ TypeError, EntityOperationFailed
252
+ """
253
+ _check_entities(entities)
254
+
255
+ dbs = []
256
+ users = []
257
+ for entity in entities:
258
+ if isinstance(entity, _db.DB):
259
+ if self._db_in_dbs_case_insensitive(entity.id, self.databases):
260
+ if '//' not in entity.name:
261
+ warnings.warn(f"Database '{entity.id}' is already added to the ACL"
262
+ " and will not be rewritten with this command."
263
+ f" Notice that databases' names are case insensitive.",
264
+ stacklevel=2)
265
+ continue
266
+ dbs.append(entity)
267
+ elif isinstance(entity, ACL.User):
268
+ users.append(entity)
269
+ else:
270
+ raise TypeError(f'Entity of type "{type(entity)}" is not supported')
271
+
272
+ operations = []
273
+ if dbs:
274
+ operations.append((dbs, self._add_db, self._remove_db))
275
+ if users:
276
+ operations.append((users, self._add_user, self._remove_user))
277
+
278
+ _apply_to_entities(self, operations)
279
+
280
+ self._added_dbs.extend(dbs)
281
+
282
+ def remove(self, *entities):
283
+ """
284
+ Remove entities from the ACL and reload it.
285
+ If it fails, then tries to roll back to the original state.
286
+
287
+ Parameters
288
+ ----------
289
+ entities: DB or ACL.User
290
+
291
+ Raises
292
+ ------
293
+ ValueError, TypeError, EntityOperationFailed
294
+ """
295
+ _check_entities(entities)
296
+
297
+ dbs = []
298
+ users = []
299
+ for entity in entities:
300
+ if isinstance(entity, _db.DB):
301
+ if entity not in self._added_dbs:
302
+ raise ValueError(f'DB "{entity}" was not added')
303
+ dbs.append(entity)
304
+ elif isinstance(entity, ACL.User):
305
+ users.append(entity)
306
+ else:
307
+ raise TypeError(f'Entity of type "{type(entity)}" is not supported')
308
+
309
+ operations = []
310
+ if dbs:
311
+ operations.append((dbs, self._remove_db, self._add_db))
312
+ if users:
313
+ operations.append((users, self._remove_user, self._add_user))
314
+
315
+ _apply_to_entities(self, operations)
316
+
317
+ for db in dbs:
318
+ self._added_dbs.remove(db)
319
+
320
+ def reload(self, db=None):
321
+ if self._session_ref is not None:
322
+ return utils.reload_config(db, config_type='ACCESS_LIST')
323
+
324
+ def _read_dbs(self):
325
+ get_db = GetAll()
326
+ get_db.add_where(_acl.DB)
327
+ self._apply_actions([get_db], print_writer=True)
328
+ return list(map(lambda x: x.id, get_db.result))
329
+
330
+ def _dbs(self):
331
+ action = GetAll()
332
+ action.add_where(_acl.DB)
333
+ self._apply_actions([action], print_writer=True)
334
+ return list(map(lambda x: x.id, action.result))
335
+
336
+ def _users(self):
337
+ action = GetAll()
338
+ action.add_where(_acl.Role, name="Admin")
339
+ action.add_where(_acl.User)
340
+ self._apply_actions([action], print_writer=True)
341
+ return list(map(lambda x: x.name, action.result))
342
+
343
+ @property
344
+ def databases(self):
345
+ return self._dbs()
346
+
347
+ @property
348
+ def users(self):
349
+ return self._users()
350
+
351
+
352
+ class ACLBuilder(_CommonBuilder):
353
+ def build(self):
354
+ params = {"clean_up": self.clean_up, "copy": self.copy, "session_ref": self.session_ref}
355
+
356
+ if isinstance(self.src, str):
357
+ return ACL(self.src, **params)
358
+ elif isinstance(self.src, utils.File):
359
+ return ACL(self.src.path, **params)
360
+ elif isinstance(self.src, ACL):
361
+ return self.src.copy(**params)
362
+ elif self.src is None:
363
+ return ACL(**params)
364
+
365
+ raise ValueError(f'It is not allowed to build ACL from the object of type "{type(self.src)}"')
366
+
367
+
368
+ class Locator(_FileHandler):
369
+ def __init__(self, path=None, clean_up=utils.default, copy=True, empty=False, session_ref=None):
370
+ """
371
+ Class representing OneTick database locator.
372
+ Locator is the file that describes database name, location and other options.
373
+
374
+ Parameters
375
+ ----------
376
+ path: str
377
+ A path to custom locator file. Default is `None`, that means to generate a temporary locator.
378
+ clean_up: bool
379
+ If True, then temporary locator will be removed when Locator object will be destroyed. It is
380
+ helpful for debug purpose.
381
+
382
+ By default,
383
+ :py:attr:`otp.config.clean_up_tmp_files<onetick.py.configuration.Config.clean_up_tmp_files>` is used.
384
+ copy: bool
385
+ If `True`, then the passed custom locator by the ``path`` parameter will be copied firstly before
386
+ usage. It might be used when you want to work with a custom locator, but don't want to change
387
+ the original file; in that case a custom locator will be copied into a temporary locator and
388
+ every request for modification will be executed for that temporary locator. Default is `True`.
389
+ empty: bool
390
+ If `True`, then a temporary locator will have no databases, otherwise it will have default
391
+ otp.config.default_db and COMMON databases. Default is `False`.
392
+ """
393
+ copied = True
394
+
395
+ # if path is set, then copy file, we should not work directly
396
+ # with externally passed files
397
+ if copy:
398
+ if path:
399
+ file_h = utils.TmpFile(".locator", clean_up=clean_up)
400
+ shutil.copyfile(path, file_h)
401
+ else:
402
+ file_h = utils.tmp_locator(clean_up=clean_up, empty=empty)
403
+
404
+ else:
405
+ if path:
406
+ file_h = utils.PermanentFile(path)
407
+ copied = False
408
+ else:
409
+ file_h = utils.tmp_locator(clean_up=clean_up, empty=empty)
410
+
411
+ assert file_h is not None
412
+
413
+ super().__init__(file_h, clean_up=clean_up, copied=copied, session_ref=session_ref)
414
+
415
+ self._added_dbs = []
416
+ self._added_ts = []
417
+
418
+ def cleanup(self):
419
+ self._remove_db(self._added_dbs)
420
+ self._remove_ts(str(server) for server in self._added_ts)
421
+ self._added_dbs = []
422
+ self._added_ts = []
423
+ self.reload()
424
+
425
+ @property
426
+ def databases(self):
427
+ return self._dbs()
428
+
429
+ @property
430
+ def tick_servers(self):
431
+ return self._ts()
432
+
433
+ def reload(self, db_=None):
434
+ if self._session_ref is not None:
435
+ return utils.reload_config(db_, config_type='LOCATOR')
436
+
437
+ def _apply_actions(self, actions, print_writer=False):
438
+ writer = PrintWriter() if print_writer else FileWriter(self.path)
439
+ flush = False if print_writer else True
440
+ return apply_actions(_locator.parse_locator, FileReader(self.path), writer, actions, flush=flush)
441
+
442
+ def _dbs(self):
443
+ action = GetAll()
444
+ action.add_where(_locator.DB)
445
+ self._apply_actions([action], print_writer=True)
446
+ return list(map(lambda x: x.id, action.result))
447
+
448
+ def _ts(self):
449
+ get_ts = GetAll()
450
+ get_ts.add_where(_locator.TickServers)
451
+ get_ts.add_where(_locator.ServerLocation)
452
+ self._apply_actions([get_ts], print_writer=True)
453
+ return [location.location for location in get_ts.result]
454
+
455
+ def _add_db(self, dbs):
456
+ actions = []
457
+ for db in dbs:
458
+ actions.append(Add(_locator.DB(id=db.id, **db.properties)))
459
+ for location in db.locations:
460
+ action = Add(_locator.Location(**location))
461
+ action.add_where(_locator.DB, id=db.id)
462
+ actions.append(action)
463
+
464
+ for raw_db in db.raw_data:
465
+ common = {k: v for k, v in raw_db.items() if k not in {'id', 'locations'}}
466
+ action = Add(_locator.RawDB(id=raw_db['id'], **common))
467
+ action.add_where(_locator.DB, id=db.id)
468
+ actions.append(action)
469
+ for location in raw_db['locations']:
470
+ action = Add(_locator.Location(**location))
471
+ action.add_where(_locator.DB, id=db.id)
472
+ action.add_where(_locator.RawDB, id=raw_db['id'])
473
+ actions.append(action)
474
+
475
+ if db.feed:
476
+ options = {k: v for k, v in db.feed.items() if k != 'type'}
477
+ action = Add(_locator.Feed(type=db.feed['type']))
478
+ action.add_where(_locator.DB, id=db.id)
479
+ actions.append(action)
480
+ action = Add(_locator.FeedOptions(**options))
481
+ action.add_where(_locator.DB, id=db.id)
482
+ action.add_where(_locator.Feed, type=db.feed['type'])
483
+ actions.append(action)
484
+
485
+ return self._apply_actions(actions)
486
+
487
+ def _remove_db(self, dbs):
488
+ actions = []
489
+ for db in dbs:
490
+ action = Delete()
491
+ action.add_where(_locator.DB, id=db.id)
492
+ actions.append(action)
493
+
494
+ return self._apply_actions(actions)
495
+
496
+ def _add_ts(self, servers):
497
+ """
498
+ Add servers to locator file (without reloading)
499
+
500
+ Parameters
501
+ ----------
502
+ servers: RemoteTS
503
+ Servers to be added to locator.
504
+ """
505
+ actions = []
506
+ for server in servers:
507
+ if server.cep:
508
+ actions.append(Add(_locator.CEPServerLocation(location=str(server))))
509
+ else:
510
+ actions.append(Add(_locator.ServerLocation(location=str(server))))
511
+
512
+ return self._apply_actions(actions)
513
+
514
+ def _remove_ts(self, servers):
515
+ """
516
+ Remove servers from locator file (without reloading)
517
+
518
+ Parameters
519
+ ----------
520
+ servers: RemoteTS
521
+ Servers to remove from locator
522
+ """
523
+ actions = []
524
+ for server in servers:
525
+ action = Delete()
526
+ action.add_where(_locator.ServerLocation, location=str(server))
527
+ actions.append(action)
528
+
529
+ return self._apply_actions(actions)
530
+
531
+ def _add_locator(self, locators):
532
+ """
533
+ Add references to locators
534
+
535
+ Parameters
536
+ ----------
537
+ locators: Locator
538
+ """
539
+ actions = []
540
+ for locator in locators:
541
+ actions.append(Add(_locator.Include(path=locator.path)))
542
+
543
+ return self._apply_actions(actions)
544
+
545
+ def _remove_locator(self, locators):
546
+ """
547
+ Remove references for locators
548
+
549
+ Parameters
550
+ ----------
551
+ locators: Locator
552
+ """
553
+ actions = []
554
+ for locator in locators:
555
+ action = Delete()
556
+ action.add_where(_locator.Include, path=locator.path)
557
+ actions.append(action)
558
+
559
+ return self._apply_actions(actions)
560
+
561
+ def add(self, *entities):
562
+ """
563
+ Add entities to the locator and reload it.
564
+ If it fails, then tries to roll back to the original state.
565
+
566
+ Parameters
567
+ ----------
568
+ entities: DB, RemoteTS or Locator
569
+
570
+ Raises
571
+ ------
572
+ TypeError, EntityOperationFailed
573
+ """
574
+ _check_entities(entities)
575
+
576
+ dbs = []
577
+ servers = []
578
+ locators = []
579
+ for entity in entities:
580
+ if isinstance(entity, _db.db._DB):
581
+ if self._db_in_dbs_case_insensitive(entity.id, self.databases):
582
+ if '//' not in entity.name:
583
+ warnings.warn(f"Database '{entity.id}' is already added to the Locator"
584
+ " and will not be rewritten with this command."
585
+ f" Notice that databases' names are case insensitive.", stacklevel=2)
586
+ continue
587
+ dbs.append(entity)
588
+ elif isinstance(entity, _servers.RemoteTS):
589
+ servers.append(entity)
590
+ elif isinstance(entity, Locator):
591
+ locators.append(entity)
592
+ else:
593
+ raise TypeError(f'Entity of type "{type(entity)}" is not supported')
594
+
595
+ operations = []
596
+ if dbs:
597
+ operations.append((dbs, self._add_db, self._remove_db))
598
+ if servers:
599
+ operations.append((servers, self._add_ts, self._remove_ts))
600
+ if locators:
601
+ operations.append((locators, self._add_locator, self._remove_locator))
602
+
603
+ _apply_to_entities(self, operations)
604
+
605
+ self._added_dbs.extend(dbs)
606
+ self._added_ts.extend(servers)
607
+
608
+ def remove(self, *entities):
609
+ """
610
+ Remove entities from the locator and reload it.
611
+ If it fails, then tries to roll back to the original state.
612
+
613
+ Raises
614
+ ------
615
+ ValueError, TypeError, EntityOperationFailed
616
+ """
617
+ _check_entities(entities)
618
+
619
+ dbs = []
620
+ servers = []
621
+ locators = []
622
+ for entity in entities:
623
+ if isinstance(entity, _db.db._DB):
624
+ if entity not in self._added_dbs:
625
+ raise ValueError(f'DB "{entity}" was not added')
626
+ dbs.append(entity)
627
+ elif isinstance(entity, _servers.RemoteTS):
628
+ if entity not in self._added_ts:
629
+ raise ValueError(f'Tick server "{entity}" was not added')
630
+ servers.append(entity)
631
+ elif isinstance(entity, Locator):
632
+ locators.append(entity)
633
+ else:
634
+ raise TypeError(f'Entity of type "{type(entity)}" is not supported')
635
+
636
+ operations = []
637
+ if dbs:
638
+ operations.append((dbs, self._remove_db, self._add_db))
639
+ if servers:
640
+ operations.append((servers, self._remove_ts, self._add_ts))
641
+ if locators:
642
+ operations.append((locators, self._remove_locator, self._add_locator))
643
+
644
+ _apply_to_entities(self, operations)
645
+
646
+ for db in dbs:
647
+ self._added_dbs.remove(db)
648
+ for server in servers:
649
+ self._added_ts.remove(server)
650
+
651
+ def __contains__(self, item):
652
+ if str(item) in self.databases:
653
+ return True
654
+ return False
655
+
656
+
657
+ class LocatorBuilder(_CommonBuilder):
658
+ def build(self):
659
+ params = {"clean_up": self.clean_up, "copy": self.copy, "session_ref": self.session_ref}
660
+
661
+ if isinstance(self.src, str):
662
+ return Locator(self.src, **params)
663
+ elif isinstance(self.src, utils.File):
664
+ return Locator(self.src.path, **params)
665
+ elif isinstance(self.src, Locator):
666
+ return self.src.copy(**params)
667
+ elif isinstance(self.src, _servers.RemoteTS):
668
+ locator = Locator(empty=True, **params)
669
+ locator.add(self.src)
670
+ return locator
671
+ elif self.src is None:
672
+ return Locator(**params)
673
+
674
+ raise ValueError(f'It is not allowed to build Locator from the object of type "{type(self.src)}"')
675
+
676
+
677
+ class Config(_FileHandler):
678
+
679
+ _CONFIG_VARIABLES_PASSED_VIA_THEIR_OWN_PARAMETER = {
680
+ 'ACCESS_CONTROL_FILE': 'acl',
681
+ 'DB_LOCATOR.DEFAULT': 'locator',
682
+ 'OTQ_FILE_PATH': 'otq_path',
683
+ 'CSV_FILE_PATH': 'csv_path',
684
+ 'LICENSE_REPOSITORY_DIR': 'license',
685
+ 'ONE_TICK_LICENSE_FILE': 'license',
686
+ }
687
+
688
+ def __init__(
689
+ self,
690
+ config=None,
691
+ locator=None,
692
+ acl=None,
693
+ otq_path=None,
694
+ csv_path=None,
695
+ clean_up=utils.default,
696
+ copy=True,
697
+ session_ref=None,
698
+ license=None,
699
+ variables=None,
700
+ ):
701
+ """
702
+ Parameters
703
+ ----------
704
+ config: path or Config
705
+ Allows to specify a custom config. None is to use temporary generated config. Default is None.
706
+ locator: Locator
707
+ Allows to specify a custom locator file. None is to use temporary generated locator. Default is None.
708
+ acl: ACL
709
+ Allows to specify a custom acl file. None is to use temporary generated acl. Default is None.
710
+ otq_path: list of paths to lookup queries
711
+ OTQ_PATH parameter in the OneTick config file. Default is None, that is equal to the empty list.
712
+ csv_path: list of paths to lookup csv files
713
+ CSV_PATH parameter in the OneTick config file. Default is None, that is equal to the empty list.
714
+ clean_up: bool
715
+ If True, then temporary config file will be removed when the Config instance will be destroyed.
716
+ It is helpful for debug purpose.
717
+
718
+ By default,
719
+ :py:attr:`otp.config.clean_up_tmp_files<onetick.py.configuration.Config.clean_up_tmp_files>` is used.
720
+ copy: bool
721
+ If True, then the passed custom config file will be copied firstly before any usage with it.
722
+ It might be used when you want to work with a custom config file, but don't want to change to
723
+ change the original file; in that case a custom config will be copied into a temporary config
724
+ file and every request for modification will be executed for that temporary config. Default
725
+ is True.
726
+ license: instance from the onetick.py.license module
727
+ License to use. If it is not set, then onetick.py.license.Default is used.
728
+ variables: dict
729
+ Other values to pass to config.
730
+ """
731
+ if config and (locator or acl):
732
+ raise ValueError("It is not allowed to use 'config' parameter along with 'locator' or 'acl'")
733
+
734
+ # builders that construct locator and acl based on parameters
735
+ acl_builder = ACLBuilder(src=acl, clean_up=clean_up, copy=copy, session_ref=session_ref)
736
+ locator_builder = LocatorBuilder(src=locator, clean_up=clean_up, copy=copy, session_ref=session_ref)
737
+ config_copied = True
738
+
739
+ if config:
740
+ # copy passed file, we should not work with externally passed files
741
+ if copy and not os.getenv('OTP_WEBAPI_TEST_MODE'):
742
+ self._file = utils.TmpFile(".cfg", clean_up=clean_up)
743
+ config_path = config.path if isinstance(config, Config) else config
744
+
745
+ shutil.copyfile(config_path, self._file.path)
746
+ else:
747
+ self._file = utils.PermanentFile(config)
748
+ config_copied = False
749
+
750
+ if utils.is_param_in_config(self._file.path, "ACCESS_CONTROL_FILE"):
751
+ acl_builder.src = utils.get_config_param(self._file.path, "ACCESS_CONTROL_FILE")
752
+
753
+ if utils.is_param_in_config(self._file.path, "DB_LOCATOR.DEFAULT"):
754
+ locator_builder.src = utils.get_config_param(self._file.path, "DB_LOCATOR.DEFAULT")
755
+
756
+ else:
757
+ self._file = utils.tmp_config(clean_up=clean_up)
758
+
759
+ self._acl = acl_builder.build()
760
+ # it is used in onetick-py-test
761
+ os.environ["ONE_TICK_SESSION_ACL_PATH"] = self._acl.path
762
+ self._locator = locator_builder.build()
763
+ # it is used in onetick-py-test
764
+ os.environ["ONE_TICK_SESSION_LOCATOR_PATH"] = self._locator.path
765
+
766
+ super().__init__(self._file, clean_up=clean_up, copied=config_copied, session_ref=session_ref)
767
+
768
+ # Here we can start to modify files - they are either copied or generated
769
+ # ------------------------------------------------------------------------
770
+
771
+ utils.modify_config_param(self.path, "ACCESS_CONTROL_FILE", self._acl.path, throw_on_missing=False)
772
+ utils.modify_config_param(self.path, "DB_LOCATOR.DEFAULT", self._locator.path, throw_on_missing=False)
773
+
774
+ if otq_path:
775
+ otq_path = map(str, otq_path)
776
+ utils.modify_config_param(self.path, "OTQ_FILE_PATH", ",".join(otq_path), throw_on_missing=False)
777
+ if csv_path:
778
+ csv_path = map(str, csv_path)
779
+ utils.modify_config_param(self.path, "CSV_FILE_PATH", ",".join(csv_path), throw_on_missing=False)
780
+ variables = variables or {}
781
+ for parameter_name, parameter_value in variables.items():
782
+ if parameter_name in self._CONFIG_VARIABLES_PASSED_VIA_THEIR_OWN_PARAMETER:
783
+ raise ValueError(f'Variable {parameter_name} should be set via '
784
+ f'{self._CONFIG_VARIABLES_PASSED_VIA_THEIR_OWN_PARAMETER[parameter_name]} parameter')
785
+ if isinstance(parameter_value, list):
786
+ parameter_value = ",".join(map(str, parameter_value))
787
+ utils.modify_config_param(self.path, parameter_name, parameter_value, throw_on_missing=False)
788
+
789
+ # set license
790
+ # ---------------------------
791
+ custom_license = utils.is_param_in_config(self.path, "LICENSE_REPOSITORY_DIR")
792
+ custom_license &= utils.is_param_in_config(self.path, "ONE_TICK_LICENSE_FILE")
793
+ if license:
794
+ self._license = license
795
+ else:
796
+ if custom_license:
797
+ lic_file = utils.get_config_param(self.path, "ONE_TICK_LICENSE_FILE")
798
+ lic_dir = utils.get_config_param(self.path, "LICENSE_REPOSITORY_DIR")
799
+ self._license = _license.Custom(lic_file, lic_dir)
800
+ else:
801
+ if isinstance(locator, _servers.RemoteTS):
802
+ self._license = _license.Remote()
803
+ else:
804
+ self._license = _license.Default()
805
+
806
+ if not custom_license: # no need to set already defined custom values
807
+ if self._license.dir:
808
+ utils.modify_config_param(self.path,
809
+ "LICENSE_REPOSITORY_DIR",
810
+ self._license.dir,
811
+ throw_on_missing=False)
812
+ if self._license.file:
813
+ utils.modify_config_param(self.path,
814
+ "ONE_TICK_LICENSE_FILE",
815
+ self._license.file,
816
+ throw_on_missing=False)
817
+
818
+ @property
819
+ def acl(self):
820
+ return self._acl
821
+
822
+ @property
823
+ def locator(self):
824
+ return self._locator
825
+
826
+ @property
827
+ def license(self):
828
+ return self._license
829
+
830
+ def copy(self, clean_up=utils.default, copy=True, session_ref=None):
831
+ """ overridden version of copy """
832
+ return self.__class__(self.path, clean_up=clean_up, copy=copy, session_ref=session_ref, license=self._license)
833
+
834
+ @staticmethod
835
+ def build(obj=None, clean_up=utils.default, copy=True, session_ref=None):
836
+ params = {"clean_up": clean_up, "copy": copy, "session_ref": session_ref}
837
+
838
+ if isinstance(obj, str):
839
+ return Config(obj, **params)
840
+ elif isinstance(obj, utils.File):
841
+ return Config(obj.path, **params)
842
+ elif isinstance(obj, Config):
843
+ return obj.copy(**params)
844
+ elif obj is None:
845
+ return Config(**params)
846
+
847
+ raise ValueError(f'It is not allowed to build Config from the object of type "{type(obj)}"')
848
+
849
+ def cleanup(self):
850
+ # no logic to clean up content
851
+
852
+ self._acl.cleanup()
853
+ self._locator.cleanup()
854
+
855
+ @property
856
+ def otq_path(self):
857
+ try:
858
+ return utils.get_config_param(self.path, "OTQ_FILE_PATH")
859
+ except AttributeError:
860
+ return None
861
+
862
+
863
+ class PerformanceMetricsParser:
864
+ metrics_fields = {
865
+ "user_time": float,
866
+ "system_time": float,
867
+ "elapsed_time": float,
868
+ "virtual_memory": int,
869
+ "virtual_memory_peak": int,
870
+ "working_set": int,
871
+ "working_set_peak": int,
872
+ "disk_read": int,
873
+ "disk_write": int,
874
+ }
875
+
876
+ metrics_units = {
877
+ "user_time": "s",
878
+ "system_time": "s",
879
+ "elapsed_time": "s",
880
+ "virtual_memory": "bytes",
881
+ "virtual_memory_peak": "bytes",
882
+ "working_set": "bytes",
883
+ "working_set_peak": "bytes",
884
+ "disk_read": "bytes",
885
+ "disk_write": "bytes",
886
+ }
887
+
888
+ def __init__(self):
889
+ self._metrics = {}
890
+
891
+ def parse(self, log_file: str):
892
+ with open(log_file, "r") as log_file_io:
893
+ for log_line in log_file_io:
894
+ result = re.search(r"Performance Metrics FINAL ([\w\s]+) \((\w+)\): (.+)$", log_line.strip())
895
+ if not result:
896
+ continue
897
+
898
+ metric_name, metric_units, metric_value = result.groups()
899
+ metric_key = "_".join(metric_name.lower().split())
900
+
901
+ if metric_key not in self.metrics_fields:
902
+ raise ValueError(
903
+ f"Unexpected performance metric `{metric_key}` was found in the log: \"{log_line.strip()}\""
904
+ )
905
+
906
+ if metric_units != self.metrics_units[metric_key]:
907
+ raise ValueError(
908
+ f"Unexpected units for metric `{metric_key}`: "
909
+ f"expected `{self.metrics_units[metric_key]}`, got `{metric_units}`."
910
+ )
911
+
912
+ if metric_key in self._metrics:
913
+ raise KeyError(f"Metric `{metric_key}` was already saved, but another entry was found in the log.")
914
+
915
+ try:
916
+ metric_value = self.metrics_fields[metric_key](metric_value)
917
+ except Exception as exc:
918
+ raise ValueError(
919
+ f"Failed to parse and convert metric {metric_key} "
920
+ f"from `str` to `{self.metrics_fields[metric_key].__name__}`: \"{metric_value}\""
921
+ ) from exc
922
+
923
+ self._metrics[metric_key] = {
924
+ "name": metric_name,
925
+ "value": metric_value,
926
+ "units": self.metrics_units[metric_key],
927
+ }
928
+
929
+ not_found_keys = set(self.metrics_fields.keys()).difference(self._metrics.keys())
930
+ if not_found_keys:
931
+ raise RuntimeError("Expected performance metrics not found in the log: " + ", ".join(not_found_keys))
932
+
933
+ @property
934
+ def metrics(self):
935
+ return self._metrics.copy()
936
+
937
+
938
+ class Session:
939
+ """
940
+ A class for setting up working OneTick session. It keeps configuration files during
941
+ the session and allows to manage them. When instance is out of scope, then instance
942
+ cleans up config files and configuration.
943
+ You can leave the scope manually with method :py:meth:`close`.
944
+ Also, session is closed automatically if this object is used as a context manager.
945
+
946
+ .. note::
947
+ It is allowed to have only one alive session instance in the process.
948
+
949
+ If you don't use Session's instance, then ``ONE_TICK_CONFIG`` environment variable
950
+ should be set to be able to work with OneTick.
951
+
952
+ If config file is not set then temporary is generated.
953
+ Config includes locator and acl file, and if they are not set, then they are generated.
954
+
955
+ Parameters
956
+ ----------
957
+ config : str, :py:class:`onetick.py.session.Config`, optional
958
+ Path to an existing OneTick config file; if it is not set, then config will be generated.
959
+ If config is not set, then temporary config is generated. Default is None.
960
+ clean_up : bool, optional
961
+ A flag to control cleaning up process: if it is True then all temporary generated files will
962
+ be automatically removed. It is helpful for debugging. The flag affects only generated files, but
963
+ does not externally passed.
964
+
965
+ By default,
966
+ :py:attr:`otp.config.clean_up_tmp_files<onetick.py.configuration.Config.clean_up_tmp_files>` is used.
967
+ copy : bool, optional
968
+ A flag to control file copy process: if it is True then all externally passed files will be
969
+ copied before usage, otherwise all modifications during an existing session happen directly with
970
+ passed config files. NOTE: we suggest to set this flag only when you fully understand it's effect.
971
+ Default is True.
972
+ override_env : bool, optional
973
+ If flag is True, then unconditionally ``ONE_TICK_CONFIG`` environment variable will be overridden
974
+ with a config that belongs to a Session. Otherwise ``ONE_TICK_CONFIG``
975
+ will be defined in the scope of session only when it is not defined externally.
976
+ For example, it is helpful when you test ascii_loader that uses 'ONE_TICK_CONFIG' only.
977
+
978
+ Default is False ( default is False, because overriding external environment variable
979
+ might be not obvious and desirable )
980
+ redirect_logs: bool, optional
981
+ If flag is True, then OneTick logs will be redirected into a temporary log file. Otherwise
982
+ logs will be mixed with output. Default is True.
983
+ gather_performance_metrics: bool, optional
984
+ If flag is True, then enables performance metrics gathering, by setting ``DUMP_PERF_METRICS`` config parameter.
985
+ Sets ``redirect_logs`` flag to ``True``.
986
+
987
+ Metrics are available after closing a session via ``session.performance_metrics`` property.
988
+
989
+ .. warning::
990
+ Due to current limitations, metrics are cumulative. So if you run multiple queries in the same process
991
+ (even with different session objects), you'll get metrics for the whole process since it's start
992
+ till end of a session with ``gather_performance_metrics=True``.
993
+
994
+ To avoid this, you can create a session in a separate process, either by using Python's ``multiprocessing``
995
+ or by moving required code to a separate Python script and running it in a new process.
996
+
997
+ .. note::
998
+ Metrics are gathered for all operations in the session between its creation and closing.
999
+
1000
+ Examples
1001
+ --------
1002
+
1003
+ If session is defined with environment, OneTick can be used right away:
1004
+
1005
+ >>> 'ONE_TICK_CONFIG' in os.environ
1006
+ True
1007
+ >>> list(otp.databases()) # doctest: +ELLIPSIS
1008
+ ['COMMON', 'DEMO_L1', ..., 'SOME_DB', 'SOME_DB_2'...
1009
+ >>> data = otp.DataSource('SOME_DB', symbol='S1', tick_type='TT')
1010
+ >>> otp.run(data)
1011
+ Time X
1012
+ 0 2003-12-01 00:00:00.000 1
1013
+ 1 2003-12-01 00:00:00.001 2
1014
+ 2 2003-12-01 00:00:00.002 3
1015
+
1016
+ Collecting performance metrics with ``gather_performance_metrics`` parameter:
1017
+
1018
+ >>> with otp.Session(gather_performance_metrics=True) as session: # doctest: +SKIP
1019
+ >>> data_a = otp.DataSource('DB_A', symbol='S1', tick_type='TT') # doctest: +SKIP
1020
+ >>> data_b = otp.DataSource('DB_B', symbol='S1', tick_type='TT') # doctest: +SKIP
1021
+ >>> _ = otp.run(otp.merge([data_a, data_b])) # doctest: +SKIP
1022
+ >>>
1023
+ >>> session.performance_metrics # doctest: +SKIP
1024
+ {
1025
+ 'user_time': {'name': 'User Time', 'value': 3.39063, 'units': 's'},
1026
+ 'system_time': {'name': 'System Time', 'value': 1.07813, 'units': 's'},
1027
+ 'elapsed_time': {'name': 'Elapsed Time', 'value': 6.78816, 'units': 's'},
1028
+ 'virtual_memory': {'name': 'Virtual Memory', 'value': 6261944320, 'units': 'bytes'},
1029
+ 'virtual_memory_peak': {'name': 'Virtual Memory Peak', 'value': 6271926272, 'units': 'bytes'},
1030
+ 'working_set': {'name': 'Working Set', 'value': 228110336, 'units': 'bytes'},
1031
+ 'working_set_peak': {'name': 'Working Set Peak', 'value': 228126720, 'units': 'bytes'},
1032
+ 'disk_read': {'name': 'Disk Read', 'value': 32289438, 'units': 'bytes'},
1033
+ 'disk_write': {'name': 'Disk Write', 'value': 172906, 'units': 'bytes'}
1034
+ }
1035
+ """
1036
+ # TODO: create article for Session in Guides or Concepts
1037
+
1038
+ _instance = None
1039
+
1040
+ def __init__(
1041
+ self, config=None, clean_up=utils.default, copy=True, override_env=False, redirect_logs=True,
1042
+ gather_performance_metrics=False,
1043
+ ):
1044
+ self._construct(config, clean_up, copy, override_env, redirect_logs, gather_performance_metrics)
1045
+
1046
+ def _construct(
1047
+ self, config=None, clean_up=utils.default, copy=True, override_env=False, redirect_logs=True,
1048
+ gather_performance_metrics=False,
1049
+ ):
1050
+
1051
+ if Session._instance:
1052
+ raise MultipleSessionsException(
1053
+ "It is forbidden to use multiple active sessions simultaniously in one process"
1054
+ )
1055
+
1056
+ def onetick_cfg_rollback(var):
1057
+ """
1058
+ function to rollback ONE_TICK_CONFIG state
1059
+ """
1060
+
1061
+ def _impl():
1062
+ if var is None:
1063
+ if "ONE_TICK_CONFIG" in os.environ:
1064
+ del os.environ["ONE_TICK_CONFIG"]
1065
+ else:
1066
+ os.environ["ONE_TICK_CONFIG"] = var
1067
+
1068
+ return _impl
1069
+
1070
+ self._lib = None
1071
+ self._env_rollback = onetick_cfg_rollback(os.environ.get("ONE_TICK_CONFIG", None))
1072
+ self._override_env = override_env
1073
+
1074
+ self._config = Config.build(config, clean_up=clean_up, copy=copy, session_ref=self)
1075
+ # it is used in onetick-py-test
1076
+ os.environ["ONE_TICK_SESSION_CFG_PATH"] = self._config.path
1077
+
1078
+ try:
1079
+ if "ONE_TICK_CONFIG" not in os.environ:
1080
+ os.environ["ONE_TICK_CONFIG"] = self._config.path
1081
+ else:
1082
+ if override_env:
1083
+ os.environ["ONE_TICK_CONFIG"] = self._config.path
1084
+ else:
1085
+ warnings.warn(
1086
+ UserWarning(
1087
+ "ONE_TICK_CONFIG env variable has been set before a session, "
1088
+ "and in the session scope it is not related to the session config. "
1089
+ "If you want to make ONE_TICK_CONFIG env variable be consistent "
1090
+ "with the session, then look at the override_env flag "
1091
+ "for the Session constructor"
1092
+ )
1093
+ )
1094
+
1095
+ otli.OneTickLib().cleanup()
1096
+
1097
+ self._performance_metrics_parser = None
1098
+ if gather_performance_metrics:
1099
+ self._performance_metrics_parser = PerformanceMetricsParser()
1100
+ utils.modify_config_param(
1101
+ self._config.path, "DUMP_PERF_METRICS", "ON", throw_on_missing=False,
1102
+ )
1103
+ redirect_logs = True
1104
+
1105
+ self._log_file = log_file = None
1106
+ if redirect_logs:
1107
+ self._log_file = utils.TmpFile(suffix=".onetick.log", clean_up=clean_up)
1108
+ log_file = self._log_file.path
1109
+
1110
+ self._lib = otli.OneTickLib(self._config.path, log_file=log_file)
1111
+ except Exception:
1112
+ self._env_rollback()
1113
+ # TODO: rollback, but need to wait BDS-91
1114
+ raise
1115
+
1116
+ Session._instance = self
1117
+
1118
+ # force reload cfg/locator/acl, because it is not reloaded after each time session if re-created
1119
+ if os.getenv('OTP_WEBAPI_TEST_MODE'):
1120
+ utils.reload_config(None, config_type='MAIN_CONFIG')
1121
+ self.locator.reload()
1122
+ self.acl.reload()
1123
+
1124
+ self._ts_dbs = {}
1125
+
1126
+ def use(self, *items):
1127
+ """
1128
+ Makes DB or TS available inside the session.
1129
+
1130
+ Parameters
1131
+ ----------
1132
+ items : :py:class:`~onetick.py.DB` or :py:class:`~onetick.py.servers.RemoteTS` objects
1133
+ Items to be added to session.
1134
+
1135
+ Examples
1136
+ --------
1137
+
1138
+ (note that ``session`` is created before this example)
1139
+
1140
+ >>> list(otp.databases()) # doctest: +ELLIPSIS
1141
+ ['COMMON', 'DEMO_L1', ...]
1142
+ >>> new_db = otp.DB('ZZZZ')
1143
+ >>> session.use(new_db)
1144
+ >>> list(otp.databases()) # doctest: +ELLIPSIS
1145
+ ['COMMON', 'DEMO_L1', ..., 'ZZZZ']
1146
+ """
1147
+ self.locator.add(*items)
1148
+ dbs = []
1149
+ for item in items:
1150
+ if isinstance(item, _db.db._DB):
1151
+ dbs.append(item)
1152
+ try:
1153
+ if dbs:
1154
+ self.acl.add(*dbs)
1155
+ except Exception:
1156
+ self.locator.remove(*items)
1157
+ raise
1158
+
1159
+ def use_stub(self, stub_name):
1160
+ """
1161
+ Adds stub-DB into the session.
1162
+ The shortcut for ``.use(otp.DB(stub_name))``
1163
+
1164
+ Parameters
1165
+ ----------
1166
+ stub_name : str
1167
+ name of the stub
1168
+ """
1169
+ return self.use(_db.DB(stub_name))
1170
+
1171
+ def __enter__(self):
1172
+ return self
1173
+
1174
+ def __exit__(self, exc_type, exc_val, exc_tb):
1175
+ if exc_type:
1176
+ # session will be closed due to exception, no need to parse performance metrics
1177
+ self._performance_metrics_parser = None
1178
+
1179
+ self.close()
1180
+
1181
+ @staticmethod
1182
+ def _available_dbs():
1183
+ return _databases()
1184
+
1185
+ def _get_ts_dbs(self):
1186
+ locator_dbs = self.locator.databases
1187
+
1188
+ all_dbs = self._available_dbs()
1189
+
1190
+ for db_name in all_dbs:
1191
+ if db_name not in locator_dbs and db_name not in self._ts_dbs:
1192
+ self._ts_dbs[db_name] = _db.db._DB(db_name)
1193
+
1194
+ def close(self):
1195
+ """
1196
+ Close session
1197
+ """
1198
+ if Session._instance == self:
1199
+ try:
1200
+ if self._config:
1201
+ del self._config
1202
+ self._config = None
1203
+
1204
+ finally:
1205
+ if self._lib:
1206
+ self._lib.cleanup()
1207
+ self._lib = None
1208
+
1209
+ self._env_rollback()
1210
+
1211
+ Session._instance = None
1212
+
1213
+ if self._performance_metrics_parser:
1214
+ self._performance_metrics_parser.parse(self._log_file)
1215
+
1216
+ def __del__(self):
1217
+ self.close()
1218
+
1219
+ @property
1220
+ def config(self):
1221
+ """
1222
+ A reference to the underlying Config object that represents OneTick config file.
1223
+
1224
+ Returns
1225
+ -------
1226
+ :py:class:`onetick.py.session.Config`
1227
+ """
1228
+ return self._config
1229
+
1230
+ @config.setter
1231
+ def config(self, cfg):
1232
+ self.close()
1233
+ self._construct(cfg, override_env=self._override_env)
1234
+
1235
+ @property
1236
+ def acl(self):
1237
+ """
1238
+ A reference to the underlying ACL object that represents OneTick access control list file.
1239
+
1240
+ Returns
1241
+ -------
1242
+ :py:class:`onetick.py.session.ACL`
1243
+ """
1244
+ return self._config.acl
1245
+
1246
+ @property
1247
+ def locator(self):
1248
+ """
1249
+ A reference to the underlying Locator that represents OneTick locator file.
1250
+
1251
+ Returns
1252
+ -------
1253
+ :py:class:`onetick.py.session.Locator`
1254
+ """
1255
+ return self._config.locator
1256
+
1257
+ @property
1258
+ def license(self):
1259
+ return self._config.license
1260
+
1261
+ @property
1262
+ def ts_databases(self):
1263
+ self._get_ts_dbs()
1264
+ return self._ts_dbs
1265
+
1266
+ @property
1267
+ def databases(self):
1268
+ return self._available_dbs()
1269
+
1270
+ @property
1271
+ def performance_metrics(self):
1272
+ if self._instance:
1273
+ raise RuntimeError("Trying to get performance metrics before closing the session.")
1274
+
1275
+ if not self._performance_metrics_parser:
1276
+ raise RuntimeError(
1277
+ "Trying to get performance metrics, "
1278
+ "however session was created without `gather_performance_metrics=True`."
1279
+ )
1280
+
1281
+ return self._performance_metrics_parser.metrics
1282
+
1283
+
1284
+ class TestSession(Session):
1285
+ def __init__(self, *args, **kwargs):
1286
+ """
1287
+ This class does the same as :py:class:`onetick.py.session.Session`,
1288
+ but also defines default required :py:attr:`otp.config <onetick.py.configuration.Config>` values.
1289
+
1290
+ Using this session object is the equivalent of defining these configuration values:
1291
+
1292
+ ::
1293
+
1294
+ otp.config['tz'] = 'EST5EDT'
1295
+ otp.config['default_db'] = 'DEMO_L1'
1296
+ otp.config['default_symbol'] = 'AAPL'
1297
+ otp.config['default_start_time'] = datetime(2003, 12, 1, 0, 0, 0)
1298
+ otp.config['default_end_time'] = datetime(2003, 12, 4, 0, 0, 0)
1299
+
1300
+ ``DEMO_L1`` is the database defined in the default locator generated by onetick.py
1301
+ and it is also a name of the database commonly used in the OneTick ecosystem, academy courses, etc.
1302
+ """
1303
+ config['tz'] = 'EST5EDT'
1304
+ config['default_db'] = 'DEMO_L1'
1305
+ config['default_symbol'] = 'AAPL'
1306
+ config['default_start_time'] = datetime(2003, 12, 1, 0, 0, 0)
1307
+ config['default_end_time'] = datetime(2003, 12, 4, 0, 0, 0)
1308
+ super().__init__(*args, **kwargs)
1309
+
1310
+
1311
+ class HTTPSession:
1312
+ param_list = ['http_address', 'http_username', 'http_password', 'access_token', 'http_proxy', 'https_proxy']
1313
+
1314
+ def __init__(self,
1315
+ http_address,
1316
+ http_username=None,
1317
+ http_password=None,
1318
+ access_token=None,
1319
+ http_proxy=None,
1320
+ https_proxy=None):
1321
+ """
1322
+ This class must be used only for WebAPI connection,
1323
+ to set HTTP connection parameters.
1324
+ """
1325
+ import onetick.py as otp # noqa
1326
+ self._restore_config = {}
1327
+ for param in self.param_list:
1328
+ if locals()[param]:
1329
+ self._restore_config[param] = otp.config.get(param)
1330
+ otp.config.__setattr__(param, locals()[param])
1331
+
1332
+ def close(self):
1333
+ # restore config
1334
+ import onetick.py as otp # noqa
1335
+ for param, value in self._restore_config.items():
1336
+ otp.config.__setattr__(param, value)
1337
+
1338
+ def __enter__(self):
1339
+ return self
1340
+
1341
+ def __exit__(self, exc_type, exc_val, exc_tb):
1342
+ self.close()