def test_good_key():
    """validating check_key logic"""
    connection = TinyMongoClient(CACHE_PATH)
    api_db = connection.prosperAPI.users
    vals = api_db.find()

    if not vals:
        global DO_API_TESTS
        DO_API_TESTS = False
        pytest.xfail('Unable to test without test keys')

    test_key = vals['api_key']
    assert api_utils.check_key(test_key)

    new_vals = api_db.find_one({'api_key': test_key})

    # TODO: fails on virgin key
    old_time = datetime.strptime(
        vals['last_accessed'],
        '%Y-%m-%dT%H:%M:%S.%f').timestamp()

    new_time = datetime.strptime(
        new_vals['last_accessed'],
        '%Y-%m-%dT%H:%M:%S.%f').timestamp()

    assert new_time > old_time
    connection.close()
def test_good_key():
    """validating check_key logic"""
    connection = TinyMongoClient(CACHE_PATH)
    api_db = connection.prosperAPI.users
    vals = api_db.find()

    if not vals:
        global DO_API_TESTS
        DO_API_TESTS = False
        pytest.xfail('Unable to test without test keys')

    test_key = vals['api_key']
    assert api_utils.check_key(test_key)

    new_vals = api_db.find_one({'api_key': test_key})

    # TODO: fails on virgin key
    old_time = datetime.strptime(vals['last_accessed'],
                                 '%Y-%m-%dT%H:%M:%S.%f').timestamp()

    new_time = datetime.strptime(new_vals['last_accessed'],
                                 '%Y-%m-%dT%H:%M:%S.%f').timestamp()

    assert new_time > old_time
    connection.close()
def test_get_api_key():
    """fetch api key from cache for testing"""
    global TEST_API_KEY
    connection = TinyMongoClient(CACHE_PATH)
    api_db = connection.prosperAPI.users
    vals = api_db.find()

    if not vals:
        pytest.xfail('Unable to test without test keys')

    test_key = vals['api_key']
    connection.close()
    TEST_API_KEY = test_key
Exemple #4
0
def check_key(
        api_key,
        throw_on_fail=False,
        logger=LOGGER
):
    """check if API key is valid

    Args:
        api_key (str): given API key
        cache_path (str, optional): override for cache_path
        api_file (str, optional): tinydb filename
        throw_on_fail (bool, optional): raise exception if API key fails
        logger (:obj:`logging.logger`): logging handle

    Returns:
        (bool) access allowed or not

    """
    
    # Connect to TinyMongoDB and use prosperAPI DB 
    connection = TinyMongoClient(CACHE_PATH)
    userdb = connection.prosperAPI.users
    api_value = userdb.find_one({'api_key': api_key})

    access_allowed = False
    if api_value:
        logger.info(
            'accessed service - {0}:{1}'.format(
                api_value['user_name'],
                api_value['user_info']
            )
        )
        logger.debug(api_value)
        currenttime = datetime.now().isoformat()
        userdb.update(
            {'api_key': api_key},
            {
                '$set': {'last_accessed': currenttime}
            }
            )
        connection.close()
        access_allowed = True
    else:
        logger.warning('Invalid API key: {0}'.format(api_key))
        if throw_on_fail:
            raise exceptions.APIKeyInvalid(
                status=401,
                message='Invalid API key'
            )

    return access_allowed
Exemple #5
0
def check_key(
        api_key,
        throw_on_fail=False,
        logger=logging.getLogger('publicAPI')
):
    """check if API key is valid

    Args:
        api_key (str): given API key
        cache_path (str, optional): override for cache_path
        api_file (str, optional): tinydb filename
        throw_on_fail (bool, optional): raise exception if API key fails
        logger (:obj:`logging.logger`): logging handle

    Returns:
        bool: access allowed or not

    """

    # Connect to TinyMongoDB and use prosperAPI DB
    connection = TinyMongoClient(CACHE_PATH)
    userdb = connection.prosperAPI.users
    api_value = userdb.find_one({'api_key': api_key})

    access_allowed = False
    if api_value:
        logger.info(
            'accessed service - {0}:{1}'.format(
                api_value['user_name'],
                api_value['user_info']
            )
        )
        logger.debug(api_value)
        currenttime = datetime.now().isoformat()
        userdb.update(
            {'api_key': api_key},
            {'$set': {'last_accessed': currenttime}}
        )
        connection.close()
        access_allowed = True
    else:
        logger.warning('Invalid API key: {0}'.format(api_key))
        if throw_on_fail:
            raise exceptions.APIKeyInvalid(
                status=401,
                message='Invalid API key'
            )

    return access_allowed
Exemple #6
0
    def delete(self):
        body = request.get_json()
        try:
            validate(body, schema)
        except:
            return {
                'message':
                'Input data invalid or miss some value, required: {}'.format(
                    schema['required'])
            }, 400

        # client = MongoClient(MONGO_HOST, MONGO_PORT)
        client = TinyMongoClient()
        db = client.prom
        col = db.targets
        delete_proto = {'exporter': body['exporter'], 'target': body['target']}
        find_proto = {'exporter': body['exporter']}
        col.delete_one(delete_proto)
        with open('/prom/conf/' + body['exporter'] + '.json', 'w') as f:
            targets = []
            for target in col.find(find_proto, projection={'_id': False}):
                targets.append({
                    'targets': [target['target']],
                    'labels': target.get('labels', {})
                })

            f.write(json.dumps(targets, indent=2))
            f.flush()
            os.fsync(f.fileno())
        return None, 204
Exemple #7
0
def buscaElemento_Familia(chave: str) -> Elemento:
    """Busca os elementos de determinada familia no banco de dados."""
    connection = TinyMongoClient()
    database = connection.database
    collection = database.elementos
    info = collection.find({'familia': chave})

    return info
Exemple #8
0
def buscaElemento_Nome(chave: str) -> Elemento:
    """Busca o nome do elemento no banco de dados."""
    connection = TinyMongoClient()
    database = connection.database
    collection = database.elementos
    info = collection.find_one({'nome': chave})

    return info
Exemple #9
0
def configure(app):
    """Inicia o client do TinyMongo e adiciona `app.db`
    *para usar MongoDB basta mudar para `pymongo.MongoClient`
    """
    db_folder = app.config.get('DB_FOLDER', 'database')
    db_name = app.config.get('DB_NAME', 'cms_db')
    foldername = Path(db_folder) / Path(app.root_path) / Path('database')
    client = TinyMongoClient(foldername=foldername)
    app.db = client[db_name]
Exemple #10
0
    def make_db(**collections):
        connection = TinyMongoClient(tempfile.mkdtemp())
        test_db = getattr(connection, 'banking')
        for collection_id, transactions in collections.items():
            test_collection = getattr(test_db, collection_id)
            for transaction in transactions:
                test_collection.insert_one(encode_transaction(transaction))

        return test_db
Exemple #11
0
def configure(app):
    """Creates a new database connection"""
    if app.env == "production":
        client = MongoClient(host="localhost")
    else:
        client = TinyMongoClient('database')

    db_name = app.config.get('MONGODB_NAME', 'db')
    app.db = client[db_name]
Exemple #12
0
def clear_tinymongo_cache(bool_prod=False):
    """tinymongo uses all collections in one file, clearing requires direct access"""
    if not bool_prod:
        remove(path.join(TEST_CACHE_PATH, 'prosperAPI.json'))
        return

    with open(path.join(PROD_CACHE_PATH, 'prosperAPI.json'), 'r') as tdb_fh:
        raw_json = json.load(tdb_fh)

    collections = raw_json.keys()
    tdb = TinyMongoClient(PROD_CACHE_PATH)
    for collection in collections:
        if collection in SPECIAL_CACHE_COLLECTIONS:
            #skip special tables
            continue

        tdb.prosperAPI[collection].delete_many({})  #nuke it from orbit

    tdb.close()
Exemple #13
0
def clear_tinymongo_cache(bool_prod=False):
    """tinymongo uses all collections in one file, clearing requires direct access"""
    if not bool_prod:
        remove(path.join(TEST_CACHE_PATH, 'prosperAPI.json'))
        return

    with open(path.join(PROD_CACHE_PATH, 'prosperAPI.json'), 'r') as tdb_fh:
        raw_json = json.load(tdb_fh)

    collections = raw_json.keys()
    tdb = TinyMongoClient(PROD_CACHE_PATH)
    for collection in collections:
        if collection in SPECIAL_CACHE_COLLECTIONS:
            #skip special tables
            continue

        tdb.prosperAPI[collection].delete_many({})  #nuke it from orbit

    tdb.close()
Exemple #14
0
 def get(self):
     # client = MongoClient(MONGO_HOST, MONGO_PORT)
     client = TinyMongoClient()
     db = client.prom
     col = db.targets
     targets = []
     for target in col.find():
         targets.append({
             'exporter': target['exporter'],
             'target': target['target'],
             'labels': target.get('labels', {})
         })
     return {'targets': targets}
Exemple #15
0
def init_BancoDeDados_TinyMongo(elementos: list):
    """Adição dos elementos no banco de dados."""
    connection = TinyMongoClient()
    database = connection.database
    collection = database.elementos

    for elemento in elementos:
        collection.insert_one({
            'sigla': elemento.sigla,
            'nome': elemento.nome,
            'familia': elemento.familia,
            'atomico': elemento.atomico,
            'm_molar': elemento.m_molar
        }).inserted_id
    def __init__(self):

        self._mongo = TinyMongoClient(CONFIG.LOCALSTORE_PATH)
        try:
            self._collection = self._mongo.mindsdb[self._entity_name]
        except:
            logging.error(
                'No collection will be found, db corrupted, truncating it')
            shutil.rmtree(CONFIG.LOCALSTORE_PATH)
            raise ValueError(
                'MindsDB local document store corruped. No other way to put this, trained model data will be lost'
            )

        self.setup()
Exemple #17
0
    def post(self):
        body = request.get_json()
        try:
            validate(body, schema)
        except:
            return {
                'message':
                'Input data invalid or miss some value, required: {}'.format(
                    schema['required'])
            }, 400

        # client = MongoClient(MONGO_HOST, MONGO_PORT)
        client = TinyMongoClient()
        db = client.prom
        col = db.targets
        labels = body.get('labels', {})
        result = {
            'exporter': body['exporter'],
            'target': body['target'],
            'labels': labels
        }
        replace_proto = {
            'exporter': body['exporter'],
            'target': body['target']
        }
        find_proto = {'exporter': body['exporter']}
        metrics_path = labels.get('__metrics_path__')
        if metrics_path is not None:
            replace_proto['labels.__metrics_path__'] = metrics_path
        else:
            result['labels']['__metrics_path__'] = '/metrics'

        col.replace_one(replace_proto, result, True)
        with open('/prom/conf/' + body['exporter'] + '.json', 'w') as f:
            targets = []
            for target in col.find(find_proto, projection={'_id': False}):
                targets.append({
                    'targets': [target['target']],
                    'labels': target.get('labels', {})
                })

            f.write(json.dumps(targets, indent=2))
            f.flush()
            os.fsync(f.fileno())
        return {'status': 'created', 'data': result}, 201
Exemple #18
0
def build_connection(source_name,
                     source_path=path.join(HERE, 'cache'),
                     logger=api_config.LOGGER):
    """create a connection object for a bot main() to reference

    Args:
        source_name (str): name of db
        source_path (str, optional): path to db
        logger (:obj:`logging.logger`, optional): logging handle

    Returns:
        (:obj:`tinymongo.TinyMongoDatabase`): handle to database

    """
    logger.info('Building db connection: %s',
                path.join(source_path, source_name + '.json'))

    return TinyMongoClient(source_path)[source_name]
Exemple #19
0
def get_mongo_db(url=None):
    # url should be in this form: "mongodb://<username>:<password>@<host_server>.mlab.com:<port>/<db_name>"
    heroku = os.environ.get("DYNO")
    azure = os.environ.get("APPSETTING_WEBSITE_SITE_NAME")
    gae = os.environ.get("GAE_APPLICATION")

    if heroku or azure or gae:  # heroku, azure or gae
        if heroku and not url:
            url = os.environ.get("MONGODB_URI")
        elif azure and not url:
            url = os.environ.get("APPSETTING_MONGOURL")

        from pymongo import MongoClient
        client = MongoClient(url)
        db_name = url.replace("mongodb://").split("/")[1]
        return client[db_name]
    else:  # localhost
        from tinymongo import TinyMongoClient
        client = TinyMongoClient("database")
        return client.my_db
Exemple #20
0
 def __init__(self):
     self.connection = TinyMongoClient(DBPATH)
     self.app = self.connection.app
     self.oauth = self.app.oauth
     self.activation = self.app.activation
     self.settings = self.app.settings
Exemple #21
0
import os

from tinymongo import TinyMongoClient, Query, where

from users import generate_password_hash

from utils import (slugify, login_required, form2object, object2form,
                   admin_required, token_generator, snippet)

from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename

app = Flask(__name__)
app.config.from_pyfile('app.cfg')
DB = TinyMongoClient().flaskpress


############### BLOG 2 META DEFAULTS #############
# once running, you can override these defaults
default_brand = "FlaskBlog"
default_about = """
<p>FlaskPress is an open-source micro content management system.
It is our hope it will be useful to someone in the community that have learned or are
discovering the excellence of Python and the Flask web framework.
</p>
<p>
FlaskPress leverages several Flask plugins and the simple TinyMongo to achieve a flat file storage system. The advantage of using
TinyMongo is simplicity and being a somewhat small/self-contained project.  The advantage of using Flask is it's unopinonated and
easy to deploy framework that is high extensible.
</p>
Exemple #22
0
api_key = 'EcBv9wqxfdWNMhtOI8WbkGb9XwOuITAPxBdljcxv8RYX1H7u2ucC0qokDp2KOWmr'
api_secret = 'i5Y57Gwu8sH9qUE5TbB7zLotm7deTa1D9S8K458LWLXZZzNq5wNAZOHlGJmyjq1s'

kucoin_api_key = '5a64f6a46829d247d237e7bf'
kucoin_api_secret = '93b85f5c-f164-4bea-bd40-3ffda4c03907'

from binance.client import Client
client = Client(api_key, api_secret)
from kucoin.client import Client
kuClient = Client(kucoin_api_key, kucoin_api_secret)

bm = BinanceSocketManager(client)

app = Flask(__name__)
socketio = SocketIO(app)
connection = TinyMongoClient()
db = connection.cryptoAnalytics
collection = db.arbitrage
CORS(app)


@app.route("/binance")
def binanceKlines():
    res = {}
    symbol = request.args.get('symbol')
    interval = request.args.get('interval')
    info = client.get_symbol_ticker()
    BTCprice = 0

    for ticker in info:
        if ticker['symbol'] == "BTCUSDT":
from quart import (abort, Quart, redirect, render_template, request, url_for)
from tinymongo import TinyMongoClient
    
app = Quart(__name__)
DB = TinyMongoClient().blog

@app.errorhandler(404)
async def page_not_found(e):
    """404 error handler-- 404 status set explicitly"""
    return await render_template('404.html'), 404
            
@app.route('/')
async def index():
    """return all the pages to a user view"""
    pages = DB.blog.find()
    return await render_template('page_list.html', pages=pages)

@app.route('/view/<id>')
async def page_view(id):
    """view a page by id or 404 if does not exist"""
    page = DB.blog.find_one({'_id':id})
    if page is None:
        # 
        abort(404)
    
    return await render_template('view.html', page=page)


@app.route('/edit', methods=['GET','POST'])
@app.route('/edit/<id>', methods=['GET','POST'])
async def page_edit(id=None):
Exemple #24
0
import json
from tinymongo import TinyMongoClient
from pprint import pprint

# Initial db
connection = TinyMongoClient('defender')  # create defender/ directory
defenderDb = connection.defenderDb  # create defender/defenderDb.json
clientCollection_Tbl = defenderDb.client  # create client collection(table)

clients = []
with open('data-client.json') as f:
    clients = json.load(f)
    print len(clients)

for client in clients:
    client_item = {
        'macAddress': client['macAddress'].replace(" ", ""),
        'accessPointSerialNumber': client['accessPointSerialNumber'],
        'lastSeen': client['lastSeen']
    }
    print client_item

    #found = clientCollection_Tbl.find_one({"macAddress": client_item["macAddress"], "accessPointSerialNumber": client['accessPointSerialNumber'], "lastSeen": client['lastSeen']})
    found = clientCollection_Tbl.find_one(client_item)
    found_cnt = clientCollection_Tbl.find(client_item).count()
    print "=================="
    print found_cnt
    print "------------------"
    if found_cnt == 0:
        print "insert_one"
        clientCollection_Tbl.insert_one(client_item)
Exemple #25
0
def get_db():
    conn = TinyMongoClient()
    db = conn.my_database
    return db
Exemple #26
0
def configure_db():
    conn = TinyMongoClient("teste")
    db = conn.db
    return db
Exemple #27
0
def get_db(app_name):
    if not isinstance(app_name, str):
        return jsonify({'message': 'invalid name for app'}), 400
    mocks_dir = get_mocks_dir()
    connection = TinyMongoClient(foldername=mocks_dir.resolve())
    return connection[app_name]
Exemple #28
0
    def __init__(self):

        self._mongo = TinyMongoClient(CONFIG.LOCALSTORE_PATH)
        self._collection =  self._mongo.mindsdb[self._entity_name]
        self.setup()
Exemple #29
0
import asyncio
import logging
import re
import typing

import pendulum
import discord
from discord.ext import commands, tasks
from tinymongo import TinyMongoClient

import constants
import exceptions
from modules import utility

mclient = TinyMongoClient('tinydb')


class Background(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._rsvp_triggers.start()  #pylint: disable=no-member

    def cog_unload(self):
        self._rsvp_triggers.stop()  #pylint: disable=no-member

    @tasks.loop(minutes=1)
    async def _rsvp_triggers(self):
        reservations = mclient.rsvpbot.reservations.find({'active': True})
        for rsvp in reservations:
            config = mclient.rsvpbot.config.find_one({'_id': rsvp['guild']})
            start_date = pendulum.from_timestamp(rsvp['date'],
Exemple #30
0
    def main(self):
        """application runtime"""
        global LOGGER
        LOGGER = self.__log_builder.logger

        LOGGER.info('hello world')
        if not self.testkey:
            username = cli.terminal.readline(
                message='Username for key: '
            ).rstrip()
            id_info = cli.terminal.readline(
                message='Info about user: '******'travis_test_user'
            id_info = 'automated test key'

        LOGGER.info('making key for {0}:{1}'.format(username, id_info))
        
        # Connect to TinyMongoDB and use prosperAPI DB 
        connection = TinyMongoClient(CACHE_PATH)
        userdb = connection.prosperAPI.users
        # Attach to users collection

        current_key = userdb.find_one({'user_name': username })
        
        if current_key:
        
            key_msg = \
            'user already has a key' + \
            '\n\tapi_key={0}'.format(current_key['api_key']) + \
            '\n\tuser_name={0}'.format(current_key['user_name']) + \
            '\n\tuser_info={0}'.format(current_key['user_info']) + \
            '\n\tkey_generated={0}'.format(current_key['key_generated']) + \
            '\n\tlast_accessed={0}'.format(current_key['last_accessed'])
            
            print(key_msg)
            if not self.debug:
                LOGGER.info(key_msg)

            if not self.force:
                exit()

        if current_key and not self.debug:
            print("Delete Key")
            userdb.delete_one({'user_name': username})

        last_accessed = None
        
        if self.testkey:
            last_accessed = datetime.now().isoformat()
        api_key_entry = {
            'api_key': shortuuid.uuid(),
            'user_name': username,
            'user_info': id_info,
            'key_generated': datetime.now().isoformat(),
            'last_accessed': last_accessed
        }

        if not self.debug:
            userdb.insert_one(api_key_entry)

        check_key = userdb.find_one({'user_name': username})

        if self.debug:
            api_msg = 'Key generated for {0}: {1}'.format(username, api_key_entry['api_key'])
        else:
            api_msg = 'Key generated for {0}: {1}'.\
                format(check_key['user_name'], check_key['api_key'])
        connection.close()
        print(api_msg)
        if not self.debug:
            LOGGER.info(api_msg)
Exemple #31
0
    def main(self):
        """application runtime"""
        global LOGGER
        LOGGER = self.__log_builder.logger

        LOGGER.info('hello world')
        if not self.testkey:
            username = cli.terminal.readline(
                message='Username for key: '
            ).rstrip()
            id_info = cli.terminal.readline(
                message='Info about user: '******'travis_test_user'
            id_info = 'automated test key'

        LOGGER.info('making key for {0}:{1}'.format(username, id_info))
        
        #Connect to TinyMongoDB and use prosperAPI DB 
        connection = TinyMongoClient(CACHE_PATH)
        api_db = connection.prosperAPI
        #Attach to users collection
        usersDB = api_db.users

        current_key = usersDB.find_one({'user_name': username })
        
        if current_key:
        
            key_msg = \
            'user already has a key' + \
            '\n\tapi_key={0}'.format(current_key['api_key']) + \
            '\n\tuser_name={0}'.format(current_key['user_name']) + \
            '\n\tuser_info={0}'.format(current_key['user_info']) + \
            '\n\tkey_generated={0}'.format(current_key['key_generated']) + \
            '\n\tlast_accessed={0}'.format(current_key['last_accessed'])
            
            print(key_msg)
            if not self.debug:
                LOGGER.info(key_msg)

            if not self.force:
                exit()
        """
        if current_key and not self.debug:
            api_db.remove(
                Query().user_name == username
            )
        """
        last_accessed = None
        
        if self.testkey:
            last_accessed = datetime.now().isoformat()
        api_key_entry = {
            'api_key': shortuuid.uuid(),
            'user_name': username,
            'user_info': id_info,
            'key_generated': datetime.now().isoformat(),
            'last_accessed': last_accessed
        }

        if not self.debug:
            usersDB.insert_one(api_key_entry)

        check_key = usersDB.find_one({'user_name': username})

        if self.debug:
            api_msg = 'Key generated for {0}: {1}'.format(username, api_key_entry['api_key'])
        else:
            api_msg = 'Key generated for {0}: {1}'.\
                format(check_key['user_name'], check_key['api_key'])

        print(api_msg)
        if not self.debug:
            LOGGER.info(api_msg)
Exemple #32
0
import os

from flask import Flask
from tinymongo import TinyMongoClient
from flask_jwt_simple import JWTManager

app = Flask(__name__)

app_settings = os.getenv('APP_SETTINGS',
                         'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)

connection = TinyMongoClient(app.config['DB_FILE'])
db = connection.db

from project.server.views import bp

app.register_blueprint(bp)

jwt = JWTManager(app)
Exemple #33
0
from tinymongo import TinyMongoClient

DATAFOLDER = '/tmp/flask_workshop_test'

conn = TinyMongoClient(DATAFOLDER)
db = conn.test
Exemple #34
0
def get_db():
    conn = TinyMongoClient()    # pode se passar o caminho do arquivo db ("")
    db = conn.my_database   # my_database é o nome do arquivo json do db
    return db