Created
December 10, 2010 17:50
-
-
Save imcotton/736515 to your computer and use it in GitHub Desktop.
My first Python test drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from shutil import rmtree, copytree, ignore_patterns | |
import StringIO | |
import os | |
import struct | |
import sys | |
import time | |
import zipfile | |
import zlib | |
import codecs | |
class Tester(): | |
def __init__(self): | |
timer = -time.clock() | |
prefix = ".." | |
source = prefix + "/" + "resource" | |
output = prefix + "/" + "output" | |
output_raw = output + "/" + "raw" | |
output_zip = output + "/" + "zip" | |
rmtree(output, True) | |
copytree(source, output_raw, ignore=ignore_patterns('assets')) | |
os.mkdir(output_zip) | |
typeList = [Type(i) for i in DirUtils.get_dir_list(output_raw)] #@UnusedVariable | |
zipList = [self._zip_dir(i, output_zip) for i in DirUtils.get_dir_list(output_raw)] | |
for i in zipList: | |
DirUtils.trim(i) | |
print '%s %.2f KB' % (i.split("/")[-1], os.path.getsize(i) / 1024.0) | |
rmtree(output_raw, True) | |
print "\nDone in %.2f sec" % (timer + time.clock()) | |
def _zip_dir(self, in_path, out_path): | |
name = '/'.join([out_path, '%s.zip' % in_path.split("/")[-1]]) | |
zip = zipfile.ZipFile(name, 'w', zipfile.ZIP_DEFLATED) | |
self._zip(in_path, zip) | |
zip.close() | |
return name | |
def _zip(self, path, zip): | |
for root, dirs, files in os.walk(path): #@UnusedVariable | |
for file in files: | |
str = os.path.join(root, file) | |
zip.write(str, os.path.relpath(str, path).replace('\\', '/')) | |
class Type(): | |
def __init__(self, path): | |
order_file = path + "/" + "order.txt" | |
config_file = path + "/" + "config.txt" | |
list_file = path + "/" + "list.xml" | |
self._create_config(DirUtils.get_key_value_dict(config_file)) | |
self.order = DirUtils.get_line_list(order_file) | |
self.group_list = [Group(i) for i in DirUtils.add(path, self.order)] | |
file = open(list_file, "w") | |
file.write(self.get_xml_string()) | |
file.flush() | |
file.close() | |
os.remove(config_file) | |
os.remove(order_file) | |
def get_xml_string(self): | |
xmlList =\ | |
[ | |
'<?xml version="1.0" encoding="UTF-8" ?>', | |
'<root name="%s">' % self.name, | |
''.join([i.get_xml_string() for i in self.group_list]), | |
'</root>', | |
] | |
return ''.join(xmlList) | |
def _create_config(self, obj): | |
self.name = obj["name"] | |
def __repr__(self): | |
return '<Type: name="%s">' % self.name | |
class Group(): | |
def __init__(self, path): | |
config_file = path + "/" + "config.txt" | |
self._create_config(DirUtils.get_key_value_dict(config_file)) | |
self._create_items(path) | |
os.remove(config_file) | |
def get_xml_string(self): | |
list = [i.get_xml_string() for i in self.items] | |
return '<group name="%s">%s</group>' % (self.name, ''.join(list)) | |
def _create_items(self, path): | |
def get_ordered_list(path): | |
pathList = DirUtils.get_dir_list(path) | |
nameList = [float(i) for i in os.listdir(path) if os.path.isdir(path + "/" + i)] | |
return [i[1] for i in sorted(zip(nameList, pathList), key=lambda x: x[0])] | |
self.items = [Item(i) for i in get_ordered_list(path)] | |
def _create_config(self, obj): | |
self.name = obj["name"] | |
def __repr__(self): | |
return "<Group: %d items>" % len(self.items) | |
class Item(): | |
def __init__(self, path): | |
self._prefix = "/".join(path.split("/")[-2:]) | |
self._create_stars(path + '/star') | |
self._create_dict() | |
config_file = path + "/" + "config.txt" | |
self._create_config(DirUtils.get_key_value_dict(config_file)) | |
os.remove(config_file) | |
def get_xml_string(self): | |
dictList = ["<%s>%s/%s</%s>" % (x, self._prefix, y, x) for x, y in self.dict.iteritems()] | |
if self.is_star: | |
self.star_list = ['<img>%s/%s</img>' % (self._prefix + '/star', i) for i in self.star_list] | |
dictList.append('<star>%s</star>' % ''.join(self.star_list)) | |
return '<item name="%s" prefix="%s">%s</item>'\ | |
% (self.name, self._prefix, ''.join(dictList)) | |
def _create_stars(self, path): | |
self.is_star = os.path.isdir(path) | |
if not self.is_star: | |
return | |
def get_ordered_list(path): | |
pathList = os.listdir(path) | |
nameList = [float('.'.join(i.split('.')[:-1])) for i in pathList] | |
return [i[1] for i in sorted(zip(nameList, pathList), key=lambda x: x[0])] | |
self.star_list = get_ordered_list(path) | |
def _create_dict(self): | |
self.dict =\ | |
{ | |
"big" : "big.png", | |
"small" : "small.png", | |
"detail" : "detail.png", | |
} | |
def _create_config(self, obj): | |
self.name = obj["name"] | |
def __repr__(self): | |
return "<Item name=%s isStar=%s>" % (self.name, self.is_star) | |
class DirUtils(): | |
@staticmethod | |
def get_dir_list(path): | |
list = os.listdir(path) | |
list = DirUtils.add(path, list) | |
list = DirUtils.filter_dir_list(list) | |
return list | |
@staticmethod | |
def get_key_value_dict(path): | |
obj = {} | |
for x, y in [i.split("=", 1) for i in DirUtils.get_line_list(path)]: | |
obj[x] = y | |
return obj | |
@staticmethod | |
def get_line_list(path): | |
return [line.replace(codecs.BOM_UTF8, "").strip() for line in open(path, "r").readlines()] | |
@staticmethod | |
def filter_dir_list(list): | |
return [i for i in list if os.path.isdir(i)] | |
@staticmethod | |
def join_path(list): | |
return "/".join(list) | |
@staticmethod | |
def add(root, list): | |
return [root + "/" + i for i in list] | |
@staticmethod | |
def trim(path): | |
""" | |
skip | |
""" | |
if __name__ == "__main__": | |
tester = Tester() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment