def check_site_format(path): """ Checks if directory contains Oxalis site and what is the version of site format. Returns format version as a string or False it there is no Oxalis site on the path. """ config_file = os.path.join(path, '_oxalis', 'config') if not os.path.isfile(config_file): return False # Not an oxalis site config = Configuration(config_file) return config.get('project', 'format')
class Site: """Oxalis site.""" def __init__(self, directory): self.directory = directory self.config_dir = os.path.join(self.directory, "_oxalis") self.templates_dir = os.path.join(self.config_dir, 'templates') self.config = Configuration(os.path.join(self.config_dir, 'config')) self.upload_config = Configuration( os.path.join(self.config_dir, 'upload')) self.store = SiteStore() self._load_files_tree() self.errors = ErrorMessages() def get_url_path(self): """Return path part of site preview URL.""" path = self.config.get('preview', 'url_path', fallback='').strip('/') if len(path) == 0: return path else: return path + '/' @property def url(self): """Preview URL of the site.""" return 'http://127.0.0.1:8000/' + self.get_url_path() def get_tree_model(self): return self.store.tree_model def _load_files_tree(self): """Loads tree of site files""" self._load_dir('') def _load_dir(self, dirpath): """Loads directory to files tree store dirpath - directory to load, path relative to self.directory """ document = Directory(dirpath, self) self.store.add(document) full_dir_path = os.path.join(self.directory, dirpath) for filename in os.listdir(full_dir_path): if filename != '_oxalis': path = os.path.join(dirpath, filename) full_path = os.path.join(self.directory, path) if os.path.isdir(full_path): self._load_dir(path) else: self._load_file(filename, path) document.file_monitor = Gio.File.new_for_path(full_dir_path)\ .monitor_directory(Gio.FileMonitorFlags.NONE) document.file_monitor.connect('changed', self.on_file_changed) def on_file_changed(self, monitor, file, other_file, event_type): full_path = file.get_path() path = os.path.relpath(full_path, self.directory) if (event_type == Gio.FileMonitorEvent.CREATED and not self.store.contains_path(path)): if os.path.isdir(full_path): self._load_dir(path) else: self._load_file(os.path.basename(path), path) elif (event_type == Gio.FileMonitorEvent.DELETED and self.store.contains_path(path)): document = self.store.get_by_path(path) self.store.remove(document) # Remove from store if hasattr(document, 'file_monitor'): # Stop a monitor document.file_monitor.cancel() def _load_file(self, filename, path): """Append file filename - name of the file path - path relative to self.directory """ if not filename.startswith("."): document = File(path, self) self.store.add(document) def close(self): """Close site and save its state""" self.config.save() self.upload_config.save() def new_file(self, name, parent): """Create new file.""" full_path = os.path.join(self.directory, parent.path, name) open(full_path, 'w').close() def new_directory(self, name, parent): """Create new directory.""" full_path = os.path.join(self.directory, parent.path, name) os.mkdir(full_path) def add_file(self, filename, parent): """Copy existing file to the site""" name = os.path.basename(filename) full_path = os.path.join(self.directory, parent.path, name) shutil.copyfile(filename, full_path) def generate(self): """Generate site output files""" for item in self.store.all_documents(): item.convert()