From 4e40263f697817587d80d121c4667b1e3f2c571b Mon Sep 17 00:00:00 2001 From: indroneel Date: Tue, 15 Sep 2026 23:46:06 +0530 Subject: [PATCH] the first commit --- .gitignore | 4 ++ README.md | 36 +++++++++++++- pyproject.toml | 20 ++++++++ src/unique/__init__.py | 1 + src/unique/lib/suffix.py | 62 +++++++++++++++++++++++++ src/unique/main.py | 21 +++++++++ src/unique/modules/create/command.py | 44 ++++++++++++++++++ src/unique/modules/duplicate/command.py | 13 ++++++ src/unique/modules/install/command.py | 13 ++++++ src/unique/modules/retouch/command.py | 39 ++++++++++++++++ src/unique/modules/touch/command.py | 35 ++++++++++++++ 11 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 pyproject.toml create mode 100644 src/unique/__init__.py create mode 100644 src/unique/lib/suffix.py create mode 100644 src/unique/main.py create mode 100644 src/unique/modules/create/command.py create mode 100644 src/unique/modules/duplicate/command.py create mode 100644 src/unique/modules/install/command.py create mode 100644 src/unique/modules/retouch/command.py create mode 100644 src/unique/modules/touch/command.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fdfc91 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.venv +/dist +*.egg-info +__pycache__ diff --git a/README.md b/README.md index 609800a..7e9b9a6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,37 @@ # unique -A utility that appends a suffix to files and folders to ensure name uniqueness. \ No newline at end of file +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 . +``` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..727461f --- /dev/null +++ b/pyproject.toml @@ -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"] \ No newline at end of file diff --git a/src/unique/__init__.py b/src/unique/__init__.py new file mode 100644 index 0000000..dcf2c80 --- /dev/null +++ b/src/unique/__init__.py @@ -0,0 +1 @@ +# Placeholder diff --git a/src/unique/lib/suffix.py b/src/unique/lib/suffix.py new file mode 100644 index 0000000..7461e13 --- /dev/null +++ b/src/unique/lib/suffix.py @@ -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'.""" + ) \ No newline at end of file diff --git a/src/unique/main.py b/src/unique/main.py new file mode 100644 index 0000000..55afc84 --- /dev/null +++ b/src/unique/main.py @@ -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() \ No newline at end of file diff --git a/src/unique/modules/create/command.py b/src/unique/modules/create/command.py new file mode 100644 index 0000000..7c0afce --- /dev/null +++ b/src/unique/modules/create/command.py @@ -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}") diff --git a/src/unique/modules/duplicate/command.py b/src/unique/modules/duplicate/command.py new file mode 100644 index 0000000..7f58072 --- /dev/null +++ b/src/unique/modules/duplicate/command.py @@ -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") diff --git a/src/unique/modules/install/command.py b/src/unique/modules/install/command.py new file mode 100644 index 0000000..6e5680b --- /dev/null +++ b/src/unique/modules/install/command.py @@ -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") diff --git a/src/unique/modules/retouch/command.py b/src/unique/modules/retouch/command.py new file mode 100644 index 0000000..2f1628a --- /dev/null +++ b/src/unique/modules/retouch/command.py @@ -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") diff --git a/src/unique/modules/touch/command.py b/src/unique/modules/touch/command.py new file mode 100644 index 0000000..d679f4f --- /dev/null +++ b/src/unique/modules/touch/command.py @@ -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}'") \ No newline at end of file