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

POC: exposing the watermark function outside Jupyter #46

Merged
merged 7 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
Tymoteusz Wolodzko committed Jan 3, 2019
commit 47044df498c205366336f528567a16eec381d8cf
6 changes: 3 additions & 3 deletions watermark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
__version__ = '1.8.0'

if sys.version_info >= (3, 0):
from watermark.magick import *
from watermark.magic import *
from watermark.watermark import watermark
else:
from magick import *
from magic import *
from watermark import watermark

__all__ = ['watermark', 'magick']
__all__ = ['watermark', 'magic']
16 changes: 3 additions & 13 deletions watermark/magick.py → watermark/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
License: BSD 3 clause
"""

from . import __version__
import platform
import subprocess
from time import strftime
from time import time
import datetime
from socket import gethostname
from multiprocessing import cpu_count
import warnings
import types

import IPython
from IPython.core.magic import Magics
from IPython.core.magic import magics_class
Expand Down Expand Up @@ -89,6 +78,7 @@ def watermark(self, line):
args['current_time'] = args.pop('time')

watermark.watermark(**args)



def load_ipython_extension(ipython):
ipython.register_magics(WaterMark)
ipython.register_magics(WaterMark)
61 changes: 35 additions & 26 deletions watermark/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import inspect
import sys


def watermark(author=None, current_date=False, datename=False, current_time=False,
iso8601=False, timezone=False, updated=False, custom_time=None,
python=False, packages=None, hostname=False, machine=False,
Expand Down Expand Up @@ -84,54 +85,56 @@ def watermark(author=None, current_date=False, datename=False, current_time=Fals
out += get_sysinfo()

else:
if args['author']:
out += '% s ' % args['author'].strip('\'"')
if args['updated'] and args['author']:
if author:
out += '% s ' % author.strip('\'"')
if updated and author:
out += '\n'
if args['updated']:
if updated:
out += 'last updated: '
if args['custom_time']:
out += '%s ' % strftime(args['custom_time'])
if args['current_date']:
if custom_time:
out += '%s ' % strftime(custom_time)
if current_date:
out += '%s ' % strftime('%Y-%m-%d')
elif args['datename']:
elif datename:
out += '%s ' % strftime('%a %b %d %Y')
if args['current_time']:
if current_time:
out += '%s ' % strftime('%H:%M:%S')
if args['timezone']:
if timezone:
out += strftime('%Z')
if args['iso8601']:
if iso8601:
out += iso_dt
if args['python']:
if python:
out += get_pyversions()
if args['packages']:
out += get_packages(args['packages'])
if args['machine']:
if packages:
out += get_packages(packages)
if machine:
out += get_sysinfo()
if args['hostname']:
if hostname:
space = ''
if args['machine']:
if machine:
space = ' '
out += '\nhost name%s: %s' % (space, gethostname())
if args['githash']:
out += get_commit_hash(bool(args['machine']))
if args['gitrepo']:
out += get_git_remote_origin(bool(args['machine']))
if args['gitbranch']:
out += get_git_branch(bool(args['machine']))
if args['iversions']:
if githash:
out += get_commit_hash(bool(machine))
if gitrepo:
out += get_git_remote_origin(bool(machine))
if gitbranch:
out += get_git_branch(bool(machine))
if iversions:
out += get_imported_packages() # change here
if args['watermark']:
if watermark:
out += '\nwatermark %s' % __version__
print(out.strip())


def using_jupyter():
try:
get_ipython()
return True
except NameError:
return False


def get_packages(pkgs):
if not isinstance(pkgs, list):
packages = pkgs.split(',')
Expand Down Expand Up @@ -166,6 +169,7 @@ def get_packages(pkgs):

return out


def get_pyversions():
out = '\n%s %s' % (
platform.python_implementation(),
Expand All @@ -175,6 +179,7 @@ def get_pyversions():
out += '\nIPython %s' % IPython.__version__
return out


def get_sysinfo():
return ('\ncompiler : %s\nsystem : %s\n'
'release : %s\nmachine : %s\n'
Expand All @@ -187,6 +192,7 @@ def get_sysinfo():
cpu_count(),
platform.architecture()[0])


def get_commit_hash(machine):
process = subprocess.Popen(['git', 'rev-parse', 'HEAD'],
shell=False,
Expand All @@ -197,6 +203,7 @@ def get_commit_hash(machine):
space = ' '
return '\nGit hash%s: %s' % (space, git_head_hash.decode("utf-8"))


def get_git_remote_origin(machine):
process = subprocess.Popen(['git', 'config', '--get',
'remote.origin.url'],
Expand All @@ -208,6 +215,7 @@ def get_git_remote_origin(machine):
space = ' '
return '\nGit repo%s: %s' % (space, git_remote_origin.decode("utf-8"))


def get_git_branch(machine):
process = subprocess.Popen(['git', 'rev-parse', '--abbrev-ref',
'HEAD'],
Expand All @@ -219,6 +227,7 @@ def get_git_branch(machine):
space = ' '
return '\nGit branch%s: %s' % (space, git_branch.decode("utf-8"))


def get_imported_packages():
packages = set()
for val in sys.modules:
Expand All @@ -233,4 +242,4 @@ def get_imported_packages():
out = ''
for pkg in packages:
out += '\n%-15s%s' % pkg
return out
return out