Browse Source

add options for json & html file output

Brennen Bearnes 2 years ago
parent
commit
1f77fcafd3
6 changed files with 68 additions and 44 deletions
  1. 9
    8
      README.md
  2. 17
    12
      bin/gallery-html
  3. 8
    8
      galleryhtml/galleryhtml.py
  4. 33
    0
      install_for_dev_and_test.sh
  5. 1
    1
      setup.py
  6. 0
    15
      test.sh

+ 9
- 8
README.md View File

@@ -50,19 +50,20 @@ gallery-html usage
50 50
     gallery-html - generate thumbnails and print html for images
51 51
     
52 52
     Usage:
53
-      gallery-html [--source=<pattern>...] [--output=<dir>] [--base_url=<url>] [-x=<pixels>] [-y=<pixels>] [--json] [--overwrite]
53
+      gallery-html [--source <pattern>...] [--output <dir>] [--base_url <url>] [-x <n>] [-y <n>] [--json <filename>] [--html <filename>] [--overwrite]
54 54
       gallery-html (-h | --help)
55 55
       gallery-html (-v | --version)
56 56
     
57 57
     Options:
58
-      -h --help           Show this screen
59
-      --json              Render JSON output instead of HTML
58
+      --source <pattern>  Source directory or image file(s) to make gallery from
59
+      --output <dir>      Parent directory on local filesystem for thumbnails
60
+      --base_url <url>    Base URL for images
61
+      -x <n>              Maximum width of thumbnails in pixels
62
+      -y <n>              Maximum height of thumbnails in pixels
63
+      --json <filename>   Render JSON output to filename
64
+      --html <filename>   Render HTML output to filename
60 65
       --overwrite         Replace existing thumbnail files
61
-      --base_url=<url>    Base URL for images
62
-      --output=<dir>      Path on local filesystem for images and thumbs
63
-      --source=<pattern>  Source directory or image file(s) to make gallery from
64
-      -x=<pixels>         Maximum width of thumbnails
65
-      -y=<pixels>         Maximum height of thumbnails
66
+      -h --help           Show this screen
66 67
       -v --version        Display version
67 68
 
68 69
 <!-- end -->

+ 17
- 12
bin/gallery-html View File

@@ -2,19 +2,20 @@
2 2
 """gallery-html - generate thumbnails and print html for images
3 3
 
4 4
 Usage:
5
-  gallery-html [--source=<pattern>...] [--output <dir>] [--base_url <url>] [-x <n>] [-y <n>] [--json] [--overwrite]
5
+  gallery-html [--source <pattern>...] [--output <dir>] [--base_url <url>] [-x <n>] [-y <n>] [--json <filename>] [--html <filename>] [--overwrite]
6 6
   gallery-html (-h | --help)
7 7
   gallery-html (-v | --version)
8 8
 
9 9
 Options:
10
-  -h --help           Show this screen
11
-  --json              Render JSON output instead of HTML
10
+  --source <pattern>  Source directory or image file(s) to make gallery from
11
+  --output <dir>      Parent directory on local filesystem for thumbnails
12
+  --base_url <url>    Base URL for images
13
+  -x <n>              Maximum width of thumbnails in pixels
14
+  -y <n>              Maximum height of thumbnails in pixels
15
+  --json <filename>   Render JSON output to filename
16
+  --html <filename>   Render HTML output to filename
12 17
   --overwrite         Replace existing thumbnail files
13
-  --base_url=<url>    Base URL for images
14
-  --output=<dir>      Path on local filesystem for images and thumbs
15
-  --source=<pattern>  Source directory or image file(s) to make gallery from
16
-  -x=<n>              Maximum width of thumbnails in pixels
17
-  -y=<n>              Maximum height of thumbnails in pixels
18
+  -h --help           Show this screen
18 19
   -v --version        Display version
19 20
 """
20 21
 
@@ -47,10 +48,14 @@ if __name__ == '__main__':
47 48
     else:
48 49
         gh.add_images_from('.')
49 50
 
50
-    # JSON data if requested, HTML otherwise.
51
+    # Write JSON data if requested:
51 52
     if args['--json']:
52
-        gh.print_images_json()
53
-    else:
54
-        gh.print_images_html()
53
+        with open(args['--json'], 'w') as f:
54
+            f.write(gh.images_json())
55
+
56
+    # Write HTML data if requested:
57
+    if args['--html']:
58
+        with open(args['--html'], 'w') as f:
59
+            f.write(gh.images_html())
55 60
 
56 61
     sys.exit()

+ 8
- 8
galleryhtml/galleryhtml.py View File

@@ -14,8 +14,8 @@ class GalleryHTML:
14 14
         self.base_url = ""
15 15
         self.extensions = ['JPG', 'jpg', 'jpeg', 'png', 'gif', 'bmp']
16 16
         self.image_list = []
17
-        self.x = 256
18
-        self.y = 256
17
+        self.x = 500
18
+        self.y = 500
19 19
         self.overwrite = False
20 20
 
21 21
     def add_images_from(self, source):
@@ -45,18 +45,18 @@ class GalleryHTML:
45 45
 
46 46
         return images
47 47
 
48
-    def print_images_html(self):
49
-        """Print HTML for an image list."""
48
+    def images_html(self):
49
+        """Return HTML for an image list."""
50 50
         html_output = ''
51 51
         for display_image in self.image_list:
52 52
             (x, y) = display_image[2]
53 53
             html_output += a(img(src=display_image[0], width=x, height=y),
54 54
                              href=display_image[1]).render() + "\n"
55
-        print(html_output.strip())
55
+        return html_output.strip()
56 56
 
57
-    def print_images_json(self):
58
-        """Print JSON for an image list."""
59
-        print(json.dumps(self.image_list))
57
+    def images_json(self):
58
+        """Return JSON for an image list."""
59
+        return json.dumps(self.image_list)
60 60
 
61 61
     def _handle_exif(self, source_image):
62 62
         """

+ 33
- 0
install_for_dev_and_test.sh View File

@@ -0,0 +1,33 @@
1
+#!/usr/bin/env bash
2
+
3
+# This probably isn't necessary:
4
+sudo pip3 install -e .
5
+
6
+cd test
7
+
8
+if [[ -d Thumbs ]]; then
9
+  echo "deleting any existing thumbs"
10
+  rm -rf Thumbs
11
+fi
12
+
13
+if [[ -f index.html ]]; then
14
+  echo "deleting index.html"
15
+  rm index.html
16
+fi
17
+
18
+if [[ -f index.json ]]; then
19
+  echo "deleting index.json"
20
+  rm index.json
21
+fi
22
+
23
+gallery-html --base_url "https://p1k3.com/photos/example/" --html index.html
24
+tail -3 index.html
25
+echo
26
+ls Thumbs
27
+
28
+gallery-html --json index.json
29
+tail -3 index.json
30
+echo
31
+
32
+gallery-html --overwrite -x 500 -y 500
33
+ls Thumbs

+ 1
- 1
setup.py View File

@@ -1,7 +1,7 @@
1 1
 from setuptools import setup
2 2
 
3 3
 setup(name='galleryhtml',
4
-      version='0.4.0',
4
+      version='0.5.0',
5 5
       description='Generate thumbnails for images and print a simple HTML gallery',
6 6
       url='http://github.com/brennen/galleryhtml',
7 7
       author='Brennen Bearnes',

+ 0
- 15
test.sh View File

@@ -1,15 +0,0 @@
1
-#!/usr/bin/env bash
2
-
3
-# This probably isn't necessary:
4
-sudo pip3 install -e .
5
-
6
-cd test
7
-if [[ -d Thumbs ]]; then
8
-  echo "deleting any existing thumbs"
9
-  rm -rf Thumbs
10
-fi
11
-gallery-html --base_url "https://p1k3.com/photos/example/"
12
-ls Thumbs
13
-gallery-html --json
14
-gallery-html --overwrite -x 500 -y 500
15
-ls Thumbs