def get(self, play_store_id): start_time = time.time() app = App.for_play_store_id(play_store_id).first() if not app: try: res = requests.get( 'https://play.google.com/store/apps/details?id={}'.format( play_store_id)) if (res.status_code != 200): return Response.error( 'No app found for app id {}'.format(play_store_id), 404) except requests.exceptions.ConnectionError: return Response.error( 'Unable to fetch app from playstore, please try again after sometime', 500) app = App() app.parse_html_and_save(res.text, play_store_id) if not app.reviews.count() > 0: app.parse_and_save_reviews() args = reqparse.RequestParser().add_argument( 'page_num', type=int, required=False).parse_args() return Response.pagination( 'Reviews Delivered in {} seconds'.format( round(time.time() - start_time, 2)), Review.for_play_store_id(play_store_id).with_author(), args.get('page_num') or 1, 40)
def post(): args = parser.parse_args(strict=True) try: app = App.query.filter_by(name=args['app']).first() except Exception as e: print(e) if app is not None: return Response(response=json.dumps( dict( error='App exist, you are already registered with this name' )), status=400, mimetype='application/json') try: new_app = App(name=args['app'], path_folder=args['path'], route=args['route']) except Exception as e: print(e) db.session.add(new_app) db.session.commit() return Response(response=json.dumps( dict(info='you are successfully registered')), status=200, mimetype='application/json')
def post(self, user, account, **kwargs): app_id = (kwargs['app_id'] if 'app_id' in kwargs else 'new') body_json = self.request.body body = json.loads(body_json) if 'name' not in body: logging.exception(exception) self.response.set_status(400) self.response.write('Invalid JSON data') # return BadRequest("Invalid JSON data") if app_id == 'new': app = App(name=body['name'], created_by=account.key(), editors=[account.key()]) else: app = App.get_by_id(int(app_id)) if app is None: return render_json_response(self, {'error': 'app-not-found'}) if account.key() not in app.editors: return render_json_response(self, {'error': 'access-denied'}) app.name = body['name'] app.body = db.Text(body_json.decode('utf-8')) app.put() return render_json_response(self, {'id': app.key().id()})
def apps(self, attributes=None): """The apps endpoint provides information about a customer's mobile applications. The apps endpoint returns a list of apps with links to additional metric endpoints. Information requested in the attributes parameter is made available as a series of key-value pairs inside the object representing each app. Required keyword arguments: <None> Optional keyword arguments: attributes -- list of attributes desired as part of app response data Possible contents: "appName", "appType", "appVersions", "crashPercent", "dau", "latency", "latestAppStoreReleaseDate", "latestVersionString", "linkToAppStore", "iconURL", "mau", "rating", "role" """ url_suffix = 'apps?attributes=%s' % '%2C'.join( attributes or list(self.APP_ATTRIBUTE_CHOICES)) content = self.__request('GET', url_suffix, {}) response = [ App(app_id, app_data) for app_id, app_data in content.items() ] return response
def setUp(self): """ user_a -> admin user_b -> user2 user_a uploads shared file. We authenticate to the API with user_b. user_b subscribes to user_a's shake.] """ super(APIResourceRequests, self).setUp() self.user_a = User( name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1, about="admin", website='https://mltshp.com') self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.test_file1_path = os.path.abspath("test/files/1.png") self.test_file1_sha1 = Sourcefile.get_sha1_file_key(self.test_file1_path) self.test_file1_content_type = "image/png" response = self.upload_file(file_path=self.test_file1_path, sha1=self.test_file1_sha1, content_type=self.test_file1_content_type, user_id=self.user_a.id, sid=self.sid, xsrf=self.xsrf) self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.group_shake = self.user_b.create_group_shake(title='Group Shake', name='groupshake', description='This is a group shake.') self.group_shake_2 = self.user_a.create_group_shake(title='Another Group', name='anothergroup') # Add user_b to user_a's group shake, so we get it in user_b's /shakes endpoint. shake_manager = ShakeManager(user_id=self.user_b.id, shake_id=self.group_shake_2.id) shake_manager.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.access_token = Accesstoken.generate(self.authorization.id) extra_authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id) self.ratelimited_access_token = Accesstoken.generate(extra_authorization.id) now_hour = datetime.utcnow().strftime('%Y-%m-%d %H:00:00') ratelimit = Apihit(accesstoken_id=self.ratelimited_access_token.id, hits=options.api_hits_per_hour - 2, hour_start=now_hour) ratelimit.save() #subscription self.user_b.subscribe(self.user_a.shake())
async def api_save_app_info(*, name, size, count): app = App(name=name, size=size, status=1, surplus_count=count, add_time=get_current_time()) app.id = 'adhasjdhasjdhasjdhajkdhajkhd' # await app.save() create_mobile_config(app.id, app.name) return dict(app=app)
def main(): app_context = ApplicationContext() parser = argparse.ArgumentParser() parser.add_argument("--apps", default="config/apps.json") args = parser.parse_args() app = App(args) window = MainWindow(app) window.show() app_context.app.setStyle("windowsvista") return app_context.app.exec_()
def parse_apps(session, activity_tracker): #TODO see if there is a way to query the database only for a list of app names # get a copy of the starting database so we don't have to query it all the time apps = session.query(App).all() app_names = [] for a in apps: app_names.append(a.name) # read the file, write new apps and events to the database appfile = os.path.join(os.path.expanduser(cfg.CURRENT_DIR), cfg.APPLOG) if os.path.isfile(appfile): f = open(appfile, 'r+') lines_to_save = [] for line in f: try: # get data from the line of text text = ast.literal_eval(line.rstrip()) t = text['time'] event = text['type'] app_name = text['app'] pid = None # get pid of app from database, add app to database if not already there if app_name not in app_names: # add app to the database app_to_add = App(t, app_name) session.add(app_to_add) activity_tracker.storage.sqlcommit() # update our local app list apps = session.query(App).all() app_names = [a.name for a in apps] pid = app_names.index(app_name) + 1 # array starts at 0, database ids a 1 # add the app event to the database ae = AppEvent(t, pid, event) session.add(ae) except: print "Could not save " + str(line) + " to the database. Saving for the next round of parsing." lines_to_save.append(line) # write lines that did not make it into the database to the start of the # file and delete the rest of the file f.seek(0) for line in lines_to_save: f.write(line) f.truncate() f.close()
def setUp(self): super(APIResourceOwnerPasswordCredentials, self).setUp() self.user_a = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save()
def setUp(self): """ . Need to create a user and a test app . Create second account that is going to auth """ super(APIAuthorizationTests, self).setUp() self.user_a = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_a.set_password('asdfasdf') self.user_a.save() self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.app_query = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return?query=param') self.app_query.save() self.app_no_redirect = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='') self.app_no_redirect.save()
def asymsched_test(): """ 正确性测试 模拟CPU一共有四个节点。 运行两个app,每个app有一个cluster,每个cluster使用两个node。 """ test_bandwidths = [[0, 3000000000, 1000000000, 1000000000], [3000000000, 0, 1000000000, 1000000000], [1000000000, 1000000000, 0, 7000000000], [1000000000, 1000000000, 7000000000, 0]] test_remote_access = [[0, 3000000000, 0, 0], [3000000000, 0, 0, 0], [0, 0, 0, 5000], [0, 0, 5000, 0]] test_apps = [] test_apps.append(App()) test_apps[0].tt = 50000 test_apps[0].clusters.append(Cluster()) test_apps[0].clusters[0].memories = [200, 400] test_apps[0].clusters[0].current_nodes = [0, 1] test_apps.append(App()) test_apps[1].tt = 50000 test_apps[1].clusters.append(Cluster()) test_apps[1].clusters[0].memories = [30, 300] test_apps[1].clusters[0].current_nodes = [2, 3] _, _, test_min_pid, test_do_migration = asymsched(test_apps, test_bandwidths, test_remote_access) print(test_min_pid, test_do_migration) for app in test_apps: for cluster in app.clusters: print(cluster.origin_nodes, "==>", cluster.current_nodes)
def post(self): current_user = self.get_current_user_object() title = self.get_argument('title', '').strip() description = self.get_argument('description', '').strip() redirect_url = self.get_argument('redirect_url', '').strip() new_app = App(user_id=current_user.id, title=title, description=description, redirect_url=redirect_url) if not new_app.save(): self.add_errors(new_app.errors) return self.render("developers/new-api-application.html", title=title, description=description, redirect_url=redirect_url) return self.redirect('/developers/view-app/%s' % (new_app.id))
def setUp(self): super(APITokenTests, self).setUp() self.user_a = User(name='admin', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_a.set_password('asdfasdf') self.user_a.save() self.sid = self.sign_in('admin', 'asdfasdf') self.xsrf = self.get_xsrf() self.user_b = User(name='user2', email='*****@*****.**', email_confirmed=1, is_paid=1) self.user_b.set_password('asdfasdf') self.user_b.save() self.app = App(user_id=self.user_a.id, title='An App', description='Nothing yet.', redirect_url='http://client.example.com/return') self.app.save() self.authorization = Authorizationcode.generate(self.app.id, self.app.redirect_url, self.user_b.id)
def create_app(): if request.method == 'GET': return render_template('create_app.html') else: client_type = request.form['client_type'] if client_type not in ('public', 'confidential'): return BAD_REQUEST client_secret = (str(uuid.uuid4()) if client_type == 'confidential' else None) client_id = rand_client_id() app = App(client_id=client_id, client_secret=client_secret, user=request.session.user, redirect_uri=request.form['redirect_uri'], launch_uri=request.form['launch_uri'], name=request.form['appname']) db.session.add(app) db.session.commit() return redirect(url_for('ui.update_app', client_id=client_id))
def test_disconnecting_connection(self): """ Sending a post request to /settings/connections/{{app_id}}/disconnect should mark an access token as deleted. """ # app created by Torrez. app = App(user_id=self.user.id,title='Title') app.save() # another user. new_user = User(name='newuser', email='*****@*****.**', email_confirmed=1, is_paid=1) new_user.set_password('asdfasdf') new_user.save() #connect another user and app token = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token.save() # If torrez hits URL, which he should never be able to find, nothing happens # to token. self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) self.assertEqual(0, token_reloaded.deleted) # but if the user it's about hits it, the token deletes. self.sign_in('newuser', 'asdfasdf') self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) self.assertEqual(1, token_reloaded.deleted) # Even if there are several undeleted access tokens, they all end up deleted. token_two = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token_two.save() token_three = Accesstoken(user_id=new_user.id, app_id=app.id, deleted=0) token_three.save() self.post_url("/account/settings/connections/%s/disconnect" % app.id) token_reloaded = Accesstoken.get('id = %s', token.id) token_two_reloaded = Accesstoken.get('id = %s', token_two.id) token_three_reloaded = Accesstoken.get('id = %s', token_three.id) self.assertEqual(1, token_reloaded.deleted) # still self.assertEqual(1, token_two_reloaded.deleted) self.assertEqual(1, token_three_reloaded.deleted)
def get(self, play_store_id): app_found = App.for_play_store_id(play_store_id).first() if (app_found): return Response.model('Delivered', app_found) try: res = requests.get( 'https://play.google.com/store/apps/details?id={}'.format( play_store_id)) if (res.status_code != 200): return Response.error( 'No app found for app id {}'.format(play_store_id), 404) except requests.exceptions.ConnectionError: return Response.error( 'Unable to fetch app from playstore, please try again after sometime', 500) new_app = App() new_app.parse_html_and_save(res.text, play_store_id) return Response.model('Delivered', new_app)
def registerApp(request): usr = request.user cUser = CalendarUser.objects.get(user=usr) if not cUser.isDev: return redirect(reverse('editprofile')) context = {} context['errors'] = [] context['messages'] = [] if request.method == 'GET': context['form'] = AppForm() return render(request, 'registerapp.html', context) app = App(owner=cUser) form = AppForm(request.POST, instance=app) context['form'] = form if not form.is_valid(): return render(request, 'registerapp.html', context) form.save() return redirect(reverse('devcenter'))
def asymsched_once(): """ 单次调用asymsched算法,返回最优放置策略 """ data = request.json assert 'apps' in data assert 'bandwidths' in data assert 'remote_access' in data param_apps = [] for app_id, app_data in enumerate(data['apps']): param_apps.append(App()) param_apps[app_id].set_data(app_data) apps, placements, min_pid, do_migration = asymsched( param_apps, data["bandwidths"], data["remote_access"]) return jsonify({ 'apps': list(map(App.serialize, apps)), 'placements': list(map(Placement.serialize, placements)), 'min_pid': min_pid, 'do_migration': do_migration })
def build_first_db(): """ Populate a small db with some example entries. """ db.drop_all() db.create_all() anonymous = Role(name=u'Anonymous', description=u'匿名用户') admin = Role(name=u'Admin', description=u'管理员') develop = Role(name='Develop', description=u'开发人员') test = Role(name='Test', description=u'测试人员') ops = Role(name='Ops', description=u'运维人员') admin_user = User(real_name=u'admin', email=u'*****@*****.**', login=u"admin", password=generate_password_hash(u"admin"), roles=[admin]) anonymous_user = User(real_name=u'anonymous', email=u'*****@*****.**', login=u"anonymous", password=generate_password_hash(u"anonymous"), roles=[anonymous]) ip1 = Ip( isp=u'电信', use=u'在用', ip=u"1.1.1.1", mask=(u"255.255.255.0"), mac=(u"44a8-422a-20ff"), route=(u"1.1.1.254"), switch_port=(u"5F-U09 G1/0/32"), ) ip2 = Ip( isp=u'电信', use=u'在用', ip=u"1.1.1.2", mask=(u"255.255.255.0"), mac=(u"44a8-422a-20ff"), route=(u"1.1.1.254"), switch_port=(u"5F-U09 G1/0/32"), ) ip3 = Ip( isp=u'内网', use=u'在用', ip=u"1.1.1.3", mask=(u"255.255.255.0"), mac=(u"44a8-422a-20ff"), route=(u"1.1.1.254"), switch_port=(u"5F-U09 G1/0/32"), ) ip4 = Ip( isp=u'联通', use=u'在用', ip=u"1.1.1.4", mask=(u"255.255.255.0"), mac=(u"44a8-422a-20ff"), route=(u"1.1.1.254"), switch_port=(u"5F-U09 G1/0/32"), ) app1 = App( app=u'kf_scsa', description=u"客服我也不知道", ps=(u"没什么事"), ) app2 = App( app=u'gamemanager', description=u"游戏我也不知道", ps=(u"没什么事"), ) app3 = App( app=u'webPlatform', description=u"公共我也不知道", ps=(u"没什么事"), ) app4 = App( app=u'wechat-server2', description=u"wx我也不知道", ps=(u"没什么事"), ) project1 = Project(project=u'体彩项目', apps=[app1]) project2 = Project(project=u'福彩项目', apps=[app2]) project3 = Project(project=u'公共平台项目', apps=[app3]) project4 = Project(project=u'客服系统项目', apps=[app4]) device1 = Device( device_num=u'02-1331', device_name=u'5F-U10', idc=u"东莞", location=(u"5F-U10"), hardware_type=(u"DELL-2U"), brand=(u"DELL"), fast_repair_code=(u"没什么事"), cpu=(u"没什么事"), memory=(u"没什么事"), disk=(u"没什么事"), ips=[ip1], apps=[app1], ) device2 = Device( device_num=u'02-1331', device_name=u'5F-U12', idc=u"东莞", location=(u"5F-U10"), hardware_type=(u"DELL-2U"), brand=(u"DELL"), fast_repair_code=(u"没什么事"), cpu=(u"没什么事"), memory=(u"没什么事"), disk=(u"没什么事"), ips=[ip2], apps=[app2], ) platforms_info1 = Platforms_info( platform=u'阿里云管理控制台', description=u'申请云服务器及域名解析', url=u"http://www.aliyun.com/", username=u"hhlyadmin", password=(u"hhlyadmin"), ps=(u"登陆进入后,依次点击:\ 订单管理-我的租用-最后面详细\ 下方图标-进入之后\ 点击IP即可查看流量图"), ) platforms_info2 = Platforms_info( platform=u'DNS盾', description=u'13322.com域名A记录解析网站', url=u"http://www.dnsdun.com", username=u"*****@*****.**", password=(u"*****@*****.**"), ps=(u"登陆进入后"), ) db.session.add(anonymous) db.session.add(admin) db.session.add(develop) db.session.add(test) db.session.add(ops) db.session.add(admin_user) db.session.add(anonymous_user) db.session.add(ip1) db.session.add(ip2) db.session.add(ip3) db.session.add(ip4) db.session.add(app1) db.session.add(app2) db.session.add(app3) db.session.add(app4) db.session.add(project1) db.session.add(project2) db.session.add(project3) db.session.add(project4) db.session.add(device1) db.session.add(device2) db.session.add(platforms_info1) db.session.add(platforms_info2) db.session.commit() return
import models.App as App import os app = App.App() os.chdir("..") def run_sample(): app.set_input_file("data/PhoneNumber.csv") app.set_output_file("data/PhoneNumberResult.csv") app.run() def test_output_csv_file(): run_sample() actual_result = app.read_csv("data/PhoneNumberResult.csv") expect_result = app.read_csv("tests/PhoneNumberResult_Test.csv") assert actual_result.equals(expect_result)
def add_app(data): is_awesome = True if data['rating'] > 3 else False app = App(data['name'], data['genre'], data['rating'], data['version'], data['size_bytes'], is_awesome) session.add(app)
def populate_datastore(): client_key_name1 = 'client01' client1 = Client(key_name=client_key_name1, id='12345678', name='Organic Oranges LLC', domain=db.Link("http://client1.clickin-tech.appspot.com")) client1.put() client_key_name2 = 'client02' client2 = Client(key_name=client_key_name2, id='87654321', name='Green Trees Inc.', domain=db.Link("http://client2.clickin-tech.appspot.com")) client2.put() admin_key_name1 = 'admin001' admin1 = Client_User(key_name=admin_key_name1, client=client1, id='001002', name='Daniel Alves', email='*****@*****.**') admin1.put() admin_key_name2 = 'admin002' admin2 = Client_User(key_name=admin_key_name2, client=client1, id='001005', name='Andres Iniesta', email='*****@*****.**') admin2.put() admin_key_name5 = 'admin05' admin5 = Client_User(key_name=admin_key_name5, client=client2, id='0010011', name='Josep Guardiola', email='*****@*****.**') admin5.put() admin_key_name6 = 'admin06' admin6 = Client_User(key_name=admin_key_name6, client=client2, id='0010016', name='Lionel Messi', email='*****@*****.**') admin6.put() #ClickIn People Search app_key_name1 = 'app1' app1 = App(key_name=app_key_name1, client=client1, app_id='163185560402939', api_key='807c2277abe596cfe542927858105306', app_secret='aae6abb4d6bf0b41f066c387ab36e486', name='ClickIn People Search', domain=db.Link("http://app1.client1.clickin-tech.appspot.com/")) app1.put() #Click In People Search 2 app_key_name2 = 'app2' app2 = App(key_name=app_key_name2, client=client1, app_id='114549068628127', api_key='7f15ffb2b72ff6c4a6d08daebca21a52', app_secret='61545bcd8a3d9fc6a8107eaed5cbe4de', name='ClickIn People Search 2', domain=db.Link("http://app2.client1.clickin-tech.appspot.com/")) app2.put() #Cool Running App app_key_name3 = 'app3' app3 = App(key_name=app_key_name3, client=client2, app_id='107411582680918', api_key='7a55f39fb4e0371aad78e1bd8dd517af', app_secret='c12b89b5f815cebe27636cd8c50a6264', name='Cool Running App', domain=db.Link("http://app1.client2.clickin-tech.appspot.com/")) app3.put()
def parse_windows(session, activity_tracker): # get a copy of the starting app database so we don't have to query it all the time apps = session.query(App).all() app_names = [a.name for a in apps] # get a copy of the starting window database so we don't have to query it all the time windows = session.query(Window).all() window_names = [w.title for w in windows] # read the file, write lines to database, and save lines that were not # written to the database windowfile = os.path.join(os.path.expanduser(cfg.CURRENT_DIR), cfg.WINDOWLOG) if os.path.isfile(windowfile): f = open(windowfile, 'r+') lines_to_save = [] for line in f: try: text = ast.literal_eval(line.rstrip()) t = text['time'] event = text['type'] app_name = text['app'] window = text['window'] # for now let's only save open, close, active, inactive if event not in ['Open', 'Close', 'Active', 'Inactive']: continue if app_name not in app_names: # add app to the database app_to_add = App(t, app_name) session.add(app_to_add) #TODO catch if our commit fails activity_tracker.storage.sqlcommit() # update our local app list apps = session.query(App).all() app_names = [a.name for a in apps] pid = app_names.index(app_name) + 1 # array starts at 0, database ids a 1 if window not in window_names: # add app to the database app_to_add = Window(t, pid, window) session.add(app_to_add) #TODO catch if our commit fails activity_tracker.storage.sqlcommit() # update our local app list windows = session.query(Window).all() window_names = [w.title for w in windows] wid = window_names.index(window) + 1 # array starts at 0, database ids a 1 # add the window event to the database we = WindowEvent(t, wid, event) session.add(we) except: print "Could not save " + str(line) + " to the database. Saving for the next round of parsing." lines_to_save.append(line) # write lines that did not make it into the database to the start of the # file and delete the rest of the file f.seek(0) for line in lines_to_save: f.write(line) f.truncate() f.close()
def parse_geometries(session, activity_tracker): # get names of file to read and the last arrangement geofile = os.path.join(os.path.expanduser(cfg.CURRENT_DIR), cfg.GEOLOG) q = session.query(Arrangement).order_by(Arrangement.id.desc()).first() last_arr = ast.literal_eval(q.arr) if q else {} # clean up dictionary for equal comparison for a, va in last_arr.iteritems(): del va['pid'] for w, vw in va['windows'].iteritems(): del vw['wid'] del vw['gid'] # check if db file if os.path.isfile(geofile): f = open(geofile, 'r+') lines_to_save = [] # get existing apps from the database apps = session.query(App).all() app_names = [a.name for a in apps] # get existing windows from the database windows = session.query(Window).all() window_names = [w.title for w in windows] # get existing geometries from the database geometries = session.query(Geometry).all() geometry_dicts = [[g.x, g.y, g.w, g.h] for g in geometries] # get existing urls from the database db_urls = session.query(URL).all() urls = [d.url for d in db_urls] for line in f: try: # get data text = ast.literal_eval(line.rstrip()) t = text['time'] arrangement = text['geometry'] # if this is a duplicate of the last arrangement, don't record it if arrangement == last_arr: continue # check for new windows opened or activated for app, value in arrangement.iteritems(): app_name = value['name'] active = value['active'] windows = value['windows'] # add new apps to the database, but should not need to to this if app_name not in app_names: # add app to the database app_to_add = App(t, app_name) session.add(app_to_add) activity_tracker.storage.sqlcommit() # update our local app list apps = session.query(App).all() app_names = [a.name for a in apps] # add the app's pid to the arrangement dictionary pid = app_names.index(app_name) + 1 # array starts at 0, database ids a 1 value['pid'] = pid # removing for now, may want to add back later, but will need new arrangement comparison method for window, val in windows.iteritems(): # get window information title = val['name'] w_active = val['active'] bounds = val['bounds'] x = int(bounds['x']) y = int(bounds['y']) width = int(bounds['width']) height = int(bounds['height']) # add new windows to the database, but should not need to do this if title not in window_names: # add app to the database app_to_add = Window(t, pid, title) session.add(app_to_add) activity_tracker.storage.sqlcommit() # update our local window list windows = session.query(Window).all() window_names = [w.title for w in windows] # add the window's wid to the window dictionary wid = window_names.index(title) + 1 # array starts at 0, database ids a 1 val['wid'] = wid # removing for now, may want to add back later, but will need new arrangement comparison method # add new geometries to the database gd = [x, y, width, height] if gd not in geometry_dicts: ge = Geometry(t, x, y, width, height) session.add(ge) activity_tracker.storage.sqlcommit() # update our local geomery list geometries = session.query(Geometry).all() geometry_dicts = [[g.x, g.y, g.w, g.h] for g in geometries] # add the geometry's gid to the window dictionary gid = geometry_dicts.index(gd) + 1 # array starts at 0, database ids a 1 val['gid'] = gid # removing for now, may want to add back later, but will need new arrangement comparison method # add new urls to the database if 'tabs' in val.keys(): tabs = val['tabs'] for tab, tval in tabs.iteritems(): t_active = tval['active'] t_url = tval['url'] if tval['url'] not in urls: u = URL(t, tval['title'], tval['url'], tval['host']) session.add(u) activity_tracker.storage.sqlcommit() # update our local urls list db_urls = session.query(URL).all() urls = [d.url for d in db_urls] # add the url's uid to the window dictionary uid = urls.index(tval['url']) + 1 # array starts at 0, database ids a 1 tval['uid'] = uid #TODO create url open, close, active, inactive events if not app in last_arr: te = URLEvent(t, uid, pid, wid, "Open") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Open") session.add(te) elif not window in last_arr[app]['windows']: te = URLEvent(t, uid, pid, wid, "Open") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Active") session.add(te) elif not tab in last_arr[app]['windows'][window]['tabs']: te = URLEvent(t, uid, pid, wid, "Open") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Active") session.add(te) elif t_active and (not last_arr[app]['windows'][window]['tabs'][tab]['active'] or t_url != last_arr[app]['windows'][window]['tabs'][tab]['url']): te = URLEvent(t, uid, pid, wid, "Active") session.add(te) # create open and active events if... # this app was not even open the last time around if not app in last_arr: we = WindowEvent(t, wid, "Open") session.add(we) if w_active: we = WindowEvent(t, wid, "Active") session.add(we) else: # or if the window was not present last time if not window in last_arr[app]['windows']: we = WindowEvent(t, wid, "Open") session.add(we) if w_active: we = WindowEvent(t, wid, "Active") session.add(we) else: # or the window was present but not active last time, or had a different name if w_active and (not last_arr[app]['windows'][window]['active'] or title != last_arr[app]['windows'][window]['name']): we = WindowEvent(t, wid, "Active") session.add(we) # add new arrangement to the database arr_to_add = Arrangement(t, str(arrangement)) session.add(arr_to_add) # look now at the last arrangement to see what has closed or gone inactive for app, value in last_arr.iteritems(): app_name = value['name'] active = value['active'] windows = value['windows'] if app_name not in app_names: # add app to the database app_to_add = App(t, app_name) session.add(app_to_add) #TODO catch if our commit fails activity_tracker.storage.sqlcommit() # update our local app list apps = session.query(App).all() app_names = [a.name for a in apps] pid = app_names.index(app_name) + 1 # array starts at 0, database ids a 1 for window, val in windows.iteritems(): # get window information title = val['name'] w_active = val['active'] bounds = val['bounds'] x = bounds['x'] y = bounds['y'] w = bounds['width'] h = bounds['height'] if title not in window_names: # add app to the database app_to_add = Window(t, pid, title) session.add(app_to_add) #TODO catch if our commit fails activity_tracker.storage.sqlcommit() # update our local app list windows = session.query(Window).all() window_names = [w.title for w in windows] wid = window_names.index(title) + 1 # array starts at 0, database ids a 1 if 'tabs' in val.keys(): tabs = val['tabs'] for tab, tval in tabs.iteritems(): uid = urls.index(tval['url']) + 1 t_active = tval['active'] t_url = tval['url'] if not app in arrangement: te = URLEvent(t, uid, pid, wid, "Close") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Inactive") session.add(te) elif not window in arrangement[app]['windows']: te = URLEvent(t, uid, pid, wid, "Close") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Inactive") session.add(te) elif not tab in arrangement[app]['windows'][window]['tabs']: te = URLEvent(t, uid, pid, wid, "Close") session.add(te) if t_active: te = URLEvent(t, uid, pid, wid, "Inactive") session.add(te) elif t_active and (not arrangement[app]['windows'][window]['tabs'][tab]['active'] or t_url != arrangement[app]['windows'][window]['tabs'][tab]['url']): te = URLEvent(t, uid, pid, wid, "Inactive") session.add(te) # create close and inactive events if... # this app is not longer present if not app in arrangement: we = WindowEvent(t, wid, "Close")# create open event session.add(we) if w_active: we = WindowEvent(t, wid, "Inactive")# create open event session.add(we) else: # or if the window is not longer present if not window in arrangement[app]['windows']: we = WindowEvent(t, wid, "Close")# create open event session.add(we) if w_active: we = WindowEvent(t, wid, "Inactive")# create open event session.add(we) else: # or the window is present but no longer active, or has a different name if w_active and (not arrangement[app]['windows'][window]['active'] or title != arrangement[app]['windows'][window]['name']): we = WindowEvent(t, wid, "Inactive")# create open event session.add(we) last_arr = copy.deepcopy(arrangement) except: print "Could not save " + str(line) + " to the database. Saving for the next round of parsing." lines_to_save.append(line) # write lines that did not make it into the database to the start of the # file and delete the rest of the file f.seek(0) for line in lines_to_save: f.write(line) f.truncate() f.close()
import setup import json import datetime from models import App from util import EJECT_SYMBOL with open("bot-config.json", encoding="utf8") as o: botconfigs = json.loads(o.read()) bot = discord.Client(intents=discord.Intents( guilds=True, guild_messages=True, guild_reactions=True, members=True)) metro = setup.setup(bot) # All extra attributes defined here bot.metro_state = App(metro) bot.working_guild = None @bot.event async def on_ready(): print( f"Running on {bot.user.name}#{bot.user.discriminator} ({bot.user.id})") working_guild = bot.get_guild(botconfigs["working-guild"]) if not working_guild: raise ValueError("Working Guild not found!") bot.working_guild = working_guild if bot.metro_state.started == None:
import json from mydb import db from models import App db.drop_all() db.create_all() with open("apps.json") as f: apps_from_json = json.loads(f.read()) for app, infos in apps_from_json.items(): a = App(name=app, level=infos["level"], url=infos["git"]["url"]) db.session.add(a) db.session.commit() apps_level_3 = App.query().filter(App.level == 3) for app in apps_level_3: print(app.name)
def post_tests(): if request.method == "POST": body = request.json if request.content_type == "application/json" else request.form app_id = None test_type_id = None # Checks that app info was given in the body if (not ('application' in body) or not body['application']): return jsonify({ 'success': False, 'message': "No application given" }), 404 try: # Grab the app id if the app is already in the db, or add it to the db and get its id app_info = App.query.filter_by(app=body['application']).first() if (app_info): app_id = app_info.app_id else: app_info = App(body['application']) db.session.add(app_info) db.session.flush() app_id = app_info.app_id # checks that a test type was given in the body if (not ('test_type' in body) or not body['test_type']): return jsonify({ 'success': False, 'message': "No test_type given" }), 404 # Grab the id for test type if it is already in the db, or add the test type and get its id test_type_info = Test_Type.query.filter_by( test_type=body['test_type']).first() if (test_type_info): test_type_id = test_type_info.test_type_id else: test_type_info = Test_Type(body['test_type']) db.session.add(test_type_info) db.session.flush() test_type_id = test_type_info.test_type_id # for every test in the json add it to the database. If it is already in the database instead of adding it update it tests = body['tests'] tests_badly_formatted = [] tests_total = 0 tests_entered = 0 for test in tests: # if not in proper format do not add to database, and sendback its formatted wrong tests_total = tests_total + 1 if (not ('test' in test) or not test['test']): tests_badly_formatted.append(test) continue test_name = test['test'] execution_time = test[ 'execution_time'] if 'execution_time' in test and test[ 'execution_time'] else None test_status = test[ 'result'] if 'result' in test and test['result'] else None entry_date = datetime.today() # if the test is already in the database update its information affected_row = Test.query.filter_by(test=test_name, app_id=app_id).update({ 'execution_time': execution_time, 'test_status': test_status, 'times_run': (Test.times_run + 1), 'entry_date': entry_date }) temp_test_run = None # if no rows were affected on the update then it is not already in the database so add it and create a new test_run entry if (affected_row < 1): temp_test = Test(app_id, test_type_id, test['test'], test['execution_time'], entry_date, test['result']) db.session.add(temp_test) db.session.flush() test_id = temp_test.test_id temp_test_run = Test_Run(test_id, temp_test.execution_time, temp_test.entry_date, temp_test.test_status) # find the test_id of the test and create a new test_run entry else: test_id = db.session.query( Test.test_id).filter_by(test=test['test']).first()[0] temp_test_run = Test_Run(test_id, test['execution_time'], entry_date, test['result']) # add test_run entry db.session.add(temp_test_run) tests_entered = tests_entered + 1 db.session.commit() except Exception as e: print(f"error: {e}", flush=True) return jsonify({ "success": False, "message": "Error: failed to to process data. Please try again" }), 200 finally: db.session.close() else: # send a 404 error on bad data requests abort(404) if (len(tests_badly_formatted) > 0): return jsonify({ "success": True, "message": f"{tests_entered} tests have been logged out of {tests_total}", "tests_badly_formatted": tests_badly_formatted }), 200 return jsonify({ "success": True, "message": f"{tests_entered} tests have been logged out of {tests_total}" }), 200
def instance(cls): app = cls.all().get() if not app: app = App() app.put() return app