Created
September 9, 2014 11:13
-
-
Save kaptankereviz/5e020e0f51782bbbdac6 to your computer and use it in GitHub Desktop.
Revisions
-
kaptankereviz created this gist
Sep 9, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ playground This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" /> </project> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.3 (C:\Python27\python.exe)" project-jdk-type="Python SDK" /> </project> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ProjectModuleManager"> <modules> <module fileurl="file://$PROJECT_DIR$/.idea/playground.iml" filepath="$PROJECT_DIR$/.idea/playground.iml" /> </modules> </component> </project> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <module type="PYTHON_MODULE" version="4"> <component name="NewModuleRootManager"> <content url="file://$MODULE_DIR$" /> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> </component> </module> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ <component name="DependencyValidationManager"> <state> <option name="SKIP_IMPORT_STATEMENTS" value="false" /> </state> </component> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="VcsDirectoryMappings"> <mapping directory="" vcs="" /> </component> </project> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ import os.path import xml.etree.cElementTree as xml class ConfigFile(object): """ config file class """ def __init__(self): """Configuration file is in local user folder""" self.config_file = os.path.join(os.path.expanduser('~'), 'playgroundconfig.xml') # default tag names self.root_tag = 'playground' self.user_tag = 'user' self.db_tag = 'database' def exists(self): """Check if config file exists """ return os.path.isfile(self.config_file) def create(self): """Create new config file """ # a config file has 3 tags # <playground> : root tag # <user> : user name tag # <database> : database(s) tag # (for this version only one database supported) root = xml.Element(self.root_tag) user = xml.SubElement(root, self.user_tag) db = xml.SubElement(root, self.db_tag) tree = xml.ElementTree(root) try: tree.write(self.config_file) return True except IOError: return False def set_user(self, name): # set user name # NO ERROR CHECKING !!! xmlfile = xml.parse(self.config_file) root = xmlfile.getroot() user = root.find(self.user_tag) user.text = name tree = xml.ElementTree(root) tree.write(self.config_file) def set_database(self, dbpath): # set database path # NO ERROR CHECKING !!! xmlfile = xml.parse(self.config_file) root = xmlfile.getroot() path = root.find(self.db_tag) path.text = dbpath tree = xml.ElementTree(root) tree.write(self.config_file) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ """ main modulde for zodb database access """ from ZODB import FileStorage, DB from defaults import DB_FILE import os.path class PlaygroundDatabase(object): def createdb(self, path): if self.opendb(path): self.closedb() return True else: return False def opendb(self, path): try: self.storage = FileStorage.FileStorage(os.path.join(path, DB_FILE)) self.db = DB(self.storage) self.connection = self.db.open() self.root = self.connection.root() return True except: return False def closedb(self): self.connection.close() self.db.close() self.storage.close() This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ """ default names """ DB_FILE = 'data.fs' CONTAINER_TYPES = ( 'playground', 'project', 'episodes', 'assets', 'scenes' ) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ __author__ = 'bilemem' This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,100 @@ # coding=utf-8 from BTrees.OOBTree import OOBTree from persistent import Persistent class MBase(Persistent): """ BASE Class for all classes of Playground System ----------------------------------------------- """ def __init__(self): self.name = None # main folder name (this can change after folder creation) self.path = None # main folder path self.children = OOBTree() # children list self.data = OOBTree() # data list # Maybe some global methods here # for example: # getting all children or all data # with index vs vs. (list comprehensions here) class MContainer(MBase): """ BASE MContainer CLASS --------------------- MContainer class represents folders that has same base-typed children and children grouping attributes ... a MContainer based object can have MAsset based objects only for parenting. ... a MContainer object have children data and methods to access them ... EXAMPLE 1: - Episode(s) is a MContainer class object (it's plural) - an Episode(s) class object can contain only Episode(MAsset) class objects - an MContainer has grouping attributes - so, we can group its children. (episode 1-13, episode 14-26 etc...) EXAMPLE 2: - Asset(s) is a MContainer object - it can only have Asset objects - so, we can group them like CHARACTERS, VEHICLES and so on... """ def __init__(self): super(MContainer, self).__init__() self.group_data = OOBTree() # data needed for grouping (keys: Group names, values def append(self, asset): """Adds MAsset object to self.children""" if isinstance(asset, MAsset): if self.children.insert(asset.name, asset): return True else: # if already have a same named child don't contain it # and return false. this is the default behaviour of BTrees # it simply ignores it, so i need to raise exception raise KeyError('{} is already exists. Try different name.'.format(asset.name)) else: # asset object is not MAsset object raise TypeError('Passed object is not a <MAsset> object.') # maybe we need grouping methods here class MAsset(MBase): """ BASE MAsset CLASS ----------------- MAsset class represents folders that has same base-typed children but unlike MContainer class has no children grouping attributes. ... MAsset class can only have MContainer class objects or some special type. (maya scene files, textures, organization etc.) ... EXAMPLE 1: - Shot (no plural) is a MAsset base-typed object - it has some special attributes This class is almost identical with MContainer class it's a seperate class because both have methods for allowed children control. so this class can only allow MContainer object, a MContainer object can only have this type... """ def __init__(self): super(MAsset, self).__init__() def append(self, container): """Adds MContainer object to self.children""" if isinstance(container, MContainer): if self.children.insert(container.name, container): return True else: # if already have a same named child don't contain it # and return false. this is the default behaviour of BTrees # it simply ignores it, so i need to raise exception raise KeyError('{} is already exists. Try different name.'.format(container.name)) else: # container object is not MContainer object raise TypeError('Passed object is not a <MContainer> object.') This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,111 @@ """ classes """ from base import MContainer, MAsset #--------------------------------------------------------------- # MContainers : have groupings (all are plural) #--------------------------------------------------------------- class Playground(MContainer): """ Pre-defined MContainer class for Playground ... Suggested use of this class is to be the root af all ... """ def __init__(self, name, root_path): """ This needs a root path for all children This is the main storage location for all other projects vs. so this is the playground (just another MContainer) """ super(Playground, self).__init__() self.name = name class Episodes(MContainer): """ Pre-defined MContainer class for Episodes """ def __init__(self,name): super(Episodes, self).__init__() self.name = name class Assets(MContainer): """ Pre-defined MContainer class for Assets """ def __init__(self,name): super(Assets, self).__init__() self.name = name class Shots(MContainer): """ Pre-defined MContainer class for Shots """ def __init__(self,name): super(Shots, self).__init__() self.name = name class CustomContainer(MContainer): """ Custom MContainer class """ def __init__(self, name, custom_type): super(CustomContainer, self).__init__() self.name = name self.custom_type = custom_type #--------------------------------------------------------------- # MAssets : no groupings (all are special) #--------------------------------------------------------------- class Project(MAsset): """ Pre-defined MAsset class for Project """ def __init__(self,name): super(Project, self).__init__() self.name = name class Episode(MAsset): """ Pre-defined MAsset class for Episode """ def __init__(self,name): super(Episode, self).__init__() self.name = name class Asset(MAsset): """ Pre-defined MAsset class for Asset """ def __init__(self,name): super(Asset, self).__init__() self.name = name class Shot(MAsset): """ Pre-defined MAsset class for Shot """ def __init__(self,name): super(Shot, self).__init__() self.name = name class CustomAsset(MAsset): """ Custom MAsset class """ def __init__(self, name, custom_type): super(CustomAsset, self).__init__() self.name = name self.custom_type = custom_type This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ """ Playground setup functions """ from cmd import Cmd from config import ConfigFile import os.path class PlaygroundShell(Cmd): prompt = '--->' def do_setuser(self, line): """ ------------------------- setuser() : Set user name ------------------------- """ cfg = ConfigFile() if not cfg.exists(): self.do_setupconfig('') # get user name user_input = raw_input('\nPlease enter username : ') cfg.set_user(user_input) def do_setdatabase(self, line): """ ---------------------------------------------------- setdatabase() : Set existing database to config file ---------------------------------------------------- """ cfg = ConfigFile() if not cfg.exists(): self.do_setupconfig('') # get path name user_input = raw_input('\nPlease enter a valid path(absolute) for database(not file): ') if os.path.isdir(user_input): cfg.set_database(user_input) else: print('{} is not a valid folder.'.format(user_input)) def do_setupconfig(self, line): """ ----------------------------------------------- setup() : Setup playground tool for this client ----------------------------------------------- """ # check if config file exists cfg = ConfigFile() if not cfg.exists(): # if not exists # ask if you want to create empty print('\nPlayground Configuration file not found') user_input = raw_input('Do you want to create new one (y/n) ') true_answers = ['yes', 'y'] if user_input in true_answers: # create configuration file if cfg.create(): print('\n{} is created...\n'.format(cfg.config_file)) else: print('\nError when creating file...\n') else: print('\nConfig File is already exists... Try to modify.\n') def do_exit(self, line): """ ------------------- exit() : exit shell ------------------- """ raise SystemExit if __name__ == '__main__': PlaygroundShell().cmdloop() This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ from ZODB import FileStorage, DB import transaction from models.base import MAsset, MContainer def createDB(): # create db and put an object struct to it # first open db storage = FileStorage.FileStorage('F:/test5.fs') db = DB(storage) connection = db.open() dbroot = connection.root() # now create playground object # this is root projects = MContainer('projects') dbroot['projects'] = projects projectA = MAsset('projectA') projects.is_parent(projectA) episodesA = MContainer('episodes') projectA.is_parent(episodesA) episodeA1 = MAsset('episode1') episodeA2 = MAsset('episode2') episodesA.is_parent(episodeA1) episodesA.is_parent(episodeA2) assetsA = MContainer('sharedAssets') projectA.is_parent(assetsA) assetA1 = MAsset('sharedAsset1') assetA2 = MAsset('sharedAsset2') assetsA.is_parent(assetA1) assetsA.is_parent(assetA2) transaction.commit() def getdata(): storage = FileStorage.FileStorage('F:/test4.fs') db = DB(storage) connection = db.open() dbroot = connection.root() print dbroot['projects'] print 'bok' db.close()