Package: autotagical
autotagicalautotagicalis a utility to automagically rename and sort tagged files (such
as those produced byTagSpaces)
according to user-defined schemas.  It reads in tagged files from one of more
input directories then renames and/or moves them to an output folder hierarchy
according to rules specified in user-provided schemas.  It is intended for use
in concert with file tagging software, e.g.TagSpaces.Getting Started/Installationautotagicalmay be most easily installed withpipby running:pipinstallautotagicalIf you'd like to runautotagicalby cloning this repository, then you'll need
to install the following requirements, e.g. withpip:setuptoolsjsonschema>=3packagingUsing autotagicalUsageautotagical[-h][-V][-C<configfile>][-H][-i<inputpath>][-I<ignorefile>][-R][-o<outputpath>][-O][-g<taggroupfile>][-s<schemafile>][-A][--cleanin][--cleanout][-c][-F][-k][-m][-M][-n][-N][-t][--debug][-l<logfile>][-L][-P][-q][-v][--force][--yes]Help OptionsThese options display helpful information and exit.[-h/--help]-- Display a help/usage message and exit.[-V/--version]-- Display the current version and information about known
file formats and exit.Configuration Options[-C/--config <config file>]-- Loads the config file at the specified path.Input OptionsThese options determine behavior loading in files to be moved and/or renamed.
At least one input folder must be specified.[-H/--hidden]-- Process hidden files (and directories, if -R is specified).[-i/--input <input folder>]-- Path to a folder with input files.  May be
specified more than once.[-I/--ignore <ignore file>]-- Path to file patterns (regex format) to
ignore (each on new line).  May be specified more than once.[-R/--recursive]-- Load files recursively from input folders, i.e. descend
into subfolders.Output OptionsThese options determine behavior in outputting move/renamed files (but do not
specify the rules by which they are to be moved and/or renamed).  At least one
output folder must be specified (by one option or the other).[-o/--output]-- Path to a root folder to output files to.  May be specified
more than once (output will be duplicated to each).[-O/--organize]-- Organize files in place (i.e. use the first input folder
for output).  Often used with -R.Schema OptionsThese options specify the rules for moving/renaming files and are the heart ofautotagical.  Information on the structure of these files may be found below
in theTag Group FormatandSchema Formatsections.[-g/--groups <tag group file>]-- Path to a file to read tag groups from.
May be specified more than once (groups will be combined).  Files may be in
either theTagSpacesor theautotagicalformat.[-s/--schema <schema file>]-- Path to a schema file to move/rename files
based on.  May be specified more than once, in which case rules are
prioritized in the order files are specified.Functionality OptionsThese options tweak the default functioning ofautotagicalas specified
below, typically adjusting how various circumstances are dealt with.[-A/--allmatchroot]--  Makes the root of an output folder match all tags,
i.e. every single file will be moved to the output folder, even if it does not
match anywhere more specifically.  Use of this option is bad practice
(consider using the/*|operator as a root filter instead), but it is
provided for the user's convenience.  This option doesnotimply-M,
i.e. files that could not be renamed will not be moved to the root folder
just because-Ais set.[--cleanin]-- Clean up (delete) all empty folders in the input folder/s.
Thiswillrecurse, whether or not the-Rflag is set.[--cleanout]-- Clean up (delete) all empty folders in the output folder.
Thiswillrecurse, whether or not the-Rflag is set.[-c/--clean]-- Clean up (delete) all empty folders in both input and output
folders.[-F/--failforcerename]-- This option flags failing to rename a
manually-named file that is being forcibly renamed due to the-Noption
should be considered a failure to name the file.  That sounds complicated, but
consider these cases:[-F]-- Normal behavior (option will be ignored).[-N]-- Force rename manually-named files, but manual names are "good
enough" and manually-named files that cannot be renamed will be moved.[-F -N]-- Force rename manually-named files and treat failures as
failures.  Manually-named files that cannot be renamed willnotbe moved.[-N -M]-- Force rename manually-named files.  All files will be moved.[-F -N -M]-- Equivalent to-N -M.-Fhas no effect.[-k/--keep]-- Keep original files in the input folders untouched, i.e copy
files to their new destinations rather than move them.[-m/--move]-- Only move files into a directory structure, do not try to
rename them.[-M/--moveall]-- Move all files, not only ones that are manually-named/
successfully renamed.[-n/--name]-- Only rename files, do not try to move them into any directory
structure.  All files will be placed in the root of the output folder.[-N/--renamemanual]-- Forcibly try to rename manually-named files, not just
unnamed ones.[-t/--trial]-- Trial run.  Do not actually move or rename files, just log
what would happen.  Combine with-vto check output before live run.Using this is good practice,especially after making any changes to a
schema or options.  The-toption ensures no changes will be reflected to
disk whatsoever.Logging OptionsThese options tweak what sorts of messages are displayed whenautotagicalis
run (and whether to save them or just print them to the console).[--debug]-- Display absolutely everything.  You should probably never use
this.[-l/--log <log file>]-- Output messages to the specified file rather than
just the console.  By default, messages will be appended to the end of the
file.[-L/--overwritelog]-- Overwrite the specified log file, rather than append
to it.  Has no effect without-l.[-P/--posix]-- Silence warnings specific to Windows.  Use thisonlyif
the files are never to be used with Windows systems (which are pickier about
what file names can contain).[-q/--quiet]-- Silence all warnings and only display actual errors.  Use of
this isnot recommended,as warnings are typically printed for good
reason.[-v/--verbose]-- Print all actions taken.  This will list every file
movement/renaming, rather than merely warning about failures.  Most useful
when combined with-tto check that a schema is doing what one wants before
running it for real.Unsafe OptionsDo not use these unless you have very good reason to.Data loss can occur.[--force]-- Forcibly move/rename files, even if there is a file or
directory in the way.This will clobber files;use at your own risk, as
data loss can occur.[--yes]-- Assume "yes" for all user prompts.  This implies--forceandwill clobber files and directories.Use at your own risk, as data loss
can occur.Setting Priorityautotagicalwill always preferentially use settings specified in the
following order, from highest to lowest, with each overriding any settings from
lower priority sources.  Note that onlyoneconfig file will be loaded (the
first by priority).Command-line arguments.Config file (first found from below).Config file loaded using-C/--configargument..autotagrcfile in output folder (first folder containing one is used)..autotagrcfile in input folder (first folder containing one is used)..autotagrcfile inautotagicalmodule folder.Config File Format.autotagrc(or any config file specified via command-line) should have the
same format as any command line arguments one would otherwise care to pass.Unsafe options will be ignored in config files,i.e.--forceand--yeswill have no effect if set in a config file.  This is to prevent data loss
without explicit user input.  Whitespace and newlines are ignored, e.g. one
might write:-H-Pin a config file to process hidden and ignore Windows-specific warnings.Tag Group Formatautotagicalis capable of reading the JSON files produced by exporting tag
groups fromTagSpaces.  Alternately,
tag groups may be defined in a more simple, somewhat more human-readable
fashion in JSON.  Thisautotagicaltag group format supports additional
features not available in the TagSpaces format, detailed below.{"file_type":"autotagical_tag_groups","tag_group_file_version":"1.1","tag_groups":[{"name":"tag group 1","tags":["tag1","tag2"...]},{"name":"tag group 2","tags":["tag3","/G|tag group 1","/RE|regex 1",...]},]}InheritanceIn theautotagicalformat, tag groups support simple inheritance, where a
child tag group may be defined in terms of one or more parent tag groups (as
well as any additional tags).  Inheritance is indicated by prefixing a group
name with/G|in thetagsproperty of a group.  In such cases, the child
group will inherit all tags in any of the parent groups.  This is useful for
simplicity; it ensures that one only has to manually define the most "leaf" tag
groups while still writing filters based on more broad groups, all of which
will update if the more refined groups are updated.  Consider the following
simple use case:{"file_type":"autotagical_tag_groups","tag_group_file_version":"1.1","tag_groups":[{"name":"American Styles","tags":["ipa","dipa","pale_ale"]},{"name":"Belgian Styles","tags":["witbier","dubbel","tripel"]},{"name":"Beer","tags":["/G|American Styles","/G|Belgian Styles"]}]}Here, we have defined theBeergroup in terms ofAmerican StylesandBelgian Styles, rather than having to specify all 6 tags that should fall
under it.  Not only is this quicker to write, but it prevents errors of
oversight when one decides to add tags later.  If, for example,quadrupelis added to theBelgian Stylesgroup, it will be part of theBeergroup
without one having to remember to manually add it in two places.  While this
simple case doesn't seem that unmanageable without inheritance, the number of
places where duplicate tags have to be added can quickly spiral out of control
with multiple levels of tag group.Keep in mind the following details about inheritance:Multiple inheritance-- Tag groups can inherit from multiple parent
group (as shown in the example).Multilevel inheritance-- Tag groups support multilevel inheritance,
i.e. anAlcoholgroup might inherit fromBeerwhich inherits fromAmerican Styles.The (lack of) diamond problem-- As there is no overriding in tag group
inheritance, the diamond problem (where a grandparent is inherited via two
different routes) is handled without problem.Circular inheritance-- It is okay for two groups to inherit from each
other (whether via intermediaries or not).  Each inheritance path will
only be followed once, i.e. a tag group will stop "following" an
inheritance path if it is instructed to inherit from itself.Flexible ordering-- Tag group inheritance is flexible in the order that
groups are defined.  There is no need for a tag group to be located after
a group it inherits from.  In fact, tag groups can inherit from groups in
completely different tag group files, regardless of the order they are loaded
in (so long as they are both loaded).Regex Tag GroupsTheautotagicaltag group format supports defining tag groups in terms of
regexes.  This can be incredibly powerful in certain situations.  For example,
consider tagged bank statements.  Rather than having to manually add every
account number tag to an "Account Number" group, one can instead define a tag
group as follows:{"name":"Account Number","tags":["/RE|(?:xx|\\*\\*)[0-9]{4}"]}This will make any tag of the form**1234orxx1234match the "Account
Number" tag group.  If one deals with a large number of tags with a standardized
form, this can be extremely valuable, especially if they change frequently.Keep in mind the following details about regex tag groups:Full match-- Regexes are used withre.fullmatch()and as such must
match 100% of the tag name.Inheritance-- Regexes will be inherited as with any other tags in a
group.Mixed and matched-- A tag group can be defined in terms of any
combination of inheritance, normal tags, or regexes.  They do not need to be
specified solely in terms of regexes.Tag in group (/?TIG|) operator-- Regex tag groups work normally with
the tag in group format string operator.  The first tag to match the regex
will be returned for it.Schema FormatA schema is defined in a human-readable fashion in JSON and should consist of a
single object as follows:{"file_type":"autotagical_schema","schema_file_version":"1.1","tag_formats":[],"unnamed_patterns":[],"renaming_schemas":[],"movement_schema":[]}Each of these four keys should be assigned to an array, the structure of which
is described in the following sections.tag_formats"tag_formats":[{"tag_pattern":"Regex containing groups: file, raw_tags, tags, and extension.","tag_split_pattern":"Regex to split tags with"},...]The various patterns must be valid Python regexes.  "tag_pattern" will be used
withre.fullmatch()and should match the entirety of a file name and contain
the following named groups:file-- Matches the original file name (without tags and extension).raw_tags-- Matches the entirety of tag data to be preserved, including any
delimeters/demarcating characters.tags-- Matches only the tags themselves and any separating characters.extension-- Matches the file extension (if any)."tag_split_pattern" should match whatever delimiter separates individual tags
and will be used withre.split().  An example is provided below, which
matches the tag format used byTagSpaces:"tag_formats":[{"tag_pattern":"(?P<file>.+)(?P<raw_tags>\\[(?P<tags>.+?)\\])(?P<extension>.*?)","tag_split_pattern":"\\s+"}]More than one such set of patterns may be provided in a singletag_formatsarray, allowingautotagicalto deal with files tagged in multiple formats in
one run.unnamed_patterns"unnamed_patterns":["regex pattern 1","regex pattern 2",...]The various patterns must be valid Python regexes.  Each will be used withre.match()and should match the file names (less tags) of such files to be
treated as requiring renaming.  They will be matched against the concatenation
of thefileandextensiongroups produced by the use of "tag_pattern"
above, so do not attempt to match against anything not captured by those two
groups.  An example is provided below, which might match PDF files with
timestamps produced by two different scanners:"unnamed_patterns":["[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}\\s*.pdf","(Pages\\sfrom)?\\s*XScan_[0-9]{14}\\s*.pdf"]renaming_schema"renaming_schemas":[{"filter":["condition 1","condition 2",...],"format_string":"file name format string"},...]"renaming_schemas" is simply a list of filters and an explanation of how to
name files matching any of them.  This list is ordered, and files will be
renamed according to the first they match, e.g. if a file hastag1andtag2and the first filter in the array matchestag2and the secondtag1, the
file will be renamed according totag2.  This allows one to define priorities
of renaming.  See theFilterssection for information on filters,
conditions, and the various operators one may include in them."format_string" defines how to rename files matching the filter.  It cannot
contain the/character except where it denotes operators (as
file names cannot contain/).  At its simplest, it is simply a string that
the file will be renamed to, but that string may include any number/combination
of operators.  See theFormat Stringssection for
information on format strings and the various operators one may include in them.movement_schema"movement_schema":[{"filter":["condition 1","condition 2",...],"subfolder":"<format string 1>","sublevels":[{"filter":["condition 3","condition 4",...],"subfolder":"<format string 2>","sublevels":[...]},...]},...]"movement_schema" defines an output folder hierarchy iteratively by nesting
filters.  At each folder level, multiple filters can exist that either pass
files to lower subfolders or place them at the current level.  These lists are
ordered, and files will be sorted according to the first filter they match,
e.g. if a file hastag1andtag2and at a level the first filter in the
array matchestag2and the secondtag1, the file will be sorted according
totag2.  This allows one to define priorities of sorting.  See theFilterssection for information on filters, conditions, and the
various operators one may include in them.If "subfolder" contains a format string, it will be interpreted and added to
the folder hierarchy that the file will be placed in (and further sorted based
on "sublevels"); if it is left blank"", files will be placed in the current
(in the hierarchy) directory without further sorting.  If "sublevels" is left
empty[], files will be placed in the specified subfolder without further
sorting.  Note that a movement schema does not have to have a path for every
possible file.  Files that fail to "find a home" will be left in the input
folder and a warning will be printed (unless-Ais specified).  See theFormat Stringssection for information on format strings and
the various operators one may include in them.Additionally, note that complete hierarchies (i.e. those that terminate with
explicitly placing the file in a folder) will be preferred over partial
hierarchies (i.e. if a file percolates some distance down a hierarchy and then
matches no filters).  In the event that no complete hierarchy can be found, the
first partial onewillbe used to move the file.  It is bad practice to rely
on this behavior though; one should use the/*|operator if one explicitly
wishes absolutely any file that reaches a filter level to reside there.FiltersA filter (wherever it might show up in a schema) is defined by an array of
condition sets.  These condition sets are combined in the logical sense byinclusive or, i.e. matching at least one condition set is necessary and
sufficient to match the overall filter.  At their simplest, a condition set may
simply be a tag, e.g."filter": ["tag1", "tag2"]will match any file with
eithertag1ortag2(or both) on it.  However, the following operators may
be used to construct more complex condition sets (whether in filters or in the
conditional/?|operator):/G|-- The prefix/G|is used to denote a tag group instead of a tag
name, e.g."filter": ["/G|Group 1", "tag2"]will match any file with at
least one tag inGroup 1or the tagtag2(or both)./*|-- The all operator/*|matches all files, regardless of how they are
tagged./&|--/&|is a logical "and" operator, requiring matching both
conditions, e.g."filter": ["tag1/&|tag2"]will match files that havebothtag1andtag2.  A condition may contain any number of/&|operators,
e.g. one may create the condition"tag1/&|tag2/&|/G|Group 1"./!|-- The not prefix/!|negates the next condition.This prefix must
come before any others logically,i.e. you must write/!|/G|<group>rather
than/G|/!|<group>or/!|/*|rather than/*|/!|.  The/!|operatorcanfollow the logical "and" operator/&|, e.g."<tag1>/&|/!|<tag2>",
which will match any file that hastag1and does not havetag2.There are no (realistic) limits on the degree to which these operators may be
combined or how many condition sets a filter might have.Format StringsA format string is simply a string that may or may not contain various
operators.  These operators will be replaced with the corresponding data when
the format string is interpreted./EXT|-- Anywhere it is put in the format string,/EXT|will be replaced
with the original extension of the file, as defined by theextensiongroup
in the "tag_formats" regex that matched the file.  This is obviously useful if
you're renaming multiple types of file and want to preserve extensions.  You
will almost always want to end your format string with/EXT|./FILE|-- Anywhere it is put in the format string,/FILE|will be
replaced with the original name of the file, as defined by thefilegroup in
the "tag_formats" regex that matched the file./TAGS|-- Anywhere it is put in the format string,/TAGS|will be
replaced with the tags on the original file.This is necessary to avoid
your renamed files becoming de-taggedand should almost always be included
in a renaming format string./?|<condition>/T|<true text>/F|<false text>/E?|-- The conditional operator/?|allows for conditional naming.  If<filter>is matched, the entire
expression will be replaced with<true text>; if it does not match, the
entire expression will be replaced with<false text>.  Either text (or both,
but why would you) may be empty.  The conditional operator can take anything
that can be in a filter condition.  See theFilterssection for
information on filters, conditions, and the various operators one may include
in them.<true text>and<false text>may contain other operators, i.e./EXT|,/FILE|,/TAGS|, and/ITER|.  Note: conditional operatorscannotbe nested or contain/?T|<tag>/|or/?G|<tag group>/|within
replacement text./?T|<tag>/|-- The tag conditional operator/?T|will insert the literal
name of the tag<tag>if it is present on the file.  Note that this is
equivalent to/?|<tag>/T|/<tag>/F|/E?|; it is merely a shortcut./?G|<tag group>/|-- The tag group conditional operator/?G|will insert
the literal name of the tag group<tag group>if one of its tags is present
on the file.  Note that this is equivalent to/?|/G|<tag group>/T|<tag group>/F|/E?|; it is merely a shortcut./?TIG|<tag group>/|-- The "tag in group" operator is a special operator
that will insert a tag on the file that is in the specified tag group.  For
example,/?TIG|group1?/|will resolve totag1iftag1is on the file and
ingroup1, ortag2iftag2is on the file and ingroup1.  If multiple
tags on the file are in the group, the first (in tagging order, left-to-right)
will be substituted in.  If the file lacks a tag in the group, the entire
operator will simply be blank.  This "tag in group" operator is most useful
for writing flexible schema where the exact tags cannot be predicted in
advance.  For example, if one wanted to sort bank statements into folders
based on what account they are associated with, one could use the "tag in
group" operator with an "Account Number" group and then only have to update
the tag group with the various account number tags, rather than having to
specify every account number in the schema.  As an example, consider the
following cases, withGroup 1 = tag1, tag3andformat_string = "Tag: /?TIG|Group 1/|":File [tag1, tag2]-- "Tag: tag1"File [tag2, tag4]-- "Tag: "File [tag1, tag2, tag3]-- "Tag: tag2"/ITER|<text>/#|<other text>/EITER|-- The/ITER|operator is complicated
but important.  It is invoked only in the event that multiple files are going
to be renamed to the same name.  In this case, the text is placed in the file
name, along with/#|being replaced by the n-th file that this is that has
had the same name.  Otherwise, the entirety of the/ITER|operator is
ignored.The /ITER| operator should not be used in folder name format
strings.  It will be ignored.In essence, the/ITER|tag "counts" how many times the same file name has
been produced.  It is good practice to always include an/ITER|operator in
your schema to avoid files not being renamed due to potential clobbering./#|may appear more than once in an/ITER|operator, but there is usually
no need to.  The/ITER|operatormaycontain any other operator, including
the conditional operator/?|, but may not be nested.  Note that the/ITER|operatorwill not be usedif files end up with thesame name but
different output directories.  It will only appear if necessary to avoid
clobbering.  An example will make this easier to understand.  Consider the
format stringWidget/ITER| /#|/EITER|in the following cases:1 matching file -- The file will be namedWidget.3 matching files in same folder -- The files will be namedWidget 1,Widget 2, andWidget 3.3 matching files, each in a different folder -- The files will all be namedWidget.Known IssuesOnly POSIX hidden files are considered hidden, i.e. those that begin with a.dot, not those hidden as Windows does it.  This is most likely aWon’t
Fix.Testsautotagicalmay be tested by cloning this repository and running:pythonsetup.pytestfrom within the root directory.  Note that this may requirepython3instead
ofpython, depending on yourpythoninstallation.AuthorsSiriusStarrLicenseThis project is licensed under the GNU General Public License v3.0 - see theLICENSE.mdfile for details