class SPTestCase(TestCase): """SharePoint specific test case base class""" client = None # type: ClientContext client_id = settings.get('client_credentials').get('client_id') client_secret = settings.get('client_credentials').get('client_secret') client_credentials = ClientCredential(client_id, client_secret) site_url = settings.get('url') test_user_names = settings.get('test_accounts') @classmethod def setUpClass(cls): cls.client = ClientContext(settings['url']).with_credentials(cls.client_credentials) @staticmethod def ensure_list(web, list_properties): """ :type web: office365.sharepoint.webs.web.Web :type list_properties: office365.sharepoint.lists.list_creation_information.ListCreationInformation """ lists = web.lists.filter("Title eq '{0}'".format(list_properties.Title)).get().execute_query() return lists[0] if len(lists) == 1 else web.lists.add(list_properties).execute_query() @staticmethod def read_file_as_text(path): with open(path, 'r') as content_file: file_content = content_file.read() return file_content @staticmethod def read_file_as_binary(path): with open(path, 'rb') as content_file: file_content = content_file.read() return file_content
def auto_add(self, mode, new_date = datetime.date.today()): # Check if we already have the current date in the file for line in self.lines: date_matches = self._match_date(line['text']) if date_matches is not None: date = self.process_date(date_matches) if date == new_date: return if mode == settings.AUTO_ADD_OPTIONS['TOP']: self.lines.insert(0, { 'text': '%s\n' % new_date.strftime(settings.get('default', 'date_format')), 'entry': None }) self.lines.insert(1, {'text': '\n', 'entry': None}) elif mode == settings.AUTO_ADD_OPTIONS['BOTTOM']: if len(self.lines) > 0: self.lines.append({'text': '\n', 'entry': None}) self.lines.append({ 'text': '%s\n' % new_date.strftime(settings.get('default', 'date_format')), 'entry': None }) self.lines.append({'text': '\n', 'entry': None})
def get_groups_from_ldap(username, password): # here deal with ldap to get user groups conn = get_ldap_connection() try: base_people = settings.get('ldap', 'base_people') base_group = settings.get('ldap', 'base_group') # authenticate user on ldap user_dn = "uid=%s,%s" % (username, base_people) conn.simple_bind_s(user_dn, password) print "User %s authenticated" % username # search for user's groups look_filter = "(|(&(objectClass=*)(member=%s)))" % (user_dn) results = conn.search_s(base_group, ldap.SCOPE_SUBTREE, look_filter, ['cn']) groups = [result[1]['cn'][0] for result in results] return groups except ldap.INVALID_CREDENTIALS: print "Authentication failed: username or password is incorrect." raise AuthenticationError except ldap.NO_SUCH_OBJECT as e: print "No result found: %s " + str(e) raise AuthenticationError finally: conn.unbind_s()
def test1_connect_with_app_principal(self): credentials = ClientCredential( settings.get('client_credentials').get('client_id'), settings.get('client_credentials').get('client_secret')) ctx = ClientContext(settings['url']).with_credentials(credentials) self.assertIsInstance(ctx.authentication_context._provider, ACSTokenProvider)
def get_groups_from_ldap(login, password): # here deal with ldap to get user groups conn = get_ldap_connection() try: base_people = settings.get('ldap', 'base_people') base_group = settings.get('ldap', 'base_group') # authenticate user on ldap user_dn = "uid=%s,%s" % (login, base_people) conn.simple_bind_s(user_dn, password) print "User %s authenticated" % login # search for user's groups look_filter = "(|(&(objectClass=*)(member=%s)))" % (user_dn) results = conn.search_s(base_group, ldap.SCOPE_SUBTREE, look_filter, ['cn']) groups = [result[1]['cn'][0] for result in results] return groups except ldap.INVALID_CREDENTIALS: print "Authentication failed: login id or password is incorrect." raise AuthenticationError except ldap.NO_SUCH_OBJECT as e: print "No result found: %s " + str(e) raise AuthenticationError finally: conn.unbind_s()
def __init__(self, dbman, initialcmd): """ :param dbman: :class:`~alot.db.DBManager` :param initialcmd: commandline applied after setting up interface :type initialcmd: str :param colourmode: determines which theme to chose :type colourmode: int in [1,16,256] """ self.dbman = dbman colourmode = int(settings.get('colourmode')) logging.info('setup gui in %d colours' % colourmode) global_att = settings.get_theming_attribute('global', 'body') self.mainframe = urwid.Frame(urwid.SolidFill()) self.mainframe_themed = urwid.AttrMap(self.mainframe, global_att) self.inputwrap = InputWrap(self, self.mainframe_themed) self.mainloop = urwid.MainLoop(self.inputwrap, handle_mouse=False, event_loop=urwid.TwistedEventLoop(), unhandled_input=self.unhandeled_input) self.mainloop.screen.set_terminal_properties(colors=colourmode) self.show_statusbar = settings.get('show_statusbar') self.notificationbar = None self.mode = 'global' self.commandprompthistory = [] logging.debug('fire first command') self.apply_command(initialcmd) self.mainloop.run()
def traceFromExecutable(self): if not settings.get('new trace', 'EXECUTABLE'): self.status("[!] No executable provided") return self.currentTrace = HeapTrace(self) self.currentTrace.run() self.status("Starting {}".format(settings.get('new trace', 'EXECUTABLE')))
class SPTestCase(TestCase): """SharePoint specific test case base class""" client = None client_id = settings.get('client_credentials').get('client_id') client_secret = settings.get('client_credentials').get('client_secret') site_url = settings.get('url') @classmethod def setUpClass(cls): ctx_auth = AuthenticationContext(url=cls.site_url) ctx_auth.acquire_token_for_app(client_id=cls.client_id, client_secret=cls.client_secret) cls.client = ClientContext(settings['url'], ctx_auth) @property def credentials(self): return ClientCredential(self.client_id, self.client_secret) @staticmethod def create_list(web, list_properties): """ :param Web web: :param ListCreationInformation list_properties: :return: List """ ctx = web.context list_obj = web.lists.add(list_properties) ctx.execute_query() return list_obj @staticmethod def ensure_list(web, list_properties): """ :param Web web: :param ListCreationInformation list_properties: :return: List """ ctx = web.context lists = web.lists.filter("Title eq '{0}'".format( list_properties.Title)) ctx.load(lists) ctx.execute_query() if len(lists) == 1: return lists[0] return SPTestCase.create_list(web, list_properties) @staticmethod def read_file_as_text(path): with open(path, 'r') as content_file: file_content = content_file.read() return file_content @staticmethod def read_file_as_binary(path): with open(path, 'rb') as content_file: file_content = content_file.read() return file_content
def test1_connect_with_app_principal(self): credentials = ClientCredential(settings.get('client_credentials').get('client_id'), settings.get('client_credentials').get('client_secret')) ctx = ClientContext.connect_with_credentials(settings['url'], credentials) self.assertIsInstance(ctx.authentication_context.provider, ACSTokenProvider) self.assertIsInstance(ctx.authentication_context.provider.token, TokenResponse) self.assertTrue(ctx.authentication_context.provider.token.is_valid)
def get_postgresql_url(): return 'postgres://{user}:{password}@{host}:{port}/{database}'.format( database=settings.get('database'), user=settings.get('user'), password=settings.get('password'), host=settings.get('host'), port=5432, )
def create_meta_link(cve_file: str): meta_base_link = settings.get('links', {}).get( 'meta_base_link', 'https://nvd.nist.gov/feeds/xml/cve/trans/es/nvdcve-' ) meta_ending_link = settings.get('links', {}).get('meta_ending_link', 'trans.meta') return meta_base_link + str(cve_file) + meta_ending_link
def main(): # do some stuff to ensure that there are enough ball_entry's in the db import os import sys from sqlalchemy import create_engine default_url = settings.get('DATABASE_URL') db_url = os.environ.get("DATABASE_URL", default_url) engine = create_engine(db_url) engine.echo = False if 'wipe' in sys.argv: print('Wiping') wipe(engine) print('Done wiping') Session.configure(bind=engine) Base.metadata.create_all(engine) conn = engine.connect() try: from pprint import pprint as pp if 'interact' in sys.argv: ppl = lambda x: pp(list(x)) import code l = globals() l.update(locals()) code.interact(local=l) else: table_num = settings.get('table_num', 17) session = Session() query = session.query(BallTable) query = query.all() pp(query) existing_table_ids = [ table.ball_table_num for table in query ] print('existing_table_ids:', existing_table_ids) for table_num in range(1, table_num + 1): if table_num not in existing_table_ids: print('added;', table_num) ball_table_insert = BallTable.__table__.insert({ 'ball_table_name': 'Table {}'.format(table_num), 'ball_table_num': table_num}) engine.execute(ball_table_insert) finally: conn.close()
def inner(self, *args, **kwargs): ### ticket = self.get_argument('ticket', None) za_server_url = app_settings.get('za_server_url', '') za_sso_validate = app_settings.get('za_sso_validate', '') za_sso_login = app_settings.get('za_sso_login', '') authtoken = AuthToken() if ticket: '''如果带返回值就去获取用户信息''' validate_args = {'service': za_server_url, 'ticket': ticket} v = requests.get(za_sso_validate, params=validate_args) user_info = json.loads(v.text) username = user_info.get('username', '') username = '******' user_id = str(user_info.get('user_id', '2')) self.set_secure_cookie("username", username) self.set_secure_cookie("user_id", user_id) ### 生成生成token 并且写入cookie new_token = authtoken.encode_auth_token(user_id, username) self.set_cookie('auth_key', new_token, expires_days=1) with DBContext('readonly') as session: u = session.query( Users.username).filter(Users.username == username).first() '''查询数据库是否用当前用户''' if u: ''' 如果有 获取当前用户权限,并写入redis缓存起来''' my_verify = MyVerify(user_id) my_verify.write_verify() else: '''如果没有则把用户信息导入''' '''跳转完善信息页面,并且告知必须加入权限才能访问''' pass ### 如果没有则把用户信息导入 ### 跳转完善信息页面,并且告知必须加入权限才能访问 auth_key = self.get_cookie('auth_key') if not auth_key: '''没登录,就让跳到登陆页面''' sso_login_url = za_sso_login + '?service=' + za_server_url + '&target=' + za_server_url self.redirect(sso_login_url) return else: user_info = authtoken.decode_auth_token(auth_key) ### 权限鉴定 my_verify = MyVerify(user_id) print(my_verify.get_verify(self.request.method, self.request.uri)) if my_verify.get_verify(self.request.method, self.request.uri) != 0: ### 没权限,就跳转提示没有权限页面权限页面 print('没有权限') # self.redirect('') return # 执行post方法或get方法 func(self, *args, **kwargs)
def calculateTangiblePositionAndRotationWithLiveIDs(self,id1,id2) : #translate from live ID to internal ID internalCursorID1 = self.externalIDtoTangibleCursorID(id1) internalCursorID2 = self.externalIDtoTangibleCursorID(id2) # create dictionary with live cursors liveCursors = {} for c in self.externalCursors: liveCursors[c.id] = c # calculate original rotation angle p1old = pointVector(self.tangibleCursors[internalCursorID1].offsetFromCenterX, self.tangibleCursors[internalCursorID1].offsetFromCenterY) p2old = pointVector(self.tangibleCursors[internalCursorID2].offsetFromCenterX, self.tangibleCursors[internalCursorID2].offsetFromCenterY) rotationAngleInCenteredTangible = calcClockWiseAngle(p1old,p2old) # calculate the current angle p1now = pointVector(liveCursors[id1].x, liveCursors[id1].y) p2now = pointVector(liveCursors[id2].x, liveCursors[id2].y) rotationAngleOfTangibleNow = calcClockWiseAngle(p1now, p2now); # calculate the difference between the two angles currentRotation = clockwiseDifferenceBetweenAngles(rotationAngleInCenteredTangible, rotationAngleOfTangibleNow); # check if the rotation filter is set to pre if settings.get('rotationFilterPosition') == 'pre': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # calculate the vector form current p1 to the tangible center shiftOfId1 = rotateVector(p1old, currentRotation) # calculate position currentPosition = p1now - shiftOfId1 # check if the position filter is active if settings.get('positionFilterActive'): # add current position to filter self.tangiblePositionFilter.addXvalue(currentPosition.x) self.tangiblePositionFilter.addYvalue(currentPosition.y) # get position from filter currentPosition.x = self.tangiblePositionFilter.getXstate() currentPosition.y = self.tangiblePositionFilter.getYstate() # check if post rotation filter is active if settings.get('rotationFilterPosition') == 'post': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # set position and rotation self.position = currentPosition self.rotation = currentRotation
def test2_connect_with_app_principal_alt(self): context_auth = AuthenticationContext(url=settings.get('url')) context_auth.acquire_token_for_app( client_id=settings.get('client_credentials').get('client_id'), client_secret=settings.get('client_credentials').get( 'client_secret')) ctx = ClientContext(settings.get('url'), context_auth) self.assertIsInstance(ctx.authentication_context._provider, ACSTokenProvider)
def get_role_from_ldap(username, password): # mock LDAP in development if os.environ.get('LDAP_ENV') == 'development': print "LDAP authentication mocked" if username == 'marie' or username == 'pierre': if password == 'secret': groupname = 'chimistes' if username in admins or ("@" + groupname) in admins: return 'admin' if username in users or ("@" + groupname) in users: return 'user' return 'all' raise AuthenticationError # here deal with ldap to get user role conn = get_ldap_connection() try: # authicate user on ldap conn.simple_bind_s( 'uid=%s,ou=%s,%s' % (username, settings.get( 'ldap', 'ugroup'), settings.get('ldap', 'base')), password) print "User %s authenticated" % username # retrieve user's group id results = conn.search_s( 'uid=%s,ou=%s,%s' % (username, settings.get( 'ldap', 'ugroup'), settings.get('ldap', 'base')), ldap.SCOPE_SUBTREE) print results gidNumber = results[0][1]['gidNumber'][0] print "User's group number: %s" % gidNumber # retrieve user's group name filter = 'gidNumber=%s' % gidNumber results = conn.search_s( 'ou=groups,%s' % settings.get('ldap', 'base'), ldap.SCOPE_SUBTREE, filter) print results groupname = results[0][1]['cn'][0] print "User's group name: %s" % groupname if username in admins or ("@" + groupname) in admins: return 'admin' if username in users or ("@" + groupname) in users: return 'user' return 'all' except ldap.INVALID_CREDENTIALS: print "Authentication failed: username or password is incorrect." raise AuthenticationError finally: conn.unbind_s()
def acquire_token(): authority_url = 'https://login.microsoftonline.com/{0}'.format( settings.get('tenant')) auth_ctx = adal.AuthenticationContext(authority_url) with open(cert_settings['certificate_path'], 'r') as file: key = file.read() json_token = auth_ctx.acquire_token_with_client_certificate( settings.get('url'), cert_settings['client_id'], key, cert_settings['thumbprint']) return TokenResponse(**json_token)
def _create_from_cookie(self, cookie): ''' Create session from jwt cookie Args: cookie: The cookie used to create session ''' s = jwt.decode(cookie, settings.get('jwt_secret'), algorithms=[ settings.get('jwt_algorithm') ]) self.id = s['id'] self.username = s['username'] self.exp = datetime.fromtimestamp(s['exp'], tz=timezone.utc)
def getcmd(bits): final_cmd = "" pintool_path = os.path.join(ROOT_DIR, "lib/pintool/obj-{}/heaptrace.so".format(["", "ia32", "intel64"][bits/32])) pin_cmd = "pin -t {} -d 0 -p {} -- {}".format(pintool_path, int(settings.get('main', 'PIN_SERVER_PORT')), settings.get('new trace', 'EXECUTABLE')) if settings.get('new trace', 'USE_TCP_WRAPPER') != 'False': final_cmd += settings.get('main', 'TCP_WRAPPER_COMMAND').replace('[CMD]', pin_cmd).replace('[PORT]', settings.get('new trace', 'TCP_WRAPPER_PORT')) else: final_cmd += settings.get('main', 'NEW_TERMINAL_COMMAND').replace('[CMD]', pin_cmd) return shlex.split(final_cmd)
class Application(web.Application): cache_cfg = { 'host': settings.get('redis_host'), 'port': settings.get('redis_port'), 'db': settings.get('redis_db') } access_token_cache = cache.Cache(**cache_cfg) models = models def __init__(self): super(Application, self).__init__(url_patterns, **settings)
def __init__(self, xbee, dmxout): """ :param n_sensor: number of sensor :return: """ threading.Thread.__init__(self) self._must_stop = threading.Event() self._must_stop.clear() self.xbee = xbee self.sensors = SensorManager(tuple(settings.get("sensor", "addr")), dmxout) self._config_addr = str(settings.get("reconfig_addr"))
def update(options, args): """Usage: update Synchronizes your project database with the server.""" db = ProjectsDb() db.update( settings.get('default', 'site'), settings.get('default', 'username'), settings.get('default', 'password') )
def registerCursors(self, *args,**kwargs): self.id = kwargs.get('id', 0) cursors = args[0] # set the center for tangible creation, if not provdided otherwise calibrationCenter = pointVector(settings.get('calibrationCenter')[0],settings.get('calibrationCenter')[1]) # add points to dictionary for c in cursors: self.tangibleCursors[c.id] = tangibleCursor(x=c.x,y=c.y) self.tangibleCursors[c.id].calcOffset(calibrationCenter) # create triangles from points self.tangibleTriangles = createTrianglesFromCursors(cursors)
def calculateTangiblePositionAndRotation(self, id1, id2): # calculate original rotation angle p1old = pointVector(self.tangibleCursors[id1].offsetFromCenterX, self.tangibleCursors[id1].offsetFromCenterY) p2old = pointVector(self.tangibleCursors[id2].offsetFromCenterX, self.tangibleCursors[id2].offsetFromCenterY) rotationAngleInCenteredTangible = calcClockWiseAngle(p1old, p2old) # calculate the current angle p1now = pointVector(self.tangibleCursors[id1].lastKnownPositionX, self.tangibleCursors[id1].lastKnownPositionY) p2now = pointVector(self.tangibleCursors[id2].lastKnownPositionX, self.tangibleCursors[id2].lastKnownPositionY) rotationAngleOfTangibleNow = calcClockWiseAngle(p1now, p2now) # calculate the difference between the two angles currentRotation = clockwiseDifferenceBetweenAngles( rotationAngleInCenteredTangible, rotationAngleOfTangibleNow) # check if the rotation filter is set to pre if settings.get('rotationFilterPosition') == 'pre': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # calculate the vector form current p1 to the tangible center shiftOfId1 = rotateVector(p1old, currentRotation) # calculate position currentPosition = p1now - shiftOfId1 # check if the position filter is active if settings.get('positionFilterActive'): # add current position to filter self.tangiblePositionFilter.addXvalue(currentPosition.x) self.tangiblePositionFilter.addYvalue(currentPosition.y) # get position from filter currentPosition.x = self.tangiblePositionFilter.getXstate() currentPosition.y = self.tangiblePositionFilter.getYstate() # check if post rotation filter is active if settings.get('rotationFilterPosition') == 'post': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # set position and rotation self.position = currentPosition self.rotation = currentRotation
def settingsRequest_handler(self,addr, tags, stuff, source): try: settingDict = settings.returnSettings() for key in settingDict: client = OSC.OSCClient() msg = OSC.OSCMessage() msg.setAddress("/vega/settings") msg.append(key) msg.append(settingDict[key]) client.sendto(msg, (settings.get('remoteControlAddress'), settings.get('remoteControlPort'))) except Exception, e: print 'Sending settings to control programm failed' print "Error:", e
def acquire_token(): """ Acquire token (MSAL) """ authority_url = 'https://login.microsoftonline.com/{0}'.format( settings.get('tenant')) app = msal.ConfidentialClientApplication( authority=authority_url, client_id=settings.get('client_credentials').get('client_id'), client_credential=settings.get('client_credentials').get( 'client_secret')) result = app.acquire_token_for_client( scopes=["https://graph.microsoft.com/.default"]) return result
async def listening(slack_event: http.RequestData, crypto_bot: CryptoBot, settings: Settings): """ The Slack API event handler. Also handles the Slack challenge request. """ print("SLACK EVENT listening: %s" % pformat(slack_event)) slack_event = slack_event or {} logger.debug("SLACK EVENT listening: %s", pformat(slack_event)) logger.debug("SLACK EVENT api: %s", crypto_bot) # ============= Slack URL Verification ============ # # In order to verify the url of our endpoint, Slack will send a challenge # token in a request and check for this token in the response our endpoint # sends back. # For more info: https://api.slack.com/events/url_verification if "challenge" in slack_event: return {'challenge': slack_event['challenge']} # ============ Slack Token Verification =========== # # We can verify the request is coming from Slack by checking that the # verification token in the request matches our app's settings if crypto_bot.verification != slack_event.get("token"): # By adding "X-Slack-No-Retry" : 1 to our response headers, we turn off # Slack's automatic retries during development. headers = {} if settings.get('DEBUG'): headers['X-Slack-No-Retry'] = '1' return Response('Invalid Slack verification token', status=403, headers=headers) # ====== Process Incoming Events from Slack ======= # # If the incoming request is an Event we've subcribed to if "event" in slack_event: return await crypto_bot.dispatch_event(slack_event) # event_type = slack_event["event"]["type"] # If our bot hears things that are not events we've subscribed to, # send a quirky but helpful error response headers = {} if settings.get('DEBUG'): headers['X-Slack-No-Retry'] = '1' return Response( "[NO EVENT IN SLACK REQUEST] These are not the droids you're looking for.", status=404, headers=headers)
def acquire_token_by_username_password(): """ Acquire token via MSAL """ authority_url = 'https://login.microsoftonline.com/{0}'.format(settings.get('tenant')) app = msal.PublicClientApplication( authority=authority_url, client_id=settings.get('client_credentials').get('client_id') ) result = app.acquire_token_by_username_password(username=settings.get('user_credentials').get("username"), password=settings.get('user_credentials').get("password"), scopes=["https://graph.microsoft.com/.default"]) return result
def main(): parse_arguments() init_logging() models.initialize_database(settings["database"]) command = settings.get("command", None) if command: run_command(command) return if settings.get("daemon", False): daemon = Daemon("/tmp/cachebrowser.pid", run_cachebrowser) daemon.start() else: run_cachebrowser()
def load_url(self, url): basename = os.path.basename(unicode(url.toString())) path = os.path.join(settings.get('help_path'), basename) if not os.path.exists(path): messagebox = RUrlLoadingErrorMessageBox(self) messagebox.exec_() else: if not url.scheme() in ['http', 'ftp']: url = QUrl(os.path.abspath(os.path.join(settings.get('help_path'), basename)).replace('\\', '/')) url.setScheme('file') self.ui.webView.load(url) else: messagebox = RUrlLoadingErrorMessageBox(self) messagebox.exec_()
def registerCursors(self, *args, **kwargs): self.id = kwargs.get('id', 0) cursors = args[0] # set the center for tangible creation, if not provdided otherwise calibrationCenter = pointVector( settings.get('calibrationCenter')[0], settings.get('calibrationCenter')[1]) # add points to dictionary for c in cursors: self.tangibleCursors[c.id] = tangibleCursor(x=c.x, y=c.y) self.tangibleCursors[c.id].calcOffset(calibrationCenter) # create triangles from points self.tangibleTriangles = createTrianglesFromCursors(cursors)
def download_and_unzip_cve_file(name: str): xml_base_link = settings.get('links', {}).get('xml_base_link', 'https://nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-') xml_ending_link = settings.get('links', {}).get('xml_ending_link', '.xml.gz') cve_file_name = cve_prefix + name if is_updated_on_nvd(cve_file_id=name): print('Downloading {0} file'.format(cve_file_name)) url = xml_base_link + name + xml_ending_link save_archive(name=cve_file_name, content=requests.get(url).content) save_xml(name=cve_file_name, xml_content=read_archive(name=cve_file_name)) else: print("File {0} wasn't modified".format(cve_file_name))
def __init__(self, dbman, initialcmd): """ :param dbman: :class:`~alot.db.DBManager` :param initialcmd: commandline applied after setting up interface :type initialcmd: str :param colourmode: determines which theme to chose :type colourmode: int in [1,16,256] """ # store database manager self.dbman = dbman # define empty notification pile self._notificationbar = None # should we show a status bar? self._show_statusbar = settings.get("show_statusbar") # pass keypresses to the root widget and never interpret bindings self._passall = False # indicates "input lock": only focus move commands are interpreted self._locked = False self._unlock_callback = None # will be called after input lock ended self._unlock_key = None # key that ends input lock # alarm handle for callback that clears input queue (to cancel alarm) self._alarm = None # create root widget global_att = settings.get_theming_attribute("global", "body") mainframe = urwid.Frame(urwid.SolidFill()) self.root_widget = urwid.AttrMap(mainframe, global_att) # set up main loop self.mainloop = urwid.MainLoop( self.root_widget, handle_mouse=False, event_loop=urwid.TwistedEventLoop(), unhandled_input=self._unhandeled_input, input_filter=self._input_filter, ) # set up colours colourmode = int(settings.get("colourmode")) logging.info("setup gui in %d colours" % colourmode) self.mainloop.screen.set_terminal_properties(colors=colourmode) logging.debug("fire first command") self.apply_command(initialcmd) # start urwids mainloop self.mainloop.run()
def stop_services(): """定时任务:关机服务,目前只针对ec2服务进行关机""" t, t1 = get_downtime() value = get_downtime_value() times = datetime.now() down_time_start = datetime.strptime( str(datetime.now().date()) + t, '%Y-%m-%d%H:%M') down_time_end = datetime.strptime( str(datetime.now().date()) + t1, '%Y-%m-%d%H:%M') if down_time_start < croniter(value, times).get_next(datetime) < down_time_end: ins_log.read_log('info', "开始关机") s = get_aws_session(**settings.get("aws_key")) client = s.client("ec2") list_instances = set() data = client.describe_instances(Filters=[ { 'Name': 'tag:' + 'downtime', 'Values': ['*'] }, ], ) for i in data['Reservations']: instance_id = i["Instances"][0]["InstanceId"] list_instances.add(instance_id) try: with DBContext('w') as session: for instances_id in list_instances: new_instances = RecordInstanceId(instances_id=instances_id) session.add(new_instances) session.commit() except Exception as e: ins_log.read_log('error', e) # 关机方法 client.stop_instances(InstanceIds=list(list_instances))
def start_service(): """定时任务:开机服务,目前只针对ec2服务进行开机""" t, t1 = get_uptime() print(t, t1) times = datetime.now() value = get_uptime_value() print(value) up_time_start = datetime.strptime( str(datetime.now().date()) + t, '%Y-%m-%d%H:%M') up_time_end = datetime.strptime( str(datetime.now().date()) + t1, '%Y-%m-%d%H:%M') if up_time_start < croniter(value, times).get_next(datetime) < up_time_end: ins_log.read_log('info', "开始开机") s = get_aws_session(**settings.get("aws_key")) client = s.client("ec2") with DBContext('w') as session: instance_info = session.query(RecordInstanceId).all() list_instances = [] try: for instance in instance_info: data = model_to_dict(instance) instance_id = data["instances_id"] list_instances.append(instance_id) client.start_instances(InstanceIds=list_instances) # 开机方法 except Exception as e: ins_log.read_log('error', e) session.query(RecordInstanceId).delete( synchronize_session=False) # 清空数据库的所有记录 session.commit()
def ping_handler(self,addr, tags, stuff, source): # save address where ping came from pingSource = OSC.getUrlStr(source).split(':')[0] if settings.get('remoteControlAddress') != pingSource: settings.set('remoteControlAddress',pingSource) # read the port of the remote control programm from settings port = settings.get('remoteControlPort') # send pong message back client = OSC.OSCClient() msg = OSC.OSCMessage() msg.setAddress("/vega/pong") msg.append(1234) client.sendto(msg, (pingSource, port))
def set_volume(self): sfx = settings.get("sfx") self.sfx_suck.set_volume(sfx) self.sfx_click.set_volume(sfx) self.sfx_break.set_volume(sfx) self.sfx_push.set_volume(sfx) self.sfx_pickup.set_volume(sfx)
def change_aws_value(): """定时任务:修改aws服务上的标签value""" with DBContext('w') as session: change_value_list = [] data_info = session.query(ChangeResultList).all() if data_info: for data in data_info: dict_data = model_to_dict(data) change_value_list.append(dict_data) s = get_aws_session(**settings.get("aws_key")) clients = s.client("resourcegroupstaggingapi") try: for tag_value in change_value_list: services_resource_id = tag_value["services_resource_id"] key = tag_value["key"] value = tag_value["value"] # 去除资源标签 clients.untag_resources(ResourceARNList=[ services_resource_id, ], TagKeys=[ key, ]) # 添加资源标签 clients.tag_resources(ResourceARNList=[ services_resource_id, ], Tags={key: value}) session.query(ChangeResultList).filter_by( services_resource_id=services_resource_id, key=key).delete() session.commit() except Exception as e: ins_log.read_log('error', e)
def build_blocks(source_file_name, followers, used_cache, message): data = load(source_file_name) mask_to_sentences = data['mask_to_sentences'] matched_masks = data['matched_masks'] rs_name = os.environ.get('style', settings.get('style', 'random')).upper() system = getattr(RHYME_SYSTEM, rs_name, None) if system is None: from random import choice rs_name, system = choice([(k, v) for k, v in RHYME_SYSTEM.__dict__.iteritems() if '_' not in k]) logger.info('%s: %s' % (rs_name, system)) user_id = message['user_id'] init_sign = followers.get(user_id) if not init_sign: return False, (user_id, NOT_FOLLOW_MSG) words = normalize_sentence(message['body']) logger.info('%s words %s' % (rs_name, ' '.join(words))) good_words = set([w for w in words if in_vocab(w)]) if not (0 < len(good_words) < 5): return False, (user_id, BAD_WORDS_MSG) title = ' '.join(words) logger.info('good words %s' % title) _, user_cache = used_cache.get(user_id, (None, set())) blocks = build(mask_to_sentences, matched_masks, system, user_cache, good_words) if not blocks: return False, (user_id, NO_BLOCKS_MSG) return True, (user_id, blocks, title)
def test1_get_search_results(self): params = ClientPeoplePickerQueryParameters( settings.get('first_account_name')) result = ClientPeoplePickerWebServiceInterface.client_people_picker_resolve_user( self.client, params) self.client.execute_query() self.assertIsNotNone(result.value)
def send_activated_email(to_address, username): email_from_address = settings.get('EMAIL_FROM_ADDRESS') if (email_from_address in ['\'\'', '\"\"', '']): return #text = 'Dear '+ username + ':\n' + ' Your account in docklet has been activated' text = '<html><h4>Dear '+ username + ':</h4>' text += '''<p> Your account in <a href='%s'>%s</a> has been activated</p> <p> Enjoy your personal workspace in the cloud !</p> <br> <p> Note: DO NOT reply to this email!</p> <br><br> <p> <a href='http://docklet.unias.org'>Docklet Team</a>, SEI, PKU</p> ''' % (env.getenv("PORTAL_URL"), env.getenv("PORTAL_URL")) text += '<p>'+ str(datetime.now()) + '</p>' text += '</html>' subject = 'Docklet account activated' msg = MIMEMultipart() textmsg = MIMEText(text,'html','utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = email_from_address msg['To'] = to_address msg.attach(textmsg) s = smtplib.SMTP() s.connect() s.sendmail(email_from_address, to_address, msg.as_string()) s.close()
def __init__(self): tornado.web.Application.__init__(self, urls, **settings) self.assets_env = AssetsEnvironment('../static', '') self.env = Jinja2Environment(loader=FileSystemLoader(settings.get('template_path')), extensions=[AssetsExtension]) self.env.assets_environment = self.assets_env
def rebuild(self): displayed_widgets = [] hidden = settings.get('envelope_headers_blacklist') #build lines lines = [] for (k, vlist) in self.envelope.headers.items(): if (k not in hidden) or self.all_headers: for value in vlist: lines.append((k, value)) # add header list widget iff header values exists if lines: self.header_wgt = widgets.HeadersList(lines) displayed_widgets.append(self.header_wgt) #display attachments lines = [] for a in self.envelope.attachments: lines.append(widgets.AttachmentWidget(a, selectable=False)) if lines: self.attachment_wgt = urwid.Pile(lines) displayed_widgets.append(self.attachment_wgt) self.body_wgt = urwid.Text(self.envelope.body) displayed_widgets.append(self.body_wgt) self.body = urwid.ListBox(displayed_widgets)
def reload(self): self.clear() ndb = NuclideDatabase() for n in ndb.fetch_all(): try: nuclide = elements.get_element(n.title) except elements.ElementNotFound: raise kwargs = { 'spectre': n.data.get('spectre'), 'a_num': nuclide[0], 'lat': nuclide[1], 'a_mass': n.a_mass, 'rus': nuclide[3], 'eng': nuclide[2], } if settings.get('locale') == 'ru_RU': self.update({'%s-%d' % (nuclide[3], n.a_mass): Struct(**kwargs)}) else: self.update({'%s-%d' % (n.title, n.a_mass): Struct(**kwargs)})
def __sendReport(self, msg): def handle_report(response): if response.error: print "Error report Ack:", response.error # print "Report Ack Body", response.body if not hasattr(self, "client1") or not self.client1: try: print "hasattr:", hasattr(self, "client1") print "client1:", self.client1 except: pass print "get new client" self.client1 = httpclient.AsyncHTTPClient() self.client1.fetch(settings.get('uma_mms_url'), handle_report, method='POST', user_agent='MMSC Simulator', body=msg, headers={ 'soapAction': '""', 'x-mmsc-msg-from': 'mm7', 'Mime-Version': '1.0', 'Content-Type': 'text/xml; charset=utf-8', 'Content-Transfer-Encoding': '8bit', 'Connection': 'Keep-alive' })
def build_statusbar(self): """construct and return statusbar widget""" info = {} cb = self.current_buffer btype = None if cb is not None: info = cb.get_info() btype = cb.modename info["buffer_no"] = self.buffers.index(cb) info["buffer_type"] = btype info["total_messages"] = self.dbman.count_messages("*") info["pending_writes"] = len(self.dbman.writequeue) lefttxt = righttxt = u"" if cb is not None: lefttxt, righttxt = settings.get(btype + "_statusbar", (u"", u"")) lefttxt = string_decode(lefttxt, "UTF-8") lefttxt = lefttxt.format(**info) righttxt = string_decode(righttxt, "UTF-8") righttxt = righttxt.format(**info) footerleft = urwid.Text(lefttxt, align="left") pending_writes = len(self.dbman.writequeue) if pending_writes > 0: righttxt = ("|" * pending_writes) + " " + righttxt footerright = urwid.Text(righttxt, align="right") columns = urwid.Columns([footerleft, ("fixed", len(righttxt), footerright)]) footer_att = settings.get_theming_attribute("global", "footer") return urwid.AttrMap(columns, footer_att)
def get_auto_add_direction(filepath, unparsed_filepath): try: auto_add = settings.get('default', 'auto_add') except ConfigParser.NoOptionError: auto_add = settings.AUTO_ADD_OPTIONS['AUTO'] if auto_add == settings.AUTO_ADD_OPTIONS['AUTO']: if os.path.exists(filepath): auto_add = get_parser(filepath).get_entries_direction() else: auto_add = None if auto_add is not None: return auto_add # Unable to automatically detect the entries direction, we try to get a # previous file to see if we're lucky yesterday = datetime.date.today() - datetime.timedelta(days=30) oldfile = yesterday.strftime(os.path.expanduser(unparsed_filepath)) if oldfile != filepath: try: oldparser = get_parser(oldfile) auto_add = oldparser.get_entries_direction() except ParseError: pass if auto_add is None: auto_add = settings.AUTO_ADD_OPTIONS['TOP'] return auto_add
def __init(self): self.setWindowFlags(QtCore.Qt.Dialog | QtCore.Qt.WindowMaximizeButtonHint) ReportRegistry.reload() templates = {} for template_type, template_file in TEMPLATE_TYPES.iteritems(): template_path = os.path.join(settings.get('template_path'), template_file) template = QtCore.QFile(template_path) if template.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text): template_str = unicode(QtCore.QString.fromUtf8(template.readAll())) templates.update({template_type: template_str}) template.close() self.templates = templates items = [[report.date.strftime('%d.%m.%Y %H:%M:%S')] for report in ReportRegistry.itervalues()] if items: items.sort(reverse=True) else: self.ui.saveButton.setEnabled(False) self.model = GenericTableModel(items) self.ui.tableView.setModel(self.model) self.selection_model = self.ui.tableView.selectionModel() self.connect(self.selection_model, QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.show_report) self.ui.tableView.selectRow(0)
def send_beans_email(to_address, username, beans): email_from_address = settings.get('EMAIL_FROM_ADDRESS') if (email_from_address in ['\'\'', '\"\"', '']): return #text = 'Dear '+ username + ':\n' + ' Your beans in docklet are less than' + beans + '.' text = '<html><h4>Dear '+ username + ':</h4>' text += '''<p> Your beans in <a href='%s'>docklet</a> are %d now. </p> <p> If your beans are less than or equal to 0, all your worksapces will be stopped.</p> <p> Please apply for more beans to keep your workspaces running by following link:</p> <p> <a href='%s/beans/application/'>%s/beans/application/</p> <br> <p> Note: DO NOT reply to this email!</p> <br><br> <p> <a href='http://docklet.unias.org'>Docklet Team</a>, SEI, PKU</p> ''' % (env.getenv("PORTAL_URL"), beans, env.getenv("PORTAL_URL"), env.getenv("PORTAL_URL")) text += '<p>'+ str(datetime.datetime.now()) + '</p>' text += '</html>' subject = 'Docklet beans alert' msg = MIMEMultipart() textmsg = MIMEText(text,'html','utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = email_from_address msg['To'] = to_address msg.attach(textmsg) s = smtplib.SMTP() s.connect() s.sendmail(email_from_address, to_address, msg.as_string()) s.close()
def build_statusbar(self): """construct and return statusbar widget""" info = {} cb = self.current_buffer btype = None if cb is not None: info = cb.get_info() btype = cb.modename info['buffer_no'] = self.buffers.index(cb) info['buffer_type'] = btype info['total_messages'] = self.dbman.count_messages('*') info['pending_writes'] = len(self.dbman.writequeue) lefttxt = righttxt = u'' if cb is not None: lefttxt, righttxt = settings.get(btype + '_statusbar', (u'', u'')) lefttxt = string_decode(lefttxt, 'UTF-8') lefttxt = lefttxt.format(**info) righttxt = string_decode(righttxt, 'UTF-8') righttxt = righttxt.format(**info) footerleft = urwid.Text(lefttxt, align='left') pending_writes = len(self.dbman.writequeue) if pending_writes > 0: righttxt = ('|' * pending_writes) + ' ' + righttxt footerright = urwid.Text(righttxt, align='right') columns = urwid.Columns([ footerleft, ('fixed', len(righttxt), footerright)]) footer_att = settings.get_theming_attribute('global', 'footer') return urwid.AttrMap(columns, footer_att)
def generate_auth_token(self, expiration=int(settings.get('ldap', 'expiration'))): s = Serializer(secret_key, expires_in=expiration) token = s.dumps({'login': self.login, 'role': self.role}) print("generate_auth_token : token -> %s" % token) return token
def build_statusbar(self): """construct and return statusbar widget""" info = {} cb = self.current_buffer btype = None if cb is not None: info = cb.get_info() btype = cb.modename info['buffer_no'] = self.buffers.index(cb) info['buffer_type'] = btype info['total_messages'] = self.dbman.count_messages('*') info['pending_writes'] = len(self.dbman.writequeue) info['input_queue'] = ' '.join(self.input_queue) lefttxt = righttxt = u'' if cb is not None: lefttxt, righttxt = settings.get(btype + '_statusbar', (u'', u'')) lefttxt = string_decode(lefttxt, 'UTF-8') lefttxt = lefttxt.format(**info) righttxt = string_decode(righttxt, 'UTF-8') righttxt = righttxt.format(**info) footerleft = urwid.Text(lefttxt, align='left') pending_writes = len(self.dbman.writequeue) if pending_writes > 0: righttxt = ('|' * pending_writes) + ' ' + righttxt footerright = urwid.Text(righttxt, align='right') columns = urwid.Columns([ footerleft, ('fixed', len(righttxt), footerright)]) footer_att = settings.get_theming_attribute('global', 'footer') return urwid.AttrMap(columns, footer_att)
def get_football_teams(user: str, passwd: str) -> list: postgres_settings = ["postgres_host", "postgres_port", "postgres_database"] if not all([ x in settings.get("postgres_settings", {}).keys() for x in postgres_settings ]): raise Exception("Config data for postgres is missing.") conn_dict = { "dbname": settings["postgres_settings"]["postgres_database"], "host": settings["postgres_settings"]["postgres_host"], "port": settings["postgres_settings"]["postgres_port"], "user": user, "password": passwd, } try: with (psycopg2.connect(**conn_dict)) as conn: conn.autocommit = True with (conn.cursor(cursor_factory=psycopg2.extras.DictCursor) ) as curr: # type: psycopg2.extras.DictCursor curr.execute( "select city, team_name, random() as num from tb_football_teams order by num limit 5" ) rows = curr.fetchall() ret_list = [] for row in rows: ret_list.append({ "city": str(row["city"]), "team": str(row["team_name"]) }) return ret_list except Exception as exp: logger.exception(f"Meltdown in postgres code: {exp}") raise exp
def __init__(self): self.Pyborg = pyborg.pyborg(settings.get('gentbot')) self.Learning = settings.get('gentbot').get('learning') self.ReplyRate = settings.get('gentbot').get('replyrate') self.Name = settings.get('gentbot').get('name') self.updater = telext.Updater( token=settings.get('telegram').get('token')) # Setting up handlers dispatcher = self.updater.dispatcher gent_handler = telext.MessageHandler(telext.Filters.text, self.process) # Adding handlers dispatcher.add_handler(gent_handler) dispatcher.add_handler(telext.CommandHandler("save", self.save))
def render(self, size, focus=False): if settings.get('auto_remove_unread'): logging.debug('Tbuffer: auto remove unread tag from msg?') msg = self.get_selected_message() mid = msg.get_message_id() focus_pos = self.body.get_focus()[1] summary_pos = (self.body.get_focus()[1][0], (0,)) cursor_on_non_summary = (focus_pos != summary_pos) if cursor_on_non_summary: if mid not in self._auto_unread_dont_touch_mids: if 'unread' in msg.get_tags(): logging.debug('Tbuffer: removing unread') def clear(): self._auto_unread_writing = False self._auto_unread_dont_touch_mids.add(mid) self._auto_unread_writing = True msg.remove_tags(['unread'], afterwards=clear) fcmd = commands.globals.FlushCommand(silent=True) self.ui.apply_command(fcmd) else: logging.debug('Tbuffer: No, msg not unread') else: logging.debug('Tbuffer: No, mid locked for autorm-unread') else: if not self._auto_unread_writing and \ mid in self._auto_unread_dont_touch_mids: self._auto_unread_dont_touch_mids.remove(mid) logging.debug('Tbuffer: No, cursor on summary') return self.body.render(size, focus)
def autodiscover(self): for app in settings.get('apps', []): try: __import__(app, None, None, ['admin']).admin except: print app, 'failed'
def calculateTangiblePositionAndRotation(self,id1,id2) : # calculate original rotation angle p1old = pointVector(self.tangibleCursors[id1].offsetFromCenterX,self.tangibleCursors[id1].offsetFromCenterY) p2old = pointVector(self.tangibleCursors[id2].offsetFromCenterX,self.tangibleCursors[id2].offsetFromCenterY) rotationAngleInCenteredTangible = calcClockWiseAngle(p1old,p2old) # calculate the current angle p1now = pointVector(self.tangibleCursors[id1].lastKnownPositionX,self.tangibleCursors[id1].lastKnownPositionY) p2now = pointVector(self.tangibleCursors[id2].lastKnownPositionX,self.tangibleCursors[id2].lastKnownPositionY) rotationAngleOfTangibleNow = calcClockWiseAngle(p1now, p2now); # calculate the difference between the two angles currentRotation = clockwiseDifferenceBetweenAngles(rotationAngleInCenteredTangible, rotationAngleOfTangibleNow); # check if the rotation filter is set to pre if settings.get('rotationFilterPosition') == 'pre': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # calculate the vector form current p1 to the tangible center shiftOfId1 = rotateVector(p1old, currentRotation) # calculate position currentPosition = p1now - shiftOfId1 # check if the position filter is active if settings.get('positionFilterActive'): # add current position to filter self.tangiblePositionFilter.addXvalue(currentPosition.x) self.tangiblePositionFilter.addYvalue(currentPosition.y) # get position from filter currentPosition.x = self.tangiblePositionFilter.getXstate() currentPosition.y = self.tangiblePositionFilter.getYstate() # check if post rotation filter is active if settings.get('rotationFilterPosition') == 'post': # add current rotation value to the rotation filter self.tangibleRotationFilter.addValue(currentRotation) # get rotation value from filter currentRotation = self.tangibleRotationFilter.getState() # set position and rotation self.position = currentPosition self.rotation = currentRotation