syncgit
Sync python dicts, strings and modules to files in git repository.
NOTE: syncgit calls git using subprocess, setup git so it does not ask for username or password, otherwise you will get a timeout exception.
Installation
python3 -m pip install syncgit
Class documentation
- class syncgit.SyncConfig(name: str, path: str, sync_type: str = 'auto')
- __init__(name: str, path: str, sync_type: str = 'auto') None
- Parameters
name (str) – Name of the attribute alias
path (str) – Path to file in the repository
sync_type (str) – File type. Available options are
"text"(plain text file, will be converted to python string),"json","yaml"(converted to python dict) and"module"(will be imported as a python module)."auto"to automatically detect sync type from file extension
- class syncgit.Repo(name: str, url: str, branch: str, dir_path: Optional[str] = None)
- __init__(name: str, url: str, branch: str, dir_path: Optional[str] = None) None
- Parameters
name (str) – Unique name for this repository and branch
url (str) – Repository URL to sync to (can be ssh or https)
branch (str) – Name of the branch to sync to
dir_path (Optional[str]) – Directory path to download and sync the repo to, will be synced to
f"./{os.environ["SYNCGIT_REPOS_DIR_NAME"]}/{name}/"ifNone
- set_config(config: List[SyncConfig]) None
Configure attributes to sync
- Parameters
config (List[SyncConfig]) – List of SyncConfig specifying attribute names, file path, type of the file (see
SyncConfig). These configs will be available as class attributes.
- start_sync() None
Start sync to git repository every X seconds, see
set_poll_interval()
- stop_sync() None
Stop sync to git repository
- sync() None
Sync to repository only once
- set_poll_interval(seconds: int) None
Set polling interval
- Parameters
seconds (int) – Amount of time between synchronization (in seconds, initially is
os.environ["SYNCGIT_DEFAULT_POLL_INTERVAL"])
- set_update_callback(callback: Callable[[Any, List[SyncConfig]], None]) None
Set callback to be call after synchronization is complete
- Parameters
callback (Callable[[Repo, List[SyncConfig]], None]) – This callback will be called when changes are pushed to the repo and the new changes have been synchronized. The callback should accept two arguments, the Repo class and List[SyncConfig], config attributes which got updated
- commit_hash
The commit hash of latest sync
Example
Example repo
syncgit-test
├── alice.json
│ └── {
│ "name": "Alice",
│ "age": 24,
│ "interests": [
│ "Programming",
│ "Skiing"
│ ]
│ }
├── bob.yml
│ └── name: Bob
│ age: 22
│ interests:
│ - Reading
│ - Drawing
├── say_hello.py
│ └── def say_hello(name):
│ print(f"Hello {name}, nice to meet you")
└── text_file.txt
└── This is a text file
Code
from typing import List
import time
from syncgit import Repo, SyncConfig
# This callback is called when changes are pushed to the repo
def update_callback(repo: Repo, changes: List[SyncConfig]) -> None:
print(f"Updated to commit {repo.commit_hash}")
for change in changes:
print(f"Updated {change.name}")
# Create repo class and import files from repository
rp = Repo("example_repo", "git@github.com:RainingComputers/syncgit-test.git", "main")
rp.set_config([
SyncConfig("about_alice", "alice.json", "json"),
SyncConfig("about_bob", "bob.yml"),
SyncConfig("text", "text_file.txt", "text"),
SyncConfig("hello_module", "say_hello.py")
])
# Register call back
rp.set_update_callback(update_callback)
# Start sync
rp.start_sync()
# Imported files will be available as attributes on the repo class
# Changes are reflected immediately on these attributes real time
try:
while True:
time.sleep(1)
print(rp.about_alice)
print(rp.about_bob)
print(rp.text)
rp.hello_module.say_hello("Alice")
except KeyboardInterrupt:
print("Stopping sync")
rp.stop_sync()
Environment Variables
Name |
Description |
Default |
|---|---|---|
SYNCGIT_CMD_TIMEOUT |
Timeout for pulling changes from repository |
15 |
SYNCGIT_REPOS_DIR_NAME |
Directory to store all synced repositories |
.repos |
SYNCGIT_DEFAULT_POLL_INTERVAL |
Default polling interval to pull changes and sync |
5 |