graykode

(add) setup.py and commit client

...@@ -12,23 +12,40 @@ ...@@ -12,23 +12,40 @@
12 # See the License for the specific language governing permissions and 12 # See the License for the specific language governing permissions and
13 # limitations under the License. 13 # limitations under the License.
14 14
15 +import click
15 import json 16 import json
16 import requests 17 import requests
17 -import argparse
18 import subprocess 18 import subprocess
19 +import configparser
19 import whatthepatch 20 import whatthepatch
21 +from os.path import expanduser, join, exists
22 +
23 +def get_diff_from_project():
24 + proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE)
25 + staged_files = proc.stdout.readlines()
26 + staged_files = [f.decode("utf-8") for f in staged_files]
27 + return staged_files
28 +
29 +def commit_message_parser(messages, endline=1):
30 + message = ""
31 + for idx, (path, commit) in enumerate(messages.items()):
32 + click.echo(" - " + " ".join(commit["message"]))
33 + message += " ".join(commit["message"])
34 + if len(messages) - 1 != idx:
35 + message += ("\n" * endline)
36 + return message
20 37
21 def tokenizing(code): 38 def tokenizing(code):
22 data = {"code": code } 39 data = {"code": code }
23 res = requests.post( 40 res = requests.post(
24 'http://127.0.0.1:5000/tokenizer', 41 'http://127.0.0.1:5000/tokenizer',
25 data=json.dumps(data), 42 data=json.dumps(data),
26 - headers=args.headers 43 + headers={'Content-Type': 'application/json; charset=utf-8'}
27 ) 44 )
28 return json.loads(res.text)["tokens"] 45 return json.loads(res.text)["tokens"]
29 46
30 -def autosuggestions(diffs): 47 +def commit_autosuggestions(diffs):
31 - commit_message = [] 48 + commit_message = {}
32 for idx, example in enumerate(whatthepatch.parse_patch(diffs)): 49 for idx, example in enumerate(whatthepatch.parse_patch(diffs)):
33 if not example.changes: 50 if not example.changes:
34 continue 51 continue
...@@ -43,44 +60,78 @@ def autosuggestions(diffs): ...@@ -43,44 +60,78 @@ def autosuggestions(diffs):
43 deleted.extend(tokenizing(change.line)) 60 deleted.extend(tokenizing(change.line))
44 isdeleted = True 61 isdeleted = True
45 62
46 - if isadded and isdeleted: 63 + if isadded and isdeleted and example.header.new_path:
47 data = {"idx": idx, "added" : added, "deleted" : deleted} 64 data = {"idx": idx, "added" : added, "deleted" : deleted}
48 res = requests.post( 65 res = requests.post(
49 'http://127.0.0.1:5000/diff', 66 'http://127.0.0.1:5000/diff',
50 data=json.dumps(data), 67 data=json.dumps(data),
51 - headers=args.headers 68 + headers={'Content-Type': 'application/json; charset=utf-8'}
52 ) 69 )
53 commit = json.loads(res.text) 70 commit = json.loads(res.text)
54 - commit.update({'path' : example.header.new_path}) 71 + commit_message[example.header.new_path] = commit
55 - commit_message.append(commit)
56 else: 72 else:
57 data = {"idx": idx, "added": added, "deleted": deleted} 73 data = {"idx": idx, "added": added, "deleted": deleted}
58 res = requests.post( 74 res = requests.post(
59 'http://127.0.0.1:5000/added', 75 'http://127.0.0.1:5000/added',
60 data=json.dumps(data), 76 data=json.dumps(data),
61 - headers=args.headers 77 + headers={'Content-Type': 'application/json; charset=utf-8'}
62 ) 78 )
63 commit = json.loads(res.text) 79 commit = json.loads(res.text)
64 - commit.update({'path': example.header.new_path}) 80 + commit_message[example.header.new_path] = commit
65 - commit_message.append(commit)
66 return commit_message 81 return commit_message
67 82
68 -def main(): 83 +def commit(message):
84 + subprocess.Popen(["git", "commit", "-m", message], stdout=subprocess.PIPE)
69 85
70 - proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE) 86 +@click.group(invoke_without_command=True)
71 - staged_files = proc.stdout.readlines() 87 +@click.pass_context
72 - staged_files = [f.decode("utf-8") for f in staged_files] 88 +@click.option('--file', '-f', type=click.File('r'),
73 - staged_files = [f.strip() for f in staged_files] 89 + help='patch file containing git diff '
74 - diffs = "\n".join(staged_files) 90 + '(e.g. file created by `git add` and `git diff --cached > test.diff`)')
91 +@click.option('--verbose', '-v', is_flag=True,
92 + help='print suggested commit message more detail.')
93 +@click.option('--autocommit', '-a', is_flag=True,
94 + help='automatically commit without asking if you want to commit')
95 +@click.option('--endline', '-e', type=int, default=1,
96 + help='number of endlines for each commit message generated by the diff of each file')
97 +def cli(ctx, file, verbose, autocommit, endline):
98 + if not ctx.invoked_subcommand:
99 + staged_files = file if file else get_diff_from_project()
100 + staged_files = [f.strip() for f in staged_files]
101 + diffs = "\n".join(staged_files)
75 102
76 - message = autosuggestions(diffs=diffs) 103 + messages = commit_autosuggestions(diffs=diffs)
77 - print(message) 104 + if verbose:
105 + click.echo(
106 + json.dumps(messages, indent=4, sort_keys=True) + "\n"
107 + )
108 +
109 + click.echo(click.style('[INFO]', fg='green') + " The generated message is as follows:")
110 + message = commit_message_parser(messages, endline=endline)
111 +
112 + if autocommit or click.confirm('Do you want to commit this message?'):
113 + commit(message)
78 114
79 -if __name__ == '__main__':
80 - parser = argparse.ArgumentParser(description="")
81 - parser.add_argument("--endpoint", type=str, default="http://127.0.0.1:5000/")
82 - args = parser.parse_args()
83 115
84 - args.headers = {'Content-Type': 'application/json; charset=utf-8'} 116 +@cli.command()
117 +@click.option('--profile', default='default', type=str,
118 + help='unique name for managing each independent settings')
119 +@click.option('--endpoint', default='http://127.0.0.1:5000/', type=str,
120 + help='endpoint address accessible to the server (example:http://127.0.0.1:5000/)')
85 121
86 - main()
...\ No newline at end of file ...\ No newline at end of file
122 +def configure(profile, endpoint):
123 + path = join(expanduser("~"), '.commit-autosuggestions.ini')
124 + config = configparser.ConfigParser()
125 + if exists(path):
126 + config.read(path)
127 + if profile not in config:
128 + config[profile] = {}
129 + if endpoint:
130 + config[profile]['endpoint'] = endpoint
131 +
132 + click.echo(f"configure of commit-autosuggestions is setted up in {path}.")
133 + for key in config[profile]:
134 + click.echo(click.style(key, fg='green') + config[profile][key])
135 +
136 +if __name__ == '__main__':
137 + cli()
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -13,10 +13,10 @@ ...@@ -13,10 +13,10 @@
13 # limitations under the License. 13 # limitations under the License.
14 14
15 import os 15 import os
16 -from setuptools import setup 16 +from setuptools import setup, find_packages
17 17
18 project_name = "commit" 18 project_name = "commit"
19 -version = os.environ.get('COMMIT_VERSION', '0.0.0') 19 +version = os.environ.get('COMMIT_VERSION', '0.0.1')
20 20
21 if __name__ == "__main__": 21 if __name__ == "__main__":
22 22
...@@ -41,9 +41,16 @@ if __name__ == "__main__": ...@@ -41,9 +41,16 @@ if __name__ == "__main__":
41 project_urls={ 41 project_urls={
42 "Source Code": "https://github.com/graykode/commit-autosuggestions", 42 "Source Code": "https://github.com/graykode/commit-autosuggestions",
43 }, 43 },
44 + install_requires = [
45 + 'click>=7.1.2',
46 + 'gitpython>=3.1.7',
47 + 'whatthepatch>=1.0.0',
48 + 'packaging>=20.4',
49 + ],
44 entry_points={ 50 entry_points={
45 'console_scripts': [ 51 'console_scripts': [
46 - 'commit=commit.__main__:main', 52 + 'commit=commit.commit:cli'
47 ], 53 ],
48 - } 54 + },
55 + packages=find_packages(),
49 ) 56 )
...\ No newline at end of file ...\ No newline at end of file
......
1 +diff --git a/codebert/code.py b/codebert/code.py
2 +new file mode 100644
3 +index 0000000..b4bc953
4 +--- /dev/null
5 ++++ b/codebert/code.py
6 +@@ -0,0 +1,21 @@
7 ++def dailymotion_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
8 ++
9 ++ html = get_content(rebuilt_url(url))
10 ++ info = json.loads(match1(html, r'qualities":({.+?}),"'))
11 ++ title = match1(html, r'"video_title"\s*:\s*"([^"]+)"') or \
12 ++ match1(html, r'"title"\s*:\s*"([^"]+)"')
13 ++ title = unicodize(title)
14 ++
15 ++ for quality in ['1080','720','480','380','240','144','auto']:
16 ++ try:
17 ++ real_url = info[quality][1]["url"]
18 ++ if real_url:
19 ++ break
20 ++ except KeyError:
21 ++ pass
22 ++
23 ++ mime, ext, size = url_info(real_url)
24 ++
25 ++ print_info(site_info, title, mime, size)
26 ++ if not info_only:
27 ++ download_urls([real_url], title, ext, size, output_dir=output_dir, merge=merge)
28 +diff --git a/src/train/model.py b/src/train/model.py
29 +index 20e56b3..cab82e5 100644
30 +--- a/src/train/model.py
31 ++++ b/src/train/model.py
32 +@@ -3,9 +3,7 @@
33 +
34 + import torch
35 + import torch.nn as nn
36 +-import torch
37 +-from torch.autograd import Variable
38 +-import copy
39 ++
40 + class Seq2Seq(nn.Module):
41 + """
42 + Build Seqence-to-Sequence.
...\ No newline at end of file ...\ No newline at end of file