Example #1
0
        def safe_apply(self, key, function, default_value=None):
            """
            Safely apply a function to the value of a key in storage and set
            the return value of the function to it.

            Return the result of applying the function.
            """
            key = self.key_filter_in(key)
            exists = True
            try:
                val_file = recfile.open(key, mode='r+b', path=self.folder)
            except IOError:
                exists = False
                val_file = recfile.open(key, mode='wb', path=self.folder)
            self.wait_portalock(val_file)
            if exists:
                timestamp, value = pickle.load(val_file)
            else:
                value = default_value
            new_value = function(value)
            val_file.seek(0)
            pickle.dump((time.time(), new_value), val_file,
                        pickle.HIGHEST_PROTOCOL)
            val_file.truncate()
            val_file.close()
            return new_value
Example #2
0
File: cache.py Project: fc7/web2py
        def safe_apply(self, key, function, default_value=None):
            """
            Safely apply a function to the value of a key in storage and set
            the return value of the function to it.

            Return the result of applying the function.
            """
            key = self.key_filter_in(key)
            exists = True
            try:
                val_file = recfile.open(key, mode='r+b', path=self.folder)
            except IOError:
                exists = False
                val_file = recfile.open(key, mode='wb', path=self.folder)
            self.wait_portalock(val_file)
            if exists:
                timestamp, value = pickle.load(val_file)
            else:
                value = default_value
            new_value = function(value)
            val_file.seek(0)
            pickle.dump((time.time(), new_value), val_file, pickle.HIGHEST_PROTOCOL)
            val_file.truncate()
            val_file.close()
            return new_value
Example #3
0
def save_storage(storage, filename):
    fp = None
    try:
        fp = portalocker.LockedFile(filename, 'wb')
        pickle.dump(dict(storage), fp)
    finally:
        if fp:
            fp.close()
Example #4
0
def save_storage(storage, filename):
    fp = None
    try:
        fp = portalocker.LockedFile(filename, 'wb')
        pickle.dump(dict(storage), fp)
    finally:
        if fp:
            fp.close()
Example #5
0
 def _store_on_disk(self, request, ticket_id, ticket_data):
     ef = self._error_file(request, ticket_id, 'wb')
     try:
         pickle.dump(ticket_data, ef)
     finally:
         ef.close()
Example #6
0
 def __setitem__(self, key, value):
     key = self.key_filter_in(key)
     val_file = recfile.open(key, mode='wb', path=self.folder)
     self.wait_portalock(val_file)
     pickle.dump(value, val_file, pickle.HIGHEST_PROTOCOL)
     val_file.close()
Example #7
0
 def save(self, filename):
     with open(filename, 'wb') as fp:
         pickle.dump(self.db, fp)
def main():
    """Main processing."""

    usage = '%prog [options] table [table2 table3 ...]'
    parser = OptionParser(usage=usage, version=VERSION)

    parser.add_option(
        '--man',
        action='store_true', dest='man', default=False,
        help='Display manual page-like help and exit.',
    )
    parser.add_option(
        '-v', '--verbose',
        action='store_true', dest='verbose', default=False,
        help='Print messages to stdout.',
    )
    parser.add_option(
        '--vv',
        action='store_true', dest='vv', default=False,
        help='More verbose.',
    )

    (options, unused_args) = parser.parse_args()

    if options.man:
        man_page()
        quit(0)

    set_cli_logging(LOG, options.verbose, options.vv)

    db = None

    app = 'app'                 # Replace with actual app name
    tablename = 'tablename'     # Replace with actual table name
    table = Table(
        db,
        tablename,
                                # Replace with actual Field() instances
                                # Remove unneeded attributes. See --man
        Field('name'),
        Field(
            'another_table_id',
            'integer',
        ),
        Field('time_stamp', 'datetime'),
    )

    sql_fields = {}

    for sortable, field in enumerate(table, start=1):
        field_name = field.name
        field_type = field.type
        ftype = get_ftype(table, field)
        sql_fields[field_name] = dict(
            length=field.length,
            unique=field.unique,
            notnull=field.notnull,
            sortable=sortable,
            type=str(field_type),
            sql=ftype)

    dbpath = 'applications/{a}/databases'.format(a=app)
    adapter_uri = 'sqlite://{a}.sqlite'.format(a=app)

    uri_hash = hashlib_md5(adapter_uri).hexdigest()
    print('uri_hash: {var}'.format(var=uri_hash))

    # pylint: disable=protected-access
    table._dbt = pjoin(
        dbpath, '%s_%s.table' % (uri_hash, tablename))
    print('table._dbt: {var}'.format(var=table._dbt))

    if table._dbt:
        tfile = DubBaseAdapter.file_open(table._dbt, 'wb')
        pickle.dump(sql_fields, tfile)
        DubBaseAdapter.file_close(tfile)
Example #9
0
File: cache.py Project: fc7/web2py
 def __setitem__(self, key, value):
     key = self.key_filter_in(key)
     val_file = recfile.open(key, mode='wb', path=self.folder)
     self.wait_portalock(val_file)
     pickle.dump(value, val_file, pickle.HIGHEST_PROTOCOL)
     val_file.close()