Ejemplo n.º 1
0
 def notifyDbEvent(self, tblobj, record, event, old_record=None):
     """TODO
     
     :param tblobj: the :ref:`database table <table>` object
     :param record: TODO
     :param event: TODO
     :param old_record: TODO. """
     currentEnv = self.db.currentEnv
     if currentEnv.get('hidden_transaction'):
         return
     if not currentEnv.get('env_transaction_id'):
         self.db.updateEnv(env_transaction_id= getUuid(),dbevents=dict())
     broadcast = tblobj.attributes.get('broadcast')
     if broadcast is not False and broadcast != '*old*':
         dbevents=currentEnv['dbevents']
         r=dict(dbevent=event,pkey=record.get(tblobj.pkey))
         if broadcast and broadcast is not True:
             for field in broadcast.split(','):
                 newvalue = record.get(field)
                 r[field] = self.catalog.asTypedText(newvalue) #2011/01/01::D
                 if old_record:
                     oldvalue = old_record.get(field)
                     if newvalue!=oldvalue:
                         r['old_%s' %field] = self.catalog.asTypedText(old_record.get(field))
         dbevents.setdefault(tblobj.fullname,[]).append(r)
     audit_mode = tblobj.attributes.get('audit')
     if audit_mode:
         self.db.table('adm.audit').audit(tblobj,event,audit_mode=audit_mode,record=record, old_record=old_record)
Ejemplo n.º 2
0
 def deferToCommit(self,cb,*args,**kwargs):
     deferreds = self.currentEnv.setdefault('deferredCalls',Bag())
     deferredId = kwargs.pop('_deferredId',None)
     if not deferredId:
         deferredId = getUuid()
     if not deferredId in deferreds:
         deferreds.setItem(deferredId,(cb,args,kwargs))
Ejemplo n.º 3
0
    def sendMessage(self, target='page', package=None, topic=None, msg=None):
        msgbag = Bag()
        if target == 'page':
            msgbag['page'] = msg
            fname = self.pageLocalDocument(os.path.join('_messages', '%s.xml' % getUuid()))
            msg.toXml(fname)
        elif target == 'connection':
            msgbag['connection'] = msg
            msgid = '%s.xml' % getUuid()
            for page in os.listdir(self.connectionFolder):
                pagedir = os.path.join(self.connectionFolder, page)
                if os.isdir(pagedir):
                    msg.toXml(os.path.join(pagedir, '_messages', msgid))

        elif target == 'all':
            pass
Ejemplo n.º 4
0
 def execute(self, sql, sqlargs=None, cursor=None, cursorname=None, autocommit=False, dbtable=None,storename=None):
     """Execute the sql statement using given kwargs. Return the sql cursor
     
     :param sql: the sql statement
     :param sqlargs: optional sql arguments
     :param cursor: an sql cursor
     :param cursorname: the name of the cursor
     :param autocommit: if ``True``, at the end of the execution runs the :meth:`commit()` method
     :param dbtable: specify the :ref:`database table <table>`. More information in the
                     :ref:`dbtable` section (:ref:`dbselect_examples_simple`)
     """
     # transform list and tuple parameters in named values.
     # Eg.   WHERE foo IN:bar ----> WHERE foo in (:bar_1, :bar_2..., :bar_n)
     #if 'adm.user' == dbtable:
     #    print x
     envargs = dict([('env_%s' % k, v) for k, v in self.currentEnv.items()])
     if not 'env_workdate' in envargs:
         envargs['env_workdate'] = self.workdate
     envargs.update(sqlargs or {})
     if storename is False:
         storename = self.rootstore
     storename = storename or envargs.get('env_storename', self.rootstore)
     sqlargs = envargs
     if dbtable and self.table(dbtable).use_dbstores(**sqlargs) is False:
         storename = self.rootstore
     with self.tempEnv(storename=storename):
         sql = self.adapter.adaptTupleListSet(sql,sqlargs)
         sql = self.adapter.empty_IN_patch(sql)
         sql, sqlargs = self.adapter.prepareSqlText(sql, sqlargs)
         #gnrlogger.info('Executing:%s - with kwargs:%s \n\n',sql,unicode(kwargs))
         #print 'sql:\n',sql
         try:
             t_0 = time()
             if not cursor:
                 if cursorname:
                     if cursorname == '*':
                         cursorname = 'c%s' % re.sub('\W', '_', getUuid())
                     cursor = self.adapter.cursor(self.connection, cursorname)
                 else:
                     cursor = self.adapter.cursor(self.connection)
             if isinstance(cursor, list):
                 for c in cursor:
                     c.execute(sql, sqlargs)
             else:
                 cursor.execute(sql, sqlargs)
             if self.debugger:
                 self.debugger(debugtype='sql', sql=sql, sqlargs=sqlargs, dbtable=dbtable,delta_time=time()-t_0)
         
         except Exception, e:
             #print sql
             gnrlogger.warning('error executing:%s - with kwargs:%s \n\n', sql, unicode(sqlargs))
             if self.debugger:
                 self.debugger(debugtype='sql', sql=sql, sqlargs=sqlargs, dbtable=dbtable, error=str(e))
             print str('error %s executing:%s - with kwargs:%s \n\n' % (
             str(e), sql, unicode(sqlargs).encode('ascii', 'ignore')))
             self.rollback()
             raise
         if autocommit:
             self.commit()
Ejemplo n.º 5
0
    def sendMessage(self, target='page', package=None, topic=None, msg=None):
        msgbag = Bag()
        if target == 'page':
            msgbag['page'] = msg
            fname = self.pageLocalDocument(
                os.path.join('_messages', '%s.xml' % getUuid()))
            msg.toXml(fname)
        elif target == 'connection':
            msgbag['connection'] = msg
            msgid = '%s.xml' % getUuid()
            for page in os.listdir(self.connectionFolder):
                pagedir = os.path.join(self.connectionFolder, page)
                if os.isdir(pagedir):
                    msg.toXml(os.path.join(pagedir, '_messages', msgid))

        elif target == 'all':
            pass
Ejemplo n.º 6
0
 def execute(self, sql, sqlargs=None, cursor=None, cursorname=None, autocommit=False, dbtable=None,storename=None):
     """Execute the sql statement using given kwargs. Return the sql cursor
     
     :param sql: the sql statement
     :param sqlargs: optional sql arguments
     :param cursor: an sql cursor
     :param cursorname: the name of the cursor
     :param autocommit: if ``True``, at the end of the execution runs the :meth:`commit()` method
     :param dbtable: specify the :ref:`database table <table>`. More information in the
                     :ref:`dbtable` section (:ref:`dbselect_examples_simple`)
     """
     # transform list and tuple parameters in named values.
     # Eg.   WHERE foo IN:bar ----> WHERE foo in (:bar_1, :bar_2..., :bar_n)
     #if 'adm.user' == dbtable:
     #    print x
     envargs = dict([('env_%s' % k, v) for k, v in self.currentEnv.items()])
     if not 'env_workdate' in envargs:
         envargs['env_workdate'] = self.workdate
     envargs.update(sqlargs or {})
     if storename is False:
         storename = self.rootstore
     storename = storename or envargs.get('env_storename', self.rootstore)
     sqlargs = envargs
     if dbtable and self.table(dbtable).use_dbstores(**sqlargs) is False:
         storename = self.rootstore
     with self.tempEnv(storename=storename):
         sql, sqlargs = self.adapter.prepareSqlText(sql, sqlargs)
         #gnrlogger.info('Executing:%s - with kwargs:%s \n\n',sql,unicode(kwargs))
         #print 'sql:\n',sql
         try:
             t_0 = time()
             if not cursor:
                 if cursorname:
                     if cursorname == '*':
                         cursorname = 'c%s' % re.sub('\W', '_', getUuid())
                     cursor = self.adapter.cursor(self.connection, cursorname)
                 else:
                     cursor = self.adapter.cursor(self.connection)
             if isinstance(cursor, list):
                 for c in cursor:
                     c.execute(sql, sqlargs)
             else:
                 cursor.execute(sql, sqlargs)
             if self.debugger:
                 self.debugger(debugtype='sql', sql=sql, sqlargs=sqlargs, dbtable=dbtable,delta_time=time()-t_0)
         
         except Exception, e:
             #print sql
             gnrlogger.warning('error executing:%s - with kwargs:%s \n\n', sql, unicode(sqlargs))
             if self.debugger:
                 self.debugger(debugtype='sql', sql=sql, sqlargs=sqlargs, dbtable=dbtable, error=str(e))
             print str('error %s executing:%s - with kwargs:%s \n\n' % (
             str(e), sql, unicode(sqlargs).encode('ascii', 'ignore')))
             self.rollback()
             raise
         if autocommit:
             self.commit()
Ejemplo n.º 7
0
    def execute(self, sql, sqlargs=None, cursor=None, cursorname=None, autocommit=False, dbtable=None):
        """Execute the sql statement using given kwargs
        
        :param sql: add???
        :param sqlargs: add???. Default value is ``None``
        :param cursor: add???. Default value is ``None``
        :param cursorname: add???. Default value is ``None``
        :param autocommit: add???. Default value is ``None``
        :param dbtable: add???. Default value is ``None``
        :returns: add???
        """
        # transform list and tuple parameters in named values.
        # Eg.   WHERE foo IN:bar ----> WHERE foo in (:bar_1, :bar_2..., :bar_n)
        envargs = dict([("env_%s" % k, v) for k, v in self.currentEnv.items()])
        if not "env_workdate" in envargs:
            envargs["env_workdate"] = self.workdate
        envargs.update(sqlargs or {})
        sqlargs = envargs
        if dbtable and not self.table(dbtable).use_dbstores():
            storename = "_main_db"
        else:
            storename = sqlargs.pop("storename", self.currentEnv.get("storename", "_main_db"))
        with self.tempEnv(storename=storename):
            for k, v in [(k, v) for k, v in sqlargs.items() if isinstance(v, list) or isinstance(v, tuple)]:
                sqllist = "(%s) " % ",".join([":%s%i" % (k, i) for i, ov in enumerate(v)])

                sqlargs.pop(k)
                sqlargs.update(dict([("%s%i" % (k, i), ov) for i, ov in enumerate(v)]))
                sql = re.sub(":%s(\W|$)" % k, sqllist, sql)
            sql = re.sub(IN_OPERATOR_PATCH, " FALSE", sql)
            sql, sqlargs = self.adapter.prepareSqlText(sql, sqlargs)
            # gnrlogger.info('Executing:%s - with kwargs:%s \n\n',sql,unicode(kwargs))
            # print 'sql:\n',sql
            try:
                if not cursor:
                    if cursorname:
                        if cursorname == "*":
                            cursorname = "c%s" % re.sub("\W", "_", getUuid())
                        cursor = self.adapter.cursor(self.connection, cursorname)
                    else:
                        cursor = self.adapter.cursor(self.connection)
                cursor.execute(sql, sqlargs)
                if self.debugger:
                    self.debugger(debugtype="sql", sql=sql, sqlargs=sqlargs, dbtable=dbtable)
            except Exception, e:
                # print sql
                gnrlogger.warning("error executing:%s - with kwargs:%s \n\n", sql, unicode(sqlargs))
                if self.debugger:
                    self.debugger(debugtype="sql", sql=sql, sqlargs=sqlargs, dbtable=dbtable, error=str(e))
                print str(
                    "error %s executing:%s - with kwargs:%s \n\n"
                    % (str(e), sql, unicode(sqlargs).encode("ascii", "ignore"))
                )
                self.rollback()
                raise
            if autocommit:
                self.commit()
Ejemplo n.º 8
0
 def createMessageRecord(self, emailid):
     new_mail = dict(account_id=self.account_id)
     new_mail['id'] = getUuid()
     new_mail['uid'] = emailid
     resp, data = self.imap.uid('fetch', emailid, "(RFC822)")
     email_body = data[0][1]
     mail = email.message_from_string(email_body)
     #mail = email.message_from_string(unicode(email_body.decode(encoding).encode('utf8')))
     onCreatingCallbacs = [
         fname for fname in dir(self.messages_table)
         if fname.startswith('onCreatingMessage_')
     ]
     if onCreatingCallbacs:
         make_message = False
         for fname in onCreatingCallbacs:
             make_message = make_message or getattr(
                 self.messages_table, fname)(mail) is not False
         if make_message is False:
             return False
     encoding = mail.get_content_charset()
     b = Bag(mail)
     for k, v in b.items():
         if isinstance(v, basestring):
             b[k] = self.smartConverter(v, encoding)
     new_mail['email_bag'] = b
     self.fillHeaders(mail, new_mail, encoding)
     if self.messages_table.spamChecker(new_mail) is True:
         return
     if mail.get_content_maintype() not in ('multipart', 'image'):
         content = mail.get_payload(decode=True)
         encoding = mail.get_content_charset()
         #encoding = chardet.detect(content)['encoding']
         new_mail['body'] = self.smartConverter(content, encoding)
         new_mail['body_plain'] = new_mail['body']
     else:
         for part in mail.walk():
             part_content_type = part.get_content_type()
             if part_content_type.startswith('multipart'):
                 continue
             content_disposition = part.get('Content-Disposition')
             if content_disposition is None and part_content_type in (
                     'text/html', 'text/plain'):
                 self.parseBody(part,
                                new_mail,
                                part_content_type=part_content_type)
             else:
                 self.parseAttachment(part,
                                      new_mail,
                                      part_content_type=part_content_type)
     if new_mail.get('body'):
         g = re.search("<body(.*?)>(.*?)</body>", new_mail['body'],
                       re.S | re.DOTALL)
         new_mail['body'] = g.group(2) if g else new_mail['body']
     else:
         new_mail['body'] = new_mail.get('body_plain')
     return new_mail
Ejemplo n.º 9
0
 def newPkeyValue(self):
     """Get a new unique id to use as primary key on the current table
     
     :returns: add???
     """
     pkey = self.model.pkey
     if self.model.column(pkey).dtype in ('L', 'I', 'R'):
         lastid = self.query(columns='max($%s)' % pkey, group_by='*').fetch()[0] or [0]
         return lastid[0] + 1
     else:
         return getUuid()
Ejemplo n.º 10
0
 def run(self, dest_path=None,start_ts=None,**kwargs):
     call_list = ['sudo', 'pgbadger',self.logfile]
     filename = '%s.html' %getUuid()
     dest_path = self.parent.getStaticPath(self.output_folder,'pgbadger',filename,autocreate=-1)
     call_list.append('-o')
     call_list.append(dest_path)
     call_list.append('-f')
     call_list.append(self.format)
     if start_ts:
         call_list.append('-b')
         call_list.append(str(start_ts).split('.')[0])
     result = call(call_list)
     if result !=0:
         return None
     return self.parent.getStaticUrl(self.output_folder,'pgbadger',filename)
Ejemplo n.º 11
0
 def createEvent(self,
                 uid=None,
                 dtstamp=None,
                 dtstart=None,
                 dtend=None,
                 summary=None,
                 calendar=None):
     tpl = """BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:%(prodid)s\r\nBEGIN:VEVENT\r\nUID:%(uid)s\r\nDTSTAMP:%(dtstamp)s\r\nDTSTART:%(dtstart)s\r\nDTEND:%(dtend)s\r\nSUMMARY:%(summary)s\r\nEND:VEVENT\r\nEND:VCALENDAR"""
     calendar = self.calendars.get(calendar)
     assert calendar, 'Missing calendar'
     data = tpl % dict(uid=uid or getUuid(),
                       dtstamp=dt(dtstamp or datetime.now()),
                       dtstart=dt(dtstart),
                       dtend=dt(dtend),
                       summary=summary,
                       prodid='VCALENDAR genropy')
     event = caldav.Event(self.client, data=data, parent=calendar).save()
Ejemplo n.º 12
0
 def createMessageRecord(self, emailid):
     new_mail = dict(account_id=self.account_id)
     new_mail['id'] = getUuid()
     new_mail['uid'] = emailid
     resp, data = self.imap.uid('fetch',emailid, "(RFC822)")
     email_body = data[0][1]
     mail = email.message_from_string(email_body)
     #mail = email.message_from_string(unicode(email_body.decode(encoding).encode('utf8')))
     onCreatingCallbacs = [fname for fname in dir(self.messages_table) if fname.startswith('onCreatingMessage_')]
     if onCreatingCallbacs:
         make_message = False
         for fname in onCreatingCallbacs:
             make_message = make_message or getattr(self.messages_table,fname)(mail) is not False
         if make_message is False:
             return False
     encoding = mail.get_content_charset()
     b = Bag(mail)
     for k,v in b.items():
         if isinstance(v,basestring):
             b[k] = self.smartConverter(v,encoding)
     new_mail['email_bag'] = b
     self.fillHeaders(mail, new_mail,encoding)
     if self.messages_table.spamChecker(new_mail) is True:
         return
     if mail.get_content_maintype() not in ('multipart','image'):
         content = mail.get_payload(decode=True)
         encoding = mail.get_content_charset()
         #encoding = chardet.detect(content)['encoding']
         new_mail['body'] = self.smartConverter(content,encoding)
         new_mail['body_plain'] = new_mail['body']
     else:
         for part in mail.walk():
             part_content_type = part.get_content_type()
             if part_content_type.startswith('multipart'):
                 continue
             content_disposition = part.get('Content-Disposition')
             if content_disposition is None and part_content_type in ('text/html','text/plain'): 
                 self.parseBody(part, new_mail, part_content_type=part_content_type)
             else:
                 self.parseAttachment(part, new_mail, part_content_type=part_content_type)
     if new_mail.get('body'):
         g = re.search("<body(.*?)>(.*?)</body>", new_mail['body'], re.S|re.DOTALL)
         new_mail['body'] = g.group(2) if g else new_mail['body']
     else:
         new_mail['body'] = new_mail.get('body_plain')     
     return new_mail
Ejemplo n.º 13
0
 def run(self, dest_path=None, start_ts=None, **kwargs):
     call_list = ['sudo', 'pgbadger', self.logfile]
     filename = '%s.html' % getUuid()
     dest_path = self.parent.getStaticPath(self.output_folder,
                                           'pgbadger',
                                           filename,
                                           autocreate=-1)
     call_list.append('-o')
     call_list.append(dest_path)
     call_list.append('-f')
     call_list.append(self.format)
     if start_ts:
         call_list.append('-b')
         call_list.append(str(start_ts).split('.')[0])
     result = call(call_list)
     if result != 0:
         return None
     return self.parent.getStaticUrl(self.output_folder, 'pgbadger',
                                     filename)
Ejemplo n.º 14
0
 def notifyDbEvent(self, tblobj, record, event, old_record=None):
     """TODO
     
     :param tblobj: the :ref:`database table <table>` object
     :param record: TODO
     :param event: TODO
     :param old_record: TODO. """
     currentEnv = self.db.currentEnv
     if currentEnv.get('hidden_transaction'):
         return
     dbeventKey = 'dbevents_%s' % self.db.connectionKey()
     if not currentEnv.get('env_transaction_id'):
         self.db.updateEnv(env_transaction_id=getUuid())
     broadcast = tblobj.attributes.get('broadcast')
     if broadcast is not False and broadcast != '*old*':
         dbevents = currentEnv.setdefault(dbeventKey, {})
         r = dict(
             dbevent=event,
             pkey=record.get(tblobj.pkey),
             old_pkey=old_record.get(tblobj.pkey) if old_record else None)
         if broadcast and broadcast is not True:
             for field in broadcast.split(','):
                 newvalue = record.get(field)
                 r[field] = self.catalog.asTypedText(
                     newvalue)  #2011/01/01::D
                 if old_record:
                     oldvalue = old_record.get(field)
                     if newvalue != oldvalue:
                         r['old_%s' % field] = self.catalog.asTypedText(
                             old_record.get(field))
         dbevents.setdefault(tblobj.fullname, []).append(r)
     audit_mode = tblobj.attributes.get('audit')
     if audit_mode:
         self.db.table('adm.audit').audit(tblobj,
                                          event,
                                          audit_mode=audit_mode,
                                          record=record,
                                          old_record=old_record)
Ejemplo n.º 15
0
 def createEvent(self,uid=None,dtstamp=None,dtstart=None,dtend=None,summary=None,calendar=None):
     tpl = """BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:%(prodid)s\r\nBEGIN:VEVENT\r\nUID:%(uid)s\r\nDTSTAMP:%(dtstamp)s\r\nDTSTART:%(dtstart)s\r\nDTEND:%(dtend)s\r\nSUMMARY:%(summary)s\r\nEND:VEVENT\r\nEND:VCALENDAR"""
     calendar=self.calendars.get(calendar)
     assert calendar, 'Missing calendar'
     data=tpl%dict(uid=uid or getUuid(),dtstamp=dt(dtstamp or datetime.now()),dtstart=dt(dtstart),dtend=dt(dtend),summary=summary,prodid='VCALENDAR genropy')
     event = caldav.Event(self.client, data = data, parent = calendar).save()
Ejemplo n.º 16
0
 def create(self):
     self.connection_id = getUuid()
     self.user = self.guestname
     self.register()
     self.write_cookie()
Ejemplo n.º 17
0
 def create(self):
     self.connection_id = getUuid()
     self.user = self.guestname
     self.register()
     self.write_cookie()
Ejemplo n.º 18
0
 def createPassword(self):
     password = getUuid()[0:6]
     return password
Ejemplo n.º 19
0
    def execute(self,
                sql,
                sqlargs=None,
                cursor=None,
                cursorname=None,
                autocommit=False,
                dbtable=None):
        """Execute the sql statement using given kwargs
        
        :param sql: add???
        :param sqlargs: add???. Default value is ``None``
        :param cursor: add???. Default value is ``None``
        :param cursorname: add???. Default value is ``None``
        :param autocommit: add???. Default value is ``None``
        :param dbtable: add???. Default value is ``None``
        :returns: add???
        """
        # transform list and tuple parameters in named values.
        # Eg.   WHERE foo IN:bar ----> WHERE foo in (:bar_1, :bar_2..., :bar_n)
        envargs = dict([('env_%s' % k, v) for k, v in self.currentEnv.items()])
        if not 'env_workdate' in envargs:
            envargs['env_workdate'] = self.workdate
        envargs.update(sqlargs or {})
        sqlargs = envargs
        if dbtable and not self.table(dbtable).use_dbstores():
            storename = '_main_db'
        else:
            storename = sqlargs.pop(
                'storename', self.currentEnv.get('storename', '_main_db'))
        with self.tempEnv(storename=storename):
            for k, v in [(k, v) for k, v in sqlargs.items()
                         if isinstance(v, list) or isinstance(v, tuple)]:
                sqllist = '(%s) ' % ','.join(
                    [':%s%i' % (k, i) for i, ov in enumerate(v)])

                sqlargs.pop(k)
                sqlargs.update(
                    dict([('%s%i' % (k, i), ov) for i, ov in enumerate(v)]))
                sql = re.sub(':%s(\W|$)' % k, sqllist, sql)
            sql = re.sub(IN_OPERATOR_PATCH, ' FALSE', sql)
            sql, sqlargs = self.adapter.prepareSqlText(sql, sqlargs)
            #gnrlogger.info('Executing:%s - with kwargs:%s \n\n',sql,unicode(kwargs))
            #print 'sql:\n',sql
            try:
                if not cursor:
                    if cursorname:
                        if cursorname == '*':
                            cursorname = 'c%s' % re.sub('\W', '_', getUuid())
                        cursor = self.adapter.cursor(self.connection,
                                                     cursorname)
                    else:
                        cursor = self.adapter.cursor(self.connection)
                cursor.execute(sql, sqlargs)
                if self.debugger:
                    self.debugger(debugtype='sql',
                                  sql=sql,
                                  sqlargs=sqlargs,
                                  dbtable=dbtable)
            except Exception, e:
                #print sql
                gnrlogger.warning('error executing:%s - with kwargs:%s \n\n',
                                  sql, unicode(sqlargs))
                if self.debugger:
                    self.debugger(debugtype='sql',
                                  sql=sql,
                                  sqlargs=sqlargs,
                                  dbtable=dbtable,
                                  error=str(e))
                print str(
                    'error %s executing:%s - with kwargs:%s \n\n' %
                    (str(e), sql, unicode(sqlargs).encode('ascii', 'ignore')))
                self.rollback()
                raise
            if autocommit:
                self.commit()
Ejemplo n.º 20
0
 def createPassword(self):
     password = getUuid()[0:6]
     return password