<commit_before>import os

from psycopg2.extensions import AsIs

import skygear
from skygear.container import SkygearContainer
from skygear.models import RecordID, Reference
from skygear.options import options
from skygear.utils import db

from .exc import SkygearChatException

container = SkygearContainer()
opts = vars(options)
container.api_key = os.getenv('API_KEY', opts.get('apikey'))
container.app_name = os.getenv('APP_NAME', opts.get('appname'))
schema_name = "app_%s" % container.app_name

MASTER_KEY = os.getenv('MASTER_KEY', opts.get('masterkey'))


def _get_conversation(conversation_id):
    # conversation_id can be Reference, recordID or string
    if isinstance(conversation_id, Reference):
        conversation_id = conversation_id.recordID.key
    if isinstance(conversation_id, RecordID):
        conversation_id = conversation_id.key
    data = {
        'database_id': '_public',
        'record_type': 'conversation',
        'limit': 1,
        'sort': [],
        'include': {},
        'count': False,
        'predicate': [
            'eq', {
                '$type': 'keypath',
                '$val': '_id'
            },
            conversation_id]
    }

    response = skygear.container.send_action(
        container._request_url('record:query'),
        container._payload('record:query', data)
    )

    if 'error' in response:
        raise SkygearChatException(response['error'])

    if len(response['result']) == 0:
        raise SkygearChatException("no conversation found")

    return response['result'][0]


def _get_channel_by_user_id(user_id):
    if not _check_if_table_exists('user_channel'):
        return None

    with db.conn() as conn:
        cur = conn.execute('''
            SELECT name
            FROM %(schema_name)s.user_channel
            WHERE _owner_id = %(user_id)s
            LIMIT 1;
            ''', {
            'schema_name': AsIs(schema_name),<commit_msg>Remove using internal method of skygear container (#46)<commit_after>import os

from psycopg2.extensions import AsIs

from skygear.container import SkygearContainer
from skygear.models import RecordID, Reference
from skygear.options import options
from skygear.utils import db

from .exc import SkygearChatException

container = SkygearContainer()
opts = vars(options)
container.api_key = os.getenv('API_KEY', opts.get('apikey'))
container.app_name = os.getenv('APP_NAME', opts.get('appname'))
schema_name = "app_%s" % container.app_name

MASTER_KEY = os.getenv('MASTER_KEY', opts.get('masterkey'))


def _get_conversation(conversation_id):
    # conversation_id can be Reference, recordID or string
    if isinstance(conversation_id, Reference):
        conversation_id = conversation_id.recordID.key
    if isinstance(conversation_id, RecordID):
        conversation_id = conversation_id.key
    data = {
        'database_id': '_public',
        'record_type': 'conversation',
        'limit': 1,
        'sort': [],
        'include': {},
        'count': False,
        'predicate': [
            'eq', {
                '$type': 'keypath',
                '$val': '_id'
            },
            conversation_id]
    }

    response = container.send_action('record:query', data)

    if 'error' in response:
        raise SkygearChatException(response['error'])

    if len(response['result']) == 0:
        raise SkygearChatException("no conversation found")

    return response['result'][0]


def _get_channel_by_user_id(user_id):
    if not _check_if_table_exists('user_channel'):
        return None

    with db.conn() as conn:
        cur = conn.execute('''
            SELECT name
            FROM %(schema_name)s.user_channel
            WHERE _owner_id = %(user_id)s
            LIMIT 1;
            ''', {
            'schema_name': AsIs(schema_name),
{'fraction_non_alphanumeric': 0.11470665614312023, 'fraction_numerical': 0.0031570639305445935, 'mean_word_length': 2.9034907597535935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3654789', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1130, 'n_words': 254}