Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active August 23, 2024 12:26
Show Gist options
  • Save iegik/451c66af1b949e2e43072fca020addee to your computer and use it in GitHub Desktop.
Save iegik/451c66af1b949e2e43072fca020addee to your computer and use it in GitHub Desktop.
#!/bin/python
/*
* > `#idea to write a program like #git, so it was based on #ast instead of sources, and changes will be written in #ml predictions tree instead of #diff. I'll never have time for this #inhtft, but you...`
*/
import ast
import difflib
import json
import argparse
import socket
class ASTRepository:
def __init__(self):
self.repo = {}
def parse_file(self, filename):
with open(filename, 'r') as file:
code = file.read()
return ast.parse(code)
# sit add example.py
def add(self, filename):
tree = self.parse_file(filename)
self.repo[filename] = ast.dump(tree)
# sit commit "Initial commit"
def commit(self, message):
# Store the AST changes and ML predictions here
# For simplicity, we only store the AST
with open('repo.json', 'w') as file:
json.dump(self.repo, file)
# sit diff example.py
def diff(self, filename, old_tree, new_tree):
old_ast = ast.dump(old_tree)
new_ast = ast.dump(new_tree)
return list(difflib.unified_diff(old_ast.splitlines(), new_ast.splitlines()))
class ASTRepositoryCLI(ASTRepository):
def __init__(self):
super().__init__()
self.config = self.load_config()
def load_config(self):
try:
with open('.sitconfig', 'r') as config_file:
return json.load(config_file)
except FileNotFoundError:
return {}
def save_config(self):
with open('.sitconfig', 'w') as config_file:
json.dump(self.config, config_file)
def handle_command(self):
parser = argparse.ArgumentParser(description="AST Repository CLI")
subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser('add', help="Add a file to the repository").add_argument('filename')
subparsers.add_parser('commit', help="Commit changes with a message").add_argument('message')
subparsers.add_parser('diff', help="Show differences between versions").add_argument('filename')
subparsers.add_parser('build', help="Build code from AST").add_argument('language', choices=['py', 'ts'])
subparsers.add_parser('push', help="Push to a remote repository")
subparsers.add_parser('pull', help="Pull from a remote repository")
subparsers.add_parser('remote', help="Set remote host and port").add_argument('host').add_argument('port')
args = parser.parse_args()
if args.command == 'add':
self.add(args.filename)
elif args.command == 'commit':
self.commit(args.message)
elif args.command == 'diff':
# Assuming old_tree is stored in the repository or can be passed in some way
pass
elif args.command == 'build':
# Build from the latest AST to code
pass
elif args.command == 'push':
self.push()
elif args.command == 'pull':
self.pull()
elif args.command == 'remote':
self.set_remote(args.host, args.port)
else:
parser.print_help()
def set_remote(self, host, port):
self.config['host'] = host
self.config['port'] = port
self.save_config()
# sit build py
def build(self, language):
for filename, ast_str in self.repo.items():
tree = ast.parse(ast_str)
if language == 'py':
code = compile(tree, filename, 'exec')
print(f"Generated Python code for {filename}:\n{code}")
# Add TypeScript conversion if needed
# sit push
def push(self):
host = self.config.get('host')
port = self.config.get('port')
if not host or not port:
print("Remote not set. Use the remote command first.")
return
with open('repo.json', 'r') as file:
data = file.read()
# Example socket connection to push data
# ...
# sit pull
def pull(self):
host = self.config.get('host')
port = self.config.get('port')
if not host or not port:
print("Remote not set. Use the remote command first.")
return
# Example socket connection to pull data
# ...
with open('repo.json', 'w') as file:
file.write(data)
# sit remote 127.0.0.1 65432
def simple_server(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"Server listening on {host}:{port}")
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
data = conn.recv(1024)
with open('remote_repo.json', 'w') as f:
f.write(data.decode())
conn.sendall(b'ACK')
# Example usage
repo = ASTRepository()
repo.add('example.py')
repo.commit('Initial commit')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment