Best Python code snippet using avocado_python
gnrobjectregister.py
Source:gnrobjectregister.py
1#-*- coding: UTF-8 -*-2#--------------------------------------------------------------------------3# package : GenroPy web - see LICENSE for details4# module gnrwebcore : core module for genropy web framework5# Copyright (c) : 2004 - 2007 Softwell sas - Milano 6# Written by : Giovanni Porcari, Michele Bertoldi7# Saverio Porcari, Francesco Porcari , Francesco Cavazzana8#--------------------------------------------------------------------------9#This library is free software; you can redistribute it and/or10#modify it under the terms of the GNU Lesser General Public11#License as published by the Free Software Foundation; either12#version 2.1 of the License, or (at your option) any later version.13#This library is distributed in the hope that it will be useful,14#but WITHOUT ANY WARRANTY; without even the implied warranty of15#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU16#Lesser General Public License for more details.17#You should have received a copy of the GNU Lesser General Public18#License along with this library; if not, write to the Free Software19#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA20# 21from __future__ import print_function22from builtins import str23from past.builtins import basestring24#from builtins import object25from datetime import datetime26from gnr.core.gnrbag import Bag, BagResolver27from gnr.web.gnrwebpage import ClientDataChange28BAG_INSTANCE = Bag()29import logging30import re31logger = logging.getLogger(__name__)32import time33def lock_item(func):34 def decore(self, obj, *args, **kwargs):35 key = obj if isinstance(obj, basestring) else obj['register_item_id']36 with self.locked(self.item_key(key), caller=func.__name__):37 result = func(self, obj, *args, **kwargs)38 return result39 return decore40def lock_page(func):41 def decore(self, key, *args, **kwargs):42 register = self.p_register43 with register.locked(register.item_key(key), caller=func.__name__):44 result = func(self, key, *args, **kwargs)45 return result46 return decore47def lock_connection(func):48 def decore(self, key, *args, **kwargs):49 register = self.c_register50 with register.locked(register.item_key(key), caller=func.__name__):51 result = func(self, key, *args, **kwargs)52 return result53 return decore54def lock_user(func):55 def decore(self, key, *args, **kwargs):56 register = self.u_register57 with register.locked(register.item_key(key), caller=func.__name__):58 result = func(self, key, *args, **kwargs)59 return result60 return decore61def lock_index(func):62 def decore(self, *args, **kwargs):63 index_key = self._get_index_key(index_name=kwargs.get('index_name'))64 with self.locked(index_key):65 result = func(self, *args, **kwargs)66 return result67 return decore68class ExpiredItemException(Exception):69 pass70class ServerStore(object):71 def __init__(self, parent, register_item_id=None, triggered=True):72 self.parent = parent73 self.register_item_id = register_item_id74 self.triggered = triggered75 self._register_item = '*'76 def __enter__(self):77 self.parent.lock_register_item(self.register_item_id)78 return self79 def __exit__(self, type, value, tb):80 self.parent.unlock_register_item(self.register_item_id)81 if tb:82 return83 if not self.register_item:84 return85 data = self.data86 if data is not None:87 data.unsubscribe('datachanges', any=True)88 self.parent.write(self.register_item)89 def reset_datachanges(self):90 if self.register_item:91 self.register_item['datachanges'] = list()92 self.register_item['datachanges_idx'] = 093 def set_datachange(self, path, value=None, attributes=None, fired=False, reason=None, replace=False, delete=False):94 if not self.register_item:95 return96 datachanges = self.datachanges97 self.register_item['datachanges_idx'] = self.register_item.get('datachanges_idx', 0)98 self.register_item['datachanges_idx'] += 199 datachange = ClientDataChange(path, value, attributes=attributes, fired=fired,100 reason=reason, change_idx=self.register_item['datachanges_idx'],101 delete=delete)102 if replace and datachange in datachanges:103 datachanges.pop(datachanges.index(datachange))104 datachanges.append(datachange)105 def drop_datachanges(self, path):106 self.datachanges[:] = [dc for dc in self.datachanges if not dc.path.startswith(path)]107 def subscribe_path(self, path):108 if self.register_item:109 self.subscribed_paths.add(path)110 def _on_data_trigger(self, node=None, ind=None, evt=None, pathlist=None, **kwargs):111 if evt == 'ins':112 pathlist.append(node.label)113 path = '.'.join(pathlist)114 for subscribed in self.subscribed_paths:115 if path.startswith(subscribed):116 self.datachanges.append(117 ClientDataChange(path=path, value=node.value, reason='serverChange', attributes=node.attr))118 break119 def __getattr__(self, fname):120 if hasattr(BAG_INSTANCE, fname):121 return getattr(self.data, fname)122 else:123 raise AttributeError("register_item has no attribute '%s'" % fname)124 @property125 def data(self):126 """TODO"""127 if self.register_item:128 return self.register_item['data']129 else:130 return Bag()131 @property132 def register_item(self):133 """TODO"""134 if self._register_item != '*':135 return self._register_item136 self._register_item = register_item = self.parent.read(self.register_item_id)137 if not register_item:138 return139 data = register_item.get('data')140 if data is None:141 data = Bag()142 register_item['data'] = data143 register_item['datachanges'] = list()144 register_item['datachanges_idx'] = 0145 register_item['subscribed_paths'] = set()146 if self.triggered and register_item['subscribed_paths']:147 data.subscribe('datachanges', any=self._on_data_trigger)148 return register_item149 @property150 def datachanges(self):151 """TODO"""152 datachanges = []153 if self.register_item:154 datachanges = self.register_item.setdefault('datachanges', [])155 return datachanges156 @property157 def subscribed_paths(self):158 """TODO"""159 if self.register_item:160 return self.register_item['subscribed_paths']161class SiteRegister(object):162 """TODO"""163 prefix = 'SITE'164 def __init__(self, site):165 self.site = site166 self.sd = self.site.shared_data167 self.p_register = PageRegister(site)168 self.c_register = ConnectionRegister(site)169 self.u_register = UserRegister(site)170 self.cleanup_key = '%s_CLEANUP_LAST_TS'%self.prefix171 @lock_connection172 def new_connection(self, connection_id, connection):173 assert not self.c_register.exists(174 connection_id), 'SITEREGISTER ERROR: connection_id %s already registered' % connection_id175 connection_item = self.c_register.create(connection_id, connection)176 self.attach_connections_to_user(connection_item['user'], connection_item)177 self.c_register.write(connection_item)178 @lock_user179 def attach_connections_to_user(self, user, connection_items):180 if not isinstance(connection_items, list):181 connection_items = [connection_items]182 user_item = self.u_register.read(user)183 if not user_item:184 user_item = self.u_register.create(user, connection_items[0])185 for connection_item in connection_items:186 connection_id = connection_item['register_item_id']187 user_item['connections'][connection_id] = connection_item['connection_name']188 self.u_register.write(user_item)189 @lock_user190 def pop_connections_from_user(self, user, connection_items, delete_if_empty=True):191 user_item = self.u_register.read(user)192 result = {}193 if not user_item:194 return195 if connection_items == '*':196 result = user_item['connections']197 user_item['connections'] = {}198 else:199 if not isinstance(connection_items, list):200 connection_items = [connection_items]201 for connection_item in connection_items:202 connection_id = connection_item['register_item_id']203 result[connection_id] = user_item['connections'].pop(connection_id, None)204 if not user_item['connections'] and delete_if_empty:205 self.u_register.pop(user)206 else:207 self.u_register.write(user_item)208 return result209 def change_connection_user(self, connection_id, user=None, user_tags=None, user_id=None, user_name=None,210 avatar_extra=None):211 connection_item = self.c_register.read(connection_id)212 self.pop_connections_from_user(connection_item['user'], connection_item, delete_if_empty=True)213 connection_item['user'] = user214 connection_item['user_tags'] = user_tags215 connection_item['user_name'] = user_name216 connection_item['user_id'] = user_id217 connection_item['avatar_extra'] = avatar_extra218 self.attach_connections_to_user(connection_item['user'], connection_item)219 self.c_register.write(connection_item)220 if connection_item['pages']:221 for page_id in connection_item['pages']:222 page_item = self.p_register.read(page_id)223 page_item['user'] = user224 self.p_register.write(page_item)225 @lock_connection226 def attach_pages_to_connection(self, connection_id, page_items):227 if not isinstance(page_items, list):228 page_items = [page_items]229 connection_item = self.c_register.read(connection_id)230 for page_item in page_items:231 page_id = page_item['register_item_id']232 connection_item['pages'][page_id] = page_item['pagename']233 self.c_register.write(connection_item)234 @lock_connection235 def pop_pages_from_connection(self, connection_id, page_items, delete_if_empty=False):236 connection_item = self.c_register.read(connection_id)237 result = {}238 if not connection_item:239 return240 if page_items == '*':241 result = connection_item['pages']242 connection_item['pages'] = {}243 else:244 if not isinstance(page_items, list):245 page_items = [page_items]246 for page_item in page_items:247 page_id = page_item['register_item_id']248 result[page_id] = connection_item['pages'].pop(page_id, None)249 if not connection_item['pages'] and (delete_if_empty or self.c_register.is_guest(connection_item)):250 self.drop_connection(connection_id)251 else:252 self.c_register.write(connection_item)253 return result254 @lock_page255 def new_page(self, page_id, page, data=None):256 page_item = self.p_register.create(page_id, page, data)257 self.attach_pages_to_connection(page_item['connection_id'], page_item)258 self.p_register.write(page_item)259 start_ts = datetime.now()260 self.c_register.update_lastused(page_item['connection_id'],start_ts,lastRpc=start_ts)261 return page_item262 def cleanup(self):263 lastSiteCleanup = getattr(self.site,'_lastCleanUp',None)264 if lastSiteCleanup and (time.time()-lastSiteCleanup < self.site.cleanup_interval):265 return266 with self.sd.locked(self.cleanup_key):267 lastCleanupTs = self.sd.get(self.cleanup_key)268 thisCleanupTs = time.time()269 if not lastCleanupTs or ((thisCleanupTs - lastCleanupTs) > self.site.cleanup_interval):270 for page_id, page in list(self.pages().items()):271 page_last_refresh_age = page.get('last_refresh_age')272 if (page_last_refresh_age and (page_last_refresh_age > self.site.page_max_age)):273 self.drop_page(page_id)274 for connection_id, connection in list(self.connections().items()):275 connection_last_refresh_age = connection.get('last_refresh_age')276 if (connection_last_refresh_age and connection_last_refresh_age > self.site.connection_max_age):277 self.drop_connection(connection_id,cascade=True)278 self.sd.set(self.cleanup_key, thisCleanupTs, 0)279 self.site._lastCleanUp = thisCleanupTs280 def cleanup_page_connection(self, max_age=300):281 for page_id, page in list(self.pages().items()):282 page_last_refresh_age = page.get('last_refresh_age')283 if (page_last_refresh_age and (page_last_refresh_age > max_age)):284 self.drop_page(page_id)285 for connection_id, connection in list(self.connections().items()):286 connection_last_refresh_age = connection.get('last_refresh_age')287 if (connection_last_refresh_age and connection_last_refresh_age > max_age):288 self.drop_connection(connection_id)289 def get_user(self, user):290 return self.u_register.read(user)291 def connection(self, connection_id):292 return self.c_register.read(connection_id)293 def page(self, page_id):294 return self.p_register.read(page_id)295 def user(self, user):296 return self.u_register.read(user)297 def stores(self, storename):298 return299 @lock_connection300 def drop_connection(self, connection_id, cascade=None):301 connection_item = self.c_register.pop(connection_id)302 if not connection_item:303 return304 if connection_item['pages']:305 for page_id in connection_item['pages']:306 self.p_register.pop(page_id)307 user = connection_item['user']308 self.pop_connections_from_user(user, connection_item, delete_if_empty=cascade or self.c_register.is_guest(connection_item))309 def log_drop_page(self,page_id=None,page=None,page_last_rpc_age=None,page_max_age=None):310 print('\n\n IT SHOULD DROP THE PAGE ',page_id,page.get('pagename',''),'\n',page,'page_last_rpc_age ',page_last_rpc_age,'page_max_age ',page_max_age)311 312 @lock_page313 def drop_page(self, page_id, cascade=None):314 """TODO315 316 :param page_id: the id of the page317 :param cascade: specifies the cascade policy - True to drop empty connection"""318 page_item = self.p_register.pop(page_id)319 if not page_item:320 return321 self.pop_pages_from_connection(page_item['connection_id'], page_item, delete_if_empty=cascade)322 def connectionStore(self, connection_id, triggered=False):323 return self.c_register.make_store(connection_id, triggered=triggered)324 def userStore(self, user, triggered=False):325 return self.u_register.make_store(user, triggered=triggered)326 def pageStore(self, page_id, triggered=False):327 return self.p_register.make_store(page_id, triggered=triggered)328 def refresh(self, page_id, ts=None,lastRpc=None,pageProfilers=None):329 page_item = self.p_register.read(page_id)330 if page_item:331 self.p_register.update_lastused(page_id, ts,lastRpc=lastRpc,pageProfilers=pageProfilers)332 self.c_register.update_lastused(page_item['connection_id'], ts,lastRpc=lastRpc)333 self.u_register.update_lastused(page_item['user'], ts,lastRpc=lastRpc)334 return page_item335 def users(self, *args, **kwargs):336 return self.u_register.users(*args, **kwargs)337 def user_connections(self, user):338 result = {}339 item = self.u_register.read(user)340 if item:341 result = self.c_register.get_multi_items(list(item['connections'].keys()))342 return result343 def connection_pages(self, connection_id):344 result = {}345 item = self.c_register.read(connection_id)346 if item:347 result = self.p_register.get_multi_items(list(item['pages'].keys()))348 return result349 def connections(self, *args, **kwargs):350 return self.c_register.connections(*args, **kwargs)351 def pages(self, *args, **kwargs):352 return self.p_register.pages(*args, **kwargs)353 def tree(self):354 return PagesTreeResolver()355 def getPageProfile(self,page_id):356 lskey = self.site.register.lastused_key(page_id)357 if lskey:358 last_used = self.site.register.sd.get(lskey)359 if last_used and len(last_used)==3:360 return last_used[2]361 def full_cleanup(self, max_age=30, cascade=False):362 with self.u_register as user_register:363 with self.c_register as connection_register:364 with self.p_register as page_register:365 for page_id, page in list(self.pages().items()):366 if page['last_refresh_age'] > max_age:367 self.drop_page(page_id, page_register=page_register,368 connection_register=connection_register,369 user_register=user_register, cascade=cascade)370 for connection_id, connection in list(self.connections().items()):371 if connection['last_refresh_age'] > max_age:372 self._drop_connection(connection_id, connection_register=connection_register,373 user_register=user_register, cascade=cascade)374class BaseRegister(object):375 def __init__(self, site, **kwargs):376 self.site = site377 self.sd = self.site.shared_data378 self.init(**kwargs)379 380 def __enter__(self):381 self.sd.lock(self.prefix)382 return self383 384 def __exit__(self, type, value, tb):385 self.sd.unlock(self.prefix)386 387 def init(self, **kwargs):388 pass389 390 def make_store(self, register_item_id, triggered=None):391 return ServerStore(self, register_item_id=register_item_id, triggered=triggered)392 393 def create(self, obj):394 pass395 396 def item_key(self, register_item_id):397 return '%s_IT_%s' % (self.prefix, register_item_id)398 399 def lastused_key(self, register_item_id):400 return '%s_LU_%s' % (self.prefix, register_item_id)401 402 @lock_item403 def update_lastused(self, register_item_id, ts=None,lastRpc=None,pageProfilers=None):404 last_used_key = self.lastused_key(register_item_id)405 last_used = self.sd.get(last_used_key)406 if last_used:407 ts = max(last_used[1], ts) if ts else last_used[1]408 self.sd.set(last_used_key, (datetime.now(), ts,lastRpc,pageProfilers), 0)409 410 def read(self, register_item_id):411 register_item = self.sd.get(self.item_key(register_item_id))412 if register_item:413 self._set_last_ts_in_item(register_item)414 return register_item415 416 def exists(self, register_item_id):417 return self.sd.get(self.item_key(register_item_id)) is not None418 419 @lock_item420 def write(self, register_item):421 sd = self.sd422 self.log('write', register_item=register_item)423 register_item_id = register_item['register_item_id']424 is_new_item = register_item.pop('_new', None)425 sd.set(self.item_key(register_item_id), register_item, 0)426 if is_new_item:427 self.update_lastused(register_item_id, register_item['start_ts'])428 self.set_index(register_item)429 self.on_write(register_item)430 431 def on_write(self, register_item):432 pass433 434 def _get_index_key(self, index_name=None):435 if index_name == '*':436 ind_key = '%s_MASTERINDEX' % self.prefix437 elif index_name:438 ind_key = '%s_INDEX_%s' % (self.prefix, index_name)439 else:440 ind_key = '%s_INDEX' % self.prefix441 return ind_key442 443 @lock_index444 def set_index(self, register_item, index_name=None):445 sd = self.sd446 register_item_id = register_item['register_item_id']447 ind_key = self._get_index_key(index_name)448 self.log('set_index', register_item_id=register_item['register_item_id'], index_name=index_name,449 ind_key=ind_key)450 index = sd.get(ind_key)451 if not index:452 self.log('set_index (create new)')453 index = {}454 if index_name and index_name != '*':455 self.set_index({'register_item_id': index_name}, index_name='*')456 if self.parent_index and (self.parent_index in register_item):457 index[register_item_id] = register_item[self.parent_index]458 else:459 index[register_item_id] = True460 sd.set(ind_key, index, 0)461 self.log('set_index:writing', index=index)462 463 @lock_index464 def _remove_index(self, register_item_id, index_name=None):465 """Private. It must be called only in locked mode"""466 sd = self.sd467 ind_key = self._get_index_key(index_name)468 index = sd.get(ind_key)469 if index:470 self.log('_remove_index', register_item_id=register_item_id, index_name=index_name, ind_key=ind_key)471 index.pop(register_item_id, None)472 self._index_rewrite(index_name, index)473 474 def _index_rewrite(self, index_name, index):475 self.log('_index_rewrite', index_name=index_name, index=index)476 sd = self.sd477 ind_key = self._get_index_key(index_name)478 if index == {}:479 if index_name and index_name != '*':480 self._remove_index(register_item_id=index_name, index_name='*')481 sd.delete(ind_key)482 self.log('_index_rewrite:index empty: deleted', ind_key=ind_key)483 return484 sd.set(ind_key, index, 0)485 self.log('_index_rewrite:index updated', ind_key=ind_key)486 487 @lock_item488 def pop(self, register_item_id):489 sd = self.sd490 item_key = self.item_key(register_item_id)491 register_item = sd.get(item_key)492 self.log('pop', register_item=register_item)493 sd.delete(item_key)494 sd.delete(self.lastused_key(register_item_id))495 self._remove_index(register_item_id)496 self.on_pop(register_item_id, register_item)497 return register_item498 499 def on_pop(self, register_item_id, register_item):500 pass501 def _set_last_ts_in_item(self, register_item, last_used=None, now=None):502 now = now or datetime.now()503 def age(label):504 return (now - register_item[label]).seconds505 last_used = last_used or self.sd.get(self.lastused_key(register_item['register_item_id']))506 if not last_used:507 return508 # print 'last_used: ',last_used This is 4 pars returned not three509 register_item['last_ts'], register_item['last_user_ts'],register_item['last_rpc_ts'],register_item['profile'] = last_used510 register_item['age'] = age('start_ts')511 if register_item['last_rpc_ts']:512 register_item['last_rpc_age'] = age('last_rpc_ts')513 register_item['last_refresh_age'] = age('last_ts')514 register_item['last_event_age'] = age('last_user_ts')515 @lock_item516 def upd_register_item(self, register_item_id, **kwargs):517 sd = self.sd518 self.log('set_register_item', register_item_id=register_item_id)519 item_key = self.item_key(register_item_id)520 register_item = sd.get(item_key)521 self.log('upd_register_item', register_item_id=register_item_id, register_item=register_item, updates=kwargs)522 if register_item:523 register_item.update(kwargs)524 self.write(register_item)525 def locked(self, key, **kwargs):526 return self.sd.locked(key, **kwargs)527 def lock_register_item(self, register_item_id, max_retry=None,528 lock_time=None,529 retry_time=None):530 return self.sd.lock(self.item_key(register_item_id), max_retry=max_retry, lock_time=lock_time,531 retry_time=retry_time)532 def unlock_register_item(self, register_item_id):533 return self.sd.unlock(self.item_key(register_item_id))534 def items(self, index_name=None):535 """Registered register_items"""536 index = self.sd.get(self._get_index_key(index_name)) or {}537 return self.get_multi_items(list(index.keys()))538 def get_multi_items(self, keys):539 sd = self.sd540 items = sd.get_multi(keys, '%s_IT_' % self.prefix)541 items_lastused = sd.get_multi(keys, '%s_LU_' % self.prefix)542 now = datetime.now()543 for k, register_item in list(items.items()):544 oldlastused = items_lastused.get(k)545 if oldlastused:546 self._set_last_ts_in_item(register_item, oldlastused, now=now)547 return items548 def log(self, command, **kwargs):549 if False:550 print('-->%s:%s\n (%s)' % (self.name, command, str(kwargs)))551class PageRegister(BaseRegister):552 name = 'page'553 prefix = 'PREG'554 parent_index = 'connection_id'555 def create(self, page_id, page, data=None):556 register_item_id = page_id557 start_ts = datetime.now()558 subscribed_tables = getattr(page, 'subscribed_tables', None)559 if subscribed_tables:560 subscribed_tables = subscribed_tables.split(',')561 register_item = dict(562 register_item_id=register_item_id,563 pagename=page.pagename,564 connection_id=page.connection_id,565 start_ts=start_ts,566 _new=True,567 subscribed_tables=subscribed_tables or [],568 user=page.user,569 user_ip=page.user_ip,570 user_agent=page.user_agent,571 datachanges=list(),572 subscribed_paths=set()573 )574 if data:575 register_item['data'] = Bag(data)576 return register_item577 def on_write(self, register_item):578 for table in register_item['subscribed_tables']:579 self.set_index(register_item, index_name=table)580 def on_pop(self, register_item_id, register_item):581 for table in register_item and register_item['subscribed_tables'] or []:582 self._remove_index(register_item['register_item_id'], index_name=table)583 def pages(self, connection_id=None, index_name=None, filters=None):584 """returns a list of page_id and pages.585 if no index is specified all pages are returned.586 if filters return anly pages matching with filters587 filters is a string with the propname and a regex"""588 pages = self.items(index_name=index_name)589 if not filters or filters == '*':590 return pages591 fltdict = dict()592 for flt in filters.split(' AND '):593 fltname, fltvalue = flt.split(':', 1)594 fltdict[fltname] = fltvalue595 filtered = dict()596 def checkpage(page, fltname, fltval):597 value = page[fltname]598 if not value:599 return600 if not isinstance(value, basestring):601 return fltval == value602 try:603 return re.match(fltval, value)604 except:605 return False606 for page_id, page in list(pages.items()):607 page = Bag(page)608 for fltname, fltval in list(fltdict.items()):609 if checkpage(page, fltname, fltval):610 filtered[page_id] = page611 return filtered612class ConnectionRegister(BaseRegister):613 name = 'connection'614 prefix = 'CREG'615 parent_index = 'user'616 def init(self, onAddConnection=None, onRemoveConnection=None):617 self.onAddConnection = onAddConnection618 self.onRemoveConnection = onRemoveConnection619 def create(self, connection_id, connection):620 register_item = dict(621 register_item_id=connection_id,622 start_ts=datetime.now(),623 _new=True,624 connection_name=connection.connection_name,625 user=connection.user,626 user_id=connection.user_id,627 user_name=connection.user_name,628 user_tags=connection.user_tags,629 user_ip=connection.ip,630 user_agent=connection.user_agent,631 browser_name=connection.browser_name,632 pages={}633 )634 return register_item635 def on_write(self, register_item):636 pass637 def on_pop(self, register_item_id, register_item):638 if hasattr(self.onRemoveConnection, '__call__'): # why not callable??? TOTEST639 self.onRemoveConnection(register_item_id)640 def connections(self, user=None, index_name=None):641 return self.items(index_name=index_name)642 def is_guest(self, register_item):643 return register_item['user']=='guest_%s'%register_item['register_item_id']644class UserRegister(BaseRegister):645 name = 'user'646 prefix = 'UREG'647 parent_index = None648 def create(self, user, connection_item):649 register_item = dict(650 register_item_id=user,651 start_ts=datetime.now(),652 _new=True,653 user=user,654 user_id=connection_item['user_id'],655 user_name=connection_item['user_name'],656 user_tags=connection_item['user_tags'],657 avatar_extra=connection_item.get('avatar_extra'),658 connections={}659 )660 return register_item661 def on_write(self, register_item):662 pass663 def on_pop(self, register_item_id, register_item):664 pass665 def users(self, index_name=None):666 return self.items(index_name=index_name)667class PagesTreeResolver(BagResolver):668 classKwargs = {'cacheTime': 1,669 'readOnly': False,670 'user': None,671 'connection_id': None,672 'page': None673 }674 classArgs = ['user']675 def load(self):676 if not self.user:677 return self.list_users()678 elif not self.connection_id:679 return self.list_connections(user=self.user)680 else:681 return self.list_pages(connection_id=self.connection_id)682 def list_users(self):683 usersDict = self._page.site.register.users()684 result = Bag()685 for user, item_user in list(usersDict.items()):686 item = Bag()687 data = item_user.pop('data', None)688 item_user.pop('datachanges', None)689 item_user.pop('datachanges_idx', None)690 item_user.pop('connections')691 item['info'] = Bag([('%s:%s' % (k, str(v).replace('.', '_')), v) for k, v in list(item_user.items())])692 item['data'] = data693 item.setItem('connections', PagesTreeResolver(user=user), cacheTime=3)694 result.setItem(user, item, user=user)695 return result696 def list_connections(self, user):697 connectionsDict = self._page.site.register.user_connections(user)698 result = Bag()699 for connection_id, connection in list(connectionsDict.items()):700 delta = (datetime.now() - connection['start_ts']).seconds701 user = connection['user'] or 'Anonymous'702 connection_name = connection['connection_name']703 itemlabel = '%s (%i)' % (connection_name, delta)704 item = Bag()705 connection.pop('pages', None)706 data = connection.pop('data', None)707 item['info'] = Bag([('%s:%s' % (k, str(v).replace('.', '_')), v) for k, v in list(connection.items())])708 item['data'] = data709 item.setItem('pages', PagesTreeResolver(user=user, connection_id=connection_id), cacheTime=2)710 result.setItem(itemlabel, item, user=user, connection_id=connection_id)711 return result712 def list_pages(self, connection_id):713 pagesDict = self._page.site.register.connection_pages(connection_id)714 result = Bag()715 for page_id, page in list(pagesDict.items()):716 delta = (datetime.now() - page['start_ts']).seconds717 pagename = page['pagename'].replace('.py', '')718 itemlabel = '%s (%i)' % (pagename, delta)719 item = Bag()720 data = page.pop('data', None)721 item['info'] = Bag([('%s:%s' % (k, str(v).replace('.', '_')), v) for k, v in list(page.items())])722 item['data'] = data723 result.setItem(itemlabel, item, user=item['user'], connection_id=item['connection_id'], page_id=page_id)724 return result ...
__init__.py
Source:__init__.py
1from gym.envs.registration import registry, register, make, spec2# Adversarial MuJoCo3# ----------------------------------------4# 2D5#register(6# id='ReacherAdv-v1',7# entry_point='gym.envs.adversarial.mujoco:ReacherEnv',8# timestep_limit=50,9# reward_threshold=-3.75,10#)11register(12 id='InvertedPendulumAdv-v1',13 entry_point='gym.envs.adversarial.mujoco:InvertedPendulumEnv',14 reward_threshold=950.0,15)16register(17 id='InvertedDoublePendulumAdv-v1',18 entry_point='gym.envs.adversarial.mujoco:InvertedDoublePendulumEnv',19 reward_threshold=9100.0,20)21register(22 id='HalfCheetahAdv-v1',23 entry_point='gym.envs.adversarial.mujoco:HalfCheetahEnv',24 reward_threshold=4800.0,25)26register(27 id='HalfCheetahHeelAdv-v1',28 entry_point='gym.envs.adversarial.mujoco:HalfCheetahHeelEnv',29 reward_threshold=4800.0,30)31register(32 id='HalfCheetahTorsoAdv-v1',33 entry_point='gym.envs.adversarial.mujoco:HalfCheetahTorsoEnv',34 reward_threshold=4800.0,35)36register(37 id='HopperAdv-v1',38 entry_point='gym.envs.adversarial.mujoco:HopperEnv',39 reward_threshold=3800.0,40)41register(42 id='Hopper6Adv-v1',43 entry_point='gym.envs.adversarial.mujoco:Hopper6Env',44 reward_threshold=3800.0,45)46register(47 id='HopperHeelAdv-v1',48 entry_point='gym.envs.adversarial.mujoco:HopperHeelEnv',49 reward_threshold=3800.0,50)51register(52 id='HopperHeel6Adv-v1',53 entry_point='gym.envs.adversarial.mujoco:HopperHeel6Env',54 reward_threshold=3800.0,55)56register(57 id='HopperTorso6Adv-v1',58 entry_point='gym.envs.adversarial.mujoco:HopperTorso6Env',59 reward_threshold=3800.0,60)61register(62 id='SwimmerAdv-v1',63 entry_point='gym.envs.adversarial.mujoco:SwimmerEnv',64 reward_threshold=360.0,65)66register(67 id='Walker2dAdv-v1',68 entry_point='gym.envs.adversarial.mujoco:Walker2dEnv',69)70register(71 id='Walker2dHeelAdv-v1',72 entry_point='gym.envs.adversarial.mujoco:Walker2dHeelEnv',73)74register(75 id='Walker2dTorsoAdv-v1',76 entry_point='gym.envs.adversarial.mujoco:Walker2dTorsoEnv',77)78#register(79# id='AntAdv-v1',80# entry_point='gym.envs.adversarial.mujoco:AntEnv',81# reward_threshold=6000.0,82#)83#84#register(85# id='AntHeelAdv-v1',86# entry_point='gym.envs.adversarial.mujoco:AntHeelEnv',87# reward_threshold=6000.0,88#)89#90#register(91# id='HumanoidAdv-v1',92# entry_point='gym.envs.adversarial.mujoco:HumanoidEnv',93#)94#95#register(96# id='HumanoidHeelAdv-v1',97# entry_point='gym.envs.adversarial.mujoco:HumanoidHeelEnv',98#)99#100#register(101# id='HumanoidStandupAdv-v1',102# entry_point='gym.envs.adversarial.mujoco:HumanoidStandupEnv',103#)104## Adversarial Classic105## ----------------------------------------106#107#register(108# id='CartPoleAdv-v0',109# entry_point='gym.envs.adversarial.classic_control:CartPoleEnv',110# timestep_limit=200,111# reward_threshold=195.0,112#)113#114#register(115# id='CartPoleAdv-v1',116# entry_point='gym.envs.adversarial.classic_control:CartPoleEnv',117# timestep_limit=500,118# reward_threshold=475.0,119#)120#121#register(122# id='MountainCarAdv-v0',123# entry_point='gym.envs.adversarial.classic_control:MountainCarEnv',124# timestep_limit=200,125# reward_threshold=-110.0,126#)127#128#register(129# id='MountainCarContinuousAdv-v0',130# entry_point='gym.envs.adversarial.classic_control:Continuous_MountainCarEnv',131# timestep_limit=999,132# reward_threshold=90.0,133#)134#135#register(136# id='PendulumAdv-v0',137# entry_point='gym.envs.adversarial.classic_control:PendulumEnv',138# timestep_limit=200,139#)140#141#register(142# id='AcrobotAdv-v1',143# entry_point='gym.envs.adversarial.classic_control:AcrobotEnv',144# timestep_limit=500,145#)146## Algorithmic147## ----------------------------------------148#149#register(150# id='Copy-v0',151# entry_point='gym.envs.algorithmic:CopyEnv',152# timestep_limit=200,153# reward_threshold=25.0,154#)155#156#register(157# id='RepeatCopy-v0',158# entry_point='gym.envs.algorithmic:RepeatCopyEnv',159# timestep_limit=200,160# reward_threshold=75.0,161#)162#163#register(164# id='ReversedAddition-v0',165# entry_point='gym.envs.algorithmic:ReversedAdditionEnv',166# kwargs={'rows' : 2},167# timestep_limit=200,168# reward_threshold=25.0,169#)170#171#register(172# id='ReversedAddition3-v0',173# entry_point='gym.envs.algorithmic:ReversedAdditionEnv',174# kwargs={'rows' : 3},175# timestep_limit=200,176# reward_threshold=25.0,177#)178#179#register(180# id='DuplicatedInput-v0',181# entry_point='gym.envs.algorithmic:DuplicatedInputEnv',182# timestep_limit=200,183# reward_threshold=9.0,184#)185#186#register(187# id='Reverse-v0',188# entry_point='gym.envs.algorithmic:ReverseEnv',189# timestep_limit=200,190# reward_threshold=25.0,191#)192#193## Classic194## ----------------------------------------195#196#register(197# id='CartPole-v0',198# entry_point='gym.envs.classic_control:CartPoleEnv',199# timestep_limit=200,200# reward_threshold=195.0,201#)202#203#register(204# id='CartPole-v1',205# entry_point='gym.envs.classic_control:CartPoleEnv',206# timestep_limit=500,207# reward_threshold=475.0,208#)209#210#register(211# id='MountainCar-v0',212# entry_point='gym.envs.classic_control:MountainCarEnv',213# timestep_limit=200,214# reward_threshold=-110.0,215#)216#217#register(218# id='MountainCarContinuous-v0',219# entry_point='gym.envs.classic_control:Continuous_MountainCarEnv',220# timestep_limit=999,221# reward_threshold=90.0,222#)223#224#register(225# id='Pendulum-v0',226# entry_point='gym.envs.classic_control:PendulumEnv',227# timestep_limit=200,228#)229#230#register(231# id='Acrobot-v1',232# entry_point='gym.envs.classic_control:AcrobotEnv',233# timestep_limit=500,234#)235#236## Box2d237## ----------------------------------------238#239#register(240# id='LunarLander-v2',241# entry_point='gym.envs.box2d:LunarLander',242# timestep_limit=1000,243# reward_threshold=200,244#)245#246#register(247# id='LunarLanderContinuous-v2',248# entry_point='gym.envs.box2d:LunarLanderContinuous',249# timestep_limit=1000,250# reward_threshold=200,251#)252#253#register(254# id='BipedalWalker-v2',255# entry_point='gym.envs.box2d:BipedalWalker',256# timestep_limit=1600,257# reward_threshold=300,258#)259#260#register(261# id='BipedalWalkerHardcore-v2',262# entry_point='gym.envs.box2d:BipedalWalkerHardcore',263# timestep_limit=2000,264# reward_threshold=300,265#)266#267#register(268# id='CarRacing-v0',269# entry_point='gym.envs.box2d:CarRacing',270# timestep_limit=1000,271# reward_threshold=900,272#)273#274## Toy Text275## ----------------------------------------276#277#register(278# id='Blackjack-v0',279# entry_point='gym.envs.toy_text:BlackjackEnv',280#)281#282#register(283# id='FrozenLake-v0',284# entry_point='gym.envs.toy_text:FrozenLakeEnv',285# kwargs={'map_name' : '4x4'},286# timestep_limit=100,287# reward_threshold=0.78, # optimum = .8196288#)289#290#register(291# id='FrozenLake8x8-v0',292# entry_point='gym.envs.toy_text:FrozenLakeEnv',293# kwargs={'map_name' : '8x8'},294# timestep_limit=200,295# reward_threshold=0.99, # optimum = 1296#)297#298#register(299# id='NChain-v0',300# entry_point='gym.envs.toy_text:NChainEnv',301# timestep_limit=1000,302#)303#304#register(305# id='Roulette-v0',306# entry_point='gym.envs.toy_text:RouletteEnv',307# timestep_limit=100,308#)309#310#register(311# id='Taxi-v1',312# entry_point='gym.envs.toy_text.taxi:TaxiEnv',313# timestep_limit=200,314# reward_threshold=9.7, # optimum = 10.2315#)316#317#register(318# id='GuessingGame-v0',319# entry_point='gym.envs.toy_text.guessing_game:GuessingGame',320# timestep_limit=200,321#)322#323#register(324# id='HotterColder-v0',325# entry_point='gym.envs.toy_text.hotter_colder:HotterColder',326# timestep_limit=200,327#)328#329## Mujoco330## ----------------------------------------331#332## 2D333#334#register(335# id='Reacher-v1',336# entry_point='gym.envs.mujoco:ReacherEnv',337# timestep_limit=50,338# reward_threshold=-3.75,339#)340#341#register(342# id='InvertedPendulum-v1',343# entry_point='gym.envs.mujoco:InvertedPendulumEnv',344# reward_threshold=950.0,345#)346#347#register(348# id='InvertedDoublePendulum-v1',349# entry_point='gym.envs.mujoco:InvertedDoublePendulumEnv',350# reward_threshold=9100.0,351#)352#353#register(354# id='HalfCheetah-v1',355# entry_point='gym.envs.mujoco:HalfCheetahEnv',356# reward_threshold=4800.0,357#)358#359#register(360# id='Hopper-v1',361# entry_point='gym.envs.mujoco:HopperEnv',362# reward_threshold=3800.0,363#)364#365#register(366# id='Swimmer-v1',367# entry_point='gym.envs.mujoco:SwimmerEnv',368# reward_threshold=360.0,369#)370#371#register(372# id='Walker2d-v1',373# entry_point='gym.envs.mujoco:Walker2dEnv',374#)375#376#register(377# id='Ant-v1',378# entry_point='gym.envs.mujoco:AntEnv',379# reward_threshold=6000.0,380#)381#382#register(383# id='Humanoid-v1',384# entry_point='gym.envs.mujoco:HumanoidEnv',385#)386#register(387# id='HumanoidStandup-v1',388# entry_point='gym.envs.mujoco:HumanoidStandupEnv',389#)390#391## Atari392## ----------------------------------------393#394## # print ', '.join(["'{}'".format(name.split('.')[0]) for name in atari_py.list_games()])395#for game in ['air_raid', 'alien', 'amidar', 'assault', 'asterix', 'asteroids', 'atlantis',396# 'bank_heist', 'battle_zone', 'beam_rider', 'berzerk', 'bowling', 'boxing', 'breakout', 'carnival',397# 'centipede', 'chopper_command', 'crazy_climber', 'demon_attack', 'double_dunk',398# 'elevator_action', 'enduro', 'fishing_derby', 'freeway', 'frostbite', 'gopher', 'gravitar',399# 'ice_hockey', 'jamesbond', 'journey_escape', 'kangaroo', 'krull', 'kung_fu_master',400# 'montezuma_revenge', 'ms_pacman', 'name_this_game', 'phoenix', 'pitfall', 'pong', 'pooyan',401# 'private_eye', 'qbert', 'riverraid', 'road_runner', 'robotank', 'seaquest', 'skiing',402# 'solaris', 'space_invaders', 'star_gunner', 'tennis', 'time_pilot', 'tutankham', 'up_n_down',403# 'venture', 'video_pinball', 'wizard_of_wor', 'yars_revenge', 'zaxxon']:404# for obs_type in ['image', 'ram']:405# # space_invaders should yield SpaceInvaders-v0 and SpaceInvaders-ram-v0406# name = ''.join([g.capitalize() for g in game.split('_')])407# if obs_type == 'ram':408# name = '{}-ram'.format(name)409#410# nondeterministic = False411# if game == 'elevator_action' and obs_type == 'ram':412# # ElevatorAction-ram-v0 seems to yield slightly413# # non-deterministic observations about 10% of the time. We414# # should track this down eventually, but for now we just415# # mark it as nondeterministic.416# nondeterministic = True417#418# register(419# id='{}-v0'.format(name),420# entry_point='gym.envs.atari:AtariEnv',421# kwargs={'game': game, 'obs_type': obs_type, 'repeat_action_probability': 0.25},422# timestep_limit=10000,423# nondeterministic=nondeterministic,424# )425#426# register(427# id='{}-v3'.format(name),428# entry_point='gym.envs.atari:AtariEnv',429# kwargs={'game': game, 'obs_type': obs_type},430# timestep_limit=100000,431# nondeterministic=nondeterministic,432# )433#434# # Standard Deterministic (as in the original DeepMind paper)435# if game == 'space_invaders':436# frameskip = 3437# else:438# frameskip = 4439#440# # Use a deterministic frame skip.441# register(442# id='{}Deterministic-v0'.format(name),443# entry_point='gym.envs.atari:AtariEnv',444# kwargs={'game': game, 'obs_type': obs_type, 'frameskip': frameskip, 'repeat_action_probability': 0.25},445# timestep_limit=100000,446# nondeterministic=nondeterministic,447# )448#449# register(450# id='{}Deterministic-v3'.format(name),451# entry_point='gym.envs.atari:AtariEnv',452# kwargs={'game': game, 'obs_type': obs_type, 'frameskip': frameskip},453# timestep_limit=100000,454# nondeterministic=nondeterministic,455# )456#457# register(458# id='{}NoFrameskip-v0'.format(name),459# entry_point='gym.envs.atari:AtariEnv',460# kwargs={'game': game, 'obs_type': obs_type, 'frameskip': 1, 'repeat_action_probability': 0.25}, # A frameskip of 1 means we get every frame461# timestep_limit=frameskip * 100000,462# nondeterministic=nondeterministic,463# )464#465# # No frameskip. (Atari has no entropy source, so these are466# # deterministic environments.)467# register(468# id='{}NoFrameskip-v3'.format(name),469# entry_point='gym.envs.atari:AtariEnv',470# kwargs={'game': game, 'obs_type': obs_type, 'frameskip': 1}, # A frameskip of 1 means we get every frame471# timestep_limit=frameskip * 100000,472# nondeterministic=nondeterministic,473# )474#475## Board games476## ----------------------------------------477#478#register(479# id='Go9x9-v0',480# entry_point='gym.envs.board_game:GoEnv',481# kwargs={482# 'player_color': 'black',483# 'opponent': 'pachi:uct:_2400',484# 'observation_type': 'image3c',485# 'illegal_move_mode': 'lose',486# 'board_size': 9,487# },488# # The pachi player seems not to be determistic given a fixed seed.489# # (Reproduce by running 'import gym; h = gym.make('Go9x9-v0'); h.seed(1); h.reset(); h.step(15); h.step(16); h.step(17)' a few times.)490# #491# # This is probably due to a computation time limit.492# nondeterministic=True,493#)494#495#register(496# id='Go19x19-v0',497# entry_point='gym.envs.board_game:GoEnv',498# kwargs={499# 'player_color': 'black',500# 'opponent': 'pachi:uct:_2400',501# 'observation_type': 'image3c',502# 'illegal_move_mode': 'lose',503# 'board_size': 19,504# },505# nondeterministic=True,506#)507#508#register(509# id='Hex9x9-v0',510# entry_point='gym.envs.board_game:HexEnv',511# kwargs={512# 'player_color': 'black',513# 'opponent': 'random',514# 'observation_type': 'numpy3c',515# 'illegal_move_mode': 'lose',516# 'board_size': 9,517# },518#)519#520## Debugging521## ----------------------------------------522#523#register(524# id='OneRoundDeterministicReward-v0',525# entry_point='gym.envs.debugging:OneRoundDeterministicRewardEnv',526# local_only=True527#)528#529#register(530# id='TwoRoundDeterministicReward-v0',531# entry_point='gym.envs.debugging:TwoRoundDeterministicRewardEnv',532# local_only=True533#)534#535#register(536# id='OneRoundNondeterministicReward-v0',537# entry_point='gym.envs.debugging:OneRoundNondeterministicRewardEnv',538# local_only=True539#)540#541#register(542# id='TwoRoundNondeterministicReward-v0',543# entry_point='gym.envs.debugging:TwoRoundNondeterministicRewardEnv',544# local_only=True,545#)546#547## Parameter tuning548## ----------------------------------------549#register(550# id='ConvergenceControl-v0',551# entry_point='gym.envs.parameter_tuning:ConvergenceControl',552#)553#554#register(555# id='CNNClassifierTraining-v0',556# entry_point='gym.envs.parameter_tuning:CNNClassifierTraining',557#)558#559## Safety560## ----------------------------------------561#562## interpretability envs563#register(564# id='PredictActionsCartpole-v0',565# entry_point='gym.envs.safety:PredictActionsCartpoleEnv',566# timestep_limit=200,567#)568#569#register(570# id='PredictObsCartpole-v0',571# entry_point='gym.envs.safety:PredictObsCartpoleEnv',572# timestep_limit=200,573#)574#575## semi_supervised envs576# # probably the easiest:577#register(578# id='SemisuperPendulumNoise-v0',579# entry_point='gym.envs.safety:SemisuperPendulumNoiseEnv',580# timestep_limit=200,581#)582# # somewhat harder because of higher variance:583#register(584# id='SemisuperPendulumRandom-v0',585# entry_point='gym.envs.safety:SemisuperPendulumRandomEnv',586# timestep_limit=200,587#)588# # probably the hardest because you only get a constant number of rewards in total:589#register(590# id='SemisuperPendulumDecay-v0',591# entry_point='gym.envs.safety:SemisuperPendulumDecayEnv',592# timestep_limit=200,593#)594#595## off_switch envs596#register(597# id='OffSwitchCartpole-v0',598# entry_point='gym.envs.safety:OffSwitchCartpoleEnv',599# timestep_limit=200,600#)601#602#register(603# id='OffSwitchCartpoleProb-v0',604# entry_point='gym.envs.safety:OffSwitchCartpoleProbEnv',605# timestep_limit=200,...
cin_functions.py
Source:cin_functions.py
1#! /usr/bin/python2# -*- coding: utf-8 -*-3import time4import socket5import cin_constants6import cin_register_map7#import argparse 8# ============================================================================9# Socket10# ============================================================================11try:12 cin_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)13 socket.setdefaulttimeout(0.1)14except socket.error, msg:15 cin_sock = None16 print 'could not open socket'17 sys.exit(1)18# ============================================================================19# Functions20# ============================================================================21# ------------------------------------------------------< common functions >22def ByteToHex( byteStr ):23 """24 Convert a byte string to it's hex string representation e.g. for output.25 """26 27 # Uses list comprehension which is a fractionally faster implementation than28 # the alternative, more readable, implementation below29 # 30 # hex = []31 # for aChar in byteStr:32 # hex.append( "%02X " % ord( aChar ) )33 #34 # return ''.join( hex ).strip() 35 return ''.join( [ "%02X" % ord( x ) for x in byteStr ] ).strip()36def HexToByte( hexStr ):37 """38 Convert a string hex byte values into a byte string. The Hex Byte values may39 or may not be space separated.40 """41 # The list comprehension implementation is fractionally slower in this case42 #43 # hexStr = ''.join( hexStr.split(" ") )44 # return ''.join( ["%c" % chr( int ( hexStr[i:i+2],16 ) ) \45 # for i in range(0, len( hexStr ), 2) ] )46 bytes = []47 hexStr = ''.join( hexStr.split(" ") )48 for i in range(0, len(hexStr), 2):49 bytes.append( chr( int (hexStr[i:i+2], 16 ) ) )50 return ''.join( bytes )51 52def WriteReg( regAddr, value, verify ):53 cin_sock.sendto(HexToByte(regAddr + value ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))54 time.sleep(0.001)55 return 1 # bypass for now56# if verify == 1:57# cin_sock.sendto(HexToByte(cin_register_map.REG_READ_ADDRESS + regAddr ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))58# cin_sock.sendto(HexToByte(cin_register_map.REG_COMMAND + CMD_READ ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))59# cin_sock.settimeout(1.0)60# try:61# data, addr = cin_sock.recvfrom(1024)62# except cin_sock.timeout, msg:63# return 064# 65# if ByteToHex(data) == value and ByteToHex(addr) == regAddr:66# return 167# else:68# return 069# else:70# return 171 72 73def ReadReg( regAddr ):74 cin_sock.sendto(HexToByte(cin_register_map.REG_READ_ADDRESS + regAddr ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))75 time.sleep(0.1)76 cin_sock.sendto(HexToByte(cin_register_map.REG_COMMAND + cin_register_map.CMD_READ_REG ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))77 #time.sleep(0.1)78 cin_sock.settimeout(1.0)79 try:80 data, addr = cin_sock.recvfrom(1024)81 except socket.timeout:82 time.sleep(0.1)83 cin_sock.sendto(HexToByte(cin_register_map.REG_COMMAND + cin_register_map.CMD_READ_REG ),(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_COMMAND_PORT))84 cin_sock.settimeout(1.0)85 data, addr = cin_sock.recvfrom(1024)86 return ByteToHex(data)87# ---------------------------------------------< Connect to QT DAQ88def connect2daq( ):89 cin_sock.sendto(( "dummy data" ),(cin_constants.CIN_FRAME_IP, cin_constants.CIN_STREAM_OUT_PORT))90# ---------------------------------------------< Configuration FPGA functions >91def getFrmDone(frm_done):92 temp = bin((int(ReadReg( cin_register_map.REG_FPGA_STATUS)[4:8],16)))[2:].zfill(16)93 frm_done = int(temp[-16])94 return frm_done95def current_calc(reg_val, current):96 if (int(reg_val[4:8],16) >= int("8000",16)):97# current = 0.000000238*((int("10000",16) - int(reg_val[4:8],16)))/0.00398 current = 0.000000476*((int("10000",16) - int(reg_val[4:8],16)))/0.00399 else:100# current = 0.000000238*(int(reg_val[4:8],16))/0.003101 current = 0.000000476*(int(reg_val[4:8],16))/0.003102 return current103def CINPowerUp():104 print " "105 print "Powering On CIN Board ........ "106 if WriteReg( cin_register_map.REG_PS_ENABLE, "000F", 1) != 1:107 print 'Write register could not be verified. Aborting.'108 sys.exit(1)109 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PS_ENABLE, 1)110 111 if WriteReg( cin_register_map.REG_PS_ENABLE, "001F", 1) != 1:112 print 'Write register could not be verified. Aborting.'113 sys.exit(1)114 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PS_ENABLE, 0)115 time.sleep(2)116def CINPowerDown():117 print " "118 print "Powering Off CIN Board ........ "119# if WriteReg( REG_PS_ENABLE, "000F", 1) != 1:120# print 'Write register could not be verified. Aborting.'121# sys.exit(1)122# WriteReg( REG_COMMAND, CMD_PS_ENABLE, 1)123# 124# time.sleep(2)125# 126 if WriteReg( cin_register_map.REG_PS_ENABLE, "0000", 1) != 1:127 print 'Write register could not be verified. Aborting.'128 sys.exit(1)129 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PS_ENABLE, 0)130 time.sleep(1)131def CIN_FP_PowerUp():132 print " "133 print "Powering On Front Panel Boards ........ "134 if WriteReg( cin_register_map.REG_PS_ENABLE, "003F", 1) != 1:135 print 'Write register could not be verified. Aborting.'136 sys.exit(1)137 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PS_ENABLE, 1)138 139 time.sleep(4)140def CIN_FP_PowerDown():141 print " "142 print "Powering Off Front Panel Boards ........ "143 if WriteReg( cin_register_map.REG_PS_ENABLE, "001F", 1) != 1:144 print 'Write register could not be verified. Aborting.'145 sys.exit(1)146 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PS_ENABLE, 1)147 148 time.sleep(1)149def setCameraOff():150 print " "151 print "Turning off Bias and Clocks in camera head ........ "152 if WriteReg( cin_register_map.REG_BIASCONFIGREGISTER0_REG, "0000", 1) != 1:153 print 'Write register could not be verified. Aborting.'154 sys.exit(1)155 if WriteReg( cin_register_map.REG_CLOCKCONFIGREGISTER0_REG, "0000", 1) != 1:156 print 'Write register could not be verified. Aborting.'157 sys.exit(1)158 159 time.sleep(1)160 161def clearFocusBit():162 # Get Value from Clock&Bias Control163 reg_val = ReadReg("8205")164 #print reg_val[4:]165 temp = str(hex((int(reg_val[7:],base=16)&0xD)).lstrip("0x"))166 #print temp167 str_val=reg_val[4:5]+reg_val[5:6]+reg_val[6:7]+temp168 WriteReg("8205", str_val, 1)169def setFocusBit():170 # Get Value from Clock&Bias Control171 reg_val = ReadReg("8205")172 #print reg_val[4:]173 174 #temp = str(hex((int(reg_val[7:],base=16)|0x2)).lstrip("0x"))175 #print temp176 177 str_val=reg_val[4:5]+reg_val[5:6]+reg_val[6:7]+"A" #temp178 WriteReg("8205", str_val, 1)179def loadFrmFirmware(filename):180 print " "181 print "Loading Frame (FRM) FPGA Configuration Data ........... "182 print "File: " + filename183 WriteReg( cin_register_map.REG_COMMAND, cin_register_map.CMD_PROGRAM_FRAME, 0)184 time.sleep(1)185 with open(filename, 'rb') as f:186 read_data = f.read(128)187 while read_data != "":188 cin_sock.sendto(read_data,(cin_constants.CIN_CONFIG_IP, cin_constants.CIN_STREAM_IN_PORT))189 time.sleep(0.000125) # For UDP flow control (was 0.002)190 read_data = f.read(128)191 f.closed192 time.sleep(1)193 WriteReg( cin_register_map.REG_FRM_RESET, "0001", 0)194 WriteReg( cin_register_map.REG_FRM_RESET, "0000", 0)195 time.sleep(1)196 # need to verify sucess!197def loadCameraConfigFile(filename):198 print " "199 print "Loading Configuration File to CCD Camera ........... "200 print "File: " + filename201 with open(filename, 'r') as f:202 file_line = f.readline()203 while file_line != "":204 if (file_line[:1] != "#") :205 read_addr = file_line[:4]206 read_data = file_line[5:9]207 #print read_addr + read_data208 WriteReg( read_addr, read_data, 0 )209# time.sleep(0.1)210 file_line = f.readline()211 f.closed212def flashFpCfgLeds():213# Test Front Panel LEDs214 print " "215 print "Flashing CFG FP LEDs ............ "216 WriteReg( cin_register_map.REG_SANDBOX_REG00, "AAAA", 1)217 time.sleep(1)218 WriteReg( cin_register_map.REG_SANDBOX_REG00, "5555", 1)219 time.sleep(1)220 WriteReg( cin_register_map.REG_SANDBOX_REG00, "FFFF", 1)221 time.sleep(1)222 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0001", 1)223 time.sleep(0.4)224 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0002", 1)225 time.sleep(0.4)226 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0004", 1)227 time.sleep(0.4)228 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0008", 1)229 time.sleep(0.4)230 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0010", 1)231 time.sleep(0.4)232 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0020", 1)233 time.sleep(0.4)234 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0040", 1)235 time.sleep(0.4)236 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0080", 1)237 time.sleep(0.4)238 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0100", 1)239 time.sleep(0.4)240 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0200", 1)241 time.sleep(0.4)242 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0400", 1)243 time.sleep(0.4)244 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0800", 1)245 time.sleep(0.4)246 WriteReg( cin_register_map.REG_SANDBOX_REG00, "1000", 1)247 time.sleep(0.4)248 WriteReg( cin_register_map.REG_SANDBOX_REG00, "2000", 1)249 time.sleep(0.4)250 WriteReg( cin_register_map.REG_SANDBOX_REG00, "4000", 1)251 time.sleep(0.4)252 WriteReg( cin_register_map.REG_SANDBOX_REG00, "8000", 1)253 time.sleep(0.4)254 WriteReg( cin_register_map.REG_SANDBOX_REG00, "0000", 1)255# ---------------------------------------------< Frame FPGA functions >256def flashFpFrmLeds():257# Test Front Panel LEDs258 print " "259 print "Flashing FRM FP LEDs ............ "260 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0004", 1)261 print "RED ............ "262 time.sleep(0.5)263 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0008", 1)264 print "GRN ............ "265 time.sleep(0.5)266 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "000C", 1)267 print "YEL ............ "268 time.sleep(0.5)269 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0010", 1)270 print "RED ............ "271 time.sleep(0.5)272 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0020", 1)273 print "GRN ............ "274 time.sleep(0.5)275 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0030", 1)276 print "YEL ............ "277 time.sleep(0.5)278 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0040", 1)279 print "RED ............ "280 time.sleep(0.5)281 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0080", 1)282 print "GRN ............ "283 time.sleep(0.5)284 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "00C0", 1)285 print "YEL ............ "286 time.sleep(0.5)287 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0100", 1)288 print "RED ............ "289 time.sleep(0.5)290 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0200", 1)291 print "GRN ............ "292 time.sleep(0.5)293 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0300", 1)294 print "YEL ............ "295 time.sleep(0.5)296 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0400", 1)297 print "RED ............ "298 time.sleep(0.5)299 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0800", 1)300 print "GRN ............ "301 time.sleep(0.5)302 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0C00", 1)303 print "YEL ............ "304 time.sleep(0.5)305 WriteReg( cin_register_map.REG_FRM_SANDBOX_REG00, "0000", 1)306 print "All OFF ............ "307 time.sleep(0.5)...
urls.py
Source:urls.py
...3from tastypie.api import Api4from iondb.rundb import api5from iondb.rundb import mesh_api6v1_api = Api(api_name="v1")7v1_api.register(api.GlobalConfigResource())8v1_api.register(api.ExperimentResource())9v1_api.register(api.ResultsResource())10v1_api.register(api.ReferenceGenomeResource())11v1_api.register(api.ObsoleteReferenceGenomeResource())12v1_api.register(api.LocationResource())13v1_api.register(api.RigResource())14v1_api.register(api.PluginResource())15v1_api.register(api.PluginResultResource())16v1_api.register(api.PluginResultJobResource())17v1_api.register(api.FileServerResource())18v1_api.register(api.TFMetricsResource())19v1_api.register(api.LibMetricsResource())20v1_api.register(api.AnalysisMetricsResource())21v1_api.register(api.QualityMetricsResource())22v1_api.register(api.RunTypeResource())23v1_api.register(api.dnaBarcodeResource())24v1_api.register(api.PlannedExperimentDbResource())25v1_api.register(api.PlannedExperimentResource())26v1_api.register(api.PublisherResource())27v1_api.register(api.ContentResource())28v1_api.register(api.ContentUploadResource())29v1_api.register(api.UserEventLogResource())30v1_api.register(api.KitInfoResource())31v1_api.register(api.KitPartResource())32v1_api.register(api.SequencingKitInfoResource())33v1_api.register(api.SequencingKitPartResource())34v1_api.register(api.ActiveSequencingKitInfoResource())35v1_api.register(api.ActivePGMSequencingKitInfoResource())36v1_api.register(api.ActiveProtonSequencingKitInfoResource())37v1_api.register(api.LibraryKitInfoResource())38v1_api.register(api.LibraryKitPartResource())39v1_api.register(api.ActiveLibraryKitInfoResource())40v1_api.register(api.ActivePGMLibraryKitInfoResource())41v1_api.register(api.ActiveProtonLibraryKitInfoResource())42v1_api.register(api.LibraryKeyResource())43v1_api.register(api.ThreePrimeadapterResource())44v1_api.register(api.TemplateResource())45v1_api.register(api.MessageResource())46v1_api.register(api.MonitorDataResource())47v1_api.register(api.GetSoftwareInfo())48v1_api.register(api.TorrentSuite())49v1_api.register(api.NetworkResource())50v1_api.register(api.IonReporter())51v1_api.register(api.ProjectResource())52v1_api.register(api.ProjectResultsResource())53v1_api.register(api.UserResource())54v1_api.register(api.CompositeResultResource())55v1_api.register(api.CompositeExperimentResource())56v1_api.register(api.MonitorResultResource())57v1_api.register(api.ApplProductResource())58v1_api.register(api.QCTypeResource())59v1_api.register(api.PlannedExperimentQCResource())60v1_api.register(api.EventLogResource())61v1_api.register(api.EmailAddressResource())62v1_api.register(api.ChipResource())63v1_api.register(api.AccountResource())64v1_api.register(api.SampleResource())65v1_api.register(api.ExperimentAnalysisSettingsResource())66v1_api.register(api.CompositeDataManagementResource())67v1_api.register(api.DataManagementHistoryResource())68v1_api.register(api.ClusterInfoHistoryResource())69v1_api.register(api.IonChefPrepKitInfoResource())70v1_api.register(api.ActiveIonChefPrepKitInfoResource())71v1_api.register(api.ActiveIonChefLibraryPrepKitInfoResource())72v1_api.register(api.AvailableIonChefPlannedExperimentResource())73v1_api.register(api.AvailableIonChefPlannedExperimentSummaryResource())74v1_api.register(api.IonChefPlanTemplateResource())75v1_api.register(api.IonChefPlanTemplateSummaryResource())76v1_api.register(api.GetChefScriptInfoResource())77v1_api.register(api.AvailableOneTouchPlannedExperimentResource())78v1_api.register(api.AvailableOneTouchPlannedExperimentSummaryResource())79v1_api.register(api.OneTouchPlanTemplateResource())80v1_api.register(api.OneTouchPlanTemplateSummaryResource())81v1_api.register(api.AvailablePlannedExperimentSummaryResource())82v1_api.register(api.PlanTemplateSummaryResource())83v1_api.register(api.PlanTemplateBasicInfoResource())84v1_api.register(api.ApplicationGroupResource())85v1_api.register(api.SampleGroupType_CVResource())86v1_api.register(api.SampleAnnotation_CVResource())87v1_api.register(api.SampleSetResource())88v1_api.register(api.SampleSetItemResource())89v1_api.register(api.SampleSetItemInfoResource())90v1_api.register(api.SampleAttributeResource())91v1_api.register(api.SampleAttributeDataTypeResource())92v1_api.register(api.SamplePrepDataResource())93v1_api.register(api.AnalysisArgsResource())94v1_api.register(api.FlowOrderResource())95v1_api.register(api.common_CVResource())96v1_api.register(api.FileMonitorResource())97v1_api.register(api.SupportUploadResource())98v1_api.register(api.GetChefCartridgeUsageResource())99v1_api.register(api.ChefPcrPlateconfigResource())100v1_api.register(api.PrepopulatedPlanningSessionResource())101v1_api.register(api.IonMeshNodeResource())102v1_mesh_api = Api(api_name="v1")103v1_mesh_api.register(mesh_api.MeshCompositeExperimentResource())104v1_mesh_api.register(mesh_api.MeshPrefetchResource())105v1_mesh_api.register(mesh_api.AutoDiscoveredHostsResource())106urlpatterns = patterns(107 "iondb.rundb",108 url(r"^$", "data.views.rundb_redirect"),109 url(r"^metaDataLog/(?P<pkR>.*)/$", "views.viewLog", name="report_metadata_log"),110 (r"^getCSV.csv$", "views.getCSV"),111 (r"^getPDF/(?P<pkR>.*)/$", "views.PDFGen"),112 (r"^getOldPDF/(?P<pkR>.*)/$", "views.PDFGenOld"),113 (r"^tfcsv/$", "views.tf_csv"),114 (r"^getPDF/(?P<pkR>.*)/$", "views.PDFGen"),115 (r"^islive/(\d+)$", "ajax.analysis_liveness"),116 (r"^star/(\d+)/(\d)$", "ajax.starRun"),117 (r"^progress_bar/(\d+)$", "ajax.progress_bar"),118 (r"^api$", "ajax.apibase"),119 (r"^changelibrary/(\d+)$", "ajax.change_library"),...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!