def _split_upload_config(path): """Move upload configuration into separate file.""" config = Configuration(join(path, '_oxalis', 'config')) upload_section = config['upload'] upload_conf = Configuration(join(path, '_oxalis', 'upload')) upload_conf['upload'] = upload_section upload_conf.save() os.chmod(join(path, '_oxalis', 'upload'), 0o600) config.remove_section('upload') config.remove_section('state') # Remove also unused section 'state' config.save()
def create_site(path): name = os.path.basename(path) oxalis_dir = os.path.join(path, '_oxalis') os.mkdir(oxalis_dir) # Write site configuration config = Configuration(os.path.join(oxalis_dir, 'config')) config.set('project', 'format', '0.3') config.save() upload_conf = Configuration(os.path.join(oxalis_dir, 'upload')) upload_conf.add_section('upload') upload_conf.save() # Make upload configuration file readable only by owner # (it contains FTP password) os.chmod(os.path.join(oxalis_dir, 'upload'), 0o600) index_text_path = os.path.join(path, 'index.md') index_html_path = os.path.join(path, 'index.html') # Create default index file only if index is not already present if not (os.path.exists(index_text_path) or os.path.exists(index_html_path)): with open(index_text_path, 'w') as f: f.write(DEFAULT_INDEX.format(name=name)) templates_dir = os.path.join(path, TEMPLATES_DIR) os.mkdir(templates_dir) f = open(os.path.join(templates_dir, 'default.html'), 'w') f.write(default_template) f.close() # Create sitecopy configuration file f = open(os.path.join(oxalis_dir, 'sitecopyrc'), 'w') f.close() os.chmod(os.path.join(oxalis_dir, 'sitecopyrc'), 0o600) # Sitecopy storepath os.mkdir(os.path.join(oxalis_dir, 'sitecopy')) os.chmod(os.path.join(oxalis_dir, 'sitecopy'), 0o700)
def _change_format_version(path, version): """Change format version in the main configuration file.""" config = Configuration(join(path, '_oxalis', 'config')) config['project']['format'] = version config.save()
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()