graykode

(add) setup.py and commit client

......@@ -12,23 +12,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
import json
import requests
import argparse
import subprocess
import configparser
import whatthepatch
from os.path import expanduser, join, exists
def get_diff_from_project():
proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE)
staged_files = proc.stdout.readlines()
staged_files = [f.decode("utf-8") for f in staged_files]
return staged_files
def commit_message_parser(messages, endline=1):
message = ""
for idx, (path, commit) in enumerate(messages.items()):
click.echo(" - " + " ".join(commit["message"]))
message += " ".join(commit["message"])
if len(messages) - 1 != idx:
message += ("\n" * endline)
return message
def tokenizing(code):
data = {"code": code }
res = requests.post(
'http://127.0.0.1:5000/tokenizer',
data=json.dumps(data),
headers=args.headers
headers={'Content-Type': 'application/json; charset=utf-8'}
)
return json.loads(res.text)["tokens"]
def autosuggestions(diffs):
commit_message = []
def commit_autosuggestions(diffs):
commit_message = {}
for idx, example in enumerate(whatthepatch.parse_patch(diffs)):
if not example.changes:
continue
......@@ -43,44 +60,78 @@ def autosuggestions(diffs):
deleted.extend(tokenizing(change.line))
isdeleted = True
if isadded and isdeleted:
if isadded and isdeleted and example.header.new_path:
data = {"idx": idx, "added" : added, "deleted" : deleted}
res = requests.post(
'http://127.0.0.1:5000/diff',
data=json.dumps(data),
headers=args.headers
headers={'Content-Type': 'application/json; charset=utf-8'}
)
commit = json.loads(res.text)
commit.update({'path' : example.header.new_path})
commit_message.append(commit)
commit_message[example.header.new_path] = commit
else:
data = {"idx": idx, "added": added, "deleted": deleted}
res = requests.post(
'http://127.0.0.1:5000/added',
data=json.dumps(data),
headers=args.headers
headers={'Content-Type': 'application/json; charset=utf-8'}
)
commit = json.loads(res.text)
commit.update({'path': example.header.new_path})
commit_message.append(commit)
commit_message[example.header.new_path] = commit
return commit_message
def main():
def commit(message):
subprocess.Popen(["git", "commit", "-m", message], stdout=subprocess.PIPE)
proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE)
staged_files = proc.stdout.readlines()
staged_files = [f.decode("utf-8") for f in staged_files]
@click.group(invoke_without_command=True)
@click.pass_context
@click.option('--file', '-f', type=click.File('r'),
help='patch file containing git diff '
'(e.g. file created by `git add` and `git diff --cached > test.diff`)')
@click.option('--verbose', '-v', is_flag=True,
help='print suggested commit message more detail.')
@click.option('--autocommit', '-a', is_flag=True,
help='automatically commit without asking if you want to commit')
@click.option('--endline', '-e', type=int, default=1,
help='number of endlines for each commit message generated by the diff of each file')
def cli(ctx, file, verbose, autocommit, endline):
if not ctx.invoked_subcommand:
staged_files = file if file else get_diff_from_project()
staged_files = [f.strip() for f in staged_files]
diffs = "\n".join(staged_files)
message = autosuggestions(diffs=diffs)
print(message)
messages = commit_autosuggestions(diffs=diffs)
if verbose:
click.echo(
json.dumps(messages, indent=4, sort_keys=True) + "\n"
)
click.echo(click.style('[INFO]', fg='green') + " The generated message is as follows:")
message = commit_message_parser(messages, endline=endline)
if autocommit or click.confirm('Do you want to commit this message?'):
commit(message)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="")
parser.add_argument("--endpoint", type=str, default="http://127.0.0.1:5000/")
args = parser.parse_args()
args.headers = {'Content-Type': 'application/json; charset=utf-8'}
@cli.command()
@click.option('--profile', default='default', type=str,
help='unique name for managing each independent settings')
@click.option('--endpoint', default='http://127.0.0.1:5000/', type=str,
help='endpoint address accessible to the server (example:http://127.0.0.1:5000/)')
main()
\ No newline at end of file
def configure(profile, endpoint):
path = join(expanduser("~"), '.commit-autosuggestions.ini')
config = configparser.ConfigParser()
if exists(path):
config.read(path)
if profile not in config:
config[profile] = {}
if endpoint:
config[profile]['endpoint'] = endpoint
click.echo(f"configure of commit-autosuggestions is setted up in {path}.")
for key in config[profile]:
click.echo(click.style(key, fg='green') + config[profile][key])
if __name__ == '__main__':
cli()
\ No newline at end of file
......
......@@ -13,10 +13,10 @@
# limitations under the License.
import os
from setuptools import setup
from setuptools import setup, find_packages
project_name = "commit"
version = os.environ.get('COMMIT_VERSION', '0.0.0')
version = os.environ.get('COMMIT_VERSION', '0.0.1')
if __name__ == "__main__":
......@@ -41,9 +41,16 @@ if __name__ == "__main__":
project_urls={
"Source Code": "https://github.com/graykode/commit-autosuggestions",
},
install_requires = [
'click>=7.1.2',
'gitpython>=3.1.7',
'whatthepatch>=1.0.0',
'packaging>=20.4',
],
entry_points={
'console_scripts': [
'commit=commit.__main__:main',
'commit=commit.commit:cli'
],
}
},
packages=find_packages(),
)
\ No newline at end of file
......
diff --git a/codebert/code.py b/codebert/code.py
new file mode 100644
index 0000000..b4bc953
--- /dev/null
+++ b/codebert/code.py
@@ -0,0 +1,21 @@
+def dailymotion_download(url, output_dir='.', merge=True, info_only=False, **kwargs):
+
+ html = get_content(rebuilt_url(url))
+ info = json.loads(match1(html, r'qualities":({.+?}),"'))
+ title = match1(html, r'"video_title"\s*:\s*"([^"]+)"') or \
+ match1(html, r'"title"\s*:\s*"([^"]+)"')
+ title = unicodize(title)
+
+ for quality in ['1080','720','480','380','240','144','auto']:
+ try:
+ real_url = info[quality][1]["url"]
+ if real_url:
+ break
+ except KeyError:
+ pass
+
+ mime, ext, size = url_info(real_url)
+
+ print_info(site_info, title, mime, size)
+ if not info_only:
+ download_urls([real_url], title, ext, size, output_dir=output_dir, merge=merge)
diff --git a/src/train/model.py b/src/train/model.py
index 20e56b3..cab82e5 100644
--- a/src/train/model.py
+++ b/src/train/model.py
@@ -3,9 +3,7 @@
import torch
import torch.nn as nn
-import torch
-from torch.autograd import Variable
-import copy
+
class Seq2Seq(nn.Module):
"""
Build Seqence-to-Sequence.
\ No newline at end of file