Skip to content

Commit

Permalink
Some schema documentation files take a lot of time to generate. Cache…
Browse files Browse the repository at this point in the history
… them !

This is especially the case of Compara, not much eHive.
  • Loading branch information
muffato committed May 26, 2018
1 parent f0f8498 commit 5f49d47
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/appendix/hive_schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
:foreign_keys: $EHIVE_ROOT_DIR/sql/foreign_keys.sql
:title: Hive
:embed_diagrams:
:cached:

33 changes: 33 additions & 0 deletions docs/xhive/code_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
import os
import os.path
import pickle
import subprocess
import sys

Expand Down Expand Up @@ -69,8 +70,12 @@ class SchemaDocumentation(IncludeCommand):
'intro' : directives.unchanged,
'url' : directives.unchanged,
'embed_diagrams' : directives.flag,
'cached' : directives.flag,
}

# Where to keep the cached outputs
cache_filename = os.path.join("_build", "rtd_cache.pickle")

def get_command(self):
command = [
os.path.join(os.environ["EHIVE_ROOT_DIR"], "scripts", "dev", "sql2rst.pl"),
Expand All @@ -91,6 +96,34 @@ def get_command(self):
command.extend( ['--intro', self.options['intro'].replace('$EHIVE_ROOT_DIR', os.environ["EHIVE_ROOT_DIR"])] )
return command

def get_key(self):
schema_file = self.arguments[0].replace('$EHIVE_ROOT_DIR', os.environ["EHIVE_ROOT_DIR"])
with open(schema_file, "r") as fh:
schema = fh.read()
return (schema, tuple(sorted(self.options.items())))

def get_cache(self):
if os.path.exists(self.cache_filename):
with open(self.cache_filename, "rb") as fh:
return pickle.load(fh)
return {}

def write_cache(self, content_cache):
with open(self.cache_filename, "wb") as fh:
pickle.dump(content_cache, fh)

def get_content(self):
if 'cached' not in self.options:
return super(SchemaDocumentation, self).get_content()
key = self.get_key()
content_cache = self.get_cache()
if key in content_cache:
return content_cache[key]
content = super(SchemaDocumentation, self).get_content()
content_cache[key] = content
self.write_cache(content_cache)
return content

class ScriptDocumentation(IncludeCommand):
required_arguments = 1
optional_arguments = 0
Expand Down

0 comments on commit 5f49d47

Please sign in to comment.