|
36 | 36 | """
|
37 | 37 |
|
38 | 38 | import platform
|
39 |
| -from time import strftime |
40 |
| -from platform import python_version |
41 | 39 | from pkg_resources import get_distribution
|
| 40 | +from multiprocessing import cpu_count |
42 | 41 |
|
43 | 42 | import IPython
|
44 | 43 | from IPython.core.magic import Magics, magics_class, line_magic
|
45 | 44 | from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
|
46 | 45 |
|
47 | 46 | @magics_class
|
48 |
| -class DateMagic(Magics): |
| 47 | +class WaterMark(Magics): |
49 | 48 | """
|
50 | 49 | IPython magic function for printing the current date, time, Python,
|
51 | 50 | and IPython version.
|
52 | 51 |
|
53 | 52 | """
|
54 | 53 | @magic_arguments()
|
55 |
| - @argument('-d', '--date', action='store_true', help='prints date (default)') |
56 |
| - @argument('-dd', '--dateday', action='store_true', help='prints date with abbrv. day and month names') |
57 |
| - @argument('-t', '--time', action='store_true', help='print current time') |
58 |
| - @argument('-s', '--datetime', action='store_true', help='print current time') |
59 |
| - @argument('-z', '--timezone', action='store_true', help='prints time zone') |
60 |
| - @argument('-y', '--python', action='store_true', help='prints Python version') |
61 |
| - @argument('-i', '--ipython', action='store_true', help='prints IPython version') |
| 54 | + @argument('-d', '--date', action='store_true', help='prints current date') |
| 55 | + @argument('-n', '--datename', action='store_true', help='prints date with abbrv. day and month names') |
| 56 | + @argument('-t', '--datetime', action='store_true', help='prints currenttime') |
| 57 | + @argument('-u', '--custom_time', type=str, help='prints a valid strftime() string') |
| 58 | + @argument('-v', '--python', action='store_true', help='prints Python and IPython version') |
62 | 59 | @argument('-p', '--packages', type=str, help='prints versions of Python modules and packages')
|
63 |
| - @line_magic |
64 |
| - def date(self, line): |
| 60 | + @argument('-m', '--machine', type='store_true', help='prints system and machine info') |
| 61 | + def watermark(self, line): |
65 | 62 | """
|
66 | 63 | IPython magic function for printing the current date, time, Python,
|
67 | 64 | and IPython version.
|
68 | 65 |
|
69 | 66 | """
|
70 | 67 | args = parse_argstring(self.date, line)
|
71 |
| - out = '' |
72 |
| - if args.date: |
73 |
| - out += strftime('%d/%m/%Y') |
74 |
| - elif args.dateday: |
75 |
| - out += strftime('%a %b %M %Y') |
76 |
| - if args.time: |
77 |
| - if out: |
78 |
| - out += ' ' |
79 |
| - out += strftime('%H:%M:%S') |
80 |
| - if args.timezone: |
81 |
| - if out: |
82 |
| - out += ' ' |
83 |
| - out += strftime('%Z') |
84 |
| - if args.python: |
85 |
| - if out: |
86 |
| - out += '\n' |
87 |
| - out += 'Python %s' %python_version() |
88 |
| - if args.ipython: |
89 |
| - if out: |
90 |
| - out += '\n' |
91 |
| - out += 'IPython %s' %IPython.__version__ |
92 |
| - if args.packages: |
| 68 | + |
| 69 | + |
| 70 | + if not any(vars(args).values()): |
| 71 | + print_customtime('%d/%m/%Y') |
| 72 | + print_pyver(args) |
| 73 | + print_sysinfo() |
| 74 | + |
| 75 | + |
| 76 | + def print_customtime(self, ctime): |
| 77 | + print(strftime(ctime)) |
| 78 | + |
| 79 | + def print_pack(self): |
| 80 | + out = '' |
93 | 81 | packages = args.packages.split(',')
|
94 | 82 | for p in packages:
|
95 | 83 | out += '\n%s' %get_distribution(p).version
|
96 |
| - if not out: |
97 |
| - out += strftime('%d/%m/%Y') |
98 |
| - print(out) |
| 84 | + print(out) |
| 85 | + |
| 86 | + |
| 87 | + def print_pyver(self, args): |
| 88 | + out = '' |
| 89 | + if args.python: |
| 90 | + if out: |
| 91 | + out += '\n' |
| 92 | + out += 'Python %s' %python_version() |
| 93 | + if args.ipython: |
| 94 | + if out: |
| 95 | + out += '\n' |
| 96 | + out += 'IPython %s' %IPython.__version__ |
| 97 | + print(out) |
| 98 | + |
| 99 | + |
| 100 | + def print_datetime(self, args): |
| 101 | + out = '' |
| 102 | + if args.date: |
| 103 | + out += strftime('%d/%m/%Y') |
| 104 | + elif args.dateday: |
| 105 | + out += strftime('%a %b %M %Y') |
| 106 | + if args.time: |
| 107 | + if out: |
| 108 | + out += ' ' |
| 109 | + out += strftime('%H:%M:%S') |
| 110 | + if args.timezone: |
| 111 | + if out: |
| 112 | + out += ' ' |
| 113 | + out += strftime('%Z') |
| 114 | + if args.custom_time: |
| 115 | + if out: |
| 116 | + out += ' ' |
| 117 | + out += strftime(args.custom_time) |
| 118 | + print(out) |
| 119 | + |
| 120 | + def print_sysinfo(self): |
| 121 | + print('compiler : %s' %platform.python_compiler()) |
| 122 | + print('system : %s' %platform.system()) |
| 123 | + print('release : %s' %platform.release()) |
| 124 | + print('machine : %s' %platform.machine()) |
| 125 | + print('processor : %s' %platform.processor()) |
| 126 | + print('CPU count : %s' %cpu_count()) |
| 127 | + print('interpreter: %s' %platform.architecture()[0]) |
| 128 | + |
99 | 129 |
|
100 | 130 | def load_ipython_extension(ipython):
|
101 | 131 | ipython.register_magics(DateMagic)
|
0 commit comments