Example #1
0
    def ivInline(self, parent, **kwargs):
        """An includedview on a datastore's path."""
        bc = parent.borderContainer(**kwargs)
        b = Bag()
        b.addItem('r0', Bag(dict(name="name", description="Full name", size=40, type="U")))
        b.addItem('r1', Bag(dict(name="birth date", description="Birth date", size=10, type="D")))

        bc.data('inline_data', b)

        def structInlineData(struct):
            r = struct.view().rows()
            r.cell('name', name='!!Name', width='20ex')
            r.cell('description', name='!!Description', width='40ex')
            r.cell('size', name='!!Size', width='5ex')
            r.cell('type', name='!!Type', width='20ex')

        iv = self.includedViewBox(bc, label='!!Inline IncludedView', locked=False,
                                  datamode="bag",
                                  storepath='inline_data', struct=structInlineData,
                                  autoWidth=True, add_action=True, del_action=True)

        ge = iv.gridEditor()
        ge.textbox(gridcell="name")
        ge.textbox(gridcell="description")
        ge.numberspinner(gridcell="size", min=1, max=100)
        ge.filteringselect(gridcell="type",
                           values="I:Int,R:Real,S:String,U:Unicode,D:Date,H:Time,DH:Date and Time,X:Bag")
Example #2
0
 def getMyData(self):
     feed = Bag('https://www.ansa.it/sito/ansait_rss.xml')
     result = Bag()
     for i, n in enumerate(feed['rss.channel']):
         if n.label == 'item':
             result.addItem(f'r_{i}', n.value)
     return result
Example #3
0
class GnrSqlDebugger(object):
    def __init__(self,parent):
        self.parent = parent
        self._debug_calls = Bag()

    def output(self, page, sql=None, sqlargs=None, dbtable=None, error=None,delta_time=None):
        dbtable = dbtable or '-no table-'
        kwargs=dict(sqlargs)
        kwargs.update(sqlargs)
        delta_time = int((delta_time or 0)*1000)
        if sqlargs and sql:
            formatted_sqlargs = dict([(k,'<span style="background-color:yellow;cursor:pointer;" title="%s" >%%(%s)s</span>' %(v,k)) for k,v in sqlargs.items()])
            value = sql %(formatted_sqlargs)
        if error:
            kwargs['sqlerror'] = str(error)
        self._debug_calls.addItem('%03i Table %s' % (len(self._debug_calls), dbtable.replace('.', '_')), value,_execution_time=delta_time,_time_sql=delta_time,_description=dbtable,_type='sql',
                                  **kwargs)

    def onCollectDatachanges(self):
        page = self.parent.page
        if page.debug_sql and self._debug_calls:
            path = 'gnr.debugger.main.c_%s' % page.callcounter
            attributes=dict(server_time=int((time()-page._start_time)*1000))
            call_kwargs = dict(page._call_kwargs)
            attributes['methodname'] = call_kwargs.pop('method')
            call_kwargs.pop('_lastUserEventTs',None)
            if not call_kwargs.get('_debug_info') and ('table' in call_kwargs or 'dbtable' in call_kwargs):
                call_kwargs['_debug_info'] = 'table: %s' %(call_kwargs.get('table') or call_kwargs.get('dbtable'))
            attributes['debug_info'] = call_kwargs.pop('_debug_info',None)
            #attributes['_method_parameters'] = call_kwargs
            attributes['sql_count'] = len(self._debug_calls)
            attributes['sql_total_time'] = self._debug_calls.sum('#a._time_sql')
            attributes['not_sql_time'] = attributes['server_time'] - attributes['sql_total_time']
            attributes['r_count'] = page.callcounter
            page.setInClientData(path, self._debug_calls,attributes=attributes)
Example #4
0
 def test_01_tree_res(self, pane):
     """
     tree resolver
     """
     pane.tree(storepath='.geodata', labelAttribute='nome', hideValues=True)
     geodata = Bag()
     geodata.addItem('italia', ResolverGeo(_page=self), nome='Italia')
     pane.data('.geodata', geodata)
Example #5
0
class GnrSqlDebugger(object):
    def __init__(self, parent):
        self.parent = parent
        self._debug_calls = Bag()

    def output(self,
               page,
               sql=None,
               sqlargs=None,
               dbtable=None,
               error=None,
               delta_time=None):
        dbtable = dbtable or '-no table-'
        kwargs = dict(sqlargs)
        kwargs.update(sqlargs)
        delta_time = int((delta_time or 0) * 1000)
        if sqlargs and sql:
            formatted_sqlargs = dict([(
                k,
                '<span style="background-color:yellow;cursor:pointer;" title="%s" >%%(%s)s</span>'
                % (v, k)) for k, v in sqlargs.items()])
            value = sql % (formatted_sqlargs)
        if error:
            kwargs['sqlerror'] = str(error)
        self._debug_calls.addItem(
            '%03i Table %s' %
            (len(self._debug_calls), dbtable.replace('.', '_')),
            value,
            _execution_time=delta_time,
            _time_sql=delta_time,
            _description=dbtable,
            _type='sql',
            **kwargs)

    def onCollectDatachanges(self):
        page = self.parent.page
        if page.debug_sql and self._debug_calls:
            path = 'gnr.debugger.main.c_%s' % page.callcounter
            attributes = dict(server_time=int((time() - page._start_time) *
                                              1000))
            call_kwargs = dict(page._call_kwargs)
            attributes['methodname'] = call_kwargs.pop('method')
            call_kwargs.pop('_lastUserEventTs', None)
            if not call_kwargs.get('_debug_info') and (
                    'table' in call_kwargs or 'dbtable' in call_kwargs):
                call_kwargs['_debug_info'] = 'table: %s' % (
                    call_kwargs.get('table') or call_kwargs.get('dbtable'))
            attributes['debug_info'] = call_kwargs.pop('_debug_info', None)
            #attributes['_method_parameters'] = call_kwargs
            attributes['sql_count'] = len(self._debug_calls)
            attributes['sql_total_time'] = self._debug_calls.sum(
                '#a._time_sql')
            attributes['not_sql_time'] = attributes[
                'server_time'] - attributes['sql_total_time']
            attributes['r_count'] = page.callcounter
            page.setInClientData(path,
                                 self._debug_calls,
                                 attributes=attributes)
Example #6
0
 def elencoProvince(self):
     result = Bag()
     f = self._page.db.table('glbl.provincia').query(
         where='$regione=:r', r=self.regione).fetch()
     for r in f:
         result.addItem(r['sigla'],
                        ResolverGeo(_page=self._page, provincia=r['sigla']),
                        nome=r['nome'],
                        sigla=r['sigla'])
     return result
Example #7
0
 def eventsBag(self, calendarName):
     result = Bag()
     calendar = self.calendars.get(calendarName)
     if calendar:
         events = calendar.events()
         for event in events:
             event.load()
             data = VObjectBag(event.data)
             result.addItem('evento', data)
     return result
Example #8
0
 def eventsBag(self,calendarName):
     result=Bag()
     calendar=self.calendars.get(calendarName)
     if calendar:
         events=calendar.events()
         for event in events:
             event.load()
             data=VObjectBag(event.data)
             result.addItem('evento',data)
     return result
Example #9
0
class GnrWebDebugger(GnrBaseProxy):
    def init(self, **kwargs):
        self.db = self.page.db
        self.debug = getattr(self.page, 'debug', False)
        self._debug_calls = Bag()

    def output(self, debugtype, **kwargs):
        page = self.page
        debugopt = getattr(page, 'debugopt', '') or ''
        if debugopt and debugtype in debugopt:
            getattr(self, 'output_%s' % debugtype)(page, **kwargs)

    def output_sql(self,
                   page,
                   sql=None,
                   sqlargs=None,
                   dbtable=None,
                   error=None):
        dbtable = dbtable or '-no table-'
        kwargs = dict(sqlargs)
        kwargs.update(sqlargs)
        value = sql
        if error:
            kwargs['sqlerror'] = str(error)
        self._debug_calls.addItem(
            '%03i Table:%s' %
            (len(self._debug_calls), dbtable.replace('.', '_')), value,
            **kwargs)

    def event_onCollectDatachanges(self):
        page = self.page
        if page.debugopt and self._debug_calls:
            path = 'gnr.debugger.main.c_%s' % self.page.callcounter
            page.setInClientData(path, self._debug_calls)

    def log(self, msg):
        if self.debug:
            f = file(self.logfile, 'a')
            f.write(
                '%s -- %s\n' %
                (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg))
            f.close()

    def _get_logfile(self):
        if not hasattr(self, '_logfile'):
            logdir = os.path.normpath(
                os.path.join(self.page.site.site_path, 'data', 'logs'))
            if not os.path.isdir(logdir):
                os.makedirs(logdir)
            self._logfile = os.path.join(
                logdir,
                'error_%s.log' % datetime.date.today().strftime('%Y%m%d'))
        return self._logfile

    logfile = property(_get_logfile)
Example #10
0
 def addItemBag(self, label, value, _attributes=None, **kwargs):
     tempbag = Bag()
     tempbag.addItem(label, value, _attributes=_attributes, **kwargs)
     bagxml = BagToXml().build(tempbag,
                               typeattrs=self.typeattrs,
                               typevalue=self.typevalue,
                               unresolved=True,
                               omitRoot=True,
                               docHeader=False,
                               pretty=False)
     self.output.write(bagxml)
Example #11
0
 def test_setItemPos(self):
     b = Bag()
     b.addItem('a', 1)
     b.addItem('b', 2)
     b.addItem('c', 3)
     b.addItem('d', 4)
     b.setItem('e', 5, _position='<')
     assert b['#0'] == 5
     b.setItem('f', 6, _position='<c')
     assert b['#2'] == 2
     b.setItem('g', 7, _position='<#3')
     assert b['#3'] == 7
Example #12
0
    def test_setItem_addItem(self):
        b = Bag()
        b['name'] = 'John'
        b['surname'] = 'Doe'
        b['birthday'] = datetime.date(1974, 11, 23)
        b['phone'] = Bag()
        b['phone'].setItem('office', 555450210)
        b.setItem('phone.home', 555345670, private=True)
        b.setItem('phone.mobile', 555230450, sim='vadophone', fbplayer='gattuso')
        b.addItem('phone.mobile', 444230450, sim='tom') #add is used for multiple keys
        #b.toXml('data/testbag.xml')

        assert b == self.mybag
Example #13
0
 def elencoRegioni(self):
     result = Bag()
     regioni_grouped = self._page.db.table(
         'glbl.regione').query().fetchGrouped('zona')
     for zona, regioni in regioni_grouped.items():
         zonabag = Bag()
         result.addItem(zona, zonabag, nome=zona)
         for r in regioni:
             content = ResolverGeo(_page=self._page, regione=r['sigla'])
             zonabag.addItem(r['sigla'],
                             content,
                             nome=r['nome'],
                             sigla=r['sigla'])
     return result
Example #14
0
 def elencoComuni(self):
     result = Bag()
     f = self._page.db.table('glbl.comune').query(
         where='$sigla_provincia=:p', p=self.provincia).fetch()
     for r in f:
         content = Bag(dict(r))
         content.addItem('meteo',
                         MeteoResolver(city=r['denominazione']),
                         name='Meteo')
         result.addItem(r['id'],
                        content,
                        nome=r['denominazione'],
                        comune_id=r['id'])
     return result
Example #15
0
 def rpc_curr_pages(self):
     pagesDict = self.site.register_page.pages()
     result = Bag()
     for page_id, v in pagesDict.items():
         user = v['user'] or v['user_ip'].replace('.', '_')
         pagename = v['pagename'].replace('.py', '')
         connection_id = v['connection_id']
         delta = (datetime.datetime.now() - v['start_ts']).seconds
         result.addItem('.'.join([user, '%s (%i)' % (pagename, delta)]), None,
                        connection_id=connection_id,
                        page_id=page_id, user_ip=v['user_ip'],
                        user_agent=v['user_agent'],
                        user=user,
                        start_ts=v['start_ts'])
     return result 
Example #16
0
    def test_setItem_addItem(self):
        b = Bag()
        b['name'] = 'John'
        b['surname'] = 'Doe'
        b['birthday'] = datetime.date(1974, 11, 23)
        b['phone'] = Bag()
        b['phone'].setItem('office', 555450210)
        b.setItem('phone.home', 555345670, private=True)
        b.setItem('phone.mobile',
                  555230450,
                  sim='vadophone',
                  fbplayer='gattuso')
        b.addItem('phone.mobile', 444230450,
                  sim='tom')  #add is used for multiple keys
        #b.toXml('data/testbag.xml')

        assert b == self.mybag
Example #17
0
 def rpc_curr_pages(self):
     pagesDict = self.site.register_page.pages()
     result = Bag()
     for page_id, v in pagesDict.items():
         user = v['user'] or v['user_ip'].replace('.', '_')
         pagename = v['pagename'].replace('.py', '')
         connection_id = v['connection_id']
         delta = (datetime.datetime.now() - v['start_ts']).seconds
         result.addItem('.'.join([user, '%s (%i)' % (pagename, delta)]),
                        None,
                        connection_id=connection_id,
                        page_id=page_id,
                        user_ip=v['user_ip'],
                        user_agent=v['user_agent'],
                        user=user,
                        start_ts=v['start_ts'])
     return result
Example #18
0
 def compile_inner(self, src):
     result = Bag()
     for node in src:
         src_value = node.value
         attr = dict(node.attr)
         handler = getattr(self, 'compile_%s' % attr['tag'], None)
         if handler:
             handler(src, src_value, attr)
         if isinstance(src_value, GnrHtmlSrc):
             if len(src_value) == 0:
                 dst_value = ' '
             else:
                 dst_value = self.compile_inner(src_value)
         else:
             dst_value = src_value
         tag = attr.pop('tag')
         result.addItem(tag, dst_value, attr)
     return result
Example #19
0
 def compile_inner(self, src):
     result = Bag()
     for node in src:
         src_value = node.value
         attr = dict(node.attr)
         handler = getattr(self, 'compile_%s' % attr['tag'], None)
         if handler:
             handler(src, src_value, attr)
         if isinstance(src_value, GnrHtmlSrc):
             if len(src_value) == 0:
                 dst_value = ' '
             else:
                 dst_value = self.compile_inner(src_value)
         else:
             dst_value = src_value
         tag = attr.pop('tag')
         result.addItem(tag, dst_value, attr)
     return result
Example #20
0
class GnrWebDebugger(GnrBaseProxy):
    def init(self, **kwargs):
        self.db = self.page.db
        self.debug = getattr(self.page, 'debug', False)
        self._debug_calls = Bag()

    def output(self, debugtype, **kwargs):
        page = self.page
        debugopt = getattr(page, 'debugopt', '') or ''
        if debugopt and debugtype in debugopt:
            getattr(self, 'output_%s' % debugtype)(page, **kwargs)

    def output_sql(self, page, sql=None, sqlargs=None, dbtable=None, error=None):
        dbtable = dbtable or '-no table-'
        kwargs=dict(sqlargs)
        kwargs.update(sqlargs)
        value = sql
        if error:
            kwargs['sqlerror'] = str(error)
        self._debug_calls.addItem('%03i Table:%s' % (len(self._debug_calls), dbtable.replace('.', '_')), value,
                                  **kwargs)

    def event_onCollectDatachanges(self):
        page = self.page
        if page.debugopt and self._debug_calls:
            path = 'gnr.debugger.main.c_%s' % self.page.callcounter
            page.setInClientData(path, self._debug_calls)

    def log(self, msg):
        if self.debug:
            f = file(self.logfile, 'a')
            f.write('%s -- %s\n' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg))
            f.close()

    def _get_logfile(self):
        if not hasattr(self, '_logfile'):
            logdir = os.path.normpath(os.path.join(self.page.site.site_path, 'data', 'logs'))
            if not os.path.isdir(logdir):
                os.makedirs(logdir)
            self._logfile = os.path.join(logdir, 'error_%s.log' % datetime.date.today().strftime('%Y%m%d'))
        return self._logfile

    logfile = property(_get_logfile)
Example #21
0
 def _toHtmlInner(self):
     result = Bag()
     hasRows = False
     for node in self.nodes:
         label = node.label
         attr = dict(node.attr)
         tag = attr.pop('tag')
         value = node.getValue()
         if isinstance(value, GnrHtmlSrc):
             if len(value) == 0:
                 newValue = ' '
             else:
                 newValue = value._toHtmlInner()
         else:
             newValue = value
         if hasattr(self, 'compile_%s' % tag):
             handler = getattr(self, 'compile_%s' % tag)
             tag, attr = handler(value, tag, attr)
         result.addItem(tag, newValue, attr)
     return result
Example #22
0
 def baronToBag(self,fred,formatting=False):
     if isinstance(fred,dict):
         fred = dict(fred)
         value = fred.pop('value',None)
         t = fred.pop('type')
         if not formatting:
             fred = dict([(k,v) for k,v in fred.items() if not k.endswith('_formatting')])
         value = getattr(self,'baronToBag_%s' %t,self.baronToBag_default)(value,fred)
         return (t,value,fred)
     elif isinstance(fred,list):
         result = Bag()
         for f in fred:
             label,value,attrs = self.baronToBag(f,formatting=formatting)
             if isinstance(value,tuple):
                 l,v,a = value
                 value = Bag()   
                 value.setItem(l,v,**a)                 
             result.addItem(label,value,**attrs)
         return result
     else:
         return fred
Example #23
0
 def baronToBag(self, fred, formatting=False):
     if isinstance(fred, dict):
         fred = dict(fred)
         value = fred.pop('value', None)
         t = fred.pop('type')
         if not formatting:
             fred = dict([(k, v) for k, v in fred.items()
                          if not k.endswith('_formatting')])
         value = getattr(self, 'baronToBag_%s' % t,
                         self.baronToBag_default)(value, fred)
         return (t, value, fred)
     elif isinstance(fred, list):
         result = Bag()
         for f in fred:
             label, value, attrs = self.baronToBag(f, formatting=formatting)
             if isinstance(value, tuple):
                 l, v, a = value
                 value = Bag()
                 value.setItem(l, v, **a)
             result.addItem(label, value, **attrs)
         return result
     else:
         return fred
Example #24
0
class GnrWebDeveloper(GnrBaseProxy):
    def init(self, **kwargs):
        # self.db = self.page.db
        self.debug = getattr(self.page, "debug", False)
        self._debug_calls = Bag()

    @property
    def db(self):
        return self.page.db

    def output(self, debugtype, **kwargs):
        page = self.page
        if getattr(page, "debug_%s" % debugtype, None):
            getattr(self, "output_%s" % debugtype)(page, **kwargs)

    def output_sql(self, page, sql=None, sqlargs=None, dbtable=None, error=None, delta_time=None):
        dbtable = dbtable or "-no table-"
        kwargs = dict(sqlargs)
        kwargs.update(sqlargs)
        delta_time = int((delta_time or 0) * 1000)
        if sqlargs and sql:
            formatted_sqlargs = dict(
                [
                    (k, '<span style="background-color:yellow;cursor:pointer;" title="%s" >%%(%s)s</span>' % (v, k))
                    for k, v in sqlargs.items()
                ]
            )
            value = sql % (formatted_sqlargs)
        if error:
            kwargs["sqlerror"] = str(error)
        self._debug_calls.addItem(
            "%03i Table %s" % (len(self._debug_calls), dbtable.replace(".", "_")),
            value,
            _execution_time=delta_time,
            _time_sql=delta_time,
            _description=dbtable,
            _type="sql",
            **kwargs
        )

    def output_py(self, page, methodname=None, code=None, delta_time=None, **kwargs):
        delta_time = int((delta_time or 0) * 1000)
        mlist = page.debug_py.split(",")
        if page.debug_py == "*" or methodname in mlist:
            self._debug_calls.addItem(
                "%03i Call %s" % (len(self._debug_calls), methodname),
                code,
                _execution_time=delta_time,
                _time_py=delta_time,
                _description=methodname,
                _type="py",
            )

    def event_onCollectDatachanges(self):
        page = self.page
        if (page.debug_sql or page.debug_py) and self._debug_calls:
            path = "gnr.debugger.main.c_%s" % self.page.callcounter
            attributes = dict(server_time=int((time() - page._start_time) * 1000))
            call_kwargs = dict(page._call_kwargs)
            attributes["methodname"] = call_kwargs.pop("method")
            call_kwargs.pop("_lastUserEventTs", None)
            if not call_kwargs.get("_debug_info") and ("table" in call_kwargs or "dbtable" in call_kwargs):
                call_kwargs["_debug_info"] = "table: %s" % (call_kwargs.get("table") or call_kwargs.get("dbtable"))
            attributes["debug_info"] = call_kwargs.pop("_debug_info", None)
            # attributes['_method_parameters'] = call_kwargs
            attributes["sql_count"] = len(self._debug_calls)
            attributes["sql_total_time"] = self._debug_calls.sum("#a._time_sql")
            attributes["not_sql_time"] = attributes["server_time"] - attributes["sql_total_time"]
            attributes["r_count"] = self.page.callcounter

            page.setInClientData(path, self._debug_calls, attributes=attributes)

    def onDroppedMover(self, file_path=None):
        import tarfile

        f = tarfile.open(file_path)
        f.extractall(self.page.site.getStaticPath("user:temp"))
        os.remove(file_path)
        indexpath = self.page.site.getStaticPath("user:temp", "mover", "index.xml")
        indexbag = Bag(indexpath)
        indexbag.getNode("movers").attr.update(imported=True)
        indexbag.toXml(indexpath)

    @public_method
    def loadCurrentMover(self):
        indexpath = self.page.site.getStaticPath("user:temp", "mover", "index.xml")
        if os.path.isfile(indexpath):
            indexbag = Bag(indexpath)
            imported = indexbag["movers?imported"]
            tablesbag = indexbag["movers"]
            _class = "mover_imported" if imported else None
            for n in indexbag["records"]:
                tablesbag.getNode(n.label).attr.update(
                    pkeys=dict([(pkey, True) for pkey in n.value.keys()], _customClasses=_class)
                )
            return tablesbag

    @public_method
    def importMoverLines(self, table=None, objtype=None, pkeys=None):
        databag = Bag(
            self.page.site.getStaticPath("user:temp", "mover", "data", "%s_%s.xml" % (table.replace(".", "_"), objtype))
        )
        tblobj = self.db.table(table) if objtype == "record" else self.db.table("adm.userobject")
        for pkey in pkeys.keys():
            tblobj.insertOrUpdate(databag.getItem(pkey))
        self.db.commit()

    @public_method
    def getMoverTableRows(self, tablerow=None, movercode=None, **kwargs):
        pkeys = tablerow["pkeys"].keys()
        table = tablerow["table"]
        objtype = tablerow["objtype"]
        tblobj = self.db.table(table)
        columns, mask = tblobj.rowcaptionDecode(tblobj.rowcaption)
        if columns:
            columns = ",".join(columns)
        f = tblobj.query(where="$pkey IN :pkeys", pkeys=tablerow["pkeys"].keys(), columns=columns).fetch()
        result = Bag()
        for r in f:
            result.setItem(
                r["pkey"], None, _pkey=r["pkey"], db_caption=tblobj.recordCaption(record=r), _customClasses="mover_db"
            )
        indexpath = self.page.site.getStaticPath("user:temp", "mover", "index.xml")
        if os.path.isfile(indexpath):
            indexbag = Bag(indexpath)
            moverrows = indexbag.getItem("records.%s" % movercode)
            if not moverrows:
                return result
            for pkey in pkeys:
                rownode = moverrows.getNode(pkey)
                if rownode:
                    xml_caption = rownode.attr["caption"]
                    if not pkey in result:
                        result.setItem(
                            pkey,
                            None,
                            _pkey=pkey,
                            xml_caption=xml_caption,
                            _customClasses="mover_xml",
                            objtype=objtype,
                            table=tablerow["reftable"],
                        )
                    else:
                        result.getNode(pkey).attr.update(
                            xml_caption=xml_caption,
                            _customClasses="mover_both",
                            objtype=objtype,
                            table=tablerow["reftable"],
                        )
        return result

    def tarMover(self, movername="mover"):
        import tarfile
        import StringIO

        tf = StringIO.StringIO()
        f = tarfile.open(mode="w:gz", fileobj=tf)
        moverpath = self.page.site.getStaticPath("user:temp", "mover")
        f.add(moverpath, arcname="mover")
        f.close()
        result = tf.getvalue()
        tf.close()
        return result

    @public_method
    def downloadMover(self, data=None, movername=None, **kwargs):
        self.saveCurrentMover(data=data)
        return self.tarMover(movername=movername)

    @public_method
    def saveCurrentMover(self, data):
        moverpath = self.page.site.getStaticPath("user:temp", "mover")
        indexpath = os.path.join(moverpath, "index.xml")
        indexbag = Bag()
        if not os.path.isdir(moverpath):
            os.makedirs(moverpath)
        for movercode, table, pkeys, reftable, objtype in data.digest("#k,#a.table,#a.pkeys,#a.reftable,#a.objtype"):
            pkeys = pkeys.keys()
            databag = self.db.table(table).toXml(
                pkeys=pkeys, rowcaption=True, path=os.path.join(moverpath, "data", "%s.xml" % movercode)
            )
            indexbag.setItem(
                "movers.%s" % movercode, None, table=table, count=len(pkeys), reftable=reftable, objtype=objtype
            )
            indexbag.setItem("records.%s" % movercode, None, table=table)
            for n in databag:
                indexbag.setItem(
                    "records.%s.%s" % (movercode, n.label), None, pkey=n.attr["pkey"], caption=n.attr.get("caption")
                )
        indexbag.toXml(indexpath, autocreate=True)

    def log(self, msg):
        if self.debug:
            f = file(self.logfile, "a")
            f.write("%s -- %s\n" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), msg))
            f.close()

    def _get_logfile(self):
        if not hasattr(self, "_logfile"):
            logdir = os.path.normpath(os.path.join(self.page.site.site_path, "data", "logs"))
            if not os.path.isdir(logdir):
                os.makedirs(logdir)
            self._logfile = os.path.join(logdir, "error_%s.log" % datetime.date.today().strftime("%Y%m%d"))
        return self._logfile

    logfile = property(_get_logfile)
Example #25
0
class GnrWebDeveloper(GnrBaseProxy):
    def init(self, **kwargs):
        #self.db = self.page.db
        self.debug = getattr(self.page, 'debug', False)
        self._debug_calls = Bag()

    @property
    def db(self):
        return self.page.db

    def output(self, debugtype, **kwargs):
        page = self.page
        if getattr(page, 'debug_%s' % debugtype, None):
            getattr(self, 'output_%s' % debugtype)(page, **kwargs)

    def output_sql(self,
                   page,
                   sql=None,
                   sqlargs=None,
                   dbtable=None,
                   error=None,
                   delta_time=None):
        dbtable = dbtable or '-no table-'
        kwargs = dict(sqlargs)
        kwargs.update(sqlargs)
        delta_time = int((delta_time or 0) * 1000)
        if sqlargs and sql:
            formatted_sqlargs = dict([(
                k,
                '<span style="background-color:yellow;cursor:pointer;" title="%s" >%%(%s)s</span>'
                % (v, k)) for k, v in sqlargs.items()])
            value = sql % (formatted_sqlargs)
        if error:
            kwargs['sqlerror'] = str(error)
        self._debug_calls.addItem(
            '%03i Table %s' %
            (len(self._debug_calls), dbtable.replace('.', '_')),
            value,
            _execution_time=delta_time,
            _time_sql=delta_time,
            _description=dbtable,
            _type='sql',
            **kwargs)

    def output_py(self,
                  page,
                  methodname=None,
                  code=None,
                  delta_time=None,
                  **kwargs):
        delta_time = int((delta_time or 0) * 1000)
        mlist = page.debug_py.split(',')
        if page.debug_py == '*' or methodname in mlist:
            self._debug_calls.addItem('%03i Call %s' %
                                      (len(self._debug_calls), methodname),
                                      code,
                                      _execution_time=delta_time,
                                      _time_py=delta_time,
                                      _description=methodname,
                                      _type='py')

    def event_onCollectDatachanges(self):
        page = self.page
        if (page.debug_sql or page.debug_py) and self._debug_calls:
            path = 'gnr.debugger.main.c_%s' % self.page.callcounter
            attributes = dict(server_time=int((time() - page._start_time) *
                                              1000))
            call_kwargs = dict(page._call_kwargs)
            attributes['methodname'] = call_kwargs.pop('method')
            call_kwargs.pop('_lastUserEventTs', None)
            if not call_kwargs.get('_debug_info') and (
                    'table' in call_kwargs or 'dbtable' in call_kwargs):
                call_kwargs['_debug_info'] = 'table: %s' % (
                    call_kwargs.get('table') or call_kwargs.get('dbtable'))
            attributes['debug_info'] = call_kwargs.pop('_debug_info', None)
            #attributes['_method_parameters'] = call_kwargs
            attributes['sql_count'] = len(self._debug_calls)
            attributes['sql_total_time'] = self._debug_calls.sum(
                '#a._time_sql')
            attributes['not_sql_time'] = attributes[
                'server_time'] - attributes['sql_total_time']
            attributes['r_count'] = self.page.callcounter

            page.setInClientData(path,
                                 self._debug_calls,
                                 attributes=attributes)

    def onDroppedMover(self, file_path=None):
        import tarfile
        f = tarfile.open(file_path)
        f.extractall(self.page.site.getStaticPath('user:temp'))
        os.remove(file_path)
        indexpath = self.page.site.getStaticPath('user:temp', 'mover',
                                                 'index.xml')
        indexbag = Bag(indexpath)
        indexbag.getNode('movers').attr.update(imported=True)
        indexbag.toXml(indexpath)

    @public_method
    def loadCurrentMover(self):
        indexpath = self.page.site.getStaticPath('user:temp', 'mover',
                                                 'index.xml')
        if os.path.isfile(indexpath):
            indexbag = Bag(indexpath)
            imported = indexbag['movers?imported']
            tablesbag = indexbag['movers']
            _class = 'mover_imported' if imported else None
            for n in indexbag['records']:
                tablesbag.getNode(n.label).attr.update(
                    pkeys=dict([(pkey, True) for pkey in n.value.keys()],
                               _customClasses=_class))
            return tablesbag

    @public_method
    def importMoverLines(self, table=None, objtype=None, pkeys=None):
        databag = Bag(
            self.page.site.getStaticPath(
                'user:temp', 'mover', 'data',
                '%s_%s.xml' % (table.replace('.', '_'), objtype)))
        tblobj = self.db.table(
            table) if objtype == 'record' else self.db.table('adm.userobject')
        for pkey in pkeys.keys():
            tblobj.insertOrUpdate(databag.getItem(pkey))
        self.db.commit()

    @public_method
    def getMoverTableRows(self, tablerow=None, movercode=None, **kwargs):
        pkeys = tablerow['pkeys'].keys()
        table = tablerow['table']
        objtype = tablerow['objtype']
        tblobj = self.db.table(table)
        columns, mask = tblobj.rowcaptionDecode(tblobj.rowcaption)
        if columns:
            columns = ','.join(columns)
        f = tblobj.query(where='$pkey IN :pkeys',
                         pkeys=tablerow['pkeys'].keys(),
                         columns=columns).fetch()
        result = Bag()
        for r in f:
            result.setItem(r['pkey'],
                           None,
                           _pkey=r['pkey'],
                           db_caption=tblobj.recordCaption(record=r),
                           _customClasses='mover_db')
        indexpath = self.page.site.getStaticPath('user:temp', 'mover',
                                                 'index.xml')
        if os.path.isfile(indexpath):
            indexbag = Bag(indexpath)
            moverrows = indexbag.getItem('records.%s' % movercode)
            if not moverrows:
                return result
            for pkey in pkeys:
                rownode = moverrows.getNode(pkey)
                if rownode:
                    xml_caption = rownode.attr['caption']
                    if not pkey in result:
                        result.setItem(pkey,
                                       None,
                                       _pkey=pkey,
                                       xml_caption=xml_caption,
                                       _customClasses='mover_xml',
                                       objtype=objtype,
                                       table=tablerow['reftable'])
                    else:
                        result.getNode(pkey).attr.update(
                            xml_caption=xml_caption,
                            _customClasses='mover_both',
                            objtype=objtype,
                            table=tablerow['reftable'])
        return result

    def tarMover(self, movername='mover'):
        import tarfile
        import StringIO
        tf = StringIO.StringIO()
        f = tarfile.open(mode='w:gz', fileobj=tf)
        moverpath = self.page.site.getStaticPath('user:temp', 'mover')
        f.add(moverpath, arcname='mover')
        f.close()
        result = tf.getvalue()
        tf.close()
        return result

    @public_method
    def downloadMover(self, data=None, movername=None, **kwargs):
        self.saveCurrentMover(data=data)
        return self.tarMover(movername=movername)

    @public_method
    def saveCurrentMover(self, data):
        moverpath = self.page.site.getStaticPath('user:temp', 'mover')
        indexpath = os.path.join(moverpath, 'index.xml')
        indexbag = Bag()
        if not os.path.isdir(moverpath):
            os.makedirs(moverpath)
        for movercode, table, pkeys, reftable, objtype in data.digest(
                '#k,#a.table,#a.pkeys,#a.reftable,#a.objtype'):
            pkeys = pkeys.keys()
            databag = self.db.table(table).toXml(pkeys=pkeys,
                                                 rowcaption=True,
                                                 path=os.path.join(
                                                     moverpath, 'data',
                                                     '%s.xml' % movercode))
            indexbag.setItem('movers.%s' % movercode,
                             None,
                             table=table,
                             count=len(pkeys),
                             reftable=reftable,
                             objtype=objtype)
            indexbag.setItem('records.%s' % movercode, None, table=table)
            for n in databag:
                indexbag.setItem('records.%s.%s' % (movercode, n.label),
                                 None,
                                 pkey=n.attr['pkey'],
                                 caption=n.attr.get('caption'))
        indexbag.toXml(indexpath, autocreate=True)

    def log(self, msg):
        if self.debug:
            f = file(self.logfile, 'a')
            f.write(
                '%s -- %s\n' %
                (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), msg))
            f.close()

    def _get_logfile(self):
        if not hasattr(self, '_logfile'):
            logdir = os.path.normpath(
                os.path.join(self.page.site.site_path, 'data', 'logs'))
            if not os.path.isdir(logdir):
                os.makedirs(logdir)
            self._logfile = os.path.join(
                logdir,
                'error_%s.log' % datetime.date.today().strftime('%Y%m%d'))
        return self._logfile

    logfile = property(_get_logfile)
Example #26
0
class TestBasicBag(unittest.TestCase):
    def setUp(self):
        self.bag = Bag()
        self.fullbag = Bag({
            'office':
            Bag({
                'John': Bag(),
                'Frank': Bag(),
                'Alexandra': Bag()
            }),
            'friends':
            Bag({
                'Henry': Bag(),
                'Fred': Bag()
            }),
            'relatives':
            Bag({
                'Karla': Bag(),
                'Albert': Bag()
            })
        })

    def testinit(self):
        """
        this test case check different kinds of bag's instantiation:
        - from a list of tuples
        - from a dictionary
        - empty bag
        """
        firstbag = Bag({'a': 1})  #bag from a dict
        secondbag = Bag([('a', 1)])  #bag from list of tuple
        thirdbag = Bag()
        thirdbag.setItem('a', 1)  #empty bag filled
        bagstring = '0 - (int) a: 1  '  #bag as string
        self.failUnless(firstbag.asString() == secondbag.asString() ==
                        thirdbag.asString() == bagstring)

    def testsetgetItem(self):
        """
        this test case checks the methods setItem and getItem
        """
        self.bag.setItem('a.b.c', 5)
        self.failUnless(self.bag.getItem('a.b.c') == self.bag['a.b.c'] == 5)
        self.bag['a.b.c'] = 6
        self.failUnless(self.bag.getItem('a.b.c') == self.bag['a.b.c'] == 6)

    def testindexposition(self):
        """
        this test case checks the method index and the argument "_position"
        """
        self.bag['a.b.c'] = 6
        self.bag.addItem('a.b.d', 15)
        self.failUnless(self.bag.getItem('a.b.#1') == self.bag['a.b.d'] == 15)
        self.assertEqual(self.bag['a.b'].index('d'), 1)
        self.bag.setItem('a.b.middle', 10, _position='<d')
        self.assertEqual(self.bag['a.b'].index('middle'), 1)
        self.assertEqual(self.bag['a.b'].index('d'), 2)

    def testgetIndexList(self):
        il = self.fullbag.getIndexList()
        self.assertEqual(len(il), 10)

    def testkeysitemsvalues(self):
        """
        this test case checks the methods
        -keys
        -items
        -values
        """
        self.failUnless(self.fullbag['office'].keys() == self.
                        fullbag['office?'] == ['Frank', 'John', 'Alexandra'])
        self.failUnless(
            len(self.fullbag['office'].keys()) == len(
                self.fullbag['office'].values()) == len(
                    self.fullbag['office'].items()) == 3)
        self.assertTrue(self.fullbag['office'].has_key('John'))

    def testpopin(self):
        """
        this test case checks the method pop and the clause "in"
        """
        self.assertTrue('John' in self.fullbag['office'])
        self.fullbag['office.Desmond'] = Bag()
        d = self.fullbag['office.Desmond']
        removed = self.fullbag['office'].pop('#3')
        self.assertEqual(d, removed)
        self.assertEqual(self.fullbag['office'].keys(),
                         ['Frank', 'John', 'Alexandra'])

    def testasDict(self):
        """
        this test case checks the method asDict
        """
        d = self.fullbag['office'].asDict()
        self.assertTrue(isinstance(d, dict))
        self.assertEqual(d.keys(), self.fullbag['office?'])
Example #27
0
            elif tag=='adr':
                self.setTag(tag,v)
            else:
                self.setTag(tag,v)


if __name__ == '__main__':

    x = Bag()
    x['n.family']='Smith'
    x['n.given']='Jeff'
    x['n.additional']='G.'
    x['fn.fn']='Jeff Smith'
    x['nickname.nickname']='Eddie'
    x['bday.bday']='1961-10-21'
    x['org.org']='Goodsoftware Pty Ltd'
    x.setItem('email.email','*****@*****.**', param_list=['Home','type=INTERNET','type=pref'])
    x.addItem('email.email','*****@*****.**', param_list=['Work','type=INTERNET'])
    x['adr.street']='32 Smith Waters Rd'
    x['adr.city']='Kincumber'
    x['adr.code']='2251'
    x['adr.country']='Australia'
    x.setItem('tel.tel','02 4332 0368', param_list=['Home','type=pref'])
    x.addItem('tel.tel','0421 232 249', param_list=['CELL'])
    x.setItem('url.url','02 4332 0368', param_list=['Work'])
    

    c = VCard(x)
    print dir(c)
    print c.doserialize()
    #c.doprettyprint()
Example #28
0
class TestBasicBag(unittest.TestCase):
    def setUp(self):
        self.bag = Bag()
        self.fullbag = Bag(
            {
                "office": Bag({"John": Bag(), "Frank": Bag(), "Alexandra": Bag()}),
                "friends": Bag({"Henry": Bag(), "Fred": Bag()}),
                "relatives": Bag({"Karla": Bag(), "Albert": Bag()}),
            }
        )

    def testinit(self):
        """
        this test case check different kinds of bag's instantiation:
        - from a list of tuples
        - from a dictionary
        - empty bag
        """
        firstbag = Bag({"a": 1})  # bag from a dict
        secondbag = Bag([("a", 1)])  # bag from list of tuple
        thirdbag = Bag()
        thirdbag.setItem("a", 1)  # empty bag filled
        bagstring = "0 - (int) a: 1  "  # bag as string
        self.failUnless(firstbag.asString() == secondbag.asString() == thirdbag.asString() == bagstring)

    def testsetgetItem(self):
        """
        this test case checks the methods setItem and getItem
        """
        self.bag.setItem("a.b.c", 5)
        self.failUnless(self.bag.getItem("a.b.c") == self.bag["a.b.c"] == 5)
        self.bag["a.b.c"] = 6
        self.failUnless(self.bag.getItem("a.b.c") == self.bag["a.b.c"] == 6)

    def testindexposition(self):
        """
        this test case checks the method index and the argument "_position"
        """
        self.bag["a.b.c"] = 6
        self.bag.addItem("a.b.d", 15)
        self.failUnless(self.bag.getItem("a.b.#1") == self.bag["a.b.d"] == 15)
        self.assertEqual(self.bag["a.b"].index("d"), 1)
        self.bag.setItem("a.b.middle", 10, _position="<d")
        self.assertEqual(self.bag["a.b"].index("middle"), 1)
        self.assertEqual(self.bag["a.b"].index("d"), 2)

    def testgetIndexList(self):
        il = self.fullbag.getIndexList()
        self.assertEqual(len(il), 10)

    def testkeysitemsvalues(self):
        """
        this test case checks the methods
        -keys
        -items
        -values
        """
        self.failUnless(self.fullbag["office"].keys() == self.fullbag["office?"] == ["Frank", "John", "Alexandra"])
        self.failUnless(
            len(self.fullbag["office"].keys())
            == len(self.fullbag["office"].values())
            == len(self.fullbag["office"].items())
            == 3
        )
        self.assertTrue(self.fullbag["office"].has_key("John"))

    def testpopin(self):
        """
        this test case checks the method pop and the clause "in"
        """
        self.assertTrue("John" in self.fullbag["office"])
        self.fullbag["office.Desmond"] = Bag()
        d = self.fullbag["office.Desmond"]
        removed = self.fullbag["office"].pop("#3")
        self.assertEqual(d, removed)
        self.assertEqual(self.fullbag["office"].keys(), ["Frank", "John", "Alexandra"])

    def testasDict(self):
        """
        this test case checks the method asDict
        """
        d = self.fullbag["office"].asDict()
        self.assertTrue(isinstance(d, dict))
        self.assertEqual(d.keys(), self.fullbag["office?"])