def release(ctx, path_to_info): project = Project.discover() env = project.make_env() pad = env.new_pad() later = datetime.datetime.now() + datetime.timedelta(hours=1) with io.open(path_to_info, encoding='utf8') as f: info = json.load(f) info.setdefault('min_sysver', '10.8') info.setdefault('pub_datetime', later.strftime(r'%Y-%m-%d %H:%M:%S')) info.setdefault( 'download_url', 'https://github.com/MacDownApp/macdown/releases/download/' 'v{version}/MacDown.app.zip'.format(version=info['version']), ) download = pad.get('/download') new_dirname = os.path.join( ROOT_DIR, os.path.dirname(download.source_filename), info['build_number'], ) os.mkdir(new_dirname) contents_fn = os.path.join(new_dirname, 'contents.lr') with io.open(contents_fn, mode='w', encoding='utf8') as f: f.write(release_template.format(**info))
def post(ctx, slug=None): project = Project.discover() env = project.make_env() pad = env.new_pad() blog = pad.get('/blog') latest_post = blog.children.first() if latest_post is None: next_id = 1 else: next_id = latest_post['id'] + 1 if slug is None: slug = input('Enter slug: ') or '__new_post__' new_dirname = os.path.join( ROOT_DIR, os.path.dirname(blog.source_filename), slug, ) os.mkdir(new_dirname) contents_fn = os.path.join(new_dirname, 'contents.lr') with io.open(contents_fn, mode='w', encoding='utf8') as f: f.write(blog_post_template.format( id=next_id, today=datetime.date.today().strftime(r'%Y-%m-%d'), ))
def list_local(self): all_records = [] project = Project.discover() env = project.make_env() pad = env.new_pad() root = pad.root all_records = self.add_index_children_json(pad, root) return all_records
def content_file_info_cmd(ctx, files, as_json): """Given a list of files this returns the information for those files in the context of a project. If the files are from different projects an error is generated. """ project = None def fail(msg): if as_json: echo_json({"success": False, "error": msg}) sys.exit(1) raise click.UsageError("Could not find content file info: %s" % msg) for filename in files: this_project = Project.discover(filename) if this_project is None: fail("no project found") if project is None: project = this_project elif project.project_path != this_project.project_path: fail("multiple projects") if project is None: fail("no file indicated a project") project_files = [] for filename in files: content_path = project.content_path_from_filename(filename) if content_path is not None: project_files.append(content_path) if not project_files: fail("no files resolve in project") if as_json: echo_json( { "success": True, "project": project.to_json(), "paths": project_files, } ) else: click.echo("Project:") click.echo(" Name: %s" % project.name) click.echo(" File: %s" % project.project_file) click.echo(" Tree: %s" % project.tree) click.echo("Paths:") for project_file in project_files: click.echo(" - %s" % project_file)
def get_meetup_groups(): """ Loads meetup groups from the Python Colombia lektor database. This returns only communities that are displayed on the map. """ project = Project.discover() env = project.make_env() pad = env.new_pad() groups = [ g for g in pad.query('/usuarios/') if g['type'] == 'comunidad' and g['map'] and not isinstance(g['meetup_handle'], Undefined) ] groups = {g['meetup_handle']: g['username'] for g in groups} return groups
def get_project(self, silent=False): if self._project is not None: return self._project if self._project_path is not None: rv = Project.from_path(self._project_path) else: rv = Project.discover() if rv is None: if silent: return None if self._project_path is None: raise click.UsageError("Could not automatically discover " "project. A Lektor project must " "exist in the working directory or " "any of the parent directories.") raise click.UsageError('Could not find project "%s"' % self._project_path) self._project = rv return rv
def project(self): return Project.discover()
import os import sys # Third party imports from PIL import Image as ImagePIL from jinja2 import Undefined # Local imports from lektor.db import Image from lektor.pluginsystem import Plugin from lektor.project import Project from lektor.reporter import reporter from lektor.utils import portable_popen PY3 = sys.version_info[0] == 3 PROJECT = Project.discover() ROOT_PATH = os.path.abspath(os.path.dirname(PROJECT.project_path)) CONTENT_PATH = os.path.join(ROOT_PATH, 'content') class PythonColombiaPlugin(Plugin): name = 'Python Colombia Custom Lektor Plugin' description = 'This is a custom local plugin to add extra functionality.' def on_after_build(self, builder, build_state, source, prog): if isinstance(source, Image) and source.parent['_model'] == 'user': w, h = source.width, source.height fpath = CONTENT_PATH + source.path if isinstance(w, Undefined): with ImagePIL.open(fpath) as img: w, h = img.size
def list_local() -> List[Dict[str, Any]]: project = Project.discover() env = project.make_env() pad = env.new_pad() root = pad.root return get_all_records(pad, root)
import imghdr from os.path import abspath, dirname, join, isfile import pyimagediet # Lektor stuff from lektor.pluginsystem import Plugin from lektor.build_programs import FileAssetBuildProgram, AttachmentBuildProgram, BuildProgram from lektor.assets import File from lektor.sourceobj import VirtualSourceObject from lektor.project import Project from lektor.db import Image from werkzeug._internal import _log # Load and parse the pyimagediet configuration config_filename = abspath(join(Project.discover().project_path, '..', 'configs', 'minification.yml')) if not isfile(config_filename): from pyimagediet.process import DEFAULT_CONFIG as DIET_CONFIG else: DIET_CONFIG = pyimagediet.read_yaml_configuration(config_filename) try: DIET_CONFIG = pyimagediet.parse_configuration(DIET_CONFIG) except pyimagediet.ConfigurationErrorDietException, e: raise Exception(e.msg) def copy_and_optimize(src, dst): """Make sure it's an image, and then optimize on place""" img_type = imghdr.what(src)
# -*- coding: utf-8 -*- """Script to generate the leflet map json data about communities.""" # Standard library imports import json import os import time from datetime import datetime # Third party imports from lektor.project import Project from unidecode import unidecode project = Project.discover() env = project.make_env() pad = env.new_pad() bag = pad.databags # FIXME: def aware_local_now(): return datetime.now(tz=datetime.strptime( time.strftime("%z", time.localtime()), "%z").tzinfo) def get_location(country, province, city): """Get location from databag from country, province and city.""" location = {} if country: country = unidecode(country).lower() _country = bag.get_bag('locations').get(country)