Unix-style user accounts (passwd, groups, permissions) with a web-friendly Python API.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-accounts.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-accounts.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-accounts.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add comprehensive FAQ section 369661c 1 year ago History
tsv_db.py · 3.7 KB · 111 lines
1"""
2Simple TSV database wrapper for passwd/shadow/group files
3"""
4
5import csv
6from pathlib import Path
7from typing import List, Dict, Any, Optional
8import fcntl
9
10
11class Database:
12 """Simple TSV database with headers"""
13
14 def __init__(self, path: str):
15 self.path = Path(path)
16 self.path.parent.mkdir(parents=True, exist_ok=True)
17
18 # Read headers if file exists
19 if self.path.exists():
20 with open(self.path, 'r') as f:
21 reader = csv.DictReader(f, delimiter='\t')
22 self.columns = reader.fieldnames or []
23 else:
24 self.columns = []
25
26 def insert(self, row: Dict[str, Any]):
27 """Insert a row"""
28 with open(self.path, 'a') as f:
29 fcntl.flock(f.fileno(), fcntl.LOCK_EX)
30 try:
31 writer = csv.DictWriter(f, fieldnames=self.columns, delimiter='\t')
32 writer.writerow(row)
33 finally:
34 fcntl.flock(f.fileno(), fcntl.LOCK_UN)
35
36 def select(self, where: Optional[Dict[str, Any]] = None) -> List[Dict[str, Any]]:
37 """Select rows with optional where clause"""
38 if not self.path.exists():
39 return []
40
41 results = []
42 with open(self.path, 'r') as f:
43 fcntl.flock(f.fileno(), fcntl.LOCK_SH)
44 try:
45 reader = csv.DictReader(f, delimiter='\t')
46 for row in reader:
47 if where is None:
48 results.append(row)
49 else:
50 match = all(row.get(k) == v for k, v in where.items())
51 if match:
52 results.append(row)
53 finally:
54 fcntl.flock(f.fileno(), fcntl.LOCK_UN)
55
56 return results
57
58 def update(self, where: Dict[str, Any], values: Dict[str, Any]):
59 """Update rows matching where clause"""
60 if not self.path.exists():
61 return
62
63 rows = []
64 with open(self.path, 'r') as f:
65 fcntl.flock(f.fileno(), fcntl.LOCK_SH)
66 try:
67 reader = csv.DictReader(f, delimiter='\t')
68 for row in reader:
69 match = all(row.get(k) == v for k, v in where.items())
70 if match:
71 row.update(values)
72 rows.append(row)
73 finally:
74 fcntl.flock(f.fileno(), fcntl.LOCK_UN)
75
76 # Write back
77 with open(self.path, 'w') as f:
78 fcntl.flock(f.fileno(), fcntl.LOCK_EX)
79 try:
80 writer = csv.DictWriter(f, fieldnames=self.columns, delimiter='\t')
81 writer.writeheader()
82 writer.writerows(rows)
83 finally:
84 fcntl.flock(f.fileno(), fcntl.LOCK_UN)
85
86 def delete(self, where: Dict[str, Any]):
87 """Delete rows matching where clause"""
88 if not self.path.exists():
89 return
90
91 rows = []
92 with open(self.path, 'r') as f:
93 fcntl.flock(f.fileno(), fcntl.LOCK_SH)
94 try:
95 reader = csv.DictReader(f, delimiter='\t')
96 for row in reader:
97 match = all(row.get(k) == v for k, v in where.items())
98 if not match:
99 rows.append(row)
100 finally:
101 fcntl.flock(f.fileno(), fcntl.LOCK_UN)
102
103 # Write back
104 with open(self.path, 'w') as f:
105 fcntl.flock(f.fileno(), fcntl.LOCK_EX)
106 try:
107 writer = csv.DictWriter(f, fieldnames=self.columns, delimiter='\t')
108 writer.writeheader()
109 writer.writerows(rows)
110 finally:
111 fcntl.flock(f.fileno(), fcntl.LOCK_UN)