Coverage for pyquickhelper/ipythonhelper/magic_class_crypt.py: 100%
44 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-03 02:21 +0200
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Magic command to handle files
5"""
6import os
7from IPython.core.magic import magics_class, line_magic
9from .magic_class import MagicClassWithHelpers
10from .magic_parser import MagicCommandParser
11from ..filehelper import encrypt_stream, decrypt_stream
14@magics_class
15class MagicCrypt(MagicClassWithHelpers):
17 """
18 Defines magic commands to encrypt and decrypt file.
19 """
21 @staticmethod
22 def endecrypt_file_parser(encrypt):
23 """
24 defines the way to parse the magic command ``%encrypt_file`` and ``%decrypt_file``
26 @param encrypt True to encrypt or False to decrypt
27 @return parser
28 """
29 task = "encrypt" if encrypt else "decrypt"
30 parser = MagicCommandParser(prog=f"{task[:2]}crypt_file",
31 description=f'{task} a file' +
32 '\ndoes not work well in Python 2.7 with pycryptodomex')
33 parser.add_argument(
34 'source',
35 help=f'file to {task}')
36 parser.add_argument(
37 'dest',
38 help=f'location of the {task}ed file')
39 parser.add_argument(
40 'password',
41 help='password, usually an ascii string with 16x characters')
43 return parser
45 @staticmethod
46 def encrypt_file_parser():
47 """
48 defines the way to parse the magic command ``%encrypt_file``
49 """
50 return MagicCrypt.endecrypt_file_parser(True)
52 @staticmethod
53 def decrypt_file_parser():
54 """
55 defines the way to parse the magic command ``%decrypt_file``
56 """
57 return MagicCrypt.endecrypt_file_parser(False)
59 @line_magic
60 def encrypt_file(self, line):
61 """
62 .. nbref::
63 :title: %encrypt_file
64 :tag: nb
65 :lid: l-nb-encrypt-file
67 The magic command is equivalent to::
69 from pyquickhelper.filehelper import encrypt_stream
71 password = "password"
72 source = "file source"
73 dest = "file destination"
75 if isinstance(password, str):
76 password = bytes(password, encoding="ascii")
78 encrypt_stream(key=password, filename=source, out_filename=dest,
79 chunksize=os.stat(source).st_size * 2 + 1)
80 """
81 parser = self.get_parser(
82 MagicCrypt.encrypt_file_parser, "encrypt_file")
83 args = self.get_args(line, parser)
85 if args is not None:
86 password = args.password
87 source = args.source
88 dest = args.dest
90 if isinstance(password, str):
91 password = bytes(password, encoding="ascii")
93 return encrypt_stream(key=password, filename=source, out_filename=dest,
94 chunksize=os.stat(source).st_size * 2 + 1)
95 return None # pragma: no cover
97 @line_magic
98 def decrypt_file(self, line):
99 """
100 .. nbref::
101 :title: %decrypt_file
103 The magic command is equivalent to::
105 from pyquickhelper.filehelper import decrypt_stream
107 password = "password"
108 source = "file source"
109 dest = "file destination"
111 if isinstance(password, str):
112 password = bytes(password, encoding="ascii")
114 decrypt_stream(key=password, filename=source, out_filename=dest,
115 chunksize=os.stat(source).st_size * 2 + 1)
116 """
117 parser = self.get_parser(
118 MagicCrypt.decrypt_file_parser, "decrypt_file")
119 args = self.get_args(line, parser)
121 if args is not None:
122 password = args.password
123 source = args.source
124 dest = args.dest
126 if isinstance(password, str):
127 password = bytes(password, encoding="ascii")
129 return decrypt_stream(key=password, filename=source, out_filename=dest,
130 chunksize=os.stat(source).st_size * 2 + 1)
131 return None # pragma: no cover
134def register_file_magics(ip=None): # pragma: no cover
135 """
136 Register magics function, can be called from a notebook.
138 @param ip from ``get_ipython()``
139 """
140 if ip is None:
141 from IPython import get_ipython
142 ip = get_ipython()
143 ip.register_magics(MagicCrypt)