Repository: ManiacalLabs/BiblioPixel
Pull request #924 (2018-08-17 14:58:43)

Extract GIFs to DocsFiles repository


Patch:
diff --git a/bibliopixel/util/image/extract_gif_lines.py b/bibliopixel/util/image/extract_gif_lines.py
index 7b93e1e..fd0d8b2 100644
--- a/bibliopixel/util/image/extract_gif_lines.py
+++ b/bibliopixel/util/image/extract_gif_lines.py
@@ -1,19 +1,10 @@
-import os, re
+import re
 from .. import data_file, log
 
-GIF_RE = re.compile(r'\b[\w-]+\.gif\b')
-GIF_ROOT = 'doc/'
-PATTERNS = {GIF_ROOT: GIF_ROOT}
+PREFIX = """\
+https://raw.githubusercontent.com/ManiacalLabs/DocsFiles/master/BiblioPixel/"""
 
-
-def doc_path(filename):
-    dirname = os.path.dirname(filename)
-
-    for prefix, replacement in PATTERNS.items():
-        if dirname.startswith(prefix):
-            return replacement + dirname[len(prefix):]
-
-    return GIF_ROOT + dirname
+GIF_RE = re.compile(r'! \[ .* \] \(' + PREFIX + r'(.* \.gif) \)', re.X)
 
 
 def _extract(lines):
@@ -36,22 +27,20 @@ def _extract(lines):
             code.append(line)
 
         elif state is AFTER_CODE:
-            m = GIF_RE.search(line)
+            m = GIF_RE.match(line.strip())
             if m:
-                yield m.group(), code
+                yield m.group(1), code
 
             if line.strip():
                 state = TEXT
 
 
-def extract_gif_lines(filename, lines):
-    path = doc_path(filename)
-    for base, code in _extract(lines):
-        full_path = os.path.join(path, base)
+def extract_gif_lines(lines):
+    for filename, code in _extract(lines):
         codelines = '\n'.join(code)
         try:
             project = data_file.loads(codelines)
         except:
             log.error('Unable to load code: $s\n%s', filename, codelines)
         else:
-            yield full_path, project
+            yield filename, project
diff --git a/scripts/gif-extractor b/scripts/gif-extractor
index 7b7e2a7..9a96a42 100755
--- a/scripts/gif-extractor
+++ b/scripts/gif-extractor
@@ -1,6 +1,19 @@
 #!/usr/bin/env python3
 
-import argparse, copy, math, os, subprocess, sys, tempfile
+"""
+This script extracts GIFs from the various components that make up BLiPS
+and puts them into the DocsFiles repo.
+
+It relies on a directory structure like this:
+
+/<your-path>
+    /BiblioPixel   <-- current working directory
+    /BiblioPixelAnimations
+    /DocsFiles
+
+"""
+
+import argparse, copy, math, pathlib, os, subprocess, sys, tempfile
 from unittest import mock
 from bibliopixel.util import data_file, log, walk
 from bibliopixel.util.image.extract_gif_lines import extract_gif_lines
@@ -10,14 +23,11 @@ SUFFIXES = '.md', '.py'
 RAISE = True
 IMAGE_WIDTH = 384
 PADDING_RATIO = 1 / 4
+ROOT = pathlib.Path(__file__).absolute().parents[2].joinpath(
+    'DocsFiles', 'BiblioPixel')
 
 
 def extract_one_gif(args, filename, desc):
-    dirname, fname = os.path.split(filename)
-    dirname = os.path.join(dirname, 'img')
-    filename = os.path.join(dirname, fname)
-
-    os.makedirs(dirname, exist_ok=True)
     shape = desc.get('shape')
     if shape is None:
         layout = desc.get('layout')
@@ -46,25 +56,30 @@ def extract_one_gif(args, filename, desc):
         'run': {
             'flat_out': True,
             'fps': args.fps,
-        }
+        },
+        'numbers': 'float',
     }
 
     merged = merge.merge(default, desc)
     saved_merged = copy.deepcopy(merged)
-    yml_name = filename + '.yml'
+
+    path = ROOT.joinpath(filename)
+    os.makedirs(str(path.parent), exist_ok=True)
+    yml_path = str(path) + '.yml'
     try:
-        df = data_file.load(yml_name)
+        df = data_file.load(yml_path)
         if df == merged:
             log.printer('Cached', filename)
             return
     except:
         pass
 
-    log.printer('--> Extracting', filename)
+    default['driver']['filename'] = str(path)
+    log.printer('--> Extracting to', path)
     try:
         pr = project.project(default, desc)
         pr.run()
-        data_file.dump(saved_merged, yml_name)
+        data_file.dump(saved_merged, yml_path)
     except KeyboardInterrupt:
         raise
     except:
@@ -75,8 +90,7 @@ def extract_one_gif(args, filename, desc):
 
 def extract(args):
     for root, input_file in walk.walk_suffix(args.roots, SUFFIXES):
-        relpath = os.path.relpath(input_file, root)
-        for filename, desc in extract_gif_lines(relpath, open(input_file)):
+        for filename, desc in extract_gif_lines(open(input_file)):
             extract_one_gif(args, filename, desc)
 
 


Test patch:
diff --git a/test/bibliopixel/util/image/extract_gif_lines_test.py b/test/bibliopixel/util/image/extract_gif_lines_test.py
index 5930b4b..cc50813 100644
--- a/test/bibliopixel/util/image/extract_gif_lines_test.py
+++ b/test/bibliopixel/util/image/extract_gif_lines_test.py
@@ -4,24 +4,12 @@ from bibliopixel.util.image import extract_gif_lines
 
 
 class ExtractGifLinesTest(unittest.TestCase):
-    def test_doc_path(self):
-        path = 'bibliopixel/drivers/driver.py'
-        actual = extract_gif_lines.doc_path(path)
-        expected = 'doc/bibliopixel/drivers'
-        self.assertEqual(actual, expected)
-
-        path = 'doc/bibliopixel/drivers/driver.py'
-        actual = extract_gif_lines.doc_path(path)
-        expected = 'doc/bibliopixel/drivers'
-        self.assertEqual(actual, expected)
-
     def test_extract(self):
         actual = list(extract_gif_lines._extract(GIF_LINES))
         self.assertEqual(actual, EXPECTED1)
 
     def test_extract_gif_lines(self):
-        source = 'bibliopixel/animations/test.py'
-        actual = list(extract_gif_lines.extract_gif_lines(source, GIF_LINES))
+        actual = list(extract_gif_lines.extract_gif_lines(GIF_LINES))
         self.assertEqual(actual, EXPECTED2)
 
 
@@ -46,12 +34,16 @@ GIF_LINES = """
     animation: BiblioPixelAnimations.matrix.MatrixRain
     shape: [32, 32]
 ```
-![stuff foo/bar/something.gif]#$(
+
+![Result](https://raw.githubusercontent.com/ManiacalLabs/DocsFiles/master/\
+BiblioPixel/doc/bibliopixel/animations/something.gif)
+
 ```
     animation: .split
     shape: 128
 ```
-minimal.gif
+![Result](https://raw.githubusercontent.com/ManiacalLabs/DocsFiles/master/\
+BiblioPixel/doc/bibliopixel/animations/minimal.gif)
 """.splitlines()
 
 YAML_LINES_1 = """\
@@ -65,8 +57,8 @@ YAML_LINES_2 = """\
 """.splitlines()
 
 EXPECTED1 = [
-    ('something.gif', YAML_LINES_1),
-    ('minimal.gif', YAML_LINES_2)]
+    ('doc/bibliopixel/animations/something.gif', YAML_LINES_1),
+    ('doc/bibliopixel/animations/minimal.gif', YAML_LINES_2)]
 
 DATA1 = {
     'animation': 'BiblioPixelAnimations.matrix.MatrixRain',