Skip to content

Commit 8242b80

Browse files
committed
Format .py with ruff
1 parent a311660 commit 8242b80

12 files changed

Lines changed: 56 additions & 184 deletions

File tree

locksmith/api/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ async def _default_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
2323
await create_tables()
2424
app.state.signer = FileSigner.from_files(
2525
pubkey_path=settings.pubkey_path,
26-
privkey_path=(
27-
settings.privkey_path if settings.privkey_path.exists() else None
28-
),
26+
privkey_path=(settings.privkey_path if settings.privkey_path.exists() else None),
2927
)
3028
yield
3129

locksmith/api/routes/activate.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ async def activate(body: ActivateRequest, request: Request) -> ActivateResponse:
9090
detail="user_principal is required for user-restricted licenses.",
9191
)
9292
identity = body.user_principal
93-
limit = (
94-
matched.seats
95-
if (matched is not None and matched.seats is not None)
96-
else row.user_limit
97-
)
93+
limit = matched.seats if (matched is not None and matched.seats is not None) else row.user_limit
9894
else:
9995
if not body.machine_id:
10096
raise HTTPException(
@@ -103,18 +99,10 @@ async def activate(body: ActivateRequest, request: Request) -> ActivateResponse:
10399
)
104100
identity = body.machine_id
105101
if restriction == RestrictionMode.FLOATING.value:
106-
limit = (
107-
matched.seats
108-
if (matched is not None and matched.seats is not None)
109-
else row.concurrent_limit
110-
)
102+
limit = matched.seats if (matched is not None and matched.seats is not None) else row.concurrent_limit
111103
else:
112104
# "activations" or None (unrestricted licenses still count seats if limit set)
113-
limit = (
114-
matched.seats
115-
if (matched is not None and matched.seats is not None)
116-
else row.activation_limit
117-
)
105+
limit = matched.seats if (matched is not None and matched.seats is not None) else row.activation_limit
118106

119107
activation_app_id = matched.app_id if matched is not None else "*"
120108

@@ -130,19 +118,15 @@ async def activate(body: ActivateRequest, request: Request) -> ActivateResponse:
130118
is_new = result.scalar_one_or_none() is None
131119

132120
if is_new and limit is not None:
133-
active_count = await count_active_activations(
134-
session, body.license_id, activation_app_id
135-
)
121+
active_count = await count_active_activations(session, body.license_id, activation_app_id)
136122
if active_count >= limit:
137123
raise HTTPException(
138124
status_code=403,
139125
detail=f"Limit of {limit} reached for '{activation_app_id}'.",
140126
)
141127

142128
await record_activation(session, body.license_id, activation_app_id, identity)
143-
final_count = await count_active_activations(
144-
session, body.license_id, activation_app_id
145-
)
129+
final_count = await count_active_activations(session, body.license_id, activation_app_id)
146130

147131
return ActivateResponse(
148132
status="activated",
@@ -165,9 +149,7 @@ async def deactivate(body: DeactivateRequest, request: Request) -> None:
165149
restriction = row.restriction
166150
if restriction == RestrictionMode.USERS.value:
167151
if not body.user_principal:
168-
raise HTTPException(
169-
status_code=422, detail="user_principal is required."
170-
)
152+
raise HTTPException(status_code=422, detail="user_principal is required.")
171153
identity = body.user_principal
172154
else:
173155
if not body.machine_id:

locksmith/cli/generate.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@
8989
@click.option(
9090
"--app-id",
9191
default=None,
92-
help=(
93-
"App ID for a single-app entitlement (e.g. 'com.example.myapp'). "
94-
"For bundles use --entitlements-file."
95-
),
92+
help=("App ID for a single-app entitlement (e.g. 'com.example.myapp'). For bundles use --entitlements-file."),
9693
)
9794
@click.option(
9895
"--entitlement-editions",
@@ -124,10 +121,7 @@
124121
"--entitlements-file",
125122
default=None,
126123
type=click.Path(exists=True),
127-
help=(
128-
"JSON file containing a list of entitlement objects. "
129-
"Overrides all single-entitlement options."
130-
),
124+
help=("JSON file containing a list of entitlement objects. Overrides all single-entitlement options."),
131125
)
132126
# ---- Common ----
133127
@click.option(
@@ -212,14 +206,10 @@ def main(
212206
entitlements = [
213207
Entitlement(
214208
app_id=app_id,
215-
editions=[e.strip() for e in entitlement_editions.split(",")]
216-
if entitlement_editions
217-
else None,
209+
editions=[e.strip() for e in entitlement_editions.split(",")] if entitlement_editions else None,
218210
min_version=entitlement_min_version,
219211
max_version=entitlement_max_version,
220-
platforms=[p.strip() for p in entitlement_platforms.split(",")]
221-
if entitlement_platforms
222-
else None,
212+
platforms=[p.strip() for p in entitlement_platforms.split(",")] if entitlement_platforms else None,
223213
seats=entitlement_seats,
224214
)
225215
]
@@ -259,21 +249,14 @@ def main(
259249
click.secho(f"License written to: {out_path}", fg="green")
260250
click.echo(f" ID : {lic.license_id}")
261251
click.echo(f" Email : {lic.email}")
262-
click.echo(
263-
f" Time : {lic.time_policy.value}"
264-
+ (f" (expires {lic.expires_at.date()})" if lic.expires_at else "")
265-
)
252+
click.echo(f" Time : {lic.time_policy.value}" + (f" (expires {lic.expires_at.date()})" if lic.expires_at else ""))
266253
click.echo(
267254
f" Version : {lic.version_policy.value}"
268255
+ (f" (major {lic.major_version})" if lic.major_version is not None else "")
269256
+ (f" (locked {lic.locked_version})" if lic.locked_version else "")
270257
)
271-
click.echo(
272-
f" Editions : {', '.join(lic.editions) if lic.editions else 'any'}"
273-
)
274-
click.echo(
275-
f" Platforms : {', '.join(lic.platforms) if lic.platforms else 'any'}"
276-
)
258+
click.echo(f" Editions : {', '.join(lic.editions) if lic.editions else 'any'}")
259+
click.echo(f" Platforms : {', '.join(lic.platforms) if lic.platforms else 'any'}")
277260
if lic.restriction:
278261
mode = lic.restriction.value
279262
limit_val = lic.activation_limit or lic.user_limit or lic.concurrent_limit
@@ -287,9 +270,7 @@ def main(
287270
if ent.editions:
288271
parts.append(f"editions={','.join(ent.editions)}")
289272
if ent.min_version or ent.max_version:
290-
parts.append(
291-
f"versions={ent.min_version or '*'}..{ent.max_version or '*'}"
292-
)
273+
parts.append(f"versions={ent.min_version or '*'}..{ent.max_version or '*'}")
293274
if ent.platforms:
294275
parts.append(f"platforms={','.join(ent.platforms)}")
295276
if ent.seats:

locksmith/cli/request.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def main(email: str, app_id: str | None, app_version: str, out: str | None) -> N
3333
machine_id = compute_machine_id()
3434
click.echo(f"Machine ID: {machine_id}")
3535
except RuntimeError as exc:
36-
click.secho(
37-
f"Warning: could not compute machine ID: {exc}", fg="yellow", err=True
38-
)
36+
click.secho(f"Warning: could not compute machine ID: {exc}", fg="yellow", err=True)
3937

4038
req = LicenseRequest.new(
4139
email=email,

locksmith/cli/verify.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ def main(license_file: str, pubkey: str | None, app_version: str | None) -> None
5353
click.echo(f" Email : {lic.email}")
5454
click.echo(f" Time : {lic.time_policy.value}")
5555
click.echo(f" Version : {lic.version_policy.value}")
56-
click.echo(
57-
f" Restriction : {lic.restriction.value if lic.restriction else 'none'}"
58-
)
59-
click.echo(
60-
f" Expires : {lic.expires_at.isoformat() if lic.expires_at else 'never'}"
61-
)
56+
click.echo(f" Restriction : {lic.restriction.value if lic.restriction else 'none'}")
57+
click.echo(f" Expires : {lic.expires_at.isoformat() if lic.expires_at else 'never'}")
6258
except LicenseError as exc:
6359
click.secho(f"License is INVALID: {exc}", fg="red")
6460
sys.exit(1)

locksmith/core/keys.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,14 @@ def __init__(
5757

5858
async def sign(self, data: bytes) -> bytes:
5959
if self._privkey is None:
60-
raise ValueError(
61-
"This FileSigner instance has no private key (verify-only mode)."
62-
)
60+
raise ValueError("This FileSigner instance has no private key (verify-only mode).")
6361
loop = asyncio.get_running_loop()
64-
return await loop.run_in_executor(
65-
None, lambda: rsa.sign(data, self._privkey, "SHA-512")
66-
)
62+
return await loop.run_in_executor(None, lambda: rsa.sign(data, self._privkey, "SHA-512"))
6763

6864
async def verify(self, data: bytes, signature: bytes) -> bool:
6965
loop = asyncio.get_running_loop()
7066
try:
71-
await loop.run_in_executor(
72-
None, lambda: rsa.verify(data, signature, self._pubkey)
73-
)
67+
await loop.run_in_executor(None, lambda: rsa.verify(data, signature, self._pubkey))
7468
return True
7569
except rsa.VerificationError:
7670
return False

locksmith/core/license.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ def __init__(
7373
self.editions = [e.lower() for e in editions] if editions is not None else None
7474
self.min_version = min_version
7575
self.max_version = max_version
76-
self.platforms = (
77-
[p.lower() for p in platforms] if platforms is not None else None
78-
)
76+
self.platforms = [p.lower() for p in platforms] if platforms is not None else None
7977
self.seats = seats
8078

8179
def to_dict(self) -> dict:
@@ -139,12 +137,8 @@ def __init__(
139137
self.major_version = major_version
140138
self.locked_version = locked_version
141139
self.editions = [e.lower() for e in editions] if editions is not None else None
142-
self.platforms = (
143-
[p.lower() for p in platforms] if platforms is not None else None
144-
)
145-
self.restriction = (
146-
RestrictionMode(restriction) if restriction is not None else None
147-
)
140+
self.platforms = [p.lower() for p in platforms] if platforms is not None else None
141+
self.restriction = RestrictionMode(restriction) if restriction is not None else None
148142
self.activation_limit = activation_limit
149143
self.user_limit = user_limit
150144
self.concurrent_limit = concurrent_limit

locksmith/core/signer.py

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -142,104 +142,67 @@ async def validate_license(
142142
if valid_from.tzinfo is None:
143143
valid_from = valid_from.replace(tzinfo=UTC)
144144
if now < valid_from:
145-
raise LicenseNotYetValidError(
146-
f"License is not valid until {license.valid_from.isoformat()}."
147-
)
145+
raise LicenseNotYetValidError(f"License is not valid until {license.valid_from.isoformat()}.")
148146

149147
if license.expires_at is not None:
150148
expires_at = license.expires_at
151149
if expires_at.tzinfo is None:
152150
expires_at = expires_at.replace(tzinfo=UTC)
153151
if now > expires_at:
154-
raise LicenseExpiredError(
155-
f"License expired on {license.expires_at.isoformat()}."
156-
)
152+
raise LicenseExpiredError(f"License expired on {license.expires_at.isoformat()}.")
157153

158154
# 3. Version policy
159155
if license.version_policy == VersionPolicy.MAINTENANCE:
160156
if app_version is None:
161-
raise LicenseVersionError(
162-
"app_version is required for maintenance license validation."
163-
)
157+
raise LicenseVersionError("app_version is required for maintenance license validation.")
164158
app_major = _parse_version(app_version)[0]
165159
if license.major_version != app_major:
166160
raise LicenseVersionError(
167-
f"License covers major version {license.major_version}, "
168-
f"but the application is version {app_version}."
161+
f"License covers major version {license.major_version}, but the application is version {app_version}."
169162
)
170163
elif license.version_policy == VersionPolicy.SPECIFIC:
171164
if app_version is None:
172-
raise LicenseVersionError(
173-
"app_version is required for specific-version license validation."
174-
)
165+
raise LicenseVersionError("app_version is required for specific-version license validation.")
175166
if license.locked_version != app_version:
176167
raise LicenseVersionError(
177-
f"License is locked to version {license.locked_version}, "
178-
f"but the application is version {app_version}."
168+
f"License is locked to version {license.locked_version}, but the application is version {app_version}."
179169
)
180170

181171
# 4. Entitlement matching
182172
matched: Entitlement | None = None
183173

184174
if license.entitlements:
185175
if app_id is None:
186-
raise LicenseAppError(
187-
"This license restricts access by application. "
188-
"Provide app_id to validate."
189-
)
176+
raise LicenseAppError("This license restricts access by application. Provide app_id to validate.")
190177

191178
for ent in license.entitlements:
192179
if ent.app_id == app_id:
193180
matched = ent
194181
break
195182

196183
if matched is None:
197-
raise LicenseAppError(
198-
f"This license does not cover application '{app_id}'."
199-
)
184+
raise LicenseAppError(f"This license does not cover application '{app_id}'.")
200185

201186
# 5. Effective edition check
202187
# Entitlement editions override license-level; None on either = no restriction.
203-
eff_editions = (
204-
matched.editions
205-
if (matched is not None and matched.editions is not None)
206-
else license.editions
207-
)
188+
eff_editions = matched.editions if (matched is not None and matched.editions is not None) else license.editions
208189
if eff_editions is not None and (edition is None or edition.lower() not in eff_editions):
209-
raise LicenseEditionError(
210-
f"Edition '{edition}' is not permitted. "
211-
f"Allowed editions: {', '.join(eff_editions)}."
212-
)
190+
raise LicenseEditionError(f"Edition '{edition}' is not permitted. Allowed editions: {', '.join(eff_editions)}.")
213191

214192
# 6. Effective platform check (same fallback logic)
215-
eff_platforms = (
216-
matched.platforms
217-
if (matched is not None and matched.platforms is not None)
218-
else license.platforms
219-
)
193+
eff_platforms = matched.platforms if (matched is not None and matched.platforms is not None) else license.platforms
220194
if eff_platforms is not None and (platform is None or platform.lower() not in eff_platforms):
221-
raise LicenseOSError(
222-
f"Platform '{platform}' is not permitted. "
223-
f"Allowed platforms: {', '.join(eff_platforms)}."
224-
)
195+
raise LicenseOSError(f"Platform '{platform}' is not permitted. Allowed platforms: {', '.join(eff_platforms)}.")
225196

226197
# 7. Entitlement version range (per-app, complements the license-level version policy)
227198
if matched is not None:
228199
if app_version is not None:
229200
parsed = _parse_version(app_version)
230201
if matched.min_version is not None and parsed < _parse_version(matched.min_version):
231-
raise LicenseVersionError(
232-
f"App version {app_version} is below the minimum "
233-
f"required version {matched.min_version}."
234-
)
202+
raise LicenseVersionError(f"App version {app_version} is below the minimum required version {matched.min_version}.")
235203
if matched.max_version is not None and parsed > _parse_version(matched.max_version):
236-
raise LicenseVersionError(
237-
f"App version {app_version} exceeds the maximum "
238-
f"covered version {matched.max_version}."
239-
)
204+
raise LicenseVersionError(f"App version {app_version} exceeds the maximum covered version {matched.max_version}.")
240205
elif matched.min_version is not None or matched.max_version is not None:
241-
raise LicenseVersionError(
242-
"app_version is required to validate a version-restricted entitlement."
243-
)
206+
raise LicenseVersionError("app_version is required to validate a version-restricted entitlement.")
244207

245208
return matched

0 commit comments

Comments
 (0)