git » galala » commit 528281a

Split add_album command in two functions to easily reuse code later

author Rodrigo Campos
2013-08-10 15:01:52 UTC
committer Rodrigo Campos
2013-08-13 08:59:22 UTC
parent 0d59a5b7b109b6215067d568ff9e58f3a3a795de

Split add_album command in two functions to easily reuse code later

Future patches will add more commands based on albums (like commands to delete
or rescan an album) and splitting this in two functions really helps to reuse
code.

This just almost moves the code to another function. The "logic" to use the
shortname as album title (if no title is given) is moved but the calls to
raw_input() are preserved in the original function.

Signed-off-by: Rodrigo Campos <rodrigo@sdfg.com.ar>

galala +36 -30

diff --git a/galala b/galala
index 1b7a25c..14e0912 100755
--- a/galala
+++ b/galala
@@ -109,6 +109,37 @@ def new_page(path):
     add_albums(p)
     common.page_save(p, path)
 
+def _add_album(page, shortname, title, text):
+    a = common.Album(shortname)
+
+    a.title = title
+    if not a.title:
+        a.title = a.shortname.decode('utf8')
+    a.text = text
+
+    while True:
+        img_path = raw_input("Album images path: ")
+        if os.path.isdir(img_path):
+            fnames = (img_path + '/' + f
+                    for f in os.listdir(img_path))
+        else:
+            fnames = open(img_path).read().split('\n')
+
+        c = 0
+        for fname in sorted(set(fnames)):
+            fname = os.path.realpath(fname)
+            if fname.lower().endswith(".jpg"):
+                i = common.Image(a)
+                i.realpath = fname
+                a.images.append(i)
+                c += 1
+
+        if c > 0:
+            break
+        else:
+            print 'ERROR: invalid path'
+
+    page.albums.append(a)
 
 def add_albums(page):
     while True:
@@ -119,36 +150,11 @@ def add_albums(page):
             print "ERROR: shortname has invalid characters"
             continue
 
-        a = common.Album(shortname)
-
-        a.title = raw_input("Album title: ").decode('utf8')
-        if not a.title:
-            a.title = a.shortname.decode('utf8')
-        a.text = raw_input("Album text: ").decode('utf8')
-
-        while True:
-            img_path = raw_input("Album images path: ")
-            if os.path.isdir(img_path):
-                fnames = (img_path + '/' + f
-                        for f in os.listdir(img_path))
-            else:
-                fnames = open(img_path).read().split('\n')
-
-            c = 0
-            for fname in sorted(set(fnames)):
-                fname = os.path.realpath(fname)
-                if fname.lower().endswith(".jpg"):
-                    i = common.Image(a)
-                    i.realpath = fname
-                    a.images.append(i)
-                    c += 1
-
-            if c > 0:
-                break
-            else:
-                print 'ERROR: invalid path'
-
-        page.albums.append(a)
+        title = raw_input("Album title: ").decode('utf8')
+        text = raw_input("Album text: ").decode('utf8')
+
+        _add_album(page, shortname, title, text)
+
         print