Skip to content

Commit

Permalink
Merge pull request #4 from pineapplemachine/develop
Browse files Browse the repository at this point in the history
After an arduous and very lengthy review process I have chosen to accept your merge request thank you for all your hard work would you like a raise
  • Loading branch information
pineapplemachine committed Jun 22, 2015
2 parents 1d30c4a + dc73511 commit 20de7c6
Show file tree
Hide file tree
Showing 21 changed files with 1,900 additions and 233 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
*.DS_Store
*.pyc
*.xcf
*test.py

# Manager logs go here
logs/*

# Outputted raws go here (for my personal testing and stuff anyway)
output/*
backup/*

# Older DF versions go here (for my help figuring version compatibility)
refs/*

# Stuff that's just not meant to be committed right now
build/*
13 changes: 8 additions & 5 deletions config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ def setuplogger(self):
stdouthandler.setFormatter(logging.Formatter('%(asctime)s: %(levelname)s: %(message)s', datetimeformat))
pydwarf.log.addHandler(stdouthandler)
# Handler for log file output
logfilehandler = logging.FileHandler(self.log)
logfilehandler.setLevel(logging.DEBUG)
logfilehandler.setFormatter(logging.Formatter('%(asctime)s: %(filename)s[%(lineno)s]: %(levelname)s: %(message)s', datetimeformat))
pydwarf.log.addHandler(logfilehandler)
if self.log:
logdir = os.path.dirname(self.log)
if not os.path.exists(logdir): os.makedirs(logdir)
logfilehandler = logging.FileHandler(self.log)
logfilehandler.setLevel(logging.DEBUG)
logfilehandler.setFormatter(logging.Formatter('%(asctime)s: %(filename)s[%(lineno)s]: %(levelname)s: %(message)s', datetimeformat))
pydwarf.log.addHandler(logfilehandler)

def setuppackages(self):
self.importedpackages = [importlib.import_module(package) for package in self.packages]
Expand All @@ -76,7 +79,7 @@ def setupversion(self):
pydwarf.log.info('Unable to detect Dwarf Fortress version.')
else:
pydwarf.log.info('Detected Dwarf Fortress version %s.' % self.version)
elif conf.version is None:
elif self.version is None:
pydwarf.log.warning('No Dwarf Fortress version was specified. Scripts will be run regardless of their indicated compatibility.')
else:
pydwarf.log.info('Managing Dwarf Fortress version %s.' % self.version)
Expand Down
51 changes: 51 additions & 0 deletions docs/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Disclaimer: This is a shitty WIP

import sys
import os

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))

import inspect

import raws
import pydwarf

output = 'index.html'

items = {
'raws.token': raws.token,
'raws.file': raws.file,
'raws.dir': raws.dir,
'raws.color': raws.color,
'raws.tokenfilter': raws.tokenfilter,
'raws.boolfilter': raws.boolfilter,
'pydwarf.session': pydwarf.session,
'pydwarf.urist': pydwarf.urist
}

alldoc = {}
for itemname, item in items.iteritems():
itemdoc = {}
alldoc[itemname] = itemdoc

for membername, member in inspect.getmembers(item):
memdoc = {}
itemdoc[membername] = member

with open(output, 'wb') as html:
css = 'pre { background-color: #ddd; color: #113 }'
html.write('<html><head><title>%(title)s</title><style>%(style)s</style></head><body>' % {'title': 'PyDwarf docs', 'style': css})
html.write('<h1 style="font-size: 50; color: #800">Warning: This is a shitty WIP.</h1>')
for itemname in sorted(alldoc.iterkeys()):
itemdoc = alldoc[itemname]
html.write('<h1>%s</h1>' % itemname)
for membername in sorted(itemdoc.iterkeys()):
member = itemdoc[membername]
if callable(member) and membername not in ('__module__', '__str__', '__repr__', '__doc__'):
html.write('<p><b>%s</b><br><pre>%s</pre></p>' % (membername, member.__doc__))
html.write('</body></html>')





951 changes: 951 additions & 0 deletions docs/index.html

Large diffs are not rendered by default.

Binary file added images/icon.ico
Binary file not shown.
8 changes: 4 additions & 4 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import pydwarf
import raws
from config import config
from utils import copytree



__version__ = '1.0.0'
__version__ = '1.0.1'



Expand All @@ -20,7 +19,8 @@

def getconf(args=None):
# Load initial config from json file
conf = config().json(jsonconfigpath)
conf = config()
if os.path.isfile(jsonconfigpath): conf.json(jsonconfigpath)

# Default name of configuration override package
overridename = 'config_override'
Expand Down Expand Up @@ -90,7 +90,7 @@ def __main__(args=None):
if conf.backup is not None:
pydwarf.log.info('Backing up raws to %s.' % conf.backup)
try:
copytree(conf.input, conf.backup)
raws.copytree(conf.input, conf.backup)
except:
pydwarf.log.error('Failed to create backup.')
exit(1)
Expand Down
2 changes: 1 addition & 1 deletion pydwarf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from response import *
from urist import *

__version__ = '1.0.0'
__version__ = '1.0.1'
2 changes: 1 addition & 1 deletion pydwarf/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def __init__(self, success, status):
self.status = status

def __str__(self):
return '%s: %s' % ('SUCCESS' if response.success else 'FAILURE', self.status if self.status else ('Ran %ssuccessfully.' % ('' if self.success else 'un')))
return '%s: %s' % ('SUCCESS' if self.success else 'FAILURE', self.status if self.status else ('Ran %ssuccessfully.' % ('' if self.success else 'un')))

@staticmethod
def success(status=None):
Expand Down
5 changes: 4 additions & 1 deletion pydwarf/urist.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def handle(self, info):
log.error('Found no scripts matching %s.' % info)

def handleall(self, infos):
for info in infos: self.handle(info)
if infos and len(infos):
for info in infos: self.handle(info)
else:
log.error('No scripts to run.')



Expand Down
8 changes: 7 additions & 1 deletion raws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from token import rawstoken as token
from file import rawsfile as file
from dir import rawsdir as dir
from dir import copytree
import color

__version__ = '1.0.0'
filter = tokenfilter

parse = token.parse
parseone = token.parseone

__version__ = '1.0.1'
98 changes: 86 additions & 12 deletions raws/dir.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import os
from queryable import rawsqueryable_obj
import shutil
from queryable import rawsqueryable_obj, rawstokenlist
from file import rawsfile



class rawsdir(rawsqueryable_obj):
'''Represents as a whole all the raws contained within a directory.'''

def __init__(self, *args, **kwargs):
'''Constructor for rawsdir object.'''

self.files = {}
self.otherfiles = []
if len(args) or len(kwargs): self.read(*args, **kwargs)

def getfile(self, filename, create=False):
def getfile(self, filename, create=None):
'''Gets the file with a given name. If no file by that name is found,
None is returned instead. If creature is set to something other than
None, the behavior when no file by some name exists is altered: A new
file is created and associated with that name, and then its add
method is called using the value for create as its argument.'''

rfile = self.files.get(filename)
if create and rfile is None:
return self.addfile(filename)
else:
return rfile
if create is not None and rfile is None:
rfile = self.addfile(filename)
rfile.add(create)
return rfile

def addfile(self, filename=None, rfile=None, path=None):
if path is not None:
return self.addpath(path)
Expand All @@ -26,10 +38,12 @@ def addfile(self, filename=None, rfile=None, path=None):
self.files[filename] = rfile
rfile.dir = self
return rfile

def setfile(self, filename=None, rfile=None):
if rfile and not filename: filename = rfile.header
rfile.dir = self
self.files[filename] = rfile

def removefile(self, filename=None, rfile=None):
if not rfile.dir == self: raise ValueError
if rfile and not filename: filename = rfile.header
Expand All @@ -45,31 +59,91 @@ def addpath(self, path):

def __getitem__(self, name): return self.getfile(name)
def __setitem__(self, name, value): return self.setfile(name, value)
def __contains__(self, name): return name in self.files

def read(self, path, log=None):
'''Reads raws from all text files in the specified directory.'''
for filename in os.listdir(path):
filepath = os.path.join(path, filename)
if filename.endswith('.txt') and os.path.isfile(filepath
):
if log: log.debug('Reading file %s...' % filepath)
if filename.endswith('.txt') and os.path.isfile(filepath):
if log: log.debug('Reading raws file %s.' % filepath)
with open(filepath, 'rb') as rfile:
filenamekey = os.path.splitext(os.path.basename(filename))[0]
self.files[filenamekey] = rawsfile(path=filepath, rfile=rfile, dir=self)
return self
else:
if log: log.debug('Found non-raws file %s.' % filepath)
self.otherfiles.append((path, filename))

def write(self, path, log=None):
'''Writes raws to the specified directory.'''

# Write raws files
for filename in self.files:
filepath = os.path.join(path, filename)
if not filepath.endswith('.txt'): filepath += '.txt'
with open(filepath, 'wb') as rfile:
if log: log.debug('Writing file %s...' % filepath)
if log: log.debug('Writing raws file %s.' % filepath)
self.files[filename].write(rfile)
return self
# Copy other files
for filepath, filename in self.otherfiles:
if filepath != path:
originalpath = os.path.join(filepath, filename)
writepath = os.path.join(path, filename)
if log: log.debug('Writing non-raws file %s.' % writepath)
if os.path.isfile(originalpath):
shutil.copy2(originalpath, writepath)
elif os.path.isdir(originalpath):
copytree(originalpath, writepath)
elif log:
log.error('Failed to write non-raws file %s: it\'s neither a file nor a directory.' % writepath)

def tokens(self):
'''Iterate through all tokens.'''
for filename in self.files:
for token in self.files[filename].tokens():
yield token

def getobjheaders(self, type):
'''Gets OBJECT:X tokens where X is type. Is also prepared for special cases
like type=ITEM_PANTS matching OBJECT:ITEM.
Example usage:
>>> objheaders = df.getobjheaders('INORGANIC')
>>> for token in objheaders: print token; print token.next
...
[OBJECT:INORGANIC]
[INORGANIC:PLASTER]
[OBJECT:INORGANIC]
[INORGANIC:SANDSTONE]
[OBJECT:INORGANIC]
[INORGANIC:IRON]
[OBJECT:INORGANIC]
[INORGANIC:CLAY]
[OBJECT:INORGANIC]
[INORGANIC:ONYX]
[OBJECT:INORGANIC]
[INORGANIC:HEMATITE]
'''

match_types = self.getobjheadername(type)
results = rawstokenlist()
for rfile in self.files.itervalues():
root = rfile.root()
if root is not None and root.value == 'OBJECT' and root.nargs() == 1 and root.args[0] in match_types:
results.append(root)
return results



# credit belongs to http://stackoverflow.com/a/13814557/3478907
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
else:
if not os.path.exists(d) or os.stat(src).st_mtime - os.stat(dst).st_mtime > 1:
shutil.copy2(s, d)
Loading

0 comments on commit 20de7c6

Please sign in to comment.