Skip to content

A collection of scripts I made for my own personal use/convenience.

License

Notifications You must be signed in to change notification settings

greysk/graeScripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graeScript

This is a package containing various programs written for my own personal use and for learning purposed.

WARNING: This is very much a work in progress. There are no guarantees of reliability. In fact, I guarantee that I will make changes to the code within this that will utterly change how anything and everything within here could be used, and I do so frequently. If you would like to use code within here, feel free to do so, but I would highly recommend forking this repo and being very careful about pulling any changes from here into your fork.

Currently this repository contains two, separate groups of code:

  1. A couple of small, command line scripts found in the cmdline folder that were written to be run using batch files and Windows run application.
  2. A Python package called graeScript which contains a mess of small, mostly unrelated scripts that I've written. It was made to ease reuse of these scripts easier for myself and to create a central location for them.

Details of small, independent scripts in cmdline dir

Filename Dependencies Summary
colorvalue.py pyperclip* and graeScripts.Drawing Returns/copies to the clipboard either the RGB or the Hex color values of a given HTML color name.
CRLF_to_LF.py Convert plain text files in a directory tree to have LF newline endings.
pf_fix_path_sep.py pyperclip Takes path from clipboard, replaces \ path separators with /, and copies new string to clipboard.
zipdir Creates a zipfile containing the folders and files within a directory tree.

*Code can be altered relatively easily to remove dependency.

Details of src/graeScript

Writing of the details is a work-in-progress. Last edit: 2022-02-11

Created to simplify obtaining the HEX and RGB value for the HTML Color Names. Additions were later made to simplify obtaining all HTML color names and finding a desired color name by color group.

Get a tuple from a string RGB value.

Arg:

  • s: str

Return values related to all HTML Color Names.

HtmlColors.all -> list[sqlite4.Row]

Return the color group, name, RGB value, and Hex values for every HTML color name.

HtmlColors.names -> list[str]

Return all HTML color names.

HtmlColors.map_to_name -> dict[sqlite3.Row]

Map HtmlColors.all to the HTML color names.

HtmlColors.map_to_group -> dict[list[sqlite3.Row]]

Map HtmlColors.all to the HTML color names.

color_value() -> tuple[str]

The first item in tuple is properly formatted HTML color name, and the second item is the color's RGB or HEX value depending on format.

Args:

  • format: str
  • colorname: str

Prints or copies to clipboard the properly formatted HTML color name color, and the color's RGB or HEX value depending on format.

Args:

  • color: str
  • format: str
  • to_clipboard: bool = False

If to_clipboard is True, the RGB/HEX value is also copied to the clipboard.

Print or copy to the clipboard all HTML color names in color_group.

Args:

  • color_group: str
  • to_clipboard: bool = False

A collection of functions related to moving and deleting files and folders. Initially was one function to merge folders within a directory tree, but was refactored into this. Still somewhat a work-in-progress.

All file-changing functions print the changes made.

All file-changing functions have a parameter called 'test', that defaults to True and causes the function to make no file changes.

Rename source file to destination file.

Args:

  • source: Path | str
  • destination: Path
  • test: bool = True

Deletes file.

Args:

  • file: Path | str
  • test: bool = True

Deletes dir if it is empty.

Args:

  • dir: Path | str
  • test: bool = True

Deletes empty folders within directory's tree.

Args:

  • dir: Path | str
  • test: bool = True

Prints changes made

Deletes files in directory dir based on the glob pattern in glob_pattern.

Args:

  • dir: Path | str
  • glob_pattern: str
  • test: bool = True

move_contents() -> list[dict]

Move all of source directory's contents into destination.

Args:

  • source: Path | str
  • destination: Path
  • test: bool = True
  • *args: str

*args takes either 'replace', 'delete', 'compare', or None which dictates how move conflicts will be handled.

  • 'replace': replace file in destination with the conflicting one from source
  • 'delete': delete file in source
  • 'compare': compares the last edit date of the files in conflict, keeping whichever has the most recent date. (Either moves or deletes source)
  • If no argument is passed for *args, this function returns details about files that weren't moved and their matching destination file.

In start_dir, combine folders when there is a 2 similar folders, 1 matching destination_pattern' and another matching folder1's match.group(match_group_num).

For example, if you have a folder full of music organized music// in which some artist have duplicate album folders with different, but similar names. (For example, music/Real Artist/1996 - MTV Unplugged (live) and music/Real Artist/MTV Unplugged.) This would merge any similar folders found in each album/ directory.

  • Leading underscore because hasn't been run/tested since parts of it were refactored into functions listed above.

Args:

  • start_dir: Path | str
  • dest_pattern: str
  • match_group_num: int
  • test: bool = True
  • *args: str

Continuing above example:

_walk_and_combine(start_dir='music', dest_pattern=r'(\d{2,4} - )(.*)( live)', match_group=1, test=False)

For every artist folder in music, this function:

  1. looks for an album folder that matches the entire regex in destination_pattern (we'll call this folder destination),
  2. looks for an album folder matching destination's match.group(1) (we'll call this folder source),
  3. if both are found, the fucntion moves the files from source into destination, using *args to determine how to handle conflicts, and
  4. if source is empty after the move, the function deletes the source folder.

*args takes either 'replace', 'delete', 'compare', or None which dictates how move conflicts will be handled.

  • 'replace': replace file in destination with the conflicting one from start_dir
  • 'delete': delete file in start_dir
  • 'compare': compares the last edit date of the files in conflict, keeping whichever has the most recent date. (Either moves or deletes start_dir)
  • If no argument is passed for *args, then any conflict files will not be moved.

Edit a given file name or file names in a given directory / directory tree.

Rename file by replacing it's existing extension with new_ext.

Args:

  • file: Path | str
  • new_ext: str
  • Test: bool = True

Rename files in dir by replacing their existing extension with new_ext.

Args:

  • dir: Path | str
  • new_ext: str
  • replace_ext: str = '*.*'
  • Test: bool = True

If replace_ext is uses, then only the files with replace_ext will be renamed

Search computer for certain files.

search_for() -> list[Path]

Search the directory tree dir and returns files matching glob.

Args:

  • dir: Path | str
  • glob: Path | str

found_files_to_text() -> list[Path]

Call search_for() and output found file paths to txt_file.

Args:

  • dir: Path | str
  • glob: Path | str
  • txt_file: Path | str

Search computer for certain folders.

Searches computer directory for picture folders.

Args:

  • start_dir: str

From Automate the Boring Stuff with Python by Al Sweigart Chapter 17

From input text, output a markdown list.

Create a markdown bulleted list from text.

Args:

  • text: str
  • as_todo: bool = False

If as_todo is True, bullets are written as '- [ ]' instead of '-'.

Determines list items and level by number of spaces of indentation.

Uses bullet_list, taking text from clipboard.

Args:

  • text: str

  • as_todo: bool = False

  • Requires Pyperclip

From input text, print out a markdown table.

table() -> None

Prints a markdown table of rows.

Args:

  • rows: list | tuple
  • align: str = 'l'
  • center_header: bool = True
  • col_padding: int = 0
  • header_row: list = []

Create PDFs from existing PDFs.

Obtain the text from a PDF.

Functions related to converting numbers from one base to other base(s).

Convert a decimal, binary, hexadecimal, or octal to an integer.

Args:

  • digit: int | str

Obtain decimal, hexadecimal, and binary value for an integer decimal.

Args:

  • digit: int | str
  • include_prefix: bool = False
  • hex_case=str.upper

Prints markdown number conversion table from start_num through stop_num.

Args:

  • start_num: int | str
  • stop_num: int | str
  • include_prefix: bool = False

Can be used in terminal py isleap.py <year> and prints message to inform if year is a leap year or not.

isleap() -> Bool

Returns True if year entered is a leap year.

Args:

  • year: int

Merge text files.

Merge two text files in which each line contains one word. (Made to merge CSpell dictionary text files).

Args:

  • file1: str
  • file2: str

Several classes for validating class attribute values.

From Python Documentation: HOWTO Descriptors

class Validator

A validator is a descriptor for managed attribute access. Prior to storing any data, it verifies that the new value meets various type and range restrictions. If those restrictions aren't met, it raises an exception to prevent data corruption at its source. - PyDocs

class String(Validator)

String verifies that a value is a str. Optionally, it validates a given minimum or maximum length. It can validate a user-defined predicate as well. - PyDocs

class Number(Validator)

Number verifies that a value is either an int or float. Optionally, it verifies that a value is between a given minimum or maximum. - PyDocs

class OneOf(Validator)

OneOf verifies that a value is one of a restricted set of options." - PyDocs

About

A collection of scripts I made for my own personal use/convenience.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages