Python dbm.open() Examples

The following are 30 code examples of dbm.open(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dbm , or try the search function .
Example #1
Source File: test.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_107_large_file_report():               # issue #107 {{{1
    import os

    errors = []
    fname = "sample.bin"
    for n in (4294967294, 4294967297):
        fp = open(fname, "wb")
        fp.seek(n)
        fp.write("1".encode("utf-8"))
        fp.close()

        ans = os.path.getsize(fname)
        if ans != (n + 1):
            errors.append("%s(answer) vs %s(expected)" % (ans, n + 1))
        os.remove(fname)

    if not errors:
        return True
    print("can't get size collectly with %s" % str(errors))
    return False 
Example #2
Source File: bottle.py    From annotated-py-bottle with MIT License 6 votes vote down vote up
def close(self):  # 关闭
        for db in self.open.values():
            db.close()
        self.open.clear()







###############################################################################
#                       全局变量定义 - 配置参数
###############################################################################

# Modul initialization    模块的初始化参数. 
Example #3
Source File: installer.py    From the-littlest-jupyterhub with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def ensure_config_yaml(plugin_manager):
    """
    Ensure we have a config.yaml present
    """
    # ensure config dir exists and is private
    for path in [CONFIG_DIR, os.path.join(CONFIG_DIR, 'jupyterhub_config.d')]:
        os.makedirs(path, mode=0o700, exist_ok=True)

    migrator.migrate_config_files()

    if os.path.exists(CONFIG_FILE):
        with open(CONFIG_FILE, 'r') as f:
            config = yaml.load(f)
    else:
        config = {}

    hook = plugin_manager.hook
    hook.tljh_config_post_install(config=config)

    with open(CONFIG_FILE, 'w+') as f:
        yaml.dump(config, f) 
Example #4
Source File: bottle.py    From annotated-py-projects with MIT License 6 votes vote down vote up
def close(self):  # 关闭
        for db in self.open.values():
            db.close()
        self.open.clear()







###############################################################################
#                       全局变量定义 - 配置参数
###############################################################################

# Modul initialization    模块的初始化参数. 
Example #5
Source File: loaders.py    From datatemplates with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_csv(source,
             absolute_resolved_path,
             headers=False,
             dialect=None,
             encoding='utf-8-sig',
             **options):
    with open(absolute_resolved_path, 'r', newline='', encoding=encoding) as f:
        if dialect == "auto":
            sample = f.read(8192)
            f.seek(0)
            sniffer = csv.Sniffer()
            dialect = sniffer.sniff(sample)
        if headers:
            if dialect is None:
                r = csv.DictReader(f)
            else:
                r = csv.DictReader(f, dialect=dialect)
        else:
            if dialect is None:
                r = csv.reader(f)
            else:
                r = csv.reader(f, dialect=dialect)
        yield list(r) 
Example #6
Source File: firstuseauthenticator.py    From firstuseauthenticator with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def reset_password(self, username, new_password):
        """
        This allows changing the password of a logged user.
        """
        if not self._validate_password(new_password):
            login_err = (
                'Password too short! Please choose a password at least %d characters long.'
                % self.min_password_length
            )
            self.log.error(login_err)
            # Resetting the password will fail if the new password is too short.
            return login_err
        with dbm.open(self.dbm_path, 'c', 0o600) as db:
            db[username] = bcrypt.hashpw(new_password.encode(),
                                         bcrypt.gensalt())
        login_msg = "Your password has been changed successfully!"
        self.log.info(login_msg)
        return login_msg 
Example #7
Source File: basedb.py    From scalyr-agent-2 with Apache License 2.0 6 votes vote down vote up
def keys(self):
        """
        Return a list of usernames in the database.

        :rtype: list
        :returns: The usernames in the database.
        """
        if self.db == None:
            raise AssertionError("DB not open")

        self.lock.acquire()
        try:
            usernames = self.db.keys()
        finally:
            self.lock.release()
        usernames = [u for u in usernames if not u.startswith("--Reserved--")]
        return usernames 
Example #8
Source File: nauta.py    From nauta-cli with MIT License 6 votes vote down vote up
def time_left(username, fresh=False, cached=False):
    now = time.time()
    with dbm.open(CARDS_DB, "c") as cards_db:
        card_info = json.loads(cards_db[username].decode())
        last_update = card_info.get('last_update', 0)
        password = card_info['password']
        if not cached:
            if (now - last_update > 60) or fresh:
                time_left = fetch_usertime(username)
                last_update = time.time()
                if re.match(r'[0-9:]+', time_left):
                    card_info['time_left'] = time_left
                    card_info['last_update'] = last_update
                    cards_db[username] = json.dumps(card_info)
        time_left = card_info.get('time_left', '-')
        return time_left 
Example #9
Source File: nauta.py    From nauta-cli with MIT License 6 votes vote down vote up
def down(args):
    try:
        logout_url = open(LOGOUT_URL_FILE).read().strip()
    except FileNotFoundError:
        print("Connection seems to be down already. To connect, use 'nauta up'")
        return
    session = requests.Session()
    print("Logging out...")
    for error_count in range(10):
        try:
            r = session.get(logout_url)
            break
        except requests.RequestException:
            print("There was a problem logging out, retrying...")
            continue
    print(r.text)
    if 'SUCCESS' in r.text:
        os.remove(LOGOUT_URL_FILE) 
Example #10
Source File: anydbm.py    From Computable with MIT License 6 votes vote down vote up
def open(file, flag = 'r', mode = 0666):
    # guess the type of an existing database
    from whichdb import whichdb
    result=whichdb(file)
    if result is None:
        # db doesn't exist
        if 'c' in flag or 'n' in flag:
            # file doesn't exist and the new
            # flag was used so use default type
            mod = _defaultmod
        else:
            raise error, "need 'c' or 'n' flag to open new db"
    elif result == "":
        # db type cannot be determined
        raise error, "db type could not be determined"
    else:
        mod = __import__(result)
    return mod.open(file, flag, mode) 
Example #11
Source File: BaseDB.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def keys(self):
        """Return a list of usernames in the database.

        @rtype: list
        @return: The usernames in the database.
        """
        if self.db == None:
            raise AssertionError("DB not open")

        self.lock.acquire()
        try:
            usernames = list(self.db.keys())
        finally:
            self.lock.release()
        usernames = [u for u in usernames if not u.startswith("--Reserved--")]
        return usernames 
Example #12
Source File: subunit_trace.py    From os-testr with Apache License 2.0 6 votes vote down vote up
def find_test_run_time_diff(test_id, run_time):
    times_db_path = os.path.join(os.path.join(os.getcwd(), '.testrepository'),
                                 'times.dbm')
    if os.path.isfile(times_db_path):
        try:
            test_times = dbm.open(times_db_path)
        except Exception:
            return False
        try:
            avg_runtime = float(test_times.get(str(test_id), False))
        except Exception:
            try:
                avg_runtime = float(test_times[str(test_id)])
            except Exception:
                avg_runtime = False

        if avg_runtime and avg_runtime > 0:
            run_time = float(run_time.rstrip('s'))
            perc_diff = ((run_time - avg_runtime) / avg_runtime) * 100
            return perc_diff
    return False 
Example #13
Source File: test.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_071_anydbm():                          # issue #71 {{{1
    import os
    if sys.version_info[0] == 2:
        import anydbm
    else:
        import dbm as anydbm
    # FIXME: determine path to sdcard. like: path = os.environ[""]
    del os.chmod
    for fname in (
        # failed: this is not SL4A application folder...
        # os.path.join("/data/data/com.googlecode.pythonforandroid",
        #              "files", "test_anydbm.dbm"),
        # OK: _chmod work well.
        # os.path.join("/data/local/abc", "test_anydbm.dbm"),
        # failed: _chmod not worked in FAT (SD card)
        os.path.join("/sdcard", "sl4a", "test_anydbm.dbm"),
    ):
        try:
            os.remove(fname + ".dat")
        except:
            pass
        anydbm.open(fname, "n")
        os.remove(fname + ".dat")
    return True 
Example #14
Source File: shelve.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def __init__(self, filename, flag='c', protocol=None, writeback=False):
        import dbm
        Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback) 
Example #15
Source File: BaseDB.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, username):
        if self.db == None:
            raise AssertionError("DB not open")

        self.lock.acquire()
        try:
            valueStr = self.db[username]
        finally:
            self.lock.release()

        return self._getItem(username, valueStr) 
Example #16
Source File: BaseDB.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def open(self):
        """Open a pre-existing on-disk database.

        @raise anydbm.error: If there's a problem opening the database.
        @raise ValueError: If the database is not of the right type.
        """
        if not self.filename:
            raise ValueError("Can only open on-disk databases")
        self.db = dbm.open(self.filename, "w") #raises anydbm.error
        try:
            if self.db["--Reserved--type"] != self.type:
                raise ValueError("Not a %s database" % self.type)
        except KeyError:
            raise ValueError("Not a recognized database") 
Example #17
Source File: BaseDB.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def create(self):
        """Create a new on-disk database.

        @raise anydbm.error: If there's a problem creating the database.
        """
        if self.filename:
            self.db = dbm.open(self.filename, "n") #raises anydbm.error
            self.db["--Reserved--type"] = self.type
            self.db.sync()
        else:
            self.db = {} 
Example #18
Source File: basedb.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def open(self):
        """
        Open a pre-existing on-disk database.

        :raises anydbm.error: If there's a problem opening the database.
        :raises ValueError: If the database is not of the right type.
        """
        if not self.filename:
            raise ValueError("Can only open on-disk databases")
        self.db = anydbm.open(self.filename, "w") #raises anydbm.error
        try:
            if self.db["--Reserved--type"] != self.type:
                raise ValueError("Not a %s database" % self.type)
        except KeyError:
            raise ValueError("Not a recognized database") 
Example #19
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def load(self, key):
        try:
            shelve_file = shelve.open('ShelvePersistence')
            if key in shelve_file:
                result = shelve_file[key]
            else:
                result = None
        finally:
            shelve_file.close()
        return result 
Example #20
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def save(self, key, obj):
        try:
            shelve_file = shelve.open('ShelvePersistence')
            shelve_file[key] = obj
        finally:
            shelve_file.close() 
Example #21
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def save(self, obj):
        with open('PicklePersistence', 'wb') as pickle_file:
            pickle.dump(obj, pickle_file) 
Example #22
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def load(self, key):
        try:
            dbm_file = dbm.open('DBMPersistence', 'r')
            if key in dbm_file:
                result = dbm_file[key]
            else:
                result = None
        finally:
            dbm_file.close()
        return result 
Example #23
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def save(self, key, value):
        try:
            dbm_file = dbm.open('DBMPersistence', 'c')
            dbm_file[key] = str(value)
        finally:
            dbm_file.close() 
Example #24
Source File: basedb.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def __delitem__(self, username):
        if self.db == None:
            raise AssertionError("DB not open")

        self.lock.acquire()
        try:
            del(self.db[username])
            if self.filename:
                self.db.sync()
        finally:
            self.lock.release() 
Example #25
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def load(self):
        with open('NormalFilePersistence.txt', 'r') as open_file:
            return open_file.read() 
Example #26
Source File: demo_local_disk_file_persistence.py    From SmallReptileTraining with MIT License 5 votes vote down vote up
def save(self, data):
        with open('NormalFilePersistence.txt', 'w') as open_file:
            open_file.write(data) 
Example #27
Source File: firstuseauthenticator.py    From firstuseauthenticator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def delete_user(self, user):
        """
        When user is deleted, remove their entry from password db.

        This lets passwords be reset by deleting users.
        """
        try:
            with dbm.open(self.dbm_path, 'c', 0o600) as db:
                del db[user.name]
        except KeyError as k:
            pass 
Example #28
Source File: firstuseauthenticator.py    From firstuseauthenticator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def authenticate(self, handler, data):
        username = data['username']

        if not self.create_users:
            if not self._user_exists(username):
                return None

        password = data['password']
        # Don't enforce password length requirement on existing users, since that can
        # lock users out of their hubs.
        if not self._validate_password(password) and not self._user_exists(username):
            handler.custom_login_error = (
                'Password too short! Please choose a password at least %d characters long.'
                % self.min_password_length
            )

            self.log.error(handler.custom_login_error)
            return None
        with dbm.open(self.dbm_path, 'c', 0o600) as db:
            stored_pw = db.get(username.encode(), None)
            if stored_pw is not None:
                if bcrypt.hashpw(password.encode(), stored_pw) != stored_pw:
                    return None
            else:
                db[username] = bcrypt.hashpw(password.encode(),
                                             bcrypt.gensalt())
        return username 
Example #29
Source File: basedb.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, username):
        if self.db == None:
            raise AssertionError("DB not open")

        self.lock.acquire()
        try:
            valueStr = self.db[username]
        finally:
            self.lock.release()

        return self._getItem(username, valueStr) 
Example #30
Source File: basedb.py    From scalyr-agent-2 with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, username, value):
        if self.db == None:
            raise AssertionError("DB not open")

        valueStr = self._setItem(username, value)

        self.lock.acquire()
        try:
            self.db[username] = valueStr
            if self.filename:
                self.db.sync()
        finally:
            self.lock.release()