Skip to content

Commit

Permalink
Merge pull request #753 from benoit-pierre/mark_dictionary_readonly_i…
Browse files Browse the repository at this point in the history
…f_file_is

steno_dictionary: mark readonly if file is
  • Loading branch information
morinted authored May 4, 2017
2 parents 27bd885 + 3fdc2a3 commit e051e0b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion plover/steno_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import collections
import os
import shutil

# Python 2/3 compatibility.
Expand Down Expand Up @@ -58,7 +59,8 @@ def load(cls, resource):
timestamp = resource_timestamp(filename)
d = cls()
d._load(filename)
if resource.startswith(ASSET_SCHEME):
if resource.startswith(ASSET_SCHEME) or \
not os.access(filename, os.W_OK):
d.readonly = True
d.path = resource
d.timestamp = timestamp
Expand Down
26 changes: 26 additions & 0 deletions test/test_steno_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

"""Unit tests for steno_dictionary.py."""

import os
import stat
import tempfile
import unittest

# Python 2/3 compatibility.
Expand Down Expand Up @@ -224,3 +227,26 @@ def test_dictionary_enabled(self):
self.assertEqual(dc.raw_lookup(('TEFT',)), None)
self.assertEqual(dc.casereverse_lookup('testing'), None)
assertCountEqual(self, dc.reverse_lookup('Testing'), [])

def test_dictionary_readonly(self):
class FakeDictionary(StenoDictionary):
def _load(self, filename):
pass
tf = tempfile.NamedTemporaryFile(delete=False)
try:
tf.close()
d = FakeDictionary.load(tf.name)
# Writable file: not readonly.
self.assertFalse(d.readonly)
# Readonly file: readonly dictionary.
os.chmod(tf.name, stat.S_IREAD)
d = FakeDictionary.load(tf.name)
self.assertTrue(d.readonly)
finally:
# Deleting the file will fail on Windows
# if we don't restore write permission.
os.chmod(tf.name, stat.S_IWRITE)
os.unlink(tf.name)
# Assets are always readonly.
d = FakeDictionary.load('asset:plover:assets/main.json')
self.assertTrue(d.readonly)

0 comments on commit e051e0b

Please sign in to comment.