本文整理汇总了Python中neubot.database.DATABASE类的典型用法代码示例。如果您正苦于以下问题:Python DATABASE类的具体用法?Python DATABASE怎么用?Python DATABASE使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DATABASE类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: speedtest_store
def speedtest_store(self, message):
''' Saves the results of a speedtest test '''
DATABASE.connect()
if DATABASE.readonly:
logging.warning('backend_neubot: readonly database')
return
table_speedtest.insert(DATABASE.connection(), message)
示例2: main
def main(args):
''' Main function '''
try:
options, arguments = getopt.getopt(args[1:], 'f:nv')
except getopt.error:
sys.exit(USAGE)
database_path = system.get_default_database_path()
auto_discover = True
for name, value in options:
if name == '-f':
database_path = value
elif name == '-n':
auto_discover = False
elif name == '-v':
CONFIG['verbose'] = 1
if len(arguments) != 1 and len(arguments) != 2:
sys.exit(USAGE)
DATABASE.set_path(database_path)
CONFIG.merge_database(DATABASE.connection())
if len(arguments) == 2:
RUNNER_TESTS.update({arguments[0]: [arguments[1]]})
ctx = {'uri': arguments[1]}
else:
ctx = None
deferred = Deferred()
deferred.add_callback(lambda param: None)
RUNNER_CORE.run(arguments[0], deferred, auto_discover, ctx)
POLLER.loop()
示例3: main
def main(args):
''' Main function '''
try:
options, arguments = getopt.getopt(args[1:], 'f:n')
except getopt.error:
sys.exit('Usage: %s [-n] [-f database] test [negotiate_uri]' % args[0])
if len(arguments) != 1 and len(arguments) != 2:
sys.exit('Usage: %s [-n] [-f database] test [negotiate_uri]' % args[0])
database_path = system.get_default_database_path()
auto_rendezvous = True
for name, value in options:
if name == '-f':
database_path = value
elif name == '-n':
auto_rendezvous = False
DATABASE.set_path(database_path)
CONFIG.merge_database(DATABASE.connection())
if len(arguments) == 2:
RUNNER_TESTS.update({arguments[0]: [arguments[1]]})
ctx = {'uri': arguments[1]}
else:
ctx = None
RUNNER_CORE.run(arguments[0], lambda *args: None, auto_rendezvous, ctx)
POLLER.loop()
示例4: main
def main(args):
""" Main function """
if not system.has_enough_privs():
sys.exit('FATAL: you must be root')
common.main("agent", "Run in background, periodically run tests", args)
conf = CONFIG.copy()
privacy.complain_if_needed()
BACKEND.use_backend("neubot")
BACKEND.datadir_init()
# FIXME We're ignoring agent.api.{address,port} that are now
# deprecated and should be removed soon.
background_api.start_api()
if conf["agent.daemonize"]:
LOG.redirect()
system.go_background()
if conf["agent.use_syslog"]:
LOG.redirect()
#
# When we run as an agent we also save logs into
# the database, to easily access and show them via
# the web user interface.
#
LOG.use_database()
logging.info('%s for POSIX: starting up', utils_version.PRODUCT)
system.drop_privileges()
if os.getuid() == 0 or os.geteuid() == 0:
logging.error('agent: still running as root')
os._exit(1)
if conf["agent.rendezvous"]:
BACKGROUND_RENDEZVOUS.start()
POLLER.loop()
logging.info('%s for POSIX: shutting down', utils_version.PRODUCT)
LOG.writeback()
#
# Make sure that we do not leave the database
# in an inconsistent state.
#
DATABASE.close()
示例5: __create_empty
def __create_empty(path):
'''
This functions creates an empty Neubot database at @path,
jusing Neubot internals to do that.
'''
DATABASE.set_path(path)
connection = DATABASE.connection()
connection.commit()
connection.close()
示例6: main
def main(args):
""" Main function """
try:
options, arguments = getopt.getopt(args[1:], "6A:np:vy")
except getopt.error:
sys.exit("usage: neubot skype [-6nvy] [-A address] [-p port]")
if arguments:
sys.exit("usage: neubot skype [-6nvy] [-A address] [-p port]")
prefer_ipv6 = 0
# address = 'master.neubot.org'
address = "localhost"
runner = 1
port = 8080
noisy = 0
fakeprivacy = 0
for name, value in options:
if name == "-6":
prefer_ipv6 = 1
elif name == "-A":
address = value
elif name == "-n":
runner = 0
elif name == "-p":
port = int(value)
elif name == "-v":
noisy = 1
elif name == "-y":
fakeprivacy = 1
if os.path.isfile(DATABASE.path):
DATABASE.connect()
CONFIG.merge_database(DATABASE.connection())
else:
logging.warning("skype: database file is missing: %s", DATABASE.path)
BACKEND.use_backend("null")
if noisy:
log.set_verbose()
if runner:
result = runner_clnt.runner_client(
CONFIG["agent.api.address"], CONFIG["agent.api.port"], CONFIG["verbose"], "skype"
)
if result:
sys.exit(0)
logging.info("skype: running the test in the local process context...")
if not fakeprivacy and not privacy.allowed_to_run():
privacy.complain()
logging.info("skype: otherwise use -y option to temporarily provide " "privacy permissions")
sys.exit(1)
handler = SkypeNegotiate()
handler.connect((address, port), prefer_ipv6, 0, {})
POLLER.loop()
示例7: main
def main(name, descr, args):
Eflag = False
lflag = False
try:
options, arguments = getopt.getopt(args[1:], "D:Ef:lVv", ["help"])
except getopt.GetoptError:
write_help(sys.stderr, name, descr)
sys.exit(1)
if arguments:
write_help(sys.stderr, name, descr)
sys.exit(1)
verbose = 0
for key, value in options:
if key == "-D":
# No shortcuts because it grows too confusing
CONFIG.register_property(value)
elif key == "-E":
Eflag = True
elif key == "-f":
DATABASE.set_path(value)
elif key == "--help":
write_help(sys.stdout, name, descr)
sys.exit(0)
elif key == "-l":
lflag = True
elif key == "-V":
sys.stdout.write(VERSION + "\n")
sys.exit(0)
elif key == "-v":
verbose = 1
DATABASE.connect()
CONFIG.merge_database(DATABASE.connection())
if not Eflag:
CONFIG.merge_environ()
CONFIG.merge_properties()
# Apply the setting after we've read database and environment
if verbose:
CONFIG['verbose'] = 1
if lflag:
CONFIG.print_descriptions(sys.stdout)
sys.exit(0)
示例8: main
def main(args):
''' Main() function '''
try:
options, arguments = getopt.getopt(args[1:], '')
except getopt.error:
sys.exit('usage: neubot background_win32')
if options or arguments:
sys.exit('usage: neubot background_win32')
# Read settings from database
CONFIG.merge_database(DATABASE.connection())
#
# Save logs into the database, to easily access
# and show them via the web user interface.
#
LOG.use_database()
# Complain if privacy settings are not OK
privacy.complain_if_needed()
background_api.start('127.0.0.1 ::1', '9774')
BACKGROUND_RENDEZVOUS.start()
__start_updater()
POLLER.loop()
示例9: got_response_collecting
def got_response_collecting(self, stream, request, response):
logging.info("BitTorrent: collecting ... done")
if self.success:
#
# Always measure at the receiver because there is more
# information at the receiver and also to make my friend
# Enrico happier :-P.
# The following is not a bug: it's just that the server
# returns a result using the point of view of the client,
# i.e. upload_speed is _our_ upload speed.
#
m = json.loads(response.body.read())
self.my_side["upload_speed"] = m["upload_speed"]
upload = utils.speed_formatter(m["upload_speed"])
STATE.update("test_progress", "100%", publish=False)
STATE.update("test_upload", upload)
logging.info('BitTorrent: upload speed: %s', upload)
if privacy.collect_allowed(self.my_side):
if DATABASE.readonly:
logging.warning('bittorrent_client: readonly database')
else:
table_bittorrent.insert(DATABASE.connection(), self.my_side)
# Update the upstream channel estimate
target_bytes = int(m["target_bytes"])
if target_bytes > 0:
estimate.UPLOAD = target_bytes
self.final_state = True
stream.close()
示例10: collect
def collect(self, m):
btid = _make_btid(m["ident"])
if btid not in AUTH_PEERS:
raise NegotiatorEOF()
d = m["request_body"]
result = AUTH_PEERS[btid]
#
# Note that the following is not a bug: it's just that
# the server saves results using the point of view of the
# client, i.e. upload_speed _is_ client's upload speed.
#
d["timestamp"] = result["timestamp"]
d["upload_speed"] = result["upload_speed"]
if privacy.collect_allowed(d):
table_bittorrent.insert(DATABASE.connection(), d)
#
# After we've saved the result into the dictionary we
# can add extra information we would like to return to
# the client.
#
d["target_bytes"] = result["target_bytes"]
m["response_body"] = d
示例11: got_response_collecting
def got_response_collecting(self, stream, request, response):
LOG.complete()
if self.success:
#
# Always measure at the receiver because there is more
# information at the receiver and also to make my friend
# Enrico happier :-P.
# The following is not a bug: it's just that the server
# returns a result using the point of view of the client,
# i.e. upload_speed is _our_ upload speed.
#
m = json.loads(response.body.read())
self.my_side["upload_speed"] = m["upload_speed"]
upload = utils.speed_formatter(m["upload_speed"])
STATE.update("test_upload", upload)
if privacy.collect_allowed(self.my_side):
table_bittorrent.insert(DATABASE.connection(), self.my_side)
# Update the upstream channel estimate
target_bytes = int(m["target_bytes"])
if target_bytes > 0:
estimate.UPLOAD = target_bytes
stream.close()
示例12: api_data
def api_data(stream, request, query):
''' Get data stored on the local database '''
since, until = -1, -1
test = ''
dictionary = cgi.parse_qs(query)
if "test" in dictionary:
test = str(dictionary["test"][0])
if "since" in dictionary:
since = int(dictionary["since"][0])
if "until" in dictionary:
until = int(dictionary["until"][0])
if test == 'bittorrent':
table = table_bittorrent
elif test == 'speedtest':
table = table_speedtest
elif test == 'raw':
table = table_raw
else:
raise NotImplementedTest("Test not implemented")
indent, mimetype, sort_keys = None, "application/json", False
if "debug" in dictionary and utils.intify(dictionary["debug"][0]):
indent, mimetype, sort_keys = 4, "text/plain", True
response = Message()
lst = table.listify(DATABASE.connection(), since, until)
body = json.dumps(lst, indent=indent, sort_keys=sort_keys)
response.compose(code="200", reason="Ok", body=body, mimetype=mimetype)
stream.send_response(request, response)
示例13: listify
def listify(self):
if self._use_database:
lst = table_log.listify(DATABASE.connection())
lst.extend(self._queue)
return lst
else:
return []
示例14: _api_config
def _api_config(self, stream, request, query):
response = Message()
indent, mimetype, sort_keys = None, "application/json", False
dictionary = cgi.parse_qs(query)
if "debug" in dictionary and utils.intify(dictionary["debug"][0]):
indent, mimetype, sort_keys = 4, "text/plain", True
if request.method == "POST":
s = request.body.read()
updates = qs_to_dictionary(s)
privacy.check(updates)
# Very low barrier to prevent damage from kiddies
if "agent.interval" in updates:
interval = int(updates["agent.interval"])
if interval < 1380 and interval != 0:
raise ConfigError("Bad agent.interval")
CONFIG.merge_api(updates, DATABASE.connection())
STATE.update("config", updates)
# Empty JSON b/c '204 No Content' is treated as an error
s = "{}"
else:
s = json.dumps(CONFIG.conf, sort_keys=sort_keys, indent=indent)
stringio = StringIO.StringIO(s)
response.compose(code="200", reason="Ok", body=stringio,
mimetype=mimetype)
stream.send_response(request, response)
示例15: api_results
def api_results(stream, request, query):
''' Provide results for queried tests '''
since, until = -1, -1
test = ''
dictionary = cgi.parse_qs(query)
if dictionary.has_key("test"):
test = str(dictionary["test"][0])
if dictionary.has_key("since"):
since = int(dictionary["since"][0])
if dictionary.has_key("until"):
until = int(dictionary["until"][0])
if test == 'bittorrent':
table = table_bittorrent
elif test == 'speedtest':
table = table_speedtest
else:
raise NotImplementedTest("Test '%s' is not implemented" % test)
indent, mimetype, sort_keys = None, "application/json", False
if "debug" in dictionary and utils.intify(dictionary["debug"][0]):
indent, mimetype, sort_keys = 4, "text/plain", True
response = Message()
lst = table.listify(DATABASE.connection(), since, until)
body = json.dumps(lst, indent=indent, sort_keys=sort_keys)
response.compose(code="200", reason="Ok", body=body, mimetype=mimetype)
stream.send_response(request, response)