Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 3p
import unittest
import pymemcache
# project
from ddtrace import Pin
from ddtrace.constants import ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.contrib.pymemcache.patch import patch, unpatch
from ddtrace.ext import memcached as memcachedx, net
from .utils import MockSocket
from tests.test_tracer import get_dummy_tracer
from ...base import override_config
_Client = pymemcache.client.base.Client
TEST_HOST = 'localhost'
TEST_PORT = 117711
class PymemcacheClientTestCaseMixin(unittest.TestCase):
""" Tests for a patched pymemcache.client.base.Client. """
def get_spans(self):
pin = Pin.get_from(self.client)
tracer = pin.tracer
spans = tracer.writer.pop()
return spans
def check_spans(self, num_expected, resources_expected, queries_expected):
"""A helper for validating basic span information."""
def make_client_pool(self, hostname, mock_socket_values, serializer=None, **kwargs):
mock_client = pymemcache.client.base.Client(
hostname, serializer=serializer, **kwargs
)
tracer = get_dummy_tracer()
Pin.override(mock_client, tracer=tracer)
mock_client.sock = MockSocket(mock_socket_values)
client = pymemcache.client.base.PooledClient(hostname, serializer=serializer)
client.client_pool = pymemcache.pool.ObjectPool(lambda: mock_client)
return mock_client
MemcacheIllegalInputError,
)
import pytest
import unittest
from ddtrace.vendor import wrapt
# project
from ddtrace import Pin
from ddtrace.contrib.pymemcache.patch import patch, unpatch
from .utils import MockSocket, _str
from .test_client_mixin import PymemcacheClientTestCaseMixin, TEST_HOST, TEST_PORT
from tests.test_tracer import get_dummy_tracer
_Client = pymemcache.client.base.Client
class PymemcacheClientTestCase(PymemcacheClientTestCaseMixin):
""" Tests for a patched pymemcache.client.base.Client. """
def test_patch(self):
assert issubclass(pymemcache.client.base.Client, wrapt.ObjectProxy)
client = self.make_client([])
self.assertIsInstance(client, wrapt.ObjectProxy)
def test_unpatch(self):
unpatch()
from pymemcache.client.base import Client
self.assertEqual(Client, _Client)
import pymemcache
from ddtrace.ext import memcached as memcachedx
from ddtrace.pin import Pin, _DD_PIN_NAME, _DD_PIN_PROXY_NAME
from .client import WrappedClient
_Client = pymemcache.client.base.Client
def patch():
if getattr(pymemcache.client, '_datadog_patch', False):
return
setattr(pymemcache.client, '_datadog_patch', True)
setattr(pymemcache.client.base, 'Client', WrappedClient)
# Create a global pin with default configuration for our pymemcache clients
Pin(
app=memcachedx.SERVICE, service=memcachedx.SERVICE
).onto(pymemcache)
def unpatch():
def unpatch():
"""Remove pymemcache tracing"""
if not getattr(pymemcache.client, '_datadog_patch', False):
return
setattr(pymemcache.client, '_datadog_patch', False)
setattr(pymemcache.client.base, 'Client', _Client)
# Remove any pins that may exist on the pymemcache reference
setattr(pymemcache, _DD_PIN_NAME, None)
setattr(pymemcache, _DD_PIN_PROXY_NAME, None)
def _create_client(self):
return base.Client((self.url, self.port))
print("⚠ DTDD's api is recommended for performance reasons")
try:
from config import use_memcache
if use_memcache:
try:
from config import memcache_address, memcache_port, invalidation_time
except ImportError:
print("⚠ Please set memcache_address, memcache_port and invalidation_time in config.py")
use_memcache = False
except ImportError:
print("⚠ Please set use_memcache in config.py")
use_memcache = False
if use_memcache:
from pymemcache.client import base
client = base.Client((memcache_address, memcache_port))
print("✅ Established memcache client")
else:
print("⚠ memcache is disabled - we recommend you enable it to improve performance")
base_string = "https://www.doesthedogdie.com/{media_id}"
def get_topics(media_id):
resp = requests.get(base_string.format(media_id=media_id))
soup = BeautifulSoup(resp.text, 'lxml')
try:
return(soup.find("div", {"id": "topics"}).select('.topicRow'))
except AttributeError:
print("❌ Could not find topics for {}".format(media_id))
return []
def get_topics_api(media_id):
'FLASK_EDITABLESITE_SECRET',
'secret-key') # TODO: Change me
APP_DIR = os.path.abspath(
os.path.dirname(__file__)) # This directory
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
SQLALCHEMY_DATABASE_URI = os_env.get(
'FLASK_EDITABLESITE_DATABASE_URI',
'postgresql://localhost/example') # TODO: Change me
SESSION_TYPE = os_env.get('FLASK_EDITABLESITE_SESSION_TYPE', None)
SESSION_USE_SIGNER = True
SESSION_KEY_PREFIX = 'flask-editablesite-session:'
SESSION_MEMCACHED = (
os_env.get('FLASK_EDITABLESITE_SESSION_MEMCACHED', None)
and memcached.Client(
[os_env.get('FLASK_EDITABLESITE_SESSION_MEMCACHED', None)],
binary=True)
or None)
SESSION_FILE_DIR = (
os_env.get('FLASK_EDITABLESITE_SESSION_FILE_DIR', None)
and os.path.abspath(os.path.join(
APP_DIR,
os_env.get('FLASK_EDITABLESITE_SESSION_FILE_DIR', None)))
or None)
USE_SESSIONSTORE_NOT_DB = (
os_env.get('FLASK_EDITABLESITE_USE_SESSIONSTORE_NOT_DB')
and ast.literal_eval(
os_env.get('FLASK_EDITABLESITE_USE_SESSIONSTORE_NOT_DB'))
or False)