the first commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
/.venv
|
||||||
|
/dist
|
||||||
|
*.egg-info
|
||||||
|
__pycache__
|
||||||
@@ -1,3 +1,37 @@
|
|||||||
# unique
|
# unique
|
||||||
|
|
||||||
A utility that appends a suffix to files and folders to ensure name uniqueness.
|
A utility that appends a suffix to files and folders to ensure name uniqueness.
|
||||||
|
|
||||||
|
## Building from source
|
||||||
|
|
||||||
|
### 1. Virtual Environment
|
||||||
|
|
||||||
|
Create a virtual environment for Python, and activate the same.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
python3 -m venv .venv
|
||||||
|
|
||||||
|
source .venv/bin/activate
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Resolve Dependencies
|
||||||
|
|
||||||
|
Unique has a dependency on Scott: a framework to create scoped task-oriented terminal tools. To resolve this, run:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pip install "git+https://rprime.indroneel.co/termhole/scott.git"
|
||||||
|
```
|
||||||
|
|
||||||
|
For older versions of Python, Unique has a dependency on a backported UUID library:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pip install uuid6
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Installing the Application
|
||||||
|
|
||||||
|
To install `unique` in the virtual environment, run:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pip install -e .
|
||||||
|
```
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "unique"
|
||||||
|
version = "1.0"
|
||||||
|
description = "A utility that appends a suffix to files and folders to ensure name uniqueness."
|
||||||
|
|
||||||
|
#External dependencies
|
||||||
|
dependencies = [
|
||||||
|
"scott @ git+https://rprime.indroneel.co/termhole/scott.git"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Command-line interface (CLI) entry points
|
||||||
|
[project.scripts]
|
||||||
|
unique = "unique.main:main"
|
||||||
|
|
||||||
|
[tools.setuptools.packages.find]
|
||||||
|
where = ["src"]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# Placeholder
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
import uuid6
|
||||||
|
import textwrap
|
||||||
|
|
||||||
|
def base36(value):
|
||||||
|
"""Convert a non-negative integer to a base36 string."""
|
||||||
|
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||||
|
if value == 0:
|
||||||
|
return "0"
|
||||||
|
|
||||||
|
result = []
|
||||||
|
while value:
|
||||||
|
value, remainder = divmod(value, 36)
|
||||||
|
result.append(digits[remainder])
|
||||||
|
return "".join(reversed(result))
|
||||||
|
|
||||||
|
def get_suffix(method):
|
||||||
|
if method == "datetime":
|
||||||
|
return datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||||
|
|
||||||
|
if method == "epoch":
|
||||||
|
return str(int(time.time() * 1000))
|
||||||
|
|
||||||
|
if method == "uuid":
|
||||||
|
return str(uuid.uuid4())
|
||||||
|
|
||||||
|
if method == "uuid7":
|
||||||
|
return str(uuid6.uuid7())
|
||||||
|
|
||||||
|
if method == "uuid-b36":
|
||||||
|
uuid_value = uuid.uuid4().int
|
||||||
|
msb = uuid_value >> 64
|
||||||
|
lsb = uuid_value & ((1 << 64) - 1)
|
||||||
|
return f"{base36(msb)}-{base36(lsb)}"
|
||||||
|
|
||||||
|
if method == "uuid7-b36":
|
||||||
|
uuid_value = uuid6.uuid7().int
|
||||||
|
msb = uuid_value >> 64
|
||||||
|
lsb = uuid_value & ((1 << 64) - 1)
|
||||||
|
return f"{base36(msb)}-{base36(lsb)}"
|
||||||
|
|
||||||
|
else:
|
||||||
|
return str(random.randint(0, 999999))
|
||||||
|
|
||||||
|
def add_method_argument(parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"-m", "--method",
|
||||||
|
type=str, metavar="METHOD",
|
||||||
|
choices=["datetime", "epoch", "uuid", "uuid7", "uuid-b36", "uuid7-b36"],
|
||||||
|
required=False, default="datetime",
|
||||||
|
help = """Select the method to generate the suffix:
|
||||||
|
'datetime' : Use the current date and time as a unique suffix
|
||||||
|
'epoch' : Use the timestamp (milliseconds since epoch) as a unique suffix
|
||||||
|
'uuid' : Use the v4 universally unique identifier as a unique suffix
|
||||||
|
'uuid7' : Use the v7 universally unique identifier as a unique suffix
|
||||||
|
'uuid-b36' : Use the base36 representation of a UUID as a unique suffix
|
||||||
|
'uuid7-b36' : Use the base36 representation of a UUID7 as a unique suffix
|
||||||
|
Default is 'datetime'."""
|
||||||
|
)
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import sys
|
||||||
|
sys.dont_write_bytecode = True
|
||||||
|
|
||||||
|
from scott import Scott
|
||||||
|
|
||||||
|
def main():
|
||||||
|
scott = Scott(__file__, "Append a suffix to files and folders to ensure unique names")
|
||||||
|
commands = [
|
||||||
|
"install",
|
||||||
|
"create",
|
||||||
|
"touch",
|
||||||
|
"retouch",
|
||||||
|
"duplicate",
|
||||||
|
]
|
||||||
|
for command in commands:
|
||||||
|
scott.register_command(command)
|
||||||
|
scott.run()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from unique.lib import suffix
|
||||||
|
|
||||||
|
def scott_description():
|
||||||
|
return "Create a new file or directory with a unique name"
|
||||||
|
|
||||||
|
def scott_arguments(parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"path", type=str, metavar="PATH",
|
||||||
|
help = "The path to the file or directory to be created with a unique name"
|
||||||
|
)
|
||||||
|
|
||||||
|
target = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
target.add_argument(
|
||||||
|
"-f", "--file", action="store_true",
|
||||||
|
help = "Indicates that the target is a file"
|
||||||
|
)
|
||||||
|
target.add_argument(
|
||||||
|
"-d", "--dir", action="store_true",
|
||||||
|
help = "Indicates that the target is a folder"
|
||||||
|
)
|
||||||
|
|
||||||
|
suffix.add_method_argument(parser)
|
||||||
|
|
||||||
|
def scott_run(args):
|
||||||
|
path = Path(args.path)
|
||||||
|
|
||||||
|
suffix = suffix.get_suffix(args.method)
|
||||||
|
|
||||||
|
if args.file:
|
||||||
|
candidate = path.with_name(f"{path.stem}-{suffix}{path.suffix}")
|
||||||
|
else:
|
||||||
|
candidate = path.with_name(f"{path.name}-{suffix}")
|
||||||
|
|
||||||
|
if candidate.exists():
|
||||||
|
print(f"Cannot create '{candidate}': candidate already exists.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if args.file:
|
||||||
|
candidate.touch()
|
||||||
|
else:
|
||||||
|
candidate.mkdir()
|
||||||
|
|
||||||
|
print(f"Created unique { 'file' if args.file else 'directory' }: {candidate}")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
def scott_description():
|
||||||
|
return "Creates an exact copy of a file, preserving its contents, but with an unique name"
|
||||||
|
|
||||||
|
def scott_arguments(parser):
|
||||||
|
"""
|
||||||
|
parser.add_argument(
|
||||||
|
"file_path",
|
||||||
|
help="The path where the file to be unpacked."
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def scott_run(args):
|
||||||
|
print("command: duplicate")
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
def scott_description():
|
||||||
|
return "your description here"
|
||||||
|
|
||||||
|
def scott_arguments(parser):
|
||||||
|
"""
|
||||||
|
parser.add_argument(
|
||||||
|
"file_path",
|
||||||
|
help="The path where the file to be unpacked."
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def scott_run(args):
|
||||||
|
print("command: install")
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
def scott_description():
|
||||||
|
return "updates an existing file name with a new prefix"
|
||||||
|
|
||||||
|
def scott_arguments(parser):
|
||||||
|
parser.add_argument(
|
||||||
|
"path", type=str, metavar="PATH",
|
||||||
|
help = "The path to the file or directory that needs a unique name"
|
||||||
|
)
|
||||||
|
|
||||||
|
target = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
target.add_argument(
|
||||||
|
"-f", "--file", action="store_true",
|
||||||
|
help = "Indicates that the target is a file"
|
||||||
|
)
|
||||||
|
target.add_argument(
|
||||||
|
"-d", "--dir", action="store_true",
|
||||||
|
help = "Indicates that the target is a folder"
|
||||||
|
)
|
||||||
|
|
||||||
|
method = parser.add_mutually_exclusive_group(required=True)
|
||||||
|
method.add_argument(
|
||||||
|
"--datetime", action="store_true",
|
||||||
|
help = "Use the current date and time as a unique suffix"
|
||||||
|
)
|
||||||
|
method.add_argument(
|
||||||
|
"--epoch", action="store_true",
|
||||||
|
help = "Use the timestamp (milliseconds since epoch) as a unique suffix"
|
||||||
|
)
|
||||||
|
method.add_argument(
|
||||||
|
"--uuid", action="store_true",
|
||||||
|
help = "Use the universally unique identifier as a unique suffix"
|
||||||
|
)
|
||||||
|
method.add_argument(
|
||||||
|
"--random", action="store_true",
|
||||||
|
help = "Use a random number as a unique suffix"
|
||||||
|
)
|
||||||
|
|
||||||
|
def scott_run(args):
|
||||||
|
print("command: retouch")
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from unique.lib import suffix
|
||||||
|
|
||||||
|
def scott_description():
|
||||||
|
return "appends a prefix to the original file name to create a unique name"
|
||||||
|
|
||||||
|
def scott_arguments(parser):
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"path", type=str, metavar="PATH",
|
||||||
|
help = "The path to the file or directory that needs a unique name"
|
||||||
|
)
|
||||||
|
|
||||||
|
suffix.add_method_argument(parser)
|
||||||
|
|
||||||
|
def scott_run(args):
|
||||||
|
path = Path(args.path)
|
||||||
|
if not path.exists():
|
||||||
|
print(f"Cannot touch '{path}': path does not exist.")
|
||||||
|
return
|
||||||
|
|
||||||
|
generated_suffix = suffix.get_suffix(args.method)
|
||||||
|
if path.is_file():
|
||||||
|
candidate = path.with_name(
|
||||||
|
f"{path.stem}-{generated_suffix}{path.suffix}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
candidate = path.with_name(f"{path.name}-{generated_suffix}")
|
||||||
|
|
||||||
|
if candidate.exists():
|
||||||
|
print(f"Cannot touch '{path}': candidate already exists.")
|
||||||
|
return
|
||||||
|
|
||||||
|
path.rename(candidate)
|
||||||
|
print(f"Touched '{path}' to '{candidate}'")
|
||||||
Reference in New Issue
Block a user