Beispiel #1
0
 def format(self, object, context, maxlevels, level):
     repr, readable, recursive = PrettyPrinter.format(
         self, object, context, maxlevels, level)
     if isinstance(object, int):
         return "0x{0:x}".format(object), readable, recursive
     else:
         return repr, readable, recursive
Beispiel #2
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, basestring):
         if isinstance(object, str):
             object = object.decode(db_encoding)
         return "'%s'" % unicode(object).encode(term_encoding), True, False
     else:
         return PrettyPrinter.format(self, object, context, maxlevels, level)
Beispiel #3
0
    def format(self, obj, context, maxlevels, level):
        repr_string, isreadable, isrecursive = PrettyPrinter.format(
            self, obj, context, maxlevels, level)

        if isreadable:
            repr_string = helpers.rtl(repr_string)

        return repr_string, isreadable, isrecursive
Beispiel #4
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, basestring):
         if isinstance(object, str):
             object = object.decode(db_encoding)
         return "'%s'" % unicode(object).encode(term_encoding), True, False
     else:
         return PrettyPrinter.format(self, object, context, maxlevels,
                                     level)
 def format(self, *args, **kwargs):
     repr, readable, recursive = PrettyPrinter.format(self, *args, **kwargs)
     if repr:
         if repr[0] in ('"', "'"):
             repr = repr.decode('string_escape')
         elif repr[0:2] in ("u'", 'u"'):
             repr = repr.decode('unicode_escape').encode(sys.stdout.encoding)
     return repr, readable, recursive
 def format(self, object, context, maxlevels, level):
     """
     Format an item in the result.
     Could be a dictionary key, value, etc..
     """
     try:
         return PrettyPrinter.format(self, object, context, maxlevels, level)
     except HANDLED_EXCEPTIONS as e:
         return _format_exception(e), True, False
Beispiel #7
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, unicode):
         return repr(object).lstrip('u'), True, False
     if isinstance(object, str):
         return 'b' + repr(object), True, False
     try:
         return PrettyPrinter.format(self, object, context, maxlevels, level)
     except:
         return _unrepresentable_object(object), True, False
Beispiel #8
0
 def format(self, object, context, maxlevels, level):
     # TODO: (Python 3) use ``super(PrettyPrinter, self)`` instead of
     #     ``pprint.PrettyPrinter``
     string, readable, recursive = \
         _PrettyPrinter.format(self, object, context, maxlevels, level)
     if isinstance(object, str) or isinstance(object, unicode):
         string = object
         readable = False
     return string, readable, recursive
Beispiel #9
0
 def format(self, object, context, maxlevels, level):
     try:
         if is_unicode(object):
             return repr(object).lstrip('u'), True, False
         if is_bytes(object):
             return 'b' + repr(object).lstrip('b'), True, False
         return PrettyPrinter.format(self, object, context, maxlevels, level)
     except:
         return _unrepresentable_object(object), True, False
Beispiel #10
0
 def format(self, *args, **kwargs):
     repr, readable, recursive = PrettyPrinter.format(self, *args, **kwargs)
     if repr:
         if repr[0] in ('"', "'"):
             repr = repr.decode('string_escape')
         elif repr[0:2] in ("u'", 'u"'):
             repr = repr.decode('unicode_escape').encode(
                 sys.stdout.encoding)
     return repr, readable, recursive
Beispiel #11
0
 def format(self, object, context, maxlevels, level):
     """
     Format an item in the result.
     Could be a dictionary key, value, etc..
     """
     try:
         return PrettyPrinter.format(self, object, context, maxlevels, level)
     except HANDLED_EXCEPTIONS as e:
         return _format_exception(e), True, False
Beispiel #12
0
 def format(self, object, context, maxlevels, level):
     try:
         if is_unicode(object):
             return repr(object).lstrip('u'), True, False
         if is_bytes(object):
             return 'b' + repr(object).lstrip('b'), True, False
         return PrettyPrinter.format(self, object, context, maxlevels, level)
     except:
         return _unrepresentable_object(object), True, False
Beispiel #13
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, unicode):
         return repr(object).lstrip('u'), True, False
     if isinstance(object, str):
         return 'b' + repr(object), True, False
     try:
         return PrettyPrinter.format(self, object, context, maxlevels,
                                     level)
     except:
         return _unrepresentable_object(object), True, False
Beispiel #14
0
 def format(self, *args, **kwargs):
     repr, readable, recursive = PrettyPrinter.format(self, *args, **kwargs)
     if repr:
         if repr[0] in ('"', "'"):
             repr = repr.decode("string_escape")
         elif repr[0:2] in ("u'", 'u"'):
             if hasattr(sys.stdout, "encoding"):
                 enc = sys.stdout.encoding and sys.stdout.encoding or "UTF-8"
             else:
                 enc = "UTF-8"
             repr = repr.decode("unicode_escape").encode(enc)
     return repr, readable, recursive
Beispiel #15
0
    def format(self, obj, context, maxlevels, level):
        repr_string, isreadable, isrecursive = PrettyPrinter.format(
            self,
            obj,
            context,
            maxlevels,
            level
        )

        if isreadable:
            repr_string = helpers.rtl(repr_string)

        return repr_string, isreadable, isrecursive
Beispiel #16
0
 def format(self, obj, context, maxlevels, level):
     if isinstance(obj, SWRealReference):
         return (str(obj), False, False)
     else:
         return PrettyPrinter.format(self, obj, context, maxlevels, level)
Beispiel #17
0
    def execute(self,
                sql,
                binds: Dict[str, object] = None,
                show_sql=False,
                statement_descr="No description provided",
                returning=None,
                verbose=False):
        """
        :param sql: str - sql text
        :param binds: dictionary -  bind name and value
        :param show_sql: - boolean log the sql
        :param statement_descr:
        :param returning - String "returning column_name" if postgres, if sqlite not None will return the primary key inserted

        :return:

        The dialect is inferred from type(cursor)

        When inserting into a table and the inserted primary key is desired,
        pass "returning table_name_id" where table_name_id is the column to be returned.

        Bind variables are   from %(BIND_NAME)s to :BIND_NAME if present in
        the SQL and the dialect is not *dialacts.DIALECT_POSTGRES*

        """

        logger = logging.getLogger('CursorHelper')
        logger.debug("verbose is %s" % verbose)
        if verbose:
            logger.info("dialect %s" % self.dialect)

        logger.debug("dialect is %s =======================" % self.dialect)
        logger.debug("execute:\nsql:\n%s\n binds:\n%s\n returning: %s" %
                     (sql, binds, returning))
        if not self.dialect == dialects.DIALECT_POSTGRES:
            processed_sql = to_colon_binds(sql)
        else:
            processed_sql = sql

        if returning:
            logger.debug(
                "test returning: %s dialect is '%s' dialect_postgres is '%s'" %
                (returning, self.dialect, dialects.DIALECT_POSTGRES))
            if self.dialect == dialects.DIALECT_POSTGRES:
                processed_sql += returning

        logger.debug("processed_sql\n %s" % processed_sql)

        start_time = time.time()
        pretty_binds = json.dumps(binds,
                                  indent=4,
                                  sort_keys=True,
                                  cls=JsonEncoder)
        #print("processed sql\n%s\nbinds %s\n" % (processed_sql, pretty_binds))

        if self.dialect == dialects.DIALECT_SQLITE:
            if binds is not None:
                for k, v in binds.items():
                    if v is not None and isinstance(v, decimal.Decimal):
                        binds[k] = float(v)

        # if logger.isEnabledFor(logging.DEBUG):
        #     logger.exception("----- " + processed_sql)
        try:
            if sql is None:
                if binds is None:
                    logger.debug("No sql, no binds")
                    result = self.cursor.execute()
                else:
                    logger.debug("no sql, binds")
                    result = self.cursor.execute(None, binds)
            else:
                if binds is None:
                    logger.debug("sql, no binds")
                    result = self.cursor.execute(processed_sql)
                else:
                    logger.debug("==== executing:\n%s\nwith binds\n%s" %
                                 (processed_sql, pretty_binds))
                    result = self.cursor.execute(processed_sql, binds)
        except Exception as e:
            # bind_str = ""
            # for k, v  in binds.items():
            #     bind_str += ("name: %s value: %s type: %s\n" % (k, v, str(type(v))))
            bind_str = pretty.format(binds)
            message = "While processing sql:\n %s with binds:\n%s\n%s" % (
                processed_sql, pretty_binds, bind_str)
            logger.exception(message)
            raise Exception(message)

        logger.debug("result is %s" % result)
        # if result is None:
        #     rows = self.cursor  # postgress driver returns rows in curs
        # else:
        #     rows = result
        logging.debug("just before test for returning")
        if returning:

            logger.debug("is returning %s" % returning)
            if self.dialect == dialects.DIALECT_POSTGRES:
                for row in self.cursor:
                    retval = row[0]
                logging.debug("retval from row %s" % retval)
            elif self.dialect == dialects.DIALECT_SQLITE:
                retval = self.cursor.lastrowid
                logging.debug("retval last rowid %s " % retval)
            else:
                raise Exception("logic error should never get here")
        else:

            logger.debug("is not returning")
            if result is None:
                retval = self.cursor  # postgress driver returns rows in curs
            else:
                logger.debug("returning ")
                retval = result
        logger.debug("returning '%s' dialect %s retval %s" %
                     (returning, self.dialect, retval))
        self.description = self.cursor.description
        self.rows = result

        if self.cursor.description is not None:
            self.column_names = [i[0] for i in self.cursor.description]
        elapsed_time = time.time() - start_time
        if logger.level >= logging.DEBUG:
            logger.debug("elapsed %s %s" % (elapsed_time, statement_descr))
        logger.debug("retval is %s " % retval)
        return retval
Beispiel #18
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, unicode):
         return object.encode('utf8'), True, False
     return PrettyPrinter.format(self, object, context, maxlevels, level)
Beispiel #19
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, float):
         return ('%.4f' % object), True, False
     else:
         return PrettyPrinter.format(self, object, context,
                                     maxlevels, level)
Beispiel #20
0
 def format(self, object, context, maxlevels, level):
     try:
         return PrettyPrinter.format(self, object, context, maxlevels,
                                     level)
     except:
         return _unrepresentable_object(object), True, False
Beispiel #21
0
 def format(self, obj, context, maxlevels, level):
     if isinstance(obj, SWRealReference):
         return (str(obj), False, False)
     else:
         return PrettyPrinter.format(self, obj, context, maxlevels, level)
Beispiel #22
0
		def format(self, object, context, maxlevels, level):
			r = repr(object)
			if len(r) > self._width:
				return r[:self._width-4]+'...'+r[-1], False, False
			else:
				return PrettyPrinter.format(self, object, context, maxlevels, level)
Beispiel #23
0
 def format(self, object, context, maxlevels, level):
     if isinstance(object, unicode):
         return (object.encode('utf8'), True, False)
     else:
         return PrettyPrinter.format(self, object, context, maxlevels,
                                     level)