Skip to content

Commit

Permalink
Moving more slowly to pathlib (#549)
Browse files Browse the repository at this point in the history
* Started moving to pathlib.

* Finished converting create to pathlib.
  • Loading branch information
gabrieldemarmiesse authored and JarnoRFB committed Aug 1, 2019
1 parent b9df680 commit 3faff43
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions sacred/observers/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json
import os
import os.path
from pathlib import Path
from typing import Optional

from shutil import copyfile

Expand All @@ -12,6 +14,7 @@
from sacred.observers.base import RunObserver
from sacred import optional as opt
from sacred.serializer import flatten
from sacred.utils import PathType


DEFAULT_FILE_STORAGE_PRIORITY = 20
Expand All @@ -21,26 +24,30 @@ class FileStorageObserver(RunObserver):
VERSION = 'FileStorageObserver-0.7.0'

@classmethod
def create(cls, basedir, resource_dir=None, source_dir=None,
template=None, priority=DEFAULT_FILE_STORAGE_PRIORITY):
resource_dir = resource_dir or os.path.join(basedir, '_resources')
source_dir = source_dir or os.path.join(basedir, '_sources')
def create(cls, basedir: PathType,
resource_dir: Optional[PathType] = None,
source_dir: Optional[PathType] = None,
template: Optional[PathType] = None,
priority: int = DEFAULT_FILE_STORAGE_PRIORITY):
basedir = Path(basedir)
resource_dir = resource_dir or basedir / '_resources'
source_dir = source_dir or basedir / '_sources'
if template is not None:
if not os.path.exists(template):
raise FileNotFoundError("Couldn't find template file '{}'"
.format(template))
else:
template = os.path.join(basedir, 'template.html')
if not os.path.exists(template):
template = basedir / 'template.html'
if not template.exists():
template = None
return cls(basedir, resource_dir, source_dir, template, priority)

def __init__(self, basedir, resource_dir, source_dir, template,
priority=DEFAULT_FILE_STORAGE_PRIORITY):
self.basedir = basedir
self.resource_dir = resource_dir
self.source_dir = source_dir
self.template = template
self.basedir = str(basedir)
self.resource_dir = str(resource_dir)
self.source_dir = str(source_dir)
self.template = template if template is None else str(template)
self.priority = priority
self.dir = None
self.run_entry = None
Expand Down

0 comments on commit 3faff43

Please sign in to comment.