def work(options): print >>sys.stderr, 'Generating Release Site.' print >>sys.stderr, 'Package Dir: %s' % (options.package_db,) print >>sys.stderr, 'Out file: %s' % (options.out_file,) db = PackageDatabase(options.package_db) db.load() # Load and render overview template. tpl = pyratemp.Template(filename=TPL_PATH) with open(options.out_file, 'wb') as f: f.write(tpl(FORMATS=FORMATS, seqan_apps=db.seqan_apps, seqan_library=db.seqan_library, seqan_src=db.seqan_src, softwares=db.softwares, utc_time=time.strftime('%a, %d %b %Y %H:%M:%S UTC', time.gmtime()), sorted=sorted)) # Load and render package template. tpl = pyratemp.Template(filename=PACKAGE_TPL_PATH) for sname, software in db.softwares.items(): out_path = os.path.join(options.package_db, sname, 'index.html') print >>sys.stderr, 'Writing %s.' % out_path with open(out_path, 'wb') as f: f.write(tpl(FORMATS=FORMATS, utc_time=time.strftime('%a, %d %b %Y %H:%M:%S UTC', time.gmtime()), software=software, sorted=sorted)) # Write out RSS feeds for the packages. rss_writer = RssWriter(options.package_db, db, options.base_url) rss_writer.generate()
def parse(template_name): """Parse template + set encoding according to filename-extension. :Returns: the parsed template """ ext = os.path.splitext(template_name)[1] if ext == ".htm" or ext == ".html": t = pyratemp.Template(filename=template_name, escape=pyratemp.HTML) elif ext == ".tex": t = pyratemp.Template(filename=template_name, escape=pyratemp.LATEX) else: t = pyratemp.Template(filename=template_name) return t
def _writeHtml(self, path, scriptNames): #generate html headerTemplate = pyratemp.Template(filename=self._mm.resourcePath("header.html.templ")) template = pyratemp.Template(filename=self._mm.resourcePath("index.html.templ")) result = template(**{ "scripts": scriptNames, "enterTabHeader": headerTemplate(titleHeader="<h1 id='enter-list-header'></h1>", tab="enter"), "teachTabHeader": headerTemplate(titleHeader="<h1 id='teach-me-header'></h1>", tab="teach"), }) #write html to index.html with open(os.path.join(path, "index.html"), "w") as f: f.write(result)
def work(options): print >> sys.stderr, 'Generating Release Site.' print >> sys.stderr, 'Package Dir: %s' % (options.package_db, ) print >> sys.stderr, 'Out Dir: %s' % (options.out_dir, ) try: os.makedirs(options.out_dir) except: pass db = PackageDatabase(options.package_db) db.load() # Load and render overview template. tpl = pyratemp.Template(filename=TPL_PATH) with open(options.out_dir + "/index.html", 'wb') as f: f.write( tpl(FORMATS=FORMATS, seqan_apps=db.seqan_apps, seqan_library=db.seqan_library, seqan3_library=db.seqan3_library, seqan_src=db.seqan_src, softwares=db.softwares, utc_time=time.strftime('%a, %d %b %Y %H:%M:%S UTC', time.gmtime()), sorted=sorted)) # Load and render package template. tpl = pyratemp.Template(filename=PACKAGE_TPL_PATH) for sname, software in db.softwares.items(): out_path = os.path.join(options.out_dir, sname) try: os.makedirs(out_path) except: pass out_path += "/index.html" print >> sys.stderr, 'Writing %s.' % out_path with open(out_path, 'wb') as f: f.write( tpl(FORMATS=FORMATS, utc_time=time.strftime('%a, %d %b %Y %H:%M:%S UTC', time.gmtime()), software=software, sorted=sorted)) # Write out RSS feeds for the packages. rss_writer = RssWriter(options.out_dir, db, options.base_url) rss_writer.generate() # copy design files os.system("cp -r %s/design %s/design" % (os.path.dirname(os.path.abspath(__file__)), options.out_dir))
def __init__(self, error_logger, tree, tpl_path, out_path, include_dirs): print('Setting up Docs Creator', file=sys.stderr) self.tree = tree self.error_logger = error_logger self.tpl_path = tpl_path page_tpl_filename = os.path.join(self.tpl_path, 'files', 'page.html') ##print page_tpl_filename self.page_template = pyratemp.Template(filename=page_tpl_filename) index_page_tpl_filename = os.path.join(self.tpl_path, 'files', 'index_page.html') self.index_page_template = pyratemp.Template(filename=index_page_tpl_filename) self.out_path = os.path.join(out_path, 'files') if not os.path.exists(self.out_path): os.makedirs(self.out_path) self.include_dirs = include_dirs
def parse(template_name): """Parse template + set encoding according to filename-extension. :Returns: the parsed template """ ext = os.path.splitext(template_name)[1] if ext == ".htm" or ext == ".html": t = pyratemp.Template(filename=template_name, escape=pyratemp.HTML) elif ext == ".tex": #TODO: change this to LATEX as soon as the latex-escaping works sys.stderr.write("Warning: LaTeX-escaping not yet supported.\n") t = pyratemp.Template(filename=template_name, escape=None) else: t = pyratemp.Template(filename=template_name) return t
def _run(self): try: package_release = sys.argv[1] path = sys.argv[2] except IndexError: sys.stderr.write("Please specify a package release version and a path ending in .pkg.tar.xz to save the resulting arch package to as the last command line arguments.\n") return sourcePath = self._modules.default("active", type="sourceWithSetupSaver").saveSource() with open(os.path.join(sourcePath, "PKGBUILD"), "w") as f: templ = pyratemp.Template(filename=self._mm.resourcePath("PKGBUILD.templ")) data = { "package_release": package_release, "manpages": self._manPagesSourceAndDestination(sourcePath), } data.update(self._metadata) f.write(templ(**data)) cwd = os.getcwd() os.chdir(sourcePath) subprocess.check_call(["makepkg"]) os.chdir(cwd) shutil.copy( glob.glob(os.path.join(sourcePath, "*.pkg.tar.xz"))[0], path )
def simple_build(self, ar, elem, tpl, target): context = dict(instance=elem) t = pyratemp.Template(filename=tpl) try: result = t(**context) except pyratemp.TemplateRenderError, e: raise Exception(u"%s in %s" % (e, tpl))
def save(self, type, lesson, path): class EvalPseudoSandbox(pyratemp.EvalPseudoSandbox): def __init__(self2, *args, **kwargs): pyratemp.EvalPseudoSandbox.__init__(self2, *args, **kwargs) self2.register("compose", self._compose) templatePath = self._mm.resourcePath("template.xml") t = pyratemp.Template(open(templatePath).read(), eval_class=EvalPseudoSandbox) #set to C so strftime is English locale.setlocale(locale.LC_ALL, "C") try: dt = lesson.list["items"][0]["created"] except (IndexError, KeyError): dt = datetime.datetime.now() data = { "list": lesson.list, #+0100 since our datetime objects are naive and most WRTS #users live in the Netherlands, so this is for them at max #one hour off due to DST. "created": dt.strftime("%a, %d %b %Y %H:%M:%S +0100"), "now": datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0100"), "appname": self._metadata["name"], "appversion": self._metadata["version"], } #reset locale so other modules don't get unexpected behaviour. locale.resetlocale() content = t(**data) with open(path, "w") as f: f.write(content.encode("UTF-8")) lesson.path = None
def save(self, type, lesson, path): #copy, we're going to modify it wordList = copy.deepcopy(lesson.list) self._storeRightWrongCountInWords(wordList) for test in wordList.get("tests", []): test["note"] = self._calculateNote(test) test["start"] = self._startTime(test) test["duration"] = self._duration(test) test["answerscorrect"] = self._answersCorrect(test) test["wrongonce"] = self._wrongOnce(test) test["wrongtwice"] = self._wrongTwice(test) test["wrongmorethantwice"] = self._wrongMoreThanTwice(test) templatePath = self._mm.resourcePath("template.xml") t = pyratemp.Template(open(templatePath).read()) data = { "wordList": wordList, } content = t(**data) with open(path, "w") as f: f.write(content.encode("UTF-8")) lesson.path = None
def get(self): user_info = { "email": None, "nickname": None, "selected_packages": DEFAULT_PACKAGES, } # Get the user's information from datastore if he's logged in user = users.get_current_user() if user: user_info["email"] = user.email() user_info["nickname"] = user.nickname() query = UserInfo.all() query.filter("user ="******"selected_packages"] = record.selected_packages params = { "content_version": CONTENT_VERSION, "user_info": json.dumps(user_info), "email": user_info["email"], "nickname": user_info["nickname"], "is_logged_in": user is not None, "login_url": users.create_login_url("/"), "logout_url": users.create_logout_url("/"), } template_path = os.path.join(os.path.dirname(__file__), "index.html") template = pyratemp.Template(filename=template_path, data=params, escape=pyratemp.HTML) self.response.out.write(template().encode("utf-8"))
def save(self, type, lesson, path): #Copy, because we're going to modify it wordList = copy.deepcopy(lesson.list) for word in wordList.get("items", []): #results word["results"] = {"right": 0, "wrong": 0} for test in wordList.get("tests", []): for result in test["results"]: if result["itemId"] == word["id"]: with contextlib.ignored(KeyError): word["results"][result["result"]] += 1 #known, foreign and second word["known"] = self._compose(word["questions"]) if len(word["answers"]) == 1 and len(word["answers"][0]) > 1: word["foreign"] = word["answers"][0][0] word["second"] = self._compose([word["answers"][0][1:]]) else: word["foreign"] = self._compose(word["answers"]) word["second"] = u"" templatePath = self._mm.resourcePath("template.xml") t = pyratemp.Template(open(templatePath).read()) data = {"wordList": wordList} content = t(**data) with open(path, "w") as f: f.write(content.encode("UTF-8")) lesson.path = None
def generateConfigs(self): ''' This assumes that a file for the template and dataset exists. ''' configFiles = {} try: import pyratemp for config in self.metadataset.getConfigs(): if config.has_key('dataset'): if not re.match("^(\w+\.)+(json|yaml)$",config['dataset']): # The configuration needs to have a metaconf extension. raise ImproperlyNamedCAException,"Dataset needs to have a <ca dataset>.json or <ca dataset>.yaml extension." datasetBuffer = self.service.getCustAttr(self.objref,config['dataset'],True) elif config.has_key('datasetFile'): datasetBuffer = fileReader(config['datasetFile']) else: raise NoDatasetException,"Please specify a dataset with either a dataset or datasetFile json key." #datasetBuffer = self.casub.sub(fileReader(config['datasetFile'])) #datasetBuffer = self.casub.sub(datasetBuffer) #datasetBuffer = self.hwsub.sub(datasetBuffer) datasetBuffer = self._searchAndReplace(datasetBuffer) #if not self.casub.tokenExists(config['dataset']): # raise MissingDatasetCA,"Specify @%s@ instead of %s in dataset field." % (config['dataset'],config['dataset']) #datasetBuffer = config['dataset'] dataset = load_data(datasetBuffer) #print "config['dataset']: %s" % config['dataset'] #dataset = load_data(self._searchAndReplace(config['dataset'])) #pytemp = pyratemp.Template(fileReader(config['templateFile'])) if config.has_key('template'): if not re.match("^(\w+\.)+template$",config['template']): # The configuration needs to have a metaconf extension. raise ImproperlyNamedCAException,"Dataset needs to have a <ca template>.template extension." pytemp = pyratemp.Template(self.service.getCustAttr(self.objref,config['template'],True)) elif config.has_key('templateFile'): pytemp = pyratemp.Template(fileReader(config['templateFile'])) else: raise NoTemplateException,"Please specify a template with either a template or templateFile json key." #if not self.casub.tokenExists(config['template']): # raise MissingTemplateCA,"Specify @%s@ instead of %s in template field." % (config['template'],config['template']) #pytemp = pyratemp.Template(self.casub.sub(config['template'])) # initialize dictionary with filecontents. configFiles[config['configFile']] = {'filecontent' : pytemp(**dataset).encode("utf-8")} if config.has_key('perms'): configFiles[config['configFile']]['perms'] = config['perms'] if config.has_key('og'): configFiles[config['configFile']]['og'] = config['og'] except ImportError,args: raise ImportError,args
def make_py_file(f_in, f_out, g, old_files=None): t = pyratemp.Template(filename=f_in) out = open(f_out, 'w') out.write(t(g=g).encode()) out.close() if old_files is not None and f_out in old_files: old_files.remove(f_out)
def do_GET(self): params = {"diffs": self.data} t = pyratemp.Template(filename=SCRIPT_DIR + "/index.html") result = t(**pyratemp.dictkeyclean(params)) self.wfile.write(result)
def swmmWrite(params, linestring, outfile): import pyratemp pt = pyratemp.Template(linestring) linestring = pt(**params) make_sure_path_exists(os.path.dirname(outfile)) f = open(outfile, 'w') f.write(linestring) f.close()
def make_file(f_in, f_out, cfg): f_tmp = '%s.swp' % f_out t = pyratemp.Template(filename=f_in) with open(f_tmp, 'w') as out: out.write(t(**cfg)) if os.path.exists(f_out) and filecmp.cmp(f_tmp, f_out): # Nothing changed, don't touch the file os.remove(f_tmp) return shutil.move(f_tmp, f_out)
def make_scm_file(f_in, f_out): t = pyratemp.Template( filename=f_in, data={ "srename": lambda gn, n: gn.replace("_", "-") + "." + n.replace("_", "-"), "srenameg": lambda gn: gn.replace("_", "-") }) with open(f_out, 'w') as out: out.write(t(groups=groups))
def simple_build(self, ar, elem, tpl, target): context = dict(instance=elem) t = pyratemp.Template(filename=tpl) try: result = t(**context) except pyratemp.TemplateRenderError as e: raise Exception(u"%s in %s" % (e, tpl)) fd = file(target, 'wb') fd.write(result) fd.close() return os.path.getmtime(target)
def generateConfigs(self): ''' This assumes that a file for the template and dataset exists. ''' configFiles = {} try: import pyratemp for config in metadataset.getConfigs(): dataset = metadataset.getDatasetDict(fileReader(config['datasetFile'])) pytemp = pyratemp.Template(fileReader(config['templateFile'])) configFiles[config['configFile']] = pytemp(**dataset).encode("utf-8") except ImportError,args: raise ImportError,args
def replace_variables_for_app(folder_to_search, application_name): for subdir, dirs, files in os.walk(folder_to_search): for f in files: file_name = os.path.join(subdir, f) t = pyratemp.Template(filename=os.path.join(subdir, f)) file_content = t(UPPER_CASE_APP_NAME=application_name.upper(), UPPER_CASE_APP_NAME_UNDERSCORE_ONLY=application_name.upper().replace("-", "_"), APP_NAME_UNDERSCORE_ONLY=to_camel_case(application_name.replace("-", "_")), APP_NAME=application_name, bashbang="#!/bin/bash", shbang="#!/bin/sh", ) write_to_file(file_name, file_content)
def enable(self): try: import pyratemp except ImportError: #remain inactive return self._modules = next(iter(self._mm.mods(type="modules"))) self.resourcesPath = self._mm.resourcePath("static") self._template = pyratemp.Template( filename=self._mm.resourcePath("getting-started.html")) self.active = True
def create_pdf_from_batch(self, batch): template = pyratemp.Template(filename=self.TEMPLATE_FILENAME, parser_class=DjangoFormatParser) cards = batch.registration_numbers.all() rml = six.BytesIO(self.parse_template(template, cards)) unused, filename = tempfile.mkstemp() try: rml2pdf.go(rml, filename) # This closes the file new_filename = "{}.pdf".format(time.strftime("%m%d%H%M%S")) with open(filename) as fh: batch.data_file.save(name=new_filename, content=File(fh)) finally: os.unlink(filename)
def process_app(conf, app_dir): #get the name from the user print "please provide a name for your application (default: %s)" % conf[ "name"] name = raw_input("> ") if len(name) == 0: name = conf['name'] params = process_variables(conf) params['name'] = name #create destination folder dest_dir = current_dir + params['name'] + "/" if not os.path.exists(dest_dir): os.mkdir(dest_dir) #process files that should be created try: for file in conf["create_files"]: #see if the file has a name derived from a variable try: try: name = file["name"] % params["name"] except TypeError: name = file["name"] except KeyError: name = file["template"] file_name = file["template"] if os.path.exists(app_dir + file_name): #copy existing file t = pyratemp.Template(filename=app_dir + file_name) contents = t(**pyratemp.dictkeyclean(params)).encode("utf-8") else: #create blank file contents = "" #create directories in target if necessary target_dir = os.path.dirname(name) if target_dir and not os.path.exists(dest_dir + target_dir): os.makedirs(dest_dir + target_dir) f = open(dest_dir + name, "w") f.write(contents) f.close() except KeyError: pass
def work(options): print >> sys.stderr, 'Generating Release Site.' print >> sys.stderr, 'Package Dir: %s' % (options.package_db, ) print >> sys.stderr, 'Out file: %s' % (options.out_file, ) db = PackageDatabase(options.package_db) db.load() # Load template. tpl = pyratemp.Template(filename=TPL_PATH) with open(options.out_file, 'wb') as f: f.write( tpl(FORMATS=FORMATS, seqan_apps=db.seqan_apps, seqan_library=db.seqan_library, softwares=db.softwares))
def make_file(f_in, f_out, old_files=None): f_tmp = '%s.swp' % f_out t = pyratemp.Template(filename=f_in) out = open(f_tmp, 'w') out.write(t(groups=groups).encode()) out.close() if os.path.exists(f_out): if old_files is not None: old_files.remove(f_out) if filecmp.cmp(f_tmp, f_out): # Nothing changed, don't touch the file. os.remove(f_tmp) return shutil.move(f_tmp, f_out)
def _generateAppCacheManifest(self, path): #create the AppCache manifest file template = pyratemp.Template(filename=self._mm.resourcePath("otmobile.templ.appcache")) allFiles = sorted( posixpath.relpath(posixpath.join(root, file), path) for root, dirs, files in os.walk(path) for file in files if not file in [".htaccess", "config.xml", "splash.png"] ) manifest = template(**{ "now": datetime.datetime.now(), "files": allFiles, }) with open(os.path.join(path, "otmobile.appcache"), "w") as f: f.write(manifest.encode("UTF-8")) shutil.copy(self._mm.resourcePath("htaccess"), os.path.join(path, ".htaccess"))
def replace_variables_for_app(application_root_name, folder_to_search, application_name, service_type, has_mongo=False): bootstrapPlay26Version=get_latest_library_version_in_open("bootstrap-play-26") govukTemplateVersion=get_latest_library_version_in_open("govuk-template") playUiVersion=get_latest_library_version_in_open("play-ui") simpleReactivemongoVersion=get_latest_library_version_in_open("simple-reactivemongo") microserviceBootstrapVersion=get_latest_library_version_in_open("microservice-bootstrap") sbt_auto_build = get_latest_sbt_plugin_version_in_open("sbt-auto-build") sbt_git_versioning = get_latest_sbt_plugin_version_in_open("sbt-git-versioning") sbt_artifactory = get_latest_sbt_plugin_version_in_open("sbt-artifactory") sbt_distributables = get_latest_sbt_plugin_version_in_open("sbt-distributables") print("sbt_auto_build " + sbt_auto_build) print("sbt_git_versioning " + sbt_git_versioning) print("sbt_distributables " + sbt_distributables) print("sbt_artifactory " + sbt_artifactory) for subdir, dirs, files in os.walk(folder_to_search): if '.git' in dirs: dirs.remove('.git') for f in files: file_name = os.path.join(subdir, f) print("templating: " + subdir + " " + f) t = pyratemp.Template(filename=os.path.join(subdir, f)) file_content = t(UPPER_CASE_APP_NAME=application_name.upper(), UPPER_CASE_APP_NAME_UNDERSCORE_ONLY=application_name.upper().replace("-", "_"), APP_NAME=application_name, APP_PACKAGE_NAME=application_root_name.replace("-", ""), SECRET_KEY=generate_app_secret(), type=service_type, MONGO=has_mongo, bootstrapPlay26Version=bootstrapPlay26Version, microserviceBootstrapVersion=microserviceBootstrapVersion, govukTemplateVersion=govukTemplateVersion, playUiVersion=playUiVersion, simpleReactivemongoVersion=simpleReactivemongoVersion, sbt_auto_build=sbt_auto_build, sbt_git_versioning=sbt_git_versioning, sbt_distributables=sbt_distributables, sbt_artifactory=sbt_artifactory, bashbang="#!/bin/bash", shbang="#!/bin/sh", ) write_to_file(file_name, file_content)
def mail(maildir, template, data, messageid_domainname=None): """Create a mail from a pyratemp-template and store it in a maildir. :Parameters: - maildir: maildir-directory - template: template-file - data: data for the template (dictionary) - messageid_domainname: domainname for the created messageid :Returns: the filename (without path) of the created mail :Notes: - "messageid" and "date" are are added to ``data`` if they are not yet there. - For convenience, the variables in data can both be accessed directly and by using data[...]. - The mail is first written to maildir/tmp/ and then renamed to maildir/new/. - The created file is encoded in utf-8. """ # create mail t = time.time() if "date" not in data: data["date"] = email.utils.formatdate(t, localtime=True) if "messageid" not in data: if messageid_domainname is None: messageid_domainname = socket.gethostname() data["messageid"] = datetime.datetime.fromtimestamp(t).strftime( "%Y%m%d%H%M%S.%f") + "@" + messageid_domainname t = pyratemp.Template(filename=template, escape=pyratemp.MAIL_HEADER) result = t(data=data, **data) # write mail to maildir mailname = "%fP%d.%s" % (time.time(), os.getpid(), socket.gethostname().replace( "/", r"\057").replace(":", r"\072")) mailtmp = os.path.join(maildir, "tmp", mailname) mailnew = os.path.join(maildir, "new", mailname) f = codecs.open(mailtmp, "w", encoding="utf-8") f.write(result) f.close() os.rename(mailtmp, mailnew) return mailname
def html(template, data, xmlreplace=True, filename=None, chmod=None): """Create a HTML-page from a pyratemp-template. Additionally defined variables (may be overwritten by data): - date, mtime_CCYYMMDD: current date as YYYY-MM-DD :Parameters: - template: template-file - data: data for the template (dictionary) - xmlreplace: if True, encode result in ascii with xmlcharrefreplace - filename: write result to this file (in utf-8) - chmod: access permissions for the written file :Returns: the filled template :Note: If filename is given, the result is first written to filename.tmp, then the permissions are set, and then the file is renamed. """ timestamp = time.strftime("%Y-%m-%d") t = pyratemp.Template(filename=template, data={ "date": timestamp, "mtime_CCYYMMDD": timestamp }) result = t(**data) if xmlreplace: result = result.encode("ascii", 'xmlcharrefreplace') if filename: if xmlreplace: f = open(filename + ".tmp", "wb") else: f = codecs.open(filename + ".tmp", "w", encoding="utf-8") f.write(result) f.close() if chmod: os.chmod(filename + ".tmp", chmod) os.rename(filename + ".tmp", filename) return result