Compare commits

...
9 Commits
3 changed files with 69 additions and 5 deletions
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python
import argparse
from noid import Minter
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="noid", description="Generate noid, archival persistent identifiers")
subparsers = parser.add_subparsers(dest="command")
new_sp = subparsers.add_parser("new")
new_sp.add_argument("name")
new_sp.add_argument("template")
mint_sp = subparsers.add_parser("mint")
mint_sp.add_argument("name")
args = parser.parse_args()
match args.command:
case "new":
minter = Minter()
if (total := minter.parse_template(args.template)) > 0:
minter.initcounters()
print(f"The minter is ready. {total} noid are ready to be minted.")
else:
print("Someting happened, probably a problem with the template: {args.template}")
minter.store(f"{args.name}.noid")
case "mint":
minter = Minter.load(f"{args.name}.noid")
print(f"New noid: {minter.genid()}")
minter.store(f"{args.name}.noid")
case _:
parser.print_help()
+35 -4
View File
@@ -17,6 +17,7 @@ class Counter():
@dataclass
class Minter(Persistent):
legalstring: ClassVar[str] = "0123456789bcdfghjkmnpqrstvwxz"
xdig: tuple[str] = tuple(legalstring)
alphacount: ClassVar[int] = len(legalstring)
digitcount: ClassVar[int] = 10
@@ -138,7 +139,6 @@ class Minter(Persistent):
# 8. return s
s = ""
varwidth = 0
xdig = list(self.legalstring)
rmask = list(self.template)
rmask.reverse()
while num != 0 or not varwidth:
@@ -164,13 +164,44 @@ class Minter(Persistent):
continue
remainder = num % div
num = int(num / div)
s = xdig[remainder] + s
s = self.xdig[remainder] + s
if self.template[-1] == "k":
s += "+"
return s
def _checkbar(self, id):
pass
def _checkchar(self, id):
# 1. Return undef if id is not set
# 2. init lastchar to last char of the id, and remove that char from id
# 3. init pos to 1
# 4. init sum to 0
# 5. declare c
# 6. for each character of the id
# - get the ordinal value of the character
# - increment sum with the pos * the index value of the char in legalstring
# - increment pos
# 7. init checkchar to the value of xdig[sum % alphacount]
# 8. return the concat of id and checkchar is lastchar equal to '+' or checkchar
# 9. return None
if id is None:
return None
lastchar = id[-1]
sum = 0
for idx, char in enumerate(id[:-1], 1):
sum += idx * (self.legalstring.index(char) if self.legalstring.index(char) else 0)
checkchar = self.xdig[sum % self.alphacount]
if lastchar == "+" or lastchar == checkchar:
return id[:-1] + checkchar
else:
return None
def mint(self, n=1):
for _ in range(n):
_id = self.genid()
if self.template[-1] == "k":
_id = self._checkchar(_id)
if _id is not None:
return self.prefix + _id
return self.prefix + _id
if __name__ == "__main__":
+1 -1
View File
@@ -17,7 +17,7 @@ if __name__ == "__main__":
for i in range(noid.oatop + 2):
print(f"--> {i=}")
try:
n = noid.genid()
n = noid.mint()
print("noid:", n)
print(noid.active_counters)
print(noid.inactive_counters)