summaryrefslogtreecommitdiff
path: root/app/lib/filebrowser/management/commands/fb_version_generate.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
committerluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
commitefb623af0bcb47d510501c282e1326b11343a29c (patch)
tree3a35fb19f5eba3b219c65277a5fb712cbe9604ac /app/lib/filebrowser/management/commands/fb_version_generate.py
parent0b481fd7931c2ae20ca21f89a87f2ba6a6c01e10 (diff)
site reorg
Diffstat (limited to 'app/lib/filebrowser/management/commands/fb_version_generate.py')
-rw-r--r--app/lib/filebrowser/management/commands/fb_version_generate.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/app/lib/filebrowser/management/commands/fb_version_generate.py b/app/lib/filebrowser/management/commands/fb_version_generate.py
new file mode 100644
index 0000000..be33f60
--- /dev/null
+++ b/app/lib/filebrowser/management/commands/fb_version_generate.py
@@ -0,0 +1,84 @@
+# coding: utf-8
+
+# Python
+import os, re
+
+# Django
+from django.core.management.base import BaseCommand, CommandError
+
+# filebrowser
+from filebrowser.settings import EXTENSION_LIST, EXCLUDE, MEDIA_ROOT, DIRECTORY, VERSIONS, EXTENSIONS
+from filebrowser.functions import version_generator
+
+class Command(BaseCommand):
+ args = '<media_path>'
+ help = "(Re)Generate versions of Images within the FILEBROWSER_DIRECTORY or a "
+
+ def handle(self, *args, **options):
+ media_path = ""
+
+ if len(args):
+ media_path = args[0]
+ path = os.path.join(MEDIA_ROOT, media_path)
+ else:
+ path = os.path.join(MEDIA_ROOT, DIRECTORY)
+
+ if not os.path.isdir(path):
+ raise CommandError('<media_path> must be a directory in MEDIA_ROOT (If you don\'t add a media_path the default path is FILEBROWSER_DIRECTORY).\n"%s" is no directory.' % path);
+
+ # get version name
+ while 1:
+ self.stdout.write('\nSelect a version you whant to generate:\n')
+ for version in VERSIONS:
+ self.stdout.write(' * %s\n' % version)
+
+ version_name = raw_input('(leave blank to generate all versions): ')
+
+ if version_name == "":
+ selected_version = None
+ break
+ else:
+ try:
+ tmp = VERSIONS[version_name]
+ selected_version = version_name
+ break
+ except:
+ self.stderr.write('Error: Version "%s" doesn\'t exist.\n' % version_name)
+ version_name = None
+ continue
+
+ # Precompile regular expressions
+ filter_re = []
+ for exp in EXCLUDE:
+ filter_re.append(re.compile(exp))
+ for k,v in VERSIONS.iteritems():
+ exp = (r'_%s.(%s)') % (k, '|'.join(EXTENSION_LIST))
+ filter_re.append(re.compile(exp))
+
+ # walkt throu the filebrowser directory
+ # for all/new files (except file versions itself and excludes)
+ for dirpath,dirnames,filenames in os.walk(path):
+ for filename in filenames:
+ filtered = False
+ # no "hidden" files (stating with ".")
+ if filename.startswith('.'):
+ continue
+ # check the exclude list
+ for re_prefix in filter_re:
+ if re_prefix.search(filename):
+ filtered = True
+ if filtered:
+ continue
+ (tmp, extension) = os.path.splitext(filename)
+ if extension in EXTENSIONS["Image"]:
+ self.createVersions(os.path.join(dirpath, filename), selected_version)
+
+
+ def createVersions(self, path, selected_version):
+ if selected_version:
+ self.stdout.write('generating version "%s" for: %s\n' % (selected_version, path))
+ version_generator(path, selected_version, True)
+ else:
+ self.stdout.write('generating all versions for: %s\n' % path)
+ for version in VERSIONS:
+ version_generator(path, version, True)