File size: 2,777 Bytes
8247f19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | diff --git a/rply/parsergenerator.py b/rply/parsergenerator.py
index f2ed8ee..995f78e 100644
--- a/rply/parsergenerator.py
+++ b/rply/parsergenerator.py
@@ -8,6 +8,8 @@ import sys
import tempfile
import warnings
+from appdirs import AppDirs
+
from rply.errors import ParserGeneratorError, ParserGeneratorWarning
from rply.grammar import Grammar
from rply.parser import LRParser
@@ -175,44 +177,28 @@ class ParserGenerator(object):
g.compute_first()
g.compute_follow()
- # win32 temp directories are already per-user
- if os.name == "nt":
- cache_file = os.path.join(
- tempfile.gettempdir(),
- "rply-%s-%s-%s.json" % (
- self.VERSION, self.cache_id, self.compute_grammar_hash(g)
- )
- )
- else:
- cache_file = os.path.join(
- tempfile.gettempdir(),
- "rply-%s-%s-%s-%s.json" % (
- self.VERSION,
- os.getuid(),
- self.cache_id,
- self.compute_grammar_hash(g)
- )
+ cache_dir = AppDirs("rply").user_cache_dir
+ cache_file = os.path.join(
+ cache_dir,
+ "rply-%s-%s-%s.json" % (
+ self.VERSION, self.cache_id, self.compute_grammar_hash(g)
)
+ )
+
table = None
if os.path.exists(cache_file):
with open(cache_file) as f:
data = json.load(f)
- stat_result = os.fstat(f.fileno())
- if (
- os.name == "nt" or (
- stat_result.st_uid == os.getuid() and
- stat.S_IMODE(stat_result.st_mode) == 0o0600
- )
- ):
- if self.data_is_valid(g, data):
- table = LRTable.from_cache(g, data)
+ if self.data_is_valid(g, data):
+ table = LRTable.from_cache(g, data)
if table is None:
table = LRTable.from_grammar(g)
- fd = os.open(
- cache_file, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o0600
- )
- with os.fdopen(fd, "w") as f:
+ if not os.path.exists(cache_dir):
+ os.makedirs(cache_dir)
+
+ with open(cache_file, "w") as f:
json.dump(self.serialize_table(table), f)
+
if table.sr_conflicts:
warnings.warn(
"%d shift/reduce conflict%s" % (
diff --git a/setup.py b/setup.py
index df6af7e..1072f5e 100644
--- a/setup.py
+++ b/setup.py
@@ -13,4 +13,5 @@ setup(
author="Alex Gaynor",
author_email="alex.gaynor@gmail.com",
packages=["rply"],
+ install_requires=["appdirs"],
)
|