Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 103 dynamic template #179

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ jobs:
python: 3.7
- os: linux
python: 3.8
- os: linux
python: 3.9
- os: osx
language: generic
python: 3.6
osx_image: xcode11.4
python: 3.8
osx_image: xcode12.5

cache: pip

Expand Down
7 changes: 3 additions & 4 deletions knowledge_repo/templates/knowledge_template.Rmd
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
title: This is a Knowledge Template Header
authors:
- sally_smarts
- wesley_wisdom
- {{ author }}
tags:
- knowledge
- example
created_at: 2016-06-29
updated_at: 2016-06-30
created_at: {{ current_date }}
updated_at: {{ current_date }}
tldr: This is short description of the content and findings of the post.
---

Expand Down
7 changes: 3 additions & 4 deletions knowledge_repo/templates/knowledge_template.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
"---\n",
"title: This is a Knowledge Template Header\n",
"authors:\n",
"- sally_smarts \n",
"- wesley_wisdom\n",
"- {{ author }} \n",
"tags:\n",
"- knowledge\n",
"- example\n",
"created_at: 2016-06-29\n",
"updated_at: 2016-06-30\n",
"created_at: {{ current_date }}\n",
"updated_at: {{ current_date }}\n",
"tldr: This is short description of the content and findings of the post.\n",
"---"
]
Expand Down
7 changes: 3 additions & 4 deletions knowledge_repo/templates/knowledge_template.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
title: This is a Knowledge Template Header
authors:
- sally_smarts
- wesly_wisdom
- {{ author }}
tags:
- knowledge
- example
created_at: 2016-06-29
updated_at: 2016-06-30
created_at: {{ current_date }}
updated_at: {{ current_date }}
tldr: This is short description of the content and findings of the post.
---

Expand Down
28 changes: 25 additions & 3 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ git commit -m "Update repository config." &> /dev/null
popd &> /dev/null

# Add some knowledge_posts
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.ipynb -p projects/test/ipynb_test -m "Test commit" --branch master
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.Rmd -p projects/test/Rmd_test -m "Test commit" --branch master
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.md -p projects/test/md_test -m "Test commit" --branch master
ipynb_file=$(mktemp).ipynb
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev create ipynb $ipynb_file
rmd_file=$(mktemp).Rmd
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev create Rmd $rmd_file
md_file=$(mktemp).md
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev create md $md_file
cat $md_file

`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add $ipynb_file -p projects/test/create_ipynb_test -m "Test create ipynb" --branch master
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add $rmd_file -p projects/test/create_Rmd_test -m "Test create rmd" --branch master
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add $md_file -p projects/test/create_md_test -m "Test create md" --branch master

# `dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.ipynb -p projects/test/ipynb_test -m "Test commit" --branch master
# `dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.Rmd -p projects/test/Rmd_test -m "Test commit" --branch master
# `dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" --dev add `dirname $0`/knowledge_repo/templates/knowledge_template.md -p projects/test/md_test -m "Test commit" --branch master

for post in $(ls `dirname $0`/tests/test_posts); do
if [[ "${post}" == *.ipynb ]]; then
Expand All @@ -57,6 +69,16 @@ echo "Synchronising database index"
echo "-----------------------------"
echo
`dirname $0`/scripts/knowledge_repo --repo="git://${test_repo_path}" reindex --config `dirname $0`/tests/config_server.py
ipynb_file=$(mktemp).ipynb
`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev create ipynb $ipynb_file
rmd_file=$(mktemp).Rmd
`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev create Rmd $rmd_file
md_file=$(mktemp).md
`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev create md $md_file

`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev add $ipynb_file -p projects/test/ipynb_test -m "Test commit" --branch master
`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev add $rmd_file -p projects/test/Rmd_test -m "Test commit" --branch master
`dirname $0`/scripts/knowledge_repo --repo="${test_repo_path}" --dev add $md_file -p projects/test/md_test -m "Test commit" --branch master

echo
echo "Running regression test suite"
Expand Down
30 changes: 30 additions & 0 deletions scripts/knowledge_repo
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@ import threading
import shutil
import argparse
import subprocess
import datetime

import git
import gitdb
import webbrowser
import tempfile

import jinja2
from tabulate import tabulate


def get_login():
"""
Try to retrieve the user login to in a
"""
try:
user_login = os.getlogin()
except OSError:
import pwd
user_login = pwd.getpwuid(os.geteuid()).pw_name
except ImportError:
import getpass
user_login = getpass.getuser()
return user_login

# Register handler for SIGTERM, so we can run cleanup code if we are terminated
signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit(0))

Expand Down Expand Up @@ -266,6 +285,17 @@ if args.action == 'create':
shutil.copy(src, args.filename)
print("Created a {format} knowledge post template at '{filename}'.".format(format=args.format, filename=args.filename))
sys.exit(0)

# Replace the current timestamp
with open(src) as f:
print(f'current user is {get_login()}')
result = jinja2.Template(f.read()).render(current_date='{:%Y-%m-%d}'.format(datetime.datetime.now()), author=get_login())
#result = jinja2.Template(f.read()).render(current_date='{:%Y-%m-%d}'.format(datetime.datetime.now()), author='krepo')
with open(args.filename, "w") as out:
out.write(result)

#shutil.copy(src, args.filename)
print("Created a {format} knowledge post template at {filename}.".format(format=args.format, filename=args.filename))

# # Check which branches have local work
if args.action == 'drafts':
Expand Down