def main(options): config = collections.namedtuple( 'Config', ('node_name', 'port', 'threshold', 'validators', 'faulty_percent'), )(uuid.uuid1().hex, 8001, 51, [], 0) if not pathlib.Path(options.conf).exists(): parser.error('conf file, `%s` does not exists.' % options.conf) if not pathlib.Path(options.conf).is_file(): parser.error('conf file, `%s` is not valid file.' % options.conf) conf = configparser.ConfigParser() conf.read(options.conf) log.info('conf file, `%s` was loaded', options.conf) config = config._replace(node_name=conf['node']['name']) config = config._replace(port=int(conf['node']['port'])) config = config._replace(threshold=int(conf['node']['threshold_percent'])) if conf.has_option('faulty', 'faulty_percent'): config = config._replace( faulty_percent=int(conf['faulty']['faulty_percent'])) log.debug('loaded conf: %s', config) validator_list = [] for i in filter(lambda x: len(x.strip()) > 0, conf['node']['validator_list'].split(',')): validator_list.append(Endpoint.from_uri(i.strip())) config = config._replace(validators=validator_list) log.debug('Validators: %s' % config.validators) node = node_factory( config.node_name, Endpoint(NETWORK_MODULE.SCHEME, get_local_ipaddress(), config.port), config.faulty_percent) transport = NETWORK_MODULE.Transport(bind=('0.0.0.0', config.port)) # consensus_module = get_fba_module('isaac') consensus = SCP( node, config.threshold, tuple(map(lambda x: Node(x.extras['name'], x), config.validators)), transport, ) log.metric(node=node.name, data=node.to_dict()) application = Application(consensus, transport) application.start() base_server = BaseServer(application) base_server.start() return
def runserver(dbpath, host, port, logpath): dbpool = ConnectionPool('sqlite3', dbpath, check_same_thread=False) app = Application(dbpool) print('Database: %s' % (dbpath)) if logpath: logfile = open(logpath, 'a') print('Log File: %s' % (logpath)) else: logfile = None print('Host: %s\nPort: %d\n' % (host, port)) app.run(host, port, logfile)
def application(): id = int(request.form['id']) name = request.form['name'] verb = request.form['verb'] if verb == 'create': application = Application(name) db.session.add(application) else: application = Application.query.filter_by(id=id).first() if (verb == 'delete'): db.session.delete(application) elif (verb == 'edit'): new_name = request.form['newName'] application.name = new_name name = new_name db.session.commit() id = application.id return jsonify(id=id, name=name, verb=verb, parent_id=-1, success = True, type = 'application', parent_type = 'none')
class TestVoteAPI(TestCase): app = Application(MagicMock()) def test_json_welcome(self): request = MagicMock() request.requestHeaders.getRawHeaders.return_value = 'application/json' response = self.app.welcome(request) request.setHeader.assert_called_with('Content-Type', 'application/json') self.assertEquals( json.loads(response)['message'], 'Welcome to the Vote App') def test_html_welcome(self): request = MagicMock() request.requestHeaders.getRawHeaders.return_value = 'text/html' response = self.app.welcome(request) request.setHeader.assert_called_with('Content-Type', 'text/html') self.assertEquals(response, '<h1>Welcome to the Vote App</h1>')
def test(): this_app = Application('Test App') db.session.add(this_app) db.session.commit() actorGoals = { 'Users' : ['Communicate online', 'Protect privacy'], 'Developers' : ['Develop systems that are functional', 'Develop systems that are usable', 'Develop systems that are safe'], 'Search engines' : ['Make all information on the web easily accessible to every web user', 'Follow sites directives with regards to spidering and storing information'], 'Public' : ['Find useful information online'], 'Internet service providers' : ['Offer reliable, affordable service', 'Abide by local laws and regulations'], 'Governments' : ['Ensure safety and security'] } actorData = { 'Users' : [ 'Username', 'First & last name', 'Email address', 'Password', 'Private messages', 'Clickstream activity'] } users = Actor('Users', this_app.id) developers = Actor('Developers', this_app.id) search = Actor('Search engines', this_app.id) public = Actor('Public', this_app.id) isp = Actor('Internet service providers', this_app.id) govt = Actor('Governments', this_app.id) actors = [users, developers, search, public, isp, govt] for actor in actors: db.session.add(actor) db.session.commit() if actor.name in actorGoals: for goal in actorGoals[actor.name]: db.session.add(Goal(goal, actor.id)) if actor.name in actorData: for datum in actorData[actor.name]: db.session.add(Datum(datum, actor.id)) db.session.commit() return 'Success: test'
def setUp(self): self.app = Application()
class TestLoadData(TestCase): def setUp(self): self.app = Application() def test_create_parser(self): options = self.app.get_options(None) self.assertEquals("./example/nodes.csv", options.nodes) self.assertEquals("./example/infocenters.csv", options.infocenters) self.assertEquals("./example/requests.csv", options.requests) def test_load_matrix_data(self): file_name = "../example/nodes.csv" matrix = self.app.load_matrix_data(file_name) self.assertEqual(len(matrix), 7) def test_validate_nodes_matrix_invalid(self): self.assertRaises(ValueError, self.app.validate_nodes_matrix, [[0, 0], [0]]) self.assertRaises(ValueError, self.app.validate_nodes_matrix, [[0, 0], [1, 0]]) def test_validate_nodes_matrix_valid(self): try: matrix = [ [0, 1, 0], [1, 1, 0], [0, 0, 0] ] self.app.validate_nodes_matrix(matrix) except Exception: self.fail("Shouldn't raise exception on valid matrix") def test_validate_infocenters_invalid(self): self.app.nodes = [[0, 1], [1, 0]] self.assertRaises(ValueError, self.app.validate_infocenters_matrix, [[3, 0, 0]]) self.assertRaises(ValueError, self.app.validate_infocenters_matrix, [[1, 2, 1], [2, 4, 2]]) def test_validate_infocenters_matrix_valid(self): try: self.app.nodes = [ [0, 1, 0], [1, 1, 0], [0, 0, 0] ] self.app.validate_infocenters_matrix([[1, 5, 2]]) self.app.validate_infocenters_matrix([[1, 2, 1], [2, 4, 2]]) except Exception: self.fail("Shouldn't raise exception on valid data") def test_validate_request_matrix_valid(self): try: self.app.nodes = [ [0, 1, 0], [1, 1, 0], [0, 0, 0] ] self.app.infocenters = [[2, 5, 2]] self.app.validate_request_matrix([[3, 2, 1]]) except Exception: self.fail("Shouldn't raise exception on valid data") def test_validate_request_matrix_invalid(self): self.app.nodes = [ [0, 1, 0], [1, 1, 0], [0, 0, 0] ] self.app.infocenters = [[2, 5, 2]] self.assertRaises(ValueError, self.app.validate_request_matrix, [[4, 1, 1]]) self.assertRaises(ValueError, self.app.validate_request_matrix, [[1, 3, 1]])
db_driver = value elif "--db-user" == opt: db_user = value elif "--db-password" == opt: db_password = value elif "--db-hostname" == opt: db_hostname = value elif "--db-name" == opt: db_name = value elif "--report-file" == opt: report_filename = value elif "--no-report" == opt: report = False elif opt in ("-q", "--quiet"): quiet = True elif "--force" == opt: force = True elif "--web-user" == opt: web_user = value elif "--web-password" == opt: web_password = value elif "--compressed-dir" == opt: compressed_dir = value.rstrip('/') elif "--version" == opt: print mlstats_version sys.exit(0) myapp = Application(db_driver, db_user, db_password, db_name, db_hostname, urls, report_filename, report, quiet, force, web_user, web_password, compressed_dir)
from wsgiref.simple_server import make_server from main import Application from url import fronts from views import routes application = Application(routes, fronts) if __name__ == '__main__': with make_server('', 8000, application) as httpd: print("Serving on port 8000...") httpd.serve_forever()
def setUp(self): self._application = Application()
elif "--db-password" == opt: db_password = value elif "--db-hostname" == opt: db_hostname = value elif "--db-name" == opt: db_name = value elif "--db-admin-user" == opt: db_admin_user = value elif "--db-admin-password" == opt: db_admin_password = value elif "--report-file" == opt: report_filename = value elif "--no-report" == opt: report = False elif opt in ("-q", "--quiet"): quiet = True elif "--force" == opt: force = True elif "--web-user" == opt: web_user = value elif "--web-password" == opt: web_password = value elif "--version" == opt: print mlstats_version sys.exit(0) myapp = Application(db_driver, db_user, db_password, db_name, db_hostname, db_admin_user, db_admin_password, urls, report_filename, report, quiet, force, web_user, web_password)
import sys from main import Application app = Application(sys.argv) app.window.show() sys.exit(app.exec_())
weather_underground_api_key) command_settings = CommandSettings(command_prefixes, weather_commands, forecast_commands, help_commands) home_settings = HomeSettings(home_full_name, home_display_name, periodic_forecast_channels, morning_forecast_time, evening_forecast_time) application_settings = ApplicationSettings( logging_level, language, measurement_system, concurrency_priority, integration_settings, command_settings, home_settings) return application_settings logging.basicConfig(level=logging.INFO) configuration_factory = ConfigurationFactory() configuration = configuration_factory.create() if not configuration_factory.is_configuration_valid: logging.error( "the bot configuration is invalid, the application will NOT start") sys.exit(1) logging.basicConfig(level=configuration.logging_level) Application(configuration).run()
def start(): args = argparse.ArgumentParser(description=name) args.add_argument('url', nargs='+', help='Urls of the archive web pages of the mailing ' 'list. If they are a local dir instead of a ' 'remote URL, the directory will be recursively ' 'scanned for mbox files. If the option \'-\' is ' 'passed instead of a URL(s), the URLs will be ' 'read from the standard input.') argen = args.add_argument_group('General options') argen.add_argument('-q', '--quiet', action='store_true', default=os.getenv('MLSTATS_QUIET', False), help='Do not show messages about the progress in the ' 'retrieval and analysis of the archives.' 'Environment variable "MLSTATS_QUIET"') argen.add_argument('--version', action='version', version=mlstats_version, help='Show the version number and exit.') argen.add_argument('--force', action='store_true', default=os.getenv('MLSTATS_FORCE', False), help='Force mlstats to download even the ' 'mboxes/messages already found locally for a ' 'given URL. This option is only valid for remote ' 'links. For Gmane links, it overrides the offset ' 'value to 0.' 'Environment variable "MLSTATS_FORCE"') argen.add_argument('--compressed-dir', default=os.getenv('MLSTATS_COMPRESSED_DIR', None), help='Path to a folder where the archives of the ' 'mailing list will be stored.' 'Environment variable "MLSTATS_COMPRESSED_DIR"' ) argrp = args.add_argument_group('Report options') argrp.add_argument('--report-file', default=os.getenv('MLSTATS_REPORT_FILENAME', ''), help='Filename for the report generated after the ' 'analysis (default is standard output) WARNING: ' 'The report file will be overwritten if already ' 'exists.' 'Environment variable "MLSTATS_REPORT_FILENAME"') argrp.add_argument('--no-report', action='store_true', default=os.getenv('MLSTATS_REPORT', True), help='Do not generate a report after the retrieval ' 'and parsing of the archives.' 'Environment variable "MLSTATS_REPORT"') argweb = args.add_argument_group('Private archive options') argweb.add_argument('--web-user', default=os.getenv('MLSTATS_WEB_USERNAME', None), help='If the archives of the mailing list are ' 'private, use this username to login in order ' 'to retrieve the files.' 'Environment variable "MLSTATS_WEB_USERNAME"') argweb.add_argument('--web-password', default=os.getenv('MLSTATS_WEB_PASSWORD', None), help='If the archives of the mailing list are ' 'private, this password to login in order to ' 'retrieve the files.' 'Environment variable "MLSTATS_WEB_PASSWORD"') argdb = args.add_argument_group('Database options') argdb.add_argument('--db-driver', default=os.getenv('MLSTATS_DB_DRIVER', 'mysql'), help='Database backend: mysql, postgres, or sqlite ' '(default is mysql)' 'Environment variable "MLSTATS_DB_DRIVER"') argdb.add_argument('--db-user', default=os.getenv('MLSTATS_DB_USERNAME', None), help='Username to connect to the database' 'Environment variable "MLSTATS_DB_USERNAME"') argdb.add_argument('--db-password', default=os.getenv('MLSTATS_DB_PASSWORD', None), help='Password to connect to the database' 'Environment variable "MLSTATS_DB_PASSWORD"') argdb.add_argument('--db-name', default=os.getenv('MLSTATS_DB_NAME', 'mlstats'), help='Name of the database that contains data ' 'previously analyzed' 'Environment variable "MLSTATS_DB_NAME"') argdb.add_argument('--db-hostname', default=os.getenv('MLSTATS_DB_HOSTNAME', None), help='Name of the host with a database server running' 'Environment variable "MLSTATS_DB_HOSTNAME"') argbnd = args.add_argument_group('Backend options') argbnd.add_argument('--backend', default=os.getenv('MLSTATS_BACKEND', None), help='Mailing list backend for remote repositories: ' 'gmane, mailman, or webdirectory. (default is ' 'autodetected for gmane and mailman)' 'Environment variable "MLSTATS_BACKEND"') argbnd.add_argument('--offset', default=os.getenv('MLSTATS_OFFSET', 0), type=int, help='Start from a given message. Only works with the ' 'gmane, backend. (default is 0) ' 'Environment variable "MLSTATS_OFFSET"') opts = args.parse_args() if '-' in opts.url: # Read URLs from standard input instead of command line urls = [url.strip() for url in sys.stdin.readlines()] else: urls = opts.url db_driver = opts.db_driver db_user = opts.db_user db_password = opts.db_password db_hostname = opts.db_hostname db_name = opts.db_name web_user = opts.web_user web_password = opts.web_password compressed_dir = opts.compressed_dir report_filename = opts.report_file report = not opts.no_report quiet = opts.quiet force = opts.force backend = opts.backend offset = opts.offset myapp = Application(db_driver, db_user, db_password, db_name, db_hostname, urls, report_filename, report, quiet, force, web_user, web_password, compressed_dir, backend, offset)
class TestVoteAPI(TestCase): database = MagicMock() app = Application(database) def setUp(self): self.client = KleinResourceTester(router=self.app.router, base_url='https://example.com') self.candidates = self.app.vote_api.candidates = MagicMock() self.votes = self.app.vote_api.votes = MagicMock() def test_get_candidates(self): """ Get a list of all the candidates """ d = defer.Deferred() self.votes.all_vote_totals.return_value = d values = [(1, 'Batman', None), (2, 'Spiderman', 1), (3, 'Superman', 100)] d.callback(values) request = self.client.request('GET', '/api/candidates') @request.addCallback def verify(response): self.assertEquals(response.code, 200) content_type = response.getHeaders('Content-Type')[0] self.assertEquals(content_type, 'application/json') candidates = json.loads(response.content)['candidates'] assert len(values) == len(candidates) return request def test_get_candidates_no_votes(self): """ Candidates that have no votes returns 0 """ d = defer.Deferred() self.votes.all_vote_totals.return_value = d value = [(1, 'Lex Luther', None)] d.callback(value) request = self.client.request('GET', '/api/candidates') @request.addCallback def verify(response): candidates = candidates = json.loads( response.content)['candidates'][0] for _id, name, votes in value: self.assertEquals(candidates['id'], _id) self.assertEquals(candidates['name'], name) self.assertEquals(candidates['votes'], 0) return request def test_add_candidate(self): """ Add a candidate """ form_data = {'candidate': 'Kal El'} request = self.client.request( method='POST', uri='/api/candidate', headers={'Content-Type': 'application/x-www-form-urlencoded'}, params=form_data) @request.addCallback def verify(response): self.assertEquals(response.code, 201) self.assertEquals( response.getHeaders('Content-Type')[0], 'application/json') content = json.loads(response.content) self.assertEquals(content['status'], 'Created') return request def test_add_candidate_no_name(self): """ Error when no candidate name is passed when adding a candidate """ request = self.client.request(method='POST', uri='/api/candidate') @request.addCallback def verify(response): self.assertEquals(response.code, 412) self.assertEquals( response.getHeaders('Content-Type')[0], 'application/json') content = json.loads(response.content) self.assertEquals(content['status'], 'Missing Prerequisite Input') return request def test_page_not_found(self): """ Return a particular output when a page/endpoint isn't available. """ request = self.client.request(method='GET', uri='/api/doesnt_exist') @request.addCallback def verify(response): self.assertEquals(response.code, 404) self.assertEquals( response.getHeaders('Content-Type')[0], 'application/json') content = json.loads(response.content) self.assertEquals(content['status'], 'Resource Not Available') return request def test_vote_for(self): """ Vote for a particular candidate """ form_data = {'id': 100} request = self.client.request( method='POST', uri='/api/vote', headers={'Content-Type': 'application/x-www-form-urlencoded'}, params=form_data) @request.addCallback def verify(response): self.assertEquals(response.code, 200) self.assertEquals( response.getHeaders('Content-Type')[0], 'application/json') content = json.loads(response.content) self.assertEquals(content['status'], 'Success') # verify form data is converting into int # and db functions are properly called args, kwargs = self.votes.vote_for.call_args self.assertIsInstance(args[0], int) self.votes.vote_for.assert_called_with(form_data['id']) self.assertEquals(args[0], form_data['id']) return request def test_vote_for_id_not_int(self): """ Status code 412 returned when id is not an int """ def invalid_vote(candidate_id): """ :return: `Deferred`/`Response` """ form_data = {'id': candidate_id} request = self.client.request( method='POST', uri='/api/vote', headers={'Content-Type': 'application/x-www-form-urlencoded'}, params=form_data) request.addCallback(verify) return request def verify(response): """ Verification callback """ self.assertEquals(response.code, 412) self.assertEquals( response.getHeaders('Content-Type')[0], 'application/json') content = json.loads(response.content) self.assertEquals(content['status'], 'Invalid User Input') invalid_ids = ['one', '1 hundred', ''] deferred_list = [] for invalid in invalid_ids: d = invalid_vote(invalid) deferred_list.append(d) return defer.gatherResults(deferred_list)
''' Created on Jul 22, 2019 @author: arthurquites ''' from main import Application from tkinter import Tk if __name__ == '__main__': root = Tk() app = Application(master=root) app.mainloop()
def test_download_data(self): result = Application.download_data(Application.POSTS_URL) self.assertIsInstance(result, list)
#!/usr/bin/env python # -*- coding:utf-8 -*- import bcrypt import getpass import sys from main import Application, options email = raw_input("Admin email: ") username = raw_input("Admin username: "******"Admin password: "******"Admin password again: ") if password != password2: print "Password doesn\'t match." sys.exit() hashed_password = bcrypt.hashpw(password, bcrypt.gensalt()) conn = Application() conn.db.execute( "INSERT INTO authors(email, name, role, hashed_password)" "VALUES (%s, %s, %s, %s)", email, username, options.admin_role, hashed_password) print "Create admin user %s success." % email
def _deco(cls): Application.add_handlers(r".*", [(route_path, cls)]) return cls
def setUpClass(cls, Label): # TODO The following lines should not be here in a proper unit test case cls.application = Application() cls.application.set_new_language(Languages.ENGLISH.value) cls.new_game_panel = NewGamePanel(cls.application)
def decorated_function(*args, **kwargs): return f(*args, **kwargs) if Application.is_authorized() else redirect( url_for('front.login', r=request.url))