Skip to content

modify for read-only-system #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions semidbm/compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# coding: utf-8
import os
import sys
try:
Expand All @@ -15,7 +16,10 @@
str_type = str



READ_ONLY_DATA_OPEN_FLAGS = os.O_RDONLY
DATA_OPEN_FLAGS = os.O_RDWR | os.O_CREAT | os.O_APPEND

if sys.platform.startswith('win'):
# On windows we need to specify that we should be
# reading the file as a binary file so it doesn't
Expand Down
10 changes: 8 additions & 2 deletions semidbm/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ def _create_db_dir(self):
if not os.path.exists(self._dbdir):
os.makedirs(self._dbdir)

def _load_db(self):
def _load_db(self, is_read_only=False):
self._create_db_dir()
self._index = self._load_index(self._data_filename)
self._data_fd = os.open(self._data_filename, compat.DATA_OPEN_FLAGS)
if is_read_only:
self._data_fd = os.open(self._data_filename, compat.READ_ONLY_DATA_OPEN_FLAGS)
else:
self._data_fd = os.open(self._data_filename, compat.DATA_OPEN_FLAGS)
self._current_offset = os.lseek(self._data_fd, 0, os.SEEK_END)

def _load_index(self, filename):
Expand Down Expand Up @@ -231,6 +234,9 @@ def compact(self):


class _SemiDBMReadOnly(_SemiDBM):
def _load_db(self):
super(_SemiDBMReadOnly, self)._load_db(is_read_only=True)

def __delitem__(self, key):
self._method_not_allowed('delitem')

Expand Down