X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=mediagoblin%2Fworkbench.py;h=2331b5511e59ffa377e856ab0a14b0cd8649167c;hb=1f36e6bee969fff96bb925673c045983618b60d0;hp=32229d2e3b9ae8d20f659e3236232533704e1a4f;hpb=8bfa533f8b438e57a7950a8dcd02a275cbfa19df;p=mediagoblin.git diff --git a/mediagoblin/workbench.py b/mediagoblin/workbench.py index 32229d2e..2331b551 100644 --- a/mediagoblin/workbench.py +++ b/mediagoblin/workbench.py @@ -1,5 +1,5 @@ # GNU MediaGoblin -- federated, autonomous media hosting -# Copyright (C) 2011 Free Software Foundation, Inc +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -23,32 +23,34 @@ DEFAULT_WORKBENCH_DIR = os.path.join( tempfile.gettempdir(), u'mgoblin_workbench') -# Exception(s) -# ------------ - -class WorkbenchOutsideScope(Exception): - """ - Raised when a workbench is outside a WorkbenchManager scope. - """ - pass - - # Actual workbench stuff # ---------------------- class Workbench(object): """ Represent the directory for the workbench + + WARNING: DO NOT create Workbench objects on your own, + let the WorkbenchManager do that for you! """ def __init__(self, dir): + """ + WARNING: DO NOT create Workbench objects on your own, + let the WorkbenchManager do that for you! + """ self.dir = dir def __unicode__(self): return unicode(self.dir) + def __str__(self): return str(self.dir) + def __repr__(self): - return repr(self.dir) + try: + return str(self) + except AttributeError: + return 'None' def joinpath(self, *args): return os.path.join(self.dir, *args) @@ -117,6 +119,19 @@ class Workbench(object): return full_dest_filename + def destroy_self(self): + """ + Destroy this workbench! Deletes the directory and all its contents! + + WARNING: Does no checks for a sane value in self.dir! + """ + # just in case + workbench = os.path.abspath(self.dir) + + shutil.rmtree(workbench) + + del self.dir + class WorkbenchManager(object): """ @@ -130,24 +145,9 @@ class WorkbenchManager(object): self.base_workbench_dir = os.path.abspath(base_workbench_dir) if not os.path.exists(self.base_workbench_dir): os.makedirs(self.base_workbench_dir) - + def create_workbench(self): """ Create and return the path to a new workbench (directory). """ return Workbench(tempfile.mkdtemp(dir=self.base_workbench_dir)) - - def destroy_workbench(self, workbench): - """ - Destroy this workbench! Deletes the directory and all its contents! - - Makes sure the workbench actually belongs to this manager though. - """ - # just in case - workbench = os.path.abspath(workbench.dir) - - if not workbench.startswith(self.base_workbench_dir): - raise WorkbenchOutsideScope( - "Can't destroy workbench outside the base workbench dir") - - shutil.rmtree(workbench)