Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2009 22:24
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/113184 to your computer and use it in GitHub Desktop.
Save anonymous/113184 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Gattai v0.1
# Description: simple filesorter (rules from config file)
# Copyright: George Shuklin, 2009
# License: GNU General Public License 2 or higher
# Usage: edit to replace test.conf with actual config file (functionality coming later) and run
__doc__="""
config file format:
@mail - email (not implemented)
@target - place for subfolders (where to sort)
@source - files to be sorted (no subfolders)
any-non-indented text - begin of config section. text - folder to put selected files
indened text (after non indeded):
\t match "subsring" - file match if substring in filename (case insensitive)
\t except "substring" - file does not match if subsrting in filename (ever "match" or "regex-match" are matched), case insensitive
\t regex-match "regex" - file match if regular expression (regex) match (case sensetive)
\t regex-except "regex" - file does not match if regex match (same, as "except"), case sensitive
SAMPLE:
@target /anime/ongoing
@source /anime/incoming
Bleach
match bleach
regex-except .*rus.*
regex-except .*LQ.*
regex-except .*\.rmvb$
new ongoing/Hagane no Renkinjutsushi (2009)
match alchemist
regex-match .*Hagane.no.*
except Shambala
except thora
"""
import re
import os
import shutil
def config_read(filename):
config={}
global_config_params=("@mail","@target","@source")
single_config_params=("complete", "prefer-subber")
list_config_params=("regex-match","regex-except","match", "except")
for config_line in file(filename,"r").readlines():
config_line=config_line.rstrip()
if not config_line: continue
if config_line[0]=='#': continue
if config_line[0]=='\t':
if not target: raise "No target dir (tab at first position)"
(param_name,param_value)=config_line.split(None,1)
if param_name.lower() in single_config_params:
config[target][param_name]=param_value
elif param_name.lower() in list_config_params:
config[target][param_name]=( config[target].get(param_name) or [] ) + [param_value]
else:
print "unknown parameter", config_line
raise "oops"
continue
if config_line[0]=="@":
(param_name,param_value)=config_line.split(None,1)
if param_name.lower() in global_config_params:
config[param_name]=param_value
else:
print "unknown parameter", config_line
raise "oops"
else:
target=config_line
config[target]={}
continue
return config
def ismatch(line, regext_match,str_match, regex_except, str_except):
l_line=line.lower()
for s_m in str_match or []:
if s_m.lower() in l_line:
for s_e in str_except or []:
if s_e.lower() in l_line:
return False
return True
for r_m in regext_match or []:
if re.match(r_m,line):
for r_e in regex_except or []:
if re.match(r_e,line): return False
return True
return False
def findmatch(line,conf):
for a in conf.keys():
if a[0]=="@": continue;
c=conf[a]
if ismatch(line,c.get("regex-match"), c.get("match"),c.get("regex-except"), c.get("except")):
return a
return None
config=config_read("test.conf")
source=config.get("@source") or "."
target=config.get("@target") or "."
mail=config.get("@mail")
#log=[]
filenames=filter(lambda f:os.path.isfile(os.path.join(source,f)), os.listdir(source))
for f in filenames:
m=findmatch(f,config)
if m:
mv_from=os.path.join(source,f)
mv_to=os.path.join(os.path.join(target,m),f)
try:
shutil.move(mv_from,mv_to)
#log.append("[OK ] Moving %s to %s\n" % (f,m))
print "[OK ] Moving %s to %s" % (f,m)
except:
#log.append("[ERROR] Moving %s to %s\n" % (mv_from,mv_to))
print ("[ERROR] Moving %s to %s" % (mv_from,mv_to))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment