text:
import logging

from synapse.api.errors import StoreError
from synapse.util.logutils import log_function
from synapse.util.logcontext import preserve_context_over_fn, LoggingContext
from synapse.util.lrucache import LruCache
import synapse.metrics

from util.id_generators import IdGenerator, StreamIdGenerator

from twisted.internet import defer

from collections import namedtuple, OrderedDict

import functools
import sys
import time
import threading

DEBUG_CACHES = False

logger = logging.getLogger(__name__)

sql_logger = logging.getLogger("synapse.storage.SQL")
transaction_logger = logging.getLogger("synapse.storage.txn")
perf_logger = logging.getLogger("synapse.storage.TIME")

metrics = synapse.metrics.get_metrics_for("synapse.storage")

sql_scheduling_timer = metrics.register_distribution("schedule_time")

sql_query_timer = metrics.register_distribution("query_time", labels=["verb"])
sql_txn_timer = metrics.register_distribution("transaction_time", labels=["desc"])

caches_by_name = {}
cache_counter = metrics.register_cache(
    "cache",
    lambda: {(name,): len(caches_by_name[name]) for name in caches_by_name.keys()},
    labels=["name"],
)

class Cache(object):

    def __init__(self, name, max_entries=1000, keylen=1, lru=False):
        if lru:
            self.cache = LruCache(max_size=max_entries)
            self.max_entries = None
        else:
            self.cache = OrderedDict()
            self.max_entries = max_entries

        self.name = name
        self.keylen = keylen
        self.sequence = 0
        self.thread = None
        caches_by_name[name] = self.cache

    def check_thread(self):
        expected_thread = self.thread
        if expected_thread is None:
            self.thread = threading.current_thread()
        else:
            if expected_thread is not threading.current_thread():
                raise ValueError(
                    "Cache objects can only be accessed from the main thread"
                )

    def get(self, *keyargs):
        if len(keyargs) != self.keylen:
            raise ValueError("Expected a key to have %d items", self.keylen)

        if keyargs in self.cache:
            cache_counter.inc_hits(self.name)
            return self.cache[keyargs]

        cache_counter.inc_misses(self.name)
        raise KeyError()

    def update(self, sequence, *args):
        self.check_thread()
        if self.sequence == sequence:
            # Only update the cache if the caches sequence number matches the
            # number that the cache had before the SELECT was started (SYN-369)
            self.prefill(*args)

    def prefill(self, *args):  # because I can't  *keyargs, value
        keyargs = args[:-1]
        value = args[-1]

        if len(keyargs) != self.keylen:
            raise ValueError("Expected a key to have %d items", self.keylen)

        if self.max_entries is not None:
            while len(self.cache) >= self.max_entries:
                self.cache.popitem(last=False)

        self.cache[keyargs] = value

    def invalidate(self, *keyargs):
        self.check_thread()
        if len(keyargs) != self.keylen:
            raise ValueError("Expected a key to have %d items", self.keylen)
        # Increment the sequence number so that any SELECT statements that
        # raced with the INSERT don't update the cache (SYN-369)
        self.sequence += 1
        self.cache.pop(keyargs, None)

    def invalidate_all(self):
        self.check_thread()
        self.sequence += 1
        self.cache.clear()

class CacheDescriptor(object):
    """ A method decorator that applies a memoizing cache around the function.

    The function is presumed to take zero or more arguments, which are used in
    a tuple as the key for the cache. Hits are served directly from the cache;
    misses use the function body to generate the value.

    The wrapped function has an additional member, a callable called
    "invalidate". This can be used to remove individual entries from the cache.

    The wrapped function has another additional callable, called "prefill",
    which can be used to insert values into the cache specifically, without
    calling the calculation function.
    """
    def __init__(self, orig, max_entries=1000, num_args=1, lru=False):
        self.orig = orig

        self.max_entries = max_entries
        self.num_args = num_args
        self.lru = lru

    def __get__(self, obj, objtype=None):
        cache = Cache(
            name=self.orig.__name__,
            max_entries=self.max_entries,
            keylen=self.num_args,
            lru=self.lru,
        )

        @functools.wraps(self.orig)
        @defer.inlineCallbacks
        def wrapped(*keyargs):
            try:
                cached_result = cache.get(*keyargs[:self.num_args])
                if DEBUG_CACHES:
                    actual_result = yield self.orig(obj, *keyargs)
                    if actual_result != cached_result:
                        logger.error(
                            "Stale cache entry %s%r: cached: %r, actual %r",
                            self.orig.__name__, keyargs,
                            cached_result, actual_result,
                        )
                        raise ValueError("Stale cache entry")
                defer.returnValue(cached_result)
            except KeyError:
                # Get the sequence number of the cache before reading from the
                # database so that we can tell if the cache is invalidated
                # while the SELECT is executing (SYN-369)
                sequence = cache.sequence

                ret = yield self.orig(obj, *keyargs)

                cache.update(sequence, *keyargs[:self.num_args] + (ret,))

                defer.returnValue(ret)

        wrapped.invalidate = cache.invalidate
        wrapped.invalidate_all = cache.invalidate_all
        wrapped.prefill = cache.prefill

        obj.__dict__[self.orig.__name__] = wrapped

        return wrapped

def cached(max_entries=1000, num_args=1, lru=False):
    return lambda orig: CacheDescriptor(
        orig,
        max_entries=max_entries,
        num_args=num_args,
        lru=lru
    )

class LoggingTransaction(object):
    """An object that almost-transparently proxies for the 'txn' object
    passed to the constructor. Adds logging and metrics to the .execute()
    method."""
    __slots__ = ["txn", "name", "database_engine", "after_callbacks"]

    def __init__(self, txn, name, database_engine, after_callbacks):
        object.__setattr__(self, "txn", txn)
        object.__setattr__(self, "name", name)
        object.__setattr__(self, "database_engine", database_engine)
        object.__setattr__(self, "after_callbacks", after_callbacks)

    def call_after(self, callback, *args):
        """Call the given callback on the main twisted thread after the
        transaction has finished. Used to invalidate the caches on the
        correct thread.
        """
        self.after_callbacks.append((callback, args))

    def __getattr__(self, name):
        return getattr(self.txn, name)

    def __setattr__(self, name, value):
        setattr(self.txn, name, value)

    def execute(self, sql, *args):
        self._do_execute(self.txn.execute, sql, *args)

    def executemany(self, sql, *args):
        self._do_execute(self.txn.executemany, sql, *args)

    def _do_execute(self, func, sql, *args):
        # TODO(paul): Maybe use 'info' and 'debug' for values?
        sql_logger.debug("[SQL] {%s} %s", self.name, sql)

        sql = self.database_engine.convert_param_style(sql)

        if args:
            try:
                sql_logger.debug(
                    "[SQL values] {%s} %r",
                    self.name, args[0]
                )
            except:
                # Don't let logging failures stop SQL from working
                pass

        start = time.time() * 1000

        try:
            return func(
                sql, *args
            )
        except Exception as e:
            logger.debug("[SQL FAIL] {%s} %s", self.name, e)
            raise
        finally:
            msecs = (time.time() * 1000) - start
            sql_logger.debug("[SQL time] {%s} %f", self.name, msecs)
            sql_query_timer.inc_by(msecs, sql.split()[0])

class PerformanceCounters(object):
    def __init__(self):
        self.current_counters = {}
        self.previous_counters = {}

    def update(self, key, start_time, end_time=None):
        if end_time is None:
            end_time = time.time() * 1000
        duration = end_time - start_time
        count, cum_time = self.current_counters.get(key, (0, 0))
        count += 1
        cum_time += duration
        self.current_counters[key] = (count, cum_time)
        return end_time

    def interval(self, interval_duration, limit=3):
        counters = []
        for name, (count, cum_time) in self.current_counters.items():
            prev_count, prev_time = self.previous_counters.get(name, (0, 0))
            counters.append((
                (cum_time - prev_time) / interval_duration,
                count - prev_count,
                name
            ))

        self.previous_counters = dict(self.current_counters)

        counters.sort(reverse=True)

        top_n_counters = ", ".join(
            "%s(%d): %.3f%%" % (name, count, 100 * ratio)
            for ratio, count, name in counters[:limit]
        )

        return top_n_counters

class SQLBaseStore(object):
    _TXN_ID = 0

    def __init__(self, hs):
        self.hs = hs
        self._db_pool = hs.get_db_pool()
        self._clock = hs.get_clock()

        self._previous_txn_total_time = 0
        self._current_txn_total_time = 0
        self._previous_loop_ts = 0

        # TODO(paul): These can eventually be removed once the metrics code
        #   is running in mainline, and we have some nice monitoring frontends
        #   to watch it
        self._txn_perf_counters = PerformanceCounters()
        self._get_event_counters = PerformanceCounters()

        self._get_event_cache = Cache("*getEvent*", keylen=3, lru=True,
                                      max_entries=hs.config.event_cache_size)

        self._event_fetch_lock = threading.Condition()
        self._event_fetch_list = []
        self._event_fetch_ongoing = 0

        self._pending_ds = []

        self.database_engine = hs.database_engine

        self._stream_id_gen = StreamIdGenerator()
        self._transaction_id_gen = IdGenerator("sent_transactions", "id", self)
        self._state_groups_id_gen = IdGenerator("state_groups", "id", self)
        self._access_tokens_id_gen = IdGenerator("access_tokens", "id", self)
        self._pushers_id_gen = IdGenerator("pushers", "id", self)
        self._push_rule_id_gen = IdGenerator("push_rules", "id", self)
        self._push_rules_enable_id_gen = IdGenerator("push_rules_enable", "id", self)

    def start_profiling(self):
        self._previous_loop_ts = self._clock.time_msec()

        def loop():
            curr = self._current_txn_total_time
            prev = self._previous_txn_total_time
            self._previous_txn_total_time = curr

            time_now = self._clock.time_msec()
            time_then = self._previous_loop_ts
            self._previous_loop_ts = time_now

            ratio = (curr - prev)/(time_now - time_then)

            top_three_counters = self._txn_perf_counters.interval(
                time_now - time_then, limit=3
            )

            top_3_event_counters = self._get_event_counters.interval(
                time_now - time_then, limit=3
            )

            perf_logger.info(
                "Total database time: %.3f%% {%s} {%s}",
                ratio * 100, top_three_counters, top_3_event_counters
            )

        self._clock.looping_call(loop, 10000)

    def _new_transaction(self, conn, desc, after_callbacks, func, *args, **kwargs):
        start = time.time() * 1000
        txn_id = self._TXN_ID

        # We don't really need these to be unique, so lets stop it from
        # growing really large.
        self._TXN_ID = (self._TXN_ID + 1) % (sys.maxint - 1)

        name = "%s-%x" % (desc, txn_id, )

        transaction_logger.debug("[TXN START] {%s}", name)

        try:
            i = 0
            N = 5
            while True:
                try:
                    txn = conn.cursor()
                    txn = LoggingTransaction(
                        txn, name, self.database_engine, after_callbacks
                    )
                    r = func(txn, *args, **kwargs)
                    conn.commit()
                    return r
                except self.database_engine.module.OperationalError as e:
                    # This can happen if the database disappears mid
                    # transaction.
                    logger.warn(
                        "[TXN OPERROR] {%s} %s %d/%d",
                        name, e, i, N
                    )
                    if i < N:
                        i += 1
                        try:
                            conn.rollback()
                        except self.database_engine.module.Error as e1:
                            logger.warn(
                                "[TXN EROLL] {%s} %s",
                                name, e1,
                            )
                        continue
                    raise
                except self.database_engine.module.DatabaseError as e:
                    if self.database_engine.is_deadlock(e):
                        logger.warn("[TXN DEADLOCK] {%s} %d/%d", name, i, N)
                        if i < N:
                            i += 1
                            try:
                                conn.rollback()
                            except self.database_engine.module.Error as e1:
                                logger.warn(
                                    "[TXN EROLL] {%s} %s",
                                    name, e1,
                                )
                            continue
                    raise
        except Exception as e:
            logger.debug("[TXN FAIL] {%s} %s", name, e)
            raise
        finally:
            end = time.time() * 1000
            duration = end - start

            transaction_logger.debug("[TXN END] {%s} %f", name, duration)

            self._current_txn_total_time += duration
            self._txn_perf_counters.update(desc, start, end)
            sql_txn_timer.inc_by(duration, desc)

    @defer.inlineCallbacks
    def runInteraction(self, desc, func, *args, **kwargs):
        """Wraps the .runInteraction() method on the underlying db_pool."""
        current_context = LoggingContext.current_context()

        start_time = time.time() * 1000

        after_callbacks = []

        def inner_func(conn, *args, **kwargs):
            with LoggingContext("runInteraction") as context:
                sql_scheduling_timer.inc_by(time.time() * 1000 - start_time)

                if self.database_engine.is_connection_closed(conn):
                    logger.debug("Reconnecting closed database connection")
                    conn.reconnect()

                current_context.copy_to(context)
                return self._new_transaction(
                    conn, desc, after_callbacks, func, *args, **kwargs
                )

        result = yield preserve_context_over_fn(
            self._db_pool.runWithConnection,
            inner_func, *args, **kwargs
        )

        for after_callback, after_args in after_callbacks:
            after_callback(*after_args)
        defer.returnValue(result)

    @defer.inlineCallbacks
    def runWithConnection(self, func, *args, **kwargs):
        """Wraps the .runInteraction() method on the underlying db_pool."""
        current_context = LoggingContext.current_context()

        start_time = time.time() * 1000

        def inner_func(conn, *args, **kwargs):
            with LoggingContext("runWithConnection") as context:
                sql_scheduling_timer.inc_by(time.time() * 1000 - start_time)

                if self.database_engine.is_connection_closed(conn):
                    logger.debug("Reconnecting closed database connection")
                    conn.reconnect()

                current_context.copy_to(context)

                return func(conn, *args, **kwargs)

        result = yield preserve_context_over_fn(
            self._db_pool.runWithConnection,
            inner_func, *args, **kwargs
        )

        defer.returnValue(result)

    def cursor_to_dict(self, cursor):
        """Converts a SQL cursor into an list of dicts.

        Args:
            cursor : The DBAPI cursor which has executed a query.
        Returns:
            A list of dicts where the key is the column header.
        """
        col_headers = list(column[0] for column in cursor.description)
        results = list(
            dict(zip(col_headers, row)) for row in cursor.fetchall()
        )
        return results

    def _execute(self, desc, decoder, query, *args):
        """Runs a single query for a result set.

        Args:
            decoder - The function which can resolve the cursor results to
                something meaningful.
            query - The query string to execute
            *args - Query args.
        Returns:
            The result of decoder(results)
        """
        def interaction(txn):
            txn.execute(query, args)
            if decoder:
                return decoder(txn)
            else:
                return txn.fetchall()

        return self.runInteraction(desc, interaction)

    def _execute_and_decode(self, desc, query, *args):
        return self._execute(desc, self.cursor_to_dict, query, *args)

    # "Simple" SQL API methods that operate on a single table with no JOINs,
    # no complex WHERE clauses, just a dict of values for columns.

    @defer.inlineCallbacks
    def _simple_insert(self, table, values, or_ignore=False,
                       desc="_simple_insert"):
        """Executes an INSERT query on the named table.

        Args:
            table : string giving the table name
            values : dict of new column names and values for them
        """
        try:
            yield self.runInteraction(
                desc,
                self._simple_insert_txn, table, values,
            )
        except self.database_engine.module.IntegrityError:
            # We have to do or_ignore flag at this layer, since we can't reuse
            # a cursor after we receive an error from the db.
            if not or_ignore:
                raise

    @log_function
    def _simple_insert_txn(self, txn, table, values):
        keys, vals = zip(*values.items())

        sql = "INSERT INTO %s (%s) VALUES(%s)" % (
            table,
            ", ".join(k for k in keys),
            ", ".join("?" for _ in keys)
        )

        txn.execute(sql, vals)

    def _simple_insert_many_txn(self, txn, table, values):
        if not values:
            return

        # This is a *slight* abomination to get a list of tuples of key names
        # and a list of tuples of value names.
        #
        # i.e. [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
        #         => [("a", "b",), ("c", "d",)] and [(1, 2,), (3, 4,)]
        #
        # The sort is to ensure that we don't rely on dictionary iteration
        # order.
        keys, vals = zip(*[
            zip(
                *(sorted(i.items(), key=lambda kv: kv[0]))
            )
            for i in values
            if i
        ])

        for k in keys:
            if k != keys[0]:
                raise RuntimeError(
                    "All items must have the same keys"
                )

        sql = "INSERT INTO %s (%s) VALUES(%s)" % (
            table,
            ", ".join(k for k in keys[0]),
            ", ".join("?" for _ in keys[0])
        )

        txn.executemany(sql, vals)

    def _simple_upsert(self, table, keyvalues, values,
                       insertion_values={}, desc="_simple_upsert", lock=True):
        """
        Args:
            table (str): The table to upsert into
            keyvalues (dict): The unique key tables and their new values
            values (dict): The nonunique columns and their new values
            insertion_values (dict): key/values to use when inserting
        Returns: A deferred
        """
        return self.runInteraction(
            desc,
            self._simple_upsert_txn, table, keyvalues, values, insertion_values,
            lock
        )

    def _simple_upsert_txn(self, txn, table, keyvalues, values, insertion_values={},
                           lock=True):
        # We need to lock the table :(, unless we're *really* careful
        if lock:
            self.database_engine.lock_table(txn, table)

        # Try to update
        sql = "UPDATE %s SET %s WHERE %s" % (
            table,
            ", ".join("%s = ?" % (k,) for k in values),
            " AND ".join("%s = ?" % (k,) for k in keyvalues)
        )
        sqlargs = values.values() + keyvalues.values()
        logger.debug(
            "[SQL] %s Args=%s",
            sql, sqlargs,
        )

        txn.execute(sql, sqlargs)
        if txn.rowcount == 0:
            # We didn't update and rows so insert a new one
            allvalues = {}
            allvalues.update(keyvalues)
            allvalues.update(values)
            allvalues.update(insertion_values)

            sql = "INSERT INTO %s (%s) VALUES (%s)" % (
                table,
                ", ".join(k for k in allvalues),
                ", ".join("?" for _ in allvalues)
            )
            logger.debug(
                "[SQL] %s Args=%s",
                sql, keyvalues.values(),
            )
            txn.execute(sql, allvalues.values())

    def _simple_select_one(self, table, keyvalues, retcols,
                           allow_none=False, desc="_simple_select_one"):
        """Executes a SELECT query on the named table, which is expected to
        return a single row, returning a single column from it.

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the row with
            retcols : list of strings giving the names of the columns to return

            allow_none : If true, return None instead of failing if the SELECT
              statement returns no rows
        """
        return self.runInteraction(
            desc,
            self._simple_select_one_txn,
            table, keyvalues, retcols, allow_none,
        )

    def _simple_select_one_onecol(self, table, keyvalues, retcol,
                                  allow_none=False,
                                  desc="_simple_select_one_onecol"):
        """Executes a SELECT query on the named table, which is expected to
        return a single row, returning a single column from it."

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the row with
            retcol : string giving the name of the column to return
        """
        return self.runInteraction(
            desc,
            self._simple_select_one_onecol_txn,
            table, keyvalues, retcol, allow_none=allow_none,
        )

    def _simple_select_one_onecol_txn(self, txn, table, keyvalues, retcol,
                                      allow_none=False):
        ret = self._simple_select_onecol_txn(
            txn,
            table=table,
            keyvalues=keyvalues,
            retcol=retcol,
        )

        if ret:
            return ret[0]
        else:
            if allow_none:
                return None
            else:
                raise StoreError(404, "No row found")

    def _simple_select_onecol_txn(self, txn, table, keyvalues, retcol):
        sql = (
            "SELECT %(retcol)s FROM %(table)s WHERE %(where)s"
        ) % {
            "retcol": retcol,
            "table": table,
            "where": " AND ".join("%s = ?" % k for k in keyvalues.keys()),
        }

        txn.execute(sql, keyvalues.values())

        return [r[0] for r in txn.fetchall()]

    def _simple_select_onecol(self, table, keyvalues, retcol,
                              desc="_simple_select_onecol"):
        """Executes a SELECT query on the named table, which returns a list
        comprising of the values of the named column from the selected rows.

        Args:
            table (str): table name
            keyvalues (dict): column names and values to select the rows with
            retcol (str): column whos value we wish to retrieve.

        Returns:
            Deferred: Results in a list
        """
        return self.runInteraction(
            desc,
            self._simple_select_onecol_txn,
            table, keyvalues, retcol
        )

    def _simple_select_list(self, table, keyvalues, retcols,
                            desc="_simple_select_list"):
        """Executes a SELECT query on the named table, which may return zero or
        more rows, returning the result as a list of dicts.

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the rows with,
            or None to not apply a WHERE clause.
            retcols : list of strings giving the names of the columns to return
        """
        return self.runInteraction(
            desc,
            self._simple_select_list_txn,
            table, keyvalues, retcols
        )

    def _simple_select_list_txn(self, txn, table, keyvalues, retcols):
        """Executes a SELECT query on the named table, which may return zero or
        more rows, returning the result as a list of dicts.

        Args:
            txn : Transaction object
            table : string giving the table name
            keyvalues : dict of column names and values to select the rows with
            retcols : list of strings giving the names of the columns to return
        """
        if keyvalues:
            sql = "SELECT %s FROM %s WHERE %s" % (
                ", ".join(retcols),
                table,
                " AND ".join("%s = ?" % (k, ) for k in keyvalues)
            )
            txn.execute(sql, keyvalues.values())
        else:
            sql = "SELECT %s FROM %s" % (
                ", ".join(retcols),
                table
            )
            txn.execute(sql)

        return self.cursor_to_dict(txn)

    def _simple_update_one(self, table, keyvalues, updatevalues,
                           desc="_simple_update_one"):
        """Executes an UPDATE query on the named table, setting new values for
        columns in a row matching the key values.

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the row with
            updatevalues : dict giving column names and values to update
            retcols : optional list of column names to return

        If present, retcols gives a list of column names on which to perform
        a SELECT statement *before* performing the UPDATE statement. The values
        of these will be returned in a dict.

        These are performed within the same transaction, allowing an atomic
        get-and-set.  This can be used to implement compare-and-set by putting
        the update column in the 'keyvalues' dict as well.
        """
        return self.runInteraction(
            desc,
            self._simple_update_one_txn,
            table, keyvalues, updatevalues,
        )

    def _simple_update_one_txn(self, txn, table, keyvalues, updatevalues):
        update_sql = "UPDATE %s SET %s WHERE %s" % (
            table,
            ", ".join("%s = ?" % (k,) for k in updatevalues),
            " AND ".join("%s = ?" % (k,) for k in keyvalues)
        )

        txn.execute(
            update_sql,
            updatevalues.values() + keyvalues.values()
        )

        if txn.rowcount == 0:
            raise StoreError(404, "No row found")
        if txn.rowcount > 1:
            raise StoreError(500, "More than one row matched")

    def _simple_select_one_txn(self, txn, table, keyvalues, retcols,
                               allow_none=False):
        select_sql = "SELECT %s FROM %s WHERE %s" % (
            ", ".join(retcols),
            table,
            " AND ".join("%s = ?" % (k,) for k in keyvalues)
        )

        txn.execute(select_sql, keyvalues.values())

        row = txn.fetchone()
        if not row:
            if allow_none:
                return None
            raise StoreError(404, "No row found")
        if txn.rowcount > 1:
            raise StoreError(500, "More than one row matched")

        return dict(zip(retcols, row))

    def _simple_selectupdate_one(self, table, keyvalues, updatevalues=None,
                                 retcols=None, allow_none=False,
                                 desc="_simple_selectupdate_one"):
        """ Combined SELECT then UPDATE."""
        def func(txn):
            ret = None
            if retcols:
                ret = self._simple_select_one_txn(
                    txn,
                    table=table,
                    keyvalues=keyvalues,
                    retcols=retcols,
                    allow_none=allow_none,
                )

            if updatevalues:
                self._simple_update_one_txn(
                    txn,
                    table=table,
                    keyvalues=keyvalues,
                    updatevalues=updatevalues,
                )

                # if txn.rowcount == 0:
                #     raise StoreError(404, "No row found")
                if txn.rowcount > 1:
                    raise StoreError(500, "More than one row matched")

            return ret
        return self.runInteraction(desc, func)

    def _simple_delete_one(self, table, keyvalues, desc="_simple_delete_one"):
        """Executes a DELETE query on the named table, expecting to delete a
        single row.

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the row with
        """
        sql = "DELETE FROM %s WHERE %s" % (
            table,
            " AND ".join("%s = ?" % (k, ) for k in keyvalues)
        )

        def func(txn):
            txn.execute(sql, keyvalues.values())
            if txn.rowcount == 0:
                raise StoreError(404, "No row found")
            if txn.rowcount > 1:
                raise StoreError(500, "more than one row matched")
        return self.runInteraction(desc, func)

    def _simple_delete(self, table, keyvalues, desc="_simple_delete"):
        """Executes a DELETE query on the named table.

        Args:
            table : string giving the table name
            keyvalues : dict of column names and values to select the row with
        """

        return self.runInteraction(desc, self._simple_delete_txn)

    def _simple_delete_txn(self, txn, table, keyvalues):
        sql = "DELETE FROM %s WHERE %s" % (
            table,
            " AND ".join("%s = ?" % (k, ) for k in keyvalues)
        )

        return txn.execute(sql, keyvalues.values())

    def _simple_max_id(self, table):
        """Executes a SELECT query on the named table, expecting to return the
        max value for the column "id".

        Args:
            table : string giving the table name
        """
        sql = "SELECT MAX(id) AS id FROM %s" % table

        def func(txn):
            txn.execute(sql)
            max_id = self.cursor_to_dict(txn)[0]["id"]
            if max_id is None:
                return 0
            return max_id

        return self.runInteraction("_simple_max_id", func)

    def get_next_stream_id(self):
        with self._next_stream_id_lock:
            i = self._next_stream_id
            self._next_stream_id += 1
            return i

class _RollbackButIsFineException(Exception):
    """ This exception is used to rollback a transaction without implying
    something went wrong.
    """
    pass

class Table(object):
    """ A base class used to store information about a particular table.
    """

    table_name = None
    """ str: The name of the table """

    fields = None
    """ list: The field names """

    EntryType = None
    """ Type: A tuple type used to decode the results """

    _select_where_clause = "SELECT %s FROM %s WHERE %s"
    _select_clause = "SELECT %s FROM %s"
    _insert_clause = "REPLACE INTO %s (%s) VALUES (%s)"

    @classmethod
    def select_statement(cls, where_clause=None):
        """
        Args:
            where_clause (str): The WHERE clause to use.

        Returns:
            str: An SQL statement to select rows from the table with the given
            WHERE clause.
        """
        if where_clause:
            return cls._select_where_clause % (
                ", ".join(cls.fields),
                cls.table_name,
                where_clause
            )
        else:
            return cls._select_clause % (
                ", ".join(cls.fields),
                cls.table_name,
            )

    @classmethod
    def insert_statement(cls):
        return cls._insert_clause % (
            cls.table_name,
            ", ".join(cls.fields),
            ", ".join(["?"] * len(cls.fields)),
        )

    @classmethod
    def decode_single_result(cls, results):
        """ Given an iterable of tuples, return a single instance of
            `EntryType` or None if the iterable is empty
        Args:
            results (list): The results list to convert to `EntryType`
        Returns:
            EntryType: An instance of `EntryType`
        """
        results = list(results)
        if results:
            return cls.EntryType(*results[0])
        else:
            return None

    @classmethod
    def decode_results(cls, results):
        """ Given an iterable of tuples, return a list of `EntryType`
        Args:
            results (list): The results list to convert to `EntryType`

        Returns:
            list: A list of `EntryType`
        """
        return [cls.EntryType(*row) for row in results]

    @classmethod
    def get_fields_string(cls, prefix=None):
        if prefix:
            to_join = ("%s.%s" % (prefix, f) for f in cls.fields)
        else:
            to_join = cls.fields

        return ", ".join(to_join)

class JoinHelper(object):
    """ Used to help do joins on tables by looking at the tables' fields and
    creating a list of unique fields to use with SELECTs and a namedtuple
    to dump the results into.

    Attributes:
        tables (list): List of `Table` classes
        EntryType (type)
    """

    def __init__(self, *tables):
        self.tables = tables

        res = []
        for table in self.tables:
            res += [f for f in table.fields if f not in res]

        self.EntryType = namedtuple("JoinHelperEntry", res)

    def get_fields(self, **prefixes):
        """Get a string representing a list of fields for use in SELECT
        statements with the given prefixes applied to each.

        For example::

            JoinHelper(PdusTable, StateTable).get_fields(
                PdusTable="pdus",
                StateTable="state"
            )
        """
        res = []
        for field in self.EntryType._fields:
            for table in self.tables:
                if field in table.fields:
                    res.append("%s.%s" % (prefixes[table.__name__], field))
                    break

        return ", ".join(res)

    def decode_results(self, rows):
        return [self.EntryType(*row) for row in rows]


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import aes.AdvanceEncryptionStandard;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;

/**
 * This class manages the connection between the program and the SQLite database.
 * This class is a singleton, but it wasn't implemented like that.
 *
 * @author Samuele Colombo <s.colombo003@studenti.unibs.it>
 * @since 0.2
 */
public class Account
{
    /**
     * @since 0.2
     */
    private static final String DRIVER = "org.sqlite.JDBC";

    /**
     * @since 0.2
     */
    private static final String DATABASE_NAME = "accounts.sqlite";

    /**
     * @since 0.2
     */
    private static final String DATABASE_RESOURCE = "jdbc:sqlite:.\\" + DATABASE_NAME;

    /**
     * @since 0.2
     */
    private static final String PASSWORD = "password";

    /*
    * @since 0.2
    */
    private static final String SELECT_ACCOUNT = "SELECT password FROM Account WHERE id='%s';";

    /**
     * @since 0.2
     */
    private static final String INSERT_ACCOUNT = "INSERT INTO Account (id,password) VALUES('%s', '%s');";

    /*
    * @since 0.2
    */
    private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS Account (id TEXT PRIMARY KEY,password TEXT)";

    /**
     * @since 0.2
     */
    private Account()
    {
        File file = new File(DATABASE_NAME);

        if(!file.exists())
        {
            try
            {
                // Create a new database file
                file.createNewFile();
                // Load the class for name
                Class.forName(DRIVER);
                // Initialize the connection with the database
                Connection connection = DriverManager.getConnection(DATABASE_RESOURCE);
                // Create a new statement
                Statement statement = connection.createStatement();
                // Execute the current query
                statement.executeUpdate(CREATE_TABLE);
                // Close the statement
                statement.close();
                // Close the connection
                connection.close();

            }
            catch (IOException ex)
            {
                Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch (ClassNotFoundException ex)
            {
                Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
            }
            catch (SQLException ex)
            {
                Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     *
     * @return
     * @since 0.2
     */
    public static Account getInstance()
    {
        return AccountHolder.INSTANCE;
    }

    /**
     * @since 0.2
     */
    private static class AccountHolder
    {

        private static final Account INSTANCE = new Account();
    }

    /**
     * Check if an account exists.
     *
     * @param id
     * @return
     * @since 0.2
     */
    public boolean accountExists(String id)
    {
        return (getSharedKey(id) != null);
    }

    /**
     * Get the shared key from the account id (username).
     *
     * @param id
     * @return
     * @since 0.2
     */
    public String getSharedKey(String id)
    {
        String password = null;

        try
        {
            // Load the class for name
            Class.forName(DRIVER);
            // Initialize the connection with the database
            Connection connection = DriverManager.getConnection(DATABASE_RESOURCE);
            // Create a new statement
            Statement statement = connection.createStatement();
            // Build the select query
            String query = String.format(SELECT_ACCOUNT, id);
            // Execute the current query
            ResultSet result = statement.executeQuery(query);

            while(result.next())
            {
                // Return the password field
                password = result.getString(PASSWORD);
                // Exit from the loop
                break;
            }
            // Close the statement
            statement.close();
            // Close the connection
            connection.close();
        }
        catch (SQLException | ClassNotFoundException ex)
        {
            Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Return the password (could be empty)
        return password;
    }

    /**
     *
     * @param id
     * @param password
     */
    public void saveAccount(String id, String password)
    {
        try
        {
            // Load the class for name
            Class.forName(DRIVER);
            // Initialize the connection with the database
            Connection connection = DriverManager.getConnection(DATABASE_RESOURCE);
            // Disable the auto commit property
            connection.setAutoCommit(false);
            // Create a new statement
            Statement statement = connection.createStatement();
            // Retrieve the SecretKey object from plaintext password
            SecretKey secret = AdvanceEncryptionStandard.generateKey(password);
            // Buil the insert query (and encode the shared key)
            String query = String.format(INSERT_ACCOUNT, id, Base64.encodeBase64String(secret.getEncoded()));
            // Execute the query
            statement.executeUpdate(query);
            // Close the statement
            statement.close();
            // Commit the operations
            connection.commit();
            // Close the connection
            connection.close();

        }
        catch (SQLException | ClassNotFoundException | NoSuchAlgorithmException | InvalidKeySpecException ex)
        {
            Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace Lurker\Tests\StateChecker\Inotify\Fixtures;

use Lurker\StateChecker\Inotify\FileStateChecker;

class FileStateCheckerForTest extends FileStateChecker
{
    private static $watchId;

    public static function setAddWatchReturns($value)
    {
        self::$watchId = $value;
    }

    protected function addWatch()
    {
        return self::$watchId;
    }

    protected function unwatch($id)
    {
    }
}


text:
import os

from . import makeprefs, dummyarchive
from testtools import TestCase
from testtools.matchers import Contains
from mock import Mock, patch
from StringIO import StringIO

class FetchCommandTest(TestCase):
    def setUp(self):
        super(FetchCommandTest, self).setUp()
        self.prefs = makeprefs()
        self.home = os.path.join('t', 'data', 'home')

    def tearDown(self):
        super(FetchCommandTest, self).tearDown()

    def _makeit(self, *args, **kwargs):
        from lacli.command import LaFetchCommand
        return LaFetchCommand(*args, **kwargs)

    def _makeupload(self, *args, **kwargs):
        return Mock(upload=Mock(**kwargs))

    def test_command(self):
        assert self._makeit(Mock(), Mock(), self.prefs)

    def test_fetch_archive_nonexistent(self):
        with patch('sys.stdout', new_callable=StringIO) as out:
            registry = Mock()
            registry.prefs = self.prefs
            registry.session.archives.return_value = ()
            cli = self._makeit(registry)
            cli.onecmd('fetch baz')
            self.assertEqual("Archive not found\n", out.getvalue())

    def test_fetch_archive(self):
        with patch('sys.stdout', new_callable=StringIO) as out:
            with patch('lacli.command.ask_key') as ask_key:
                ask_key.return_value = 'DEADBEEF' * 8
                registry = Mock()
                registry.prefs = self.prefs
                registry.cache.save_cert.return_value = ('LALA', None)

                registry.session.archives.return_value = (dummyarchive,)
                cli = self._makeit(registry)
                cli.onecmd('fetch baz')
                self.assertThat(out.getvalue(),
                                Contains('Fetched certificate LALA'))

                docs = registry.cache.save_cert.call_args[0][0]
                self.assertTrue('cert' in docs)
                self.assertTrue('archive' in docs)
                self.assertTrue('signature' in docs)
                self.assertEqual(docs['archive'].title, 'faz')


text:
#include <aws/mediaconvert/model/H265CodecLevel.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/core/Globals.h>
#include <aws/core/utils/EnumParseOverflowContainer.h>

using namespace Aws::Utils;

namespace Aws
{
  namespace MediaConvert
  {
    namespace Model
    {
      namespace H265CodecLevelMapper
      {

        static const int AUTO_HASH = HashingUtils::HashString("AUTO");
        static const int LEVEL_1_HASH = HashingUtils::HashString("LEVEL_1");
        static const int LEVEL_2_HASH = HashingUtils::HashString("LEVEL_2");
        static const int LEVEL_2_1_HASH = HashingUtils::HashString("LEVEL_2_1");
        static const int LEVEL_3_HASH = HashingUtils::HashString("LEVEL_3");
        static const int LEVEL_3_1_HASH = HashingUtils::HashString("LEVEL_3_1");
        static const int LEVEL_4_HASH = HashingUtils::HashString("LEVEL_4");
        static const int LEVEL_4_1_HASH = HashingUtils::HashString("LEVEL_4_1");
        static const int LEVEL_5_HASH = HashingUtils::HashString("LEVEL_5");
        static const int LEVEL_5_1_HASH = HashingUtils::HashString("LEVEL_5_1");
        static const int LEVEL_5_2_HASH = HashingUtils::HashString("LEVEL_5_2");
        static const int LEVEL_6_HASH = HashingUtils::HashString("LEVEL_6");
        static const int LEVEL_6_1_HASH = HashingUtils::HashString("LEVEL_6_1");
        static const int LEVEL_6_2_HASH = HashingUtils::HashString("LEVEL_6_2");

        H265CodecLevel GetH265CodecLevelForName(const Aws::String& name)
        {
          int hashCode = HashingUtils::HashString(name.c_str());
          if (hashCode == AUTO_HASH)
          {
            return H265CodecLevel::AUTO;
          }
          else if (hashCode == LEVEL_1_HASH)
          {
            return H265CodecLevel::LEVEL_1;
          }
          else if (hashCode == LEVEL_2_HASH)
          {
            return H265CodecLevel::LEVEL_2;
          }
          else if (hashCode == LEVEL_2_1_HASH)
          {
            return H265CodecLevel::LEVEL_2_1;
          }
          else if (hashCode == LEVEL_3_HASH)
          {
            return H265CodecLevel::LEVEL_3;
          }
          else if (hashCode == LEVEL_3_1_HASH)
          {
            return H265CodecLevel::LEVEL_3_1;
          }
          else if (hashCode == LEVEL_4_HASH)
          {
            return H265CodecLevel::LEVEL_4;
          }
          else if (hashCode == LEVEL_4_1_HASH)
          {
            return H265CodecLevel::LEVEL_4_1;
          }
          else if (hashCode == LEVEL_5_HASH)
          {
            return H265CodecLevel::LEVEL_5;
          }
          else if (hashCode == LEVEL_5_1_HASH)
          {
            return H265CodecLevel::LEVEL_5_1;
          }
          else if (hashCode == LEVEL_5_2_HASH)
          {
            return H265CodecLevel::LEVEL_5_2;
          }
          else if (hashCode == LEVEL_6_HASH)
          {
            return H265CodecLevel::LEVEL_6;
          }
          else if (hashCode == LEVEL_6_1_HASH)
          {
            return H265CodecLevel::LEVEL_6_1;
          }
          else if (hashCode == LEVEL_6_2_HASH)
          {
            return H265CodecLevel::LEVEL_6_2;
          }
          EnumParseOverflowContainer* overflowContainer = Aws::GetEnumOverflowContainer();
          if(overflowContainer)
          {
            overflowContainer->StoreOverflow(hashCode, name);
            return static_cast<H265CodecLevel>(hashCode);
          }

          return H265CodecLevel::NOT_SET;
        }

        Aws::String GetNameForH265CodecLevel(H265CodecLevel enumValue)
        {
          switch(enumValue)
          {
          case H265CodecLevel::AUTO:
            return "AUTO";
          case H265CodecLevel::LEVEL_1:
            return "LEVEL_1";
          case H265CodecLevel::LEVEL_2:
            return "LEVEL_2";
          case H265CodecLevel::LEVEL_2_1:
            return "LEVEL_2_1";
          case H265CodecLevel::LEVEL_3:
            return "LEVEL_3";
          case H265CodecLevel::LEVEL_3_1:
            return "LEVEL_3_1";
          case H265CodecLevel::LEVEL_4:
            return "LEVEL_4";
          case H265CodecLevel::LEVEL_4_1:
            return "LEVEL_4_1";
          case H265CodecLevel::LEVEL_5:
            return "LEVEL_5";
          case H265CodecLevel::LEVEL_5_1:
            return "LEVEL_5_1";
          case H265CodecLevel::LEVEL_5_2:
            return "LEVEL_5_2";
          case H265CodecLevel::LEVEL_6:
            return "LEVEL_6";
          case H265CodecLevel::LEVEL_6_1:
            return "LEVEL_6_1";
          case H265CodecLevel::LEVEL_6_2:
            return "LEVEL_6_2";
          default:
            EnumParseOverflowContainer* overflowContainer = Aws::GetEnumOverflowContainer();
            if(overflowContainer)
            {
              return overflowContainer->RetrieveOverflow(static_cast<int>(enumValue));
            }

            return {};
          }
        }

      } // namespace H265CodecLevelMapper
    } // namespace Model
  } // namespace MediaConvert
} // namespace Aws


text:
```
fab clean
fab install_project:project_branch=<x.x.x>
```

Note: project_branch will normally be a tag.  You will prevented from releasing a LIVE version if you use the master branch.

#### Release BETA Version

```
fab release_android
```

#### Release LIVE Version

```
fab release_android:beta=False
```

You will be prompted for a keystore passphrase.


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>USART: File Members</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>

<link href="navtree.css" rel="stylesheet" type="text/css"/>

<link href="search/search.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

USART
    00001

Main Page

Related Pages

Classes

Files

          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)"
               onblur="searchBox.OnSearchFieldFocus(false)"
               onkeyup="searchBox.OnSearchFieldChange(event)"/>

File List

File Members

All

Functions

Variables

Macros

 All Classes Files Functions Variables Macros Pages

SerialPort2
: usart2.c
, usart2.h

Generated on Wed Dec 23 2015 09:32:50 for USART by

     1.8.6


text:
![sensu docs](https://raw.github.com/sensu/sensu/master/sensu-logo.png)

Documentation for the Sensu monitoring framework.

## Overview

All documentation is written in [Markdown][markdown]. To preview the documentation in your browser run the following:

~~~ shell
bundle install
bundle exec middleman
~~~

Then go view the docs at [http://localhost:4567](http://localhost:4567) (which redirects to [http://localhost:4567/docs/](http://localhost:4567/docs/)). Edits to the documentation source files should cause the `middleman` server to reload so you can see your changes update live.

*NOTE:* _the middleman stuff was yanked from the http://sensuapp.org middleman project. An attempt was made to strip any/all styling and theme sources, as well as removal of any references to commercial support offerings. If any of that was left over and it offends you, please feel free to remove it and submit a pull request._

## Hosting

All documentation is hosted on the official Sensu website (http://sensuapp.org), which is a static site built with [Middleman][middleman], and hosted on [GitHub Pages][pages].

The important parts to familiarize yourself with for contributing to the Sensu documentation are the markdown renderer and syntax highlighting engines used to power http://sensuapp.org.

Markdown rendering is handled by [Kramdown][kramdown].

Syntax Highlighting is handled by [middleman-syntax][syntax], which uses [Rouge][rouge], which is a ruby-based syntax highlighting engine that is compatible with [Pygments][pygments] templates and supports things like "fenced code blocks" and language-specific syntax highlighting from Markdown.

## Metadata

Each page also contains some YAML "frontmatter" that is used to compile the navigation menus (etc) on http://sensuapp.org. There are three "required" properties to include - **version**, **category**, and **title**.

~~~ yaml
---
version: "0.12"
category: "overview"
title: "Sensu Overview"
---
~~~

In addition to these required properties, there are also some optional metadata properties which can be employed:

**User Guide / Next Steps**

Some portions of the documentation should be read like a guide, prompting you on to the next step in the process. So, when present the "next" property will cause a prompt to appear at the bottom of the documentation page on http://sensuapp.org to guide the reader to the next relevant topic. If this property is missing (or if the corresponding "url" + "text" properties are omitted), no such prompt will appear.

~~~ yaml
---
version: "0.12"
category: "overview"
title: "Sensu Overview"
next:
  url: installing_sensu
  text: "Install Sensu"
---
~~~

**Banners**

Sometimes it is helpful to alert the reader to changes, warn them of common pitfalls, or make it known that _there be dragons_. Adding a **alert**, **warning**, or **danger** property to the frontmatter will cause a corresponding blue, yellow, or red banner to be displayed at the top of the content section of the documentation page on http://sensuapp.org.

~~~ yaml
---
version: "0.12"
category: "API"
title: "Health"
alert: "Added in Sensu version 0.9.13+"
---
~~~

## License
The Sensu Documentation is released under the
[MIT license][mit-license].

[markdown]: http://daringfireball.net/projects/markdown/syntax
[sensuapp]: http://sensuapp.org
[middleman]: http://middlemanapp.com
[pages]: http://pages.github.com/
[kramdown]: http://kramdown.gettalong.org/
[syntax]: https://github.com/middleman/middleman-syntax
[rouge]: https://github.com/jayferd/rouge
[pygments]: http://pygments.org/
[mit-license]: https://raw.github.com/sensu/sensu-docs/master/MIT-LICENSE.txt


text:
ArgentumFeedBundle
==================

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/020f9643-8b15-4542-9f19-7e8fdc86d392/mini.png)](https://insight.sensiolabs.com/projects/020f9643-8b15-4542-9f19-7e8fdc86d392)
[![Build Status](https://travis-ci.org/argentumua/ArgentumFeedBundle.svg?branch=master)](https://travis-ci.org/argentumua/ArgentumFeedBundle)
[![Coverage Status](https://img.shields.io/coveralls/argentumua/ArgentumFeedBundle.svg)](https://coveralls.io/r/argentumua/ArgentumFeedBundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/argentumua/ArgentumFeedBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/argentumua/ArgentumFeedBundle/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/argentum/feed-bundle/v/stable.svg)](https://packagist.org/packages/argentum/feed-bundle)
[![License](https://poser.pugx.org/argentum/feed-bundle/license.svg)](https://packagist.org/packages/argentum/feed-bundle)

ArgentumFeedBundle allows you to create a set of feeds for your content with
different settings. All predefined settings can be overridden at runtime.

You can create your own Feed classes and Renderers by defining a service with
tag ```argentum_feed.feed``` and ```argentum_feed.renderer``` respectively.

Usage
-----

Render a predefined feed:
```php
$rss = $this->get('argentum_feed.factory')
    ->createFeed('news')
    ->addFeedableItems($news)
    ->render();
```
where ```$news``` is a collection of ```News``` entities.

To get it to work your ```News``` entity should implement ```Feedable``` interface:
```php
use Argentum\FeedBundle\Feed\Feedable;
use Argentum\FeedBundle\Feed\FeedItem;
use Argentum\FeedBundle\Feed\FeedItemEnclosure;
use Argentum\FeedBundle\Feed\FeedItemSource;

class News implements Feedable
{
    // ...

    /**
     * Returns FeedItem instance.
     *
     * @return FeedItem
     */
    public function getFeedItem()
    {
        $item = new FeedItem();

        $item
            ->setRouteName('news_show')
            ->setRouteParameters([
                'category' => $this->getCategory()->getSlug(),
                'id' => $this->getId(),
                'slug' => $this->getSlug(),
            ])
            ->setTitle($this->getTitle())
            ->setDescription($this->getAnnounce())
            ->setPubDate($this->getPublishedAt())
            ->addCustomValue('yandex:full-text', $this->getBody())
            ->addCustomValue('mailru:full-text', $this->getBody());

        if ($this->getImageMedium()) {
            $item->addEnclosure(
                new FeedItemEnclosure($this->getImageMedium()['path'], 'image/jpeg')
            );
        }

        if ($this->getSourceTitle()) {
            $item->setSource(
                new FeedItemSource($this->getSourceTitle(), $this->getSourceUrl())
            );
        }

        return $item;
    }
}
```

Also you can specify a data source provider in the configuration:
```yml
argentum_feed:
    channels:
        news:
            title: 'News'
            link: '/'
            description: 'News feed'
            provider:
                repository: 'ArgentumNewsBundle:News'
                method: 'findAllPublished'
                arguments: [10, 'ru']
```
and then you can just create the feed and render it:
```php
$rss = $this->get('argentum_feed.factory')
    ->createFeed('news')
    ->render();
```

Configuration
-------------

Full configuration:
```yml
argentum_feed:
    channels:
        news:
            title: 'News'
            link: '/'
            description: 'News feed'
            language: 'ru'
            copyright: 'ACME'
            managingEditor: 'Editor'
            webMaster: 'Webmaster'
            pubDate: 'now'
            lastBuildDate: 'now'
            categories:
                - { title: 'Category 1', domain: 'domain' }
                - { title: 'Category 2' }
            generator: 'ArgentumFeedBundle'
            docs: 'docs'
            cloud:
                domain: 'domain.tld'
                port: 80
                path: '/'
                registerProcedure: 'register'
                protocol: 'xmlrpc'
            ttl: 300
            image:
                url: '/images/logo.png'
                title: 'News'
                link: '/'
                width: 128
                height: 96
                description: 'News'
            rating: 'Best'
            textInput:
                title: 'Title'
                description: 'Description'
                name: 'name'
                link: 'http://domain.tld'
            skipHours: [0, 1, 2, 3, 4, 5, 6, 7, 8]
            skipDays:
                - 'Saturday'
                - 'Sunday'
            namespaces:
                default: 'http://backend.userland.com/rss2'
                yandex: 'http://news.yandex.ru'
                mailru: 'http://news.mail.ru'
            customElements:
                - 'yandex:full-text'
                - 'mailru:full-text'
            encoding: 'utf-8'
            translationDomain: 'news'
            feed: 'feed'
            renderer: 'rss'
            provider:
                repository: 'ArgentumNewsBundle:News'
                method: 'findAllPublished'
                arguments: [10, 'ru']
```

Minimal configuration:
```yml
argentum_feed:
    channels:
        news:
            title: 'News'
            link: '/'
            description: 'News feed'
```

All relative links will be converted to absolute using request host.
All text content from the predefined configuration will be translated using specified translationDomain.


text:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <logger name="org.springframework" level="ERROR"/>
    <logger name="org.apache" level="ERROR"/>
    <logger name="org.xnio" level="ERROR"/>
    <logger name="io.undertow" level="ERROR"/>
    <logger name="de.tototec" level="ERROR"/>
    <logger name="org.hibernate" level="ERROR"/>
    <logger name="org.jboss" level="ERROR"/>
    <logger name="com.dooapp" level="ERROR"/>

    <property resource="application.properties"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>

        <encoder>
            <charset>utf-8</charset>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <appender name="TABLE" class="com.kodcu.logging.TableViewLogAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder>
            <charset>utf-8</charset>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${HOME:-${USERPROFILE}}/${application.config.folder}/log/asciidocfx.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>asciidocfx.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- keep 3 days' worth of history -->
            <maxHistory>3</maxHistory>
        </rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>

        <encoder>
            <charset>utf-8</charset>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="TABLE"/>
        <appender-ref ref="FILE"/>
    </root>

</configuration>


text:
<?php

class ChargeIO_Credentials {
	private $public_key;
	private $secret_key;

	public function __construct($public_key, $secret_key) {
		$this->public_key = $public_key;
		$this->secret_key = $secret_key;
	}

	public function getPublicKey() {
		return $this->public_key;
	}

	public function getSecretKey() {
		return $this->secret_key;
	}
}


text:
var config = require('./config'),
    connect = require('connect'),
    gzippo;

try {
  gzippo = require('gzippo');
} catch (err) { /* NOP */ }

var staticImpl;

module.exports = function(req, res, next) {
  staticImpl(req, res, next);
};

module.exports.init = function() {
  var path = config.appDir + '/' + ((config.buildTarget || {}).path || 'build');
  console.error('set static path', path);

  staticImpl = gzippo ? gzippo.staticGzip(path) : connect.static(path);
};
config.on('reload', function() {
  module.exports.init();
});
config.on('build-target', function() {
  module.exports.init();
});


text:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import skimage.io

# This script will simulate diffusion of a particle over time. While we can
# solve the diffusion equation analytically, this will serve as a fun way to
# learn how to write stochastic simulations.

# We'll begin by considering the diffusion of a single particle in one
# dimension. We will say that at any point in time, the particle has an equal
# probability of taking a step to the left as it can take a step to the right.
# Let's takea  look at the trajectory of a single particle.

# We'll seed the random number generator such that our results will be
# the same each time we run it.
#np.random.seed(42)

# Define the nubmer of steps the simulation will run.
n_steps = 1000

# Define the stepping probability.
p = 0.5  # Equal probability of stepping right or left.

# We can simulate the random walk by flipping a coin at each step and testing
# its value with the probability.

position = 0
displacement = np.zeros(n_steps)

for i in range(n_steps):
    # Flip a coin.
    flip = np.random.rand()

    # Test what the value of this flip is.
    if flip < p:
        position = position + 1
    else:
        position = position - 1

    # We want some way to store the position of our walker at this step.
    displacement[i] = position

# And that's it! Let's plot eh trajectory as a function of time (step number)
# and see where it went.

plt.figure()
time = np.arange(0, n_steps, 1)
plt.plot(time, displacement)
plt.xlabel('time (number of steps)')
plt.ylabel('position')
plt.show()

# It's not very useful to see where a single particle diffused. Let's up our
# number of walkers to something huge. That will be representative of the
# behavior of our system. Let's modify our above loop to do this.
n_simulations = 1000
displacement = np.zeros((n_simulations, n_steps))
for i in range(n_simulations):

    # Reset the position for each simulation
    position = 0

    # Loop through each step.
    for j in range(n_steps):

        # Flip a coin.
        flip = np.random.rand()
        if flip < p:
            position = position + 1
        else:
            position = position - 1

        # Keep track of the displacement for each walker.
        displacement[i, j] = position

# Now, let's loop through each simulation and plot the trajectory.
plt.figure()
for i in range(n_simulations):
    plt.plot(time, displacement[i, :])

plt.xlabel('time (number of steps)')
plt.ylabel('position')
plt.show()

# Now we can see a bunch of different trajectories. Let's look at the
# distribution of ending positions. We can make a new array using the
# sotred displacements from the previous simulation.
ending_position = np.zeros(n_simulations)
for i in range(n_simulations):
    ending_position[i] = displacement[i, -1]

# Plot the distribution.
plt.figure()
plt.hist(ending_position, bins=100)
plt.xlabel('ending position')
plt.ylabel('counts')
plt.show()

# Let's see how the displacemtn would change as a function of the step number.
# In our simulation, we are storing positions that are both positive and
# netative. If we wanted to get an idea of the average distance traveled, we
# can simply square the final position and take the average of all of the
# simulations.
n_steps = [10, 100, 500, 1000, 5000, 10000]
final_position = np.zeros(n_simulations)
msd = np.zeros_like(n_steps)
for i in range(len(n_steps)):
    for j in range(n_simulations):
        position = 0
        for k in range(n_steps[i]):
            # Perorm the coin flip.
            flip = np.random.rand()
            if flip < p:
                position = position + 1
            else:
                position = position - 1

        # Save the final position.
        final_position[j] = position

    # Compute and store the mean square displacement.
    msd[i] = np.mean(final_position**2)

# Let's look at how it scales with time.
plt.figure()
plt.loglog(n_steps, np.sqrt(msd), 'o', label='simulation')

# We predict the root mean squared displacement would scale linear with the
# root of the number of steps we've taken
n_vec = np.logspace(-1, 5, 500)
plt.loglog(n_vec, np.sqrt(n_vec), '-', label='data')
plt.xlabel('time (number of steps)')
plt.ylabel('mean squared displacement')
plt.show()


text:
Detalles de Oportunidad

                        CIF

                            <input name="cif" value="" class="form-control" type="text">

                        * Nombre de la oportunidad

                            <input name="oportunidad" value="" class="form-control" type="text">

                        *Asignado a

                        <select name="idUsuario" class="form-control">

                            <option value=""></option>

                            <?php foreach($getUsuarios as $usuario): ?>

                                <option value="<?= $usuario->getId() ?>"><?= $usuario->getNombre() ?> <?= $usuario->getApellidos() ?></option>

                            <?php endforeach ?>

                        </select>

                        * Cuenta

                                <button data-toggle="modal" data-target=".search-modal" class="btn default" type="button"></button>

                            <input name="cuenta" value="" class="form-control" type="text">
                            <input type="hidden" value="" name="idcuenta">

                                <button data-toggle="modal" data-target=".add-modal" class="btn default" type="button"></button>

                        *Fase venta

                        <select name="venta" class="form-control">

                            <option value=""></option>

                            <?php foreach($getFaseVenta as $fase): ?>

                                <option value="<?= $fase->getId() ?>"><?= $fase->getValor() ?> </option>

                            <?php endforeach ?>

                        </select>


text:
.class public abstract Landroid/accounts/IAccountManagerResponse$Stub;
.super Landroid/os/Binder;
.source "IAccountManagerResponse.java"

# interfaces
.implements Landroid/accounts/IAccountManagerResponse;

# annotations
.annotation system Ldalvik/annotation/EnclosingClass;
    value = Landroid/accounts/IAccountManagerResponse;
.end annotation

.annotation system Ldalvik/annotation/InnerClass;
    accessFlags = 0x409
    name = "Stub"
.end annotation

.annotation system Ldalvik/annotation/MemberClasses;
    value = {
        Landroid/accounts/IAccountManagerResponse$Stub$Proxy;
    }
.end annotation

# static fields
.field private static final DESCRIPTOR:Ljava/lang/String; = "android.accounts.IAccountManagerResponse"

.field static final TRANSACTION_onError:I = 0x2

.field static final TRANSACTION_onResult:I = 0x1

# direct methods
.method public constructor <init>()V
    .locals 1

    .prologue
    .line 17
    invoke-direct {p0}, Landroid/os/Binder;-><init>()V

    .line 19
    const-string/jumbo v0, "android.accounts.IAccountManagerResponse"

    invoke-virtual {p0, p0, v0}, Landroid/accounts/IAccountManagerResponse$Stub;->attachInterface(Landroid/os/IInterface;Ljava/lang/String;)V

    .line 17
    return-void
.end method

.method public static asInterface(Landroid/os/IBinder;)Landroid/accounts/IAccountManagerResponse;
    .locals 2
    .param p0, "obj"    # Landroid/os/IBinder;

    .prologue
    const/4 v1, 0x0

    .line 27
    if-nez p0, :cond_0

    .line 28
    return-object v1

    .line 30
    :cond_0
    const-string/jumbo v1, "android.accounts.IAccountManagerResponse"

    invoke-interface {p0, v1}, Landroid/os/IBinder;->queryLocalInterface(Ljava/lang/String;)Landroid/os/IInterface;

    move-result-object v0

    .line 31
    .local v0, "iin":Landroid/os/IInterface;
    if-eqz v0, :cond_1

    instance-of v1, v0, Landroid/accounts/IAccountManagerResponse;

    if-eqz v1, :cond_1

    .line 32
    check-cast v0, Landroid/accounts/IAccountManagerResponse;

    .end local v0    # "iin":Landroid/os/IInterface;
    return-object v0

    .line 34
    .restart local v0    # "iin":Landroid/os/IInterface;
    :cond_1
    new-instance v1, Landroid/accounts/IAccountManagerResponse$Stub$Proxy;

    invoke-direct {v1, p0}, Landroid/accounts/IAccountManagerResponse$Stub$Proxy;-><init>(Landroid/os/IBinder;)V

    return-object v1
.end method

# virtual methods
.method public asBinder()Landroid/os/IBinder;
    .locals 0

    .prologue
    .line 38
    return-object p0
.end method

.method public onTransact(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z
    .locals 5
    .param p1, "code"    # I
    .param p2, "data"    # Landroid/os/Parcel;
    .param p3, "reply"    # Landroid/os/Parcel;
    .param p4, "flags"    # I
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Landroid/os/RemoteException;
        }
    .end annotation

    .prologue
    const/4 v4, 0x1

    .line 42
    sparse-switch p1, :sswitch_data_0

    .line 73
    invoke-super {p0, p1, p2, p3, p4}, Landroid/os/Binder;->onTransact(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z

    move-result v3

    return v3

    .line 46
    :sswitch_0
    const-string/jumbo v3, "android.accounts.IAccountManagerResponse"

    invoke-virtual {p3, v3}, Landroid/os/Parcel;->writeString(Ljava/lang/String;)V

    .line 47
    return v4

    .line 51
    :sswitch_1
    const-string/jumbo v3, "android.accounts.IAccountManagerResponse"

    invoke-virtual {p2, v3}, Landroid/os/Parcel;->enforceInterface(Ljava/lang/String;)V

    .line 53
    invoke-virtual {p2}, Landroid/os/Parcel;->readInt()I

    move-result v3

    if-eqz v3, :cond_0

    .line 54
    sget-object v3, Landroid/os/Bundle;->CREATOR:Landroid/os/Parcelable$Creator;

    invoke-interface {v3, p2}, Landroid/os/Parcelable$Creator;->createFromParcel(Landroid/os/Parcel;)Ljava/lang/Object;

    move-result-object v1

    check-cast v1, Landroid/os/Bundle;

    .line 59
    :goto_0
    invoke-virtual {p0, v1}, Landroid/accounts/IAccountManagerResponse$Stub;->onResult(Landroid/os/Bundle;)V

    .line 60
    return v4

    .line 57
    :cond_0
    const/4 v1, 0x0

    .local v1, "_arg0":Landroid/os/Bundle;
    goto :goto_0

    .line 64
    .end local v1    # "_arg0":Landroid/os/Bundle;
    :sswitch_2
    const-string/jumbo v3, "android.accounts.IAccountManagerResponse"

    invoke-virtual {p2, v3}, Landroid/os/Parcel;->enforceInterface(Ljava/lang/String;)V

    .line 66
    invoke-virtual {p2}, Landroid/os/Parcel;->readInt()I

    move-result v0

    .line 68
    .local v0, "_arg0":I
    invoke-virtual {p2}, Landroid/os/Parcel;->readString()Ljava/lang/String;

    move-result-object v2

    .line 69
    .local v2, "_arg1":Ljava/lang/String;
    invoke-virtual {p0, v0, v2}, Landroid/accounts/IAccountManagerResponse$Stub;->onError(ILjava/lang/String;)V

    .line 70
    return v4

    .line 42
    :sswitch_data_0
    .sparse-switch
        0x1 -> :sswitch_1
        0x2 -> :sswitch_2
        0x5f4e5446 -> :sswitch_0
    .end sparse-switch
.end method


text:
<!--
@Author: Yingya Zhang <zyy>
@Date:   2016-07-09 14:11:00
@Email:  zyy7259@gmail.com
@Last modified by:   zyy
@Last modified time: 2016-07-09 14:19:79
-->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Class</title>
</head>
<body>
  <script>
    class Shape {
      constructor (numberOfSides) {
        console.log('constructing shape')
        this.type = 'shape'
        this.numberOfSides = numberOfSides || 0
      }
      printDescription () {
        console.log(this.type + ' has ' + this.numberOfSides + ' sides')
      }
    }

    class Square extends Shape {
      constructor (sideLength) {
        super(4)
        console.log('constructing square')
        this.type = 'square'
        this.sideLength = sideLength || 0
      }
      printDescription () {
        super.printDescription()
        console.log('square sideLength ' + this.sideLength)
      }
      static defaultSquare () {
        return new Square(42)
      }
    }
    var square = Square.defaultSquare()
    square.printDescription()
  </script>
</body>
</html>


text:
from __future__ import unicode_literals, absolute_import

import json as _json

from . import fernet_encode, fernet_decode, force_text

MIMETYPE = 'application/x-fernet-json'

register_args = (
    fernet_encode(_json.dumps),
    fernet_decode(force_text(_json.loads)),
    MIMETYPE,
    'utf-8',
)


text:
require 'spec_helper'

describe 'crowd::install' do

  on_supported_os.each do |os, facts|
    context "on #{os}" do
      let(:facts) { facts }
      context 'defaults' do
        let :pre_condition do
          "class { 'crowd': }"
        end
        it { is_expected.to contain_user('crowd').with({
          :home     => '/var/local/crowd',
          :password => '*',
        })}

        case facts[:osfamily]
        when 'Debian'
          it { is_expected.to contain_user('crowd').with({:shell => '/usr/sbin/nologin'}) }
        else
          it { is_expected.to contain_user('crowd').with({:shell => '/sbin/nologin'}) }
        end

        it { is_expected.to contain_group('crowd') }

        it { is_expected.to contain_file('/opt/crowd') }
        it { is_expected.to contain_file('/opt/crowd/atlassian-crowd-2.11.1-standalone') }
        it { is_expected.to contain_file('/var/local/crowd') }

        it { is_expected.to contain_staging__file('atlassian-crowd-2.11.1.tar.gz').with({
          :source => 'https://www.atlassian.com/software/crowd/downloads/binary/atlassian-crowd-2.11.1.tar.gz',
        })}

        it { is_expected.to contain_staging__extract('atlassian-crowd-2.11.1.tar.gz').with({
          :target  => '/opt/crowd/atlassian-crowd-2.11.1-standalone',
          :creates => '/opt/crowd/atlassian-crowd-2.11.1-standalone/apache-tomcat',
          :user    => 'crowd',
          :group   => 'crowd',
        })}

        it { is_expected.to contain_staging__file('jdbc driver').with({
          :source => 'http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.jar',
          :target => '/opt/crowd/atlassian-crowd-2.11.1-standalone/apache-tomcat/lib/mysql-connector-java-5.1.36.jar',
        })}

        it { is_expected.to contain_exec('chown_/opt/crowd/atlassian-crowd-2.11.1-standalone').with({
          :command => 'chown -R crowd:crowd /opt/crowd/atlassian-crowd-2.11.1-standalone',
        })}

        it { is_expected.to contain_file('/var/log/crowd') }
        it { is_expected.to contain_file('/opt/crowd/atlassian-crowd-2.11.1-standalone/apache-tomcat/logs').with({
          'ensure' => 'link',
          'target' => '/var/log/crowd'
        })}

      end
    end

    context 'update crowd' do
      let(:facts) do
        facts.merge({
          :crowd_version => '2.9.0'
        })
      end
      let :pre_condition do
        "class { 'crowd': version => '2.11.1' }"
      end
      it { is_expected.to contain_notify('Updating Crowd from version 2.9.0 to 2.11.1') }
      it { is_expected.to contain_exec('stop crowd for update').with({
        :command => 'service crowd stop && sleep 10',
      })}
    end

    context 'custom parameters' do
      let(:facts) { facts }

      describe 'installdir' do
        let :pre_condition do
          "class { 'crowd': installdir => '/svr/crowd' }"
        end
        it { is_expected.to contain_file('/svr/crowd') }
        it { is_expected.to contain_file('/svr/crowd/atlassian-crowd-2.11.1-standalone') }
        it { is_expected.to contain_staging__extract('atlassian-crowd-2.11.1.tar.gz').with({
          :target => '/svr/crowd/atlassian-crowd-2.11.1-standalone',
          :creates => '/svr/crowd/atlassian-crowd-2.11.1-standalone/apache-tomcat',
        })}
      end

      describe 'homedir' do
        let :pre_condition do
          "class { 'crowd': homedir => '/home/crowd' }"
        end
        it { is_expected.to contain_file('/home/crowd') }
      end

      describe 'version' do
        let :pre_condition do
          "class { 'crowd': version => '2.11.1' }"
        end
        it { is_expected.to contain_file('/opt/crowd/atlassian-crowd-2.11.1-standalone') }

        it { is_expected.to contain_staging__file('atlassian-crowd-2.11.1.tar.gz').with({
          :source => 'https://www.atlassian.com/software/crowd/downloads/binary/atlassian-crowd-2.11.1.tar.gz',
        })}

        it { is_expected.to contain_staging__extract('atlassian-crowd-2.11.1.tar.gz').with({
          :target => '/opt/crowd/atlassian-crowd-2.11.1-standalone',
          :creates => '/opt/crowd/atlassian-crowd-2.11.1-standalone/apache-tomcat',
        })}
      end

      describe 'postgres' do
        let :pre_condition do
          "class { 'crowd':
            db => 'postgres',
            iddb => 'postgres',
          }"
        end
        it { is_expected.not_to contain_staging__file('jdbc driver') }
      end

      describe 'mysql_driver using dns' do
        let :pre_condition do
          "class { 'crowd':
            mysql_driver => 'http://mirror.foo.com/mysql-connector-java.jar',
          }"
        end
        it { is_expected.to contain_staging__file('jdbc driver').with({
          :source => 'http://mirror.foo.com/mysql-connector-java.jar',
        })}
      end

      describe 'mysql_driver using ip and port' do
        let :pre_condition do
          "class { 'crowd':
            mysql_driver => 'http://169.254.0.0/mysql-connector-java.jar',
          }"
        end
        it { is_expected.to contain_staging__file('jdbc driver').with({
          :source => 'http://169.254.0.0/mysql-connector-java.jar',
        })}
      end

      describe 'download_url using dns' do
        let :pre_condition do
          "class { 'crowd':
            download_url => 'http://mirror.foo.com/',
          }"
        end
        it { is_expected.to contain_staging__file('atlassian-crowd-2.11.1.tar.gz').with({
          :source => 'http://mirror.foo.com/atlassian-crowd-2.11.1.tar.gz',
        })}
      end

      describe 'download_url from ip and port' do
        let :pre_condition do
          "class { 'crowd':
            download_url => 'http://169.254.0.0/',
          }"
        end
        it { is_expected.to contain_staging__file('atlassian-crowd-2.11.1.tar.gz').with({
          :source => 'http://169.254.0.0/atlassian-crowd-2.11.1.tar.gz',
        })}
      end

      describe 'download_driver false' do
        let :pre_condition do
          "class { 'crowd': download_driver => false }"
        end
        it { is_expected.not_to contain_staging__file('jdbc driver') }
      end
    end
  end
end