Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2014 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/11152139 to your computer and use it in GitHub Desktop.
Save anonymous/11152139 to your computer and use it in GitHub Desktop.
#!python
"""
except when specified dual licensed
under simplified BSD license
Copyright (c) 2014, hownoemail
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
or
under CC BY-SA 3.0
author hownoemail
https://creativecommons.org/licenses/by-sa/3.0/
"""
import sys
import os
import argparse
pre = [
'$INIT$',
'$OVER$',
'$RESET$',
]
class readable_dir(argparse.Action):
"""
from
https://stackoverflow.com/questions/11415570/directory-path-types-with-argparse/11415816#11415816
under CC BY-SA 3.0
https://creativecommons.org/licenses/by-sa/3.0/
author mgilson
https://stackoverflow.com/users/748858/mgilson
"""
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir = values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
"readable_dir:{0} is not a valid path".format(
prospective_dir))
if os.access(prospective_dir, os.R_OK):
setattr(namespace, self.dest, prospective_dir)
else:
raise argparse.ArgumentTypeError(
"readable_dir:{0} is not a readable dir".format(
prospective_dir))
def organize(args=None):
source = args.directory
print 'processing directory {0}'.format(source)
process_directory(source, initial=True, args=args)
def process_directory(source, initial=False, args=None, indent=''):
ret = open(source + '/CharacterFolders.txt', 'w')
if initial:
# handle standard directories
for f in pre:
ret.write('{0}:{0}\n'.format(f))
# first write the directories with more mods
for f in os.listdir(source):
if os.path.isdir(os.path.join(source, f)) and f not in pre:
if 'CharacterFolders.txt' in os.listdir(os.path.join(source, f)):
ret.write('[{0}]\n'.format(f))
if args.verbose:
print '{1}found directory [{0}]'.format(f, indent)
if args.recursive:
process_directory(
os.path.join(source, f), args=args,
indent=indent + '\t')
# standard mods
for f in os.listdir(source):
if os.path.isdir(os.path.join(source, f)) and f not in pre:
if 'CharacterFolders.txt' not in os.listdir(os.path.join(source, f)):
ret.write('{0}:{0}\n'.format(f))
if args.verbose:
print '{1}found mod {0}'.format(f, indent)
def do_main():
original_source = os.path.dirname(os.path.abspath(__file__))
action_choices = ['organize']
parser = argparse.ArgumentParser(
description='Run Mods Directory Organizer.')
parser.add_argument(
'-d', metavar='directory',
dest='directory', default=original_source, action=readable_dir,
help='directory with the mods (default=directory of this file)'
)
parser.add_argument(
'-v', action='store_true', dest='verbose',
help='verbose, print more data')
parser.add_argument(
'-r', action='store_true', dest='recursive',
default=True,
help=('recursive, go into directories with a '
'CharactersFolder.txt file, on by default'))
parser.add_argument(
'-s', action='store_false', dest='recursive',
default=True,
help=('shallow, DON\'T go into directories with a '
'CharactersFolder.txt file'))
parser.add_argument(
'action', help='action to take', default='organize',
nargs='?',
choices=action_choices)
args = parser.parse_args()
if args.action not in action_choices:
parser.print_help()
elif args.action == 'organize':
organize(args=args)
if __name__ == '__main__':
do_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment