Skip to content

Commit

Permalink
Merge pull request #9 from OpenPathView/master
Browse files Browse the repository at this point in the history
Add autosave arg, Fixes #7
  • Loading branch information
philae-ael authored May 5, 2017
2 parents 9a6d6fc + a7e6dd7 commit e49cf33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 4 additions & 3 deletions opv_directorymanagerclient/directorymanagerclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,16 @@ def __fetch_protocols(self):

return list(filter(None.__ne__, map(self.__str2Protocol, r.json())))

def Open(self, uuid=None):
def Open(self, uuid=None, autosave=True):
"""
Get a directory form it's uuid or create one.
:param uuid: Optional directory's uuid.
:param autosave: Save back to the server at ext/close (default: True).
"""
if self.__default_protocol == Protocol.FTP:
return DirectoryUuidFtp(uuid=uuid, api_base=self.__api_base, workspace_directory=self.__workspace_directory)
return DirectoryUuidFtp(uuid=uuid, api_base=self.__api_base, workspace_directory=self.__workspace_directory, autosave=autosave)
if self.__default_protocol == Protocol.FILE:
return DirectoryUuidFile(uuid=uuid, api_base=self.__api_base, workspace_directory=self.__workspace_directory)
return DirectoryUuidFile(uuid=uuid, api_base=self.__api_base, workspace_directory=self.__workspace_directory, autosave=autosave)
raise NotImplemented

@property
Expand Down
10 changes: 7 additions & 3 deletions opv_directorymanagerclient/directoryuuid/directoryuuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ class DirectoryUuid():
implement a ContextManager that return a (uuid, local path).
"""

def __init__(self, workspace_directory, api_base: str, uuid=None):
def __init__(self, workspace_directory, api_base: str, uuid=None, autosave=True):
"""
:param uuid: Directory UUID.
:param api_base: Api base URL.
:param work_directory: Local directory used to store file. If you use local protocol you may use
a folder on the same partition so that cp will be hard link.
:param autosave: Save changed data on the server at exit or context manager close (Default: True).
"""
self.__api_base = api_base
self.__workspace_directory = workspace_directory
self._uuid = uuid if uuid is not None else self.__generate_uuid()
self._syncable_local = None
self._syncable_remote = None # User need to define it in their implementation
self.__create_local_directory()
self._autosave = autosave

# Fetching files for existing uuids
if uuid is not None:
Expand Down Expand Up @@ -181,10 +183,12 @@ def __exit__(self, type, value, traceback):
Context manager.
Save files back to server.
"""
self.save()
if self._autosave:
self.save()

def __del__(self):
"""
Save files back to server. Might not be called.
"""
self.save()
if self._autosave:
self.save()

0 comments on commit e49cf33

Please sign in to comment.