def create_search_types(my): from tactic.ui.app import SearchTypeCreatorCmd project_code = my.kwargs.get('project_code') search_types = my.kwargs.get('project_stype') if not search_types: return for search_type in search_types: if search_type == "": continue search_type = Common.get_filesystem_name(search_type) if search_type.find("/") != -1: parts = search_type.split("/") namespace = parts[0] table = parts[1] else: namespace = project_code table = search_type search_type = "%s/%s" % (namespace, search_type) description = Common.get_display_name(table) title = description has_pipeline = True kwargs = { 'database': project_code, 'namespace': project_code, 'schema': 'public', 'search_type_name': search_type, 'asset_description': description, 'asset_title': title, 'sobject_pipeline': has_pipeline, } creator = SearchTypeCreatorCmd(**kwargs) creator.execute()
def create_search_types(my): from tactic.ui.app import SearchTypeCreatorCmd project_code = my.kwargs.get('project_code') search_types = my.kwargs.get('project_stype') if not search_types: return for search_type in search_types: if search_type == "": continue search_type = Common.get_filesystem_name(search_type) if search_type.find("/") != -1: parts = search_type.split("/") namespace = parts[0] table = parts[1] else: namespace = project_code table = search_type search_type = "%s/%s" % (namespace, search_type) description = Common.get_display_name(table) title = description has_pipeline = True kwargs = { 'database': project_code, 'namespace': project_code, 'schema': 'public', 'search_type_name': search_type, 'asset_description': description, 'asset_title': title, 'sobject_pipeline': has_pipeline, } creator = SearchTypeCreatorCmd(**kwargs) creator.execute()
def process_file_path(file_path): '''makes a file path completely kosher with the file system. Only do it on basename or it would remove the : from C:/''' return Common.get_filesystem_name(file_path)
class FileNaming(object): VERSIONLESS_EXPR = "{basefile}_{snapshot.process}.{ext}" def __init__(my, sobject=None, snapshot=None, file_object=None, ext='', naming_expr=None): my.sobject = sobject my.snapshot = snapshot my.file_object = file_object my.set_ext(ext) my.naming_expr = naming_expr my.checkin_type = '' def add_default_ending(my, parts, auto_version=True, is_sequence=True): context = my.snapshot.get_value("context") filename = my.file_object.get_full_file_name() # make sure that the version in the file name does not yet exist version = my.get_version_from_file_name(filename) if not auto_version and version: # if the file version is not the same as the snapshot version # then check to see if the snapshot already exists if version != my.snapshot.get_value("version"): existing_snap = Snapshot.get_by_version(my.snapshot.get_value("search_type"),\ my.snapshot.get_value("search_id"), context, version) if existing_snap: raise TacticException( 'A snapshot with context "%s" and version "%s" already exists.' % (context, version)) my.snapshot.set_value("version", version) my.snapshot.commit() else: version = my.snapshot.get_value("version") if version == 0: version = "CURRENT" elif version == -1: version = "LATEST" else: if version == "": version = 1 # pad the version by by the global setting padding = Config.get_value("checkin", "version_padding") if not padding: padding = 3 else: padding = int(padding) expr = "v%%0.%sd" % padding version = expr % version revision = my.snapshot.get_value("revision", no_exception=True) if revision: revision = "r%0.2d" % revision ext = my.get_ext() # by default publish is not put into the file name if context != "publish": parts.append(context.replace("/", "_")) # add the server location #value = ProdSetting.get_value_by_key("naming/add_server") server = Config.get_value("install", "server") if server: parts.append(server) if my.is_tactic_repo(): parts.append(version) if revision: parts.append(revision) from pyasm.prod.biz import ProdSetting value = ProdSetting.get_value_by_key("naming/add_initials") if value == "false": project = Project.get() initials = Project.get().get_initials() parts.append(initials) filename = "_".join(parts) if is_sequence: filename = "%s.####.%s" % (filename, ext) elif ext: # dir don't need extension filename = "%s%s" % (filename, ext) return filename def get_version_from_file_name(my, file_name): '''utility function to extract version information from a file''' # get current file name of maya session and extract version. # ? is needed in case it's a folder which does not have the # . or _ after the v### pattern = re.compile(r'v(\d+)[\.|_]?', re.IGNORECASE) matches = pattern.findall(file_name) if not matches: version = 0 else: version = int(matches[0]) return version def get_file_type(my): if not my.file_object: file_type = '' else: # old file object may not have this filled in file_type = my.file_object.get_type() if not file_type: file_code = my.file_object.get_code() file_type = my.snapshot.get_type_by_file_code(file_code) return file_type def get_ext(my): if my.ext: return my.ext base_type = my.file_object.get_value('base_type') if base_type == 'directory': return None base, ext = os.path.splitext(my.file_object.get_full_file_name()) return ext def is_tactic_repo(my): '''returns whether the current state is a tactic repo''' repo_handler = my.sobject.get_repo_handler(my.snapshot) return repo_handler.is_tactic_repo() def get_padding(my): '''returns the padding of the file code, should the project use this feature''' return 10 # set the various objects needed to build a directory def set_sobject(my, sobject): my.sobject = sobject def set_snapshot(my, snapshot): my.snapshot = snapshot def set_naming(my, naming_expr): my.naming_expr = naming_expr def set_checkin_type(my, checkin_type): my.checkin_type = checkin_type def set_file_object(my, file_object): my.file_object = file_object def set_ext(my, ext): if not ext: my.ext = '' return if not ext.startswith("."): ext = ".%s" % ext my.ext = ext def get_file_name(my): assert my.sobject != None assert my.snapshot != None # File object can be none #assert my.file_object != None # determine whether naming is used file_type = my.get_file_type() if file_type and my.snapshot: # if there is a snapshot check the file to see if naming conventions # are even used if not my.snapshot.get_use_naming_by_type(file_type): file_name = my.file_object.get_value("file_name") if file_name: return file_name if my.naming_expr: file_name = my.get_from_expression(my.naming_expr) return file_name search_type = my.sobject.get_base_search_type() # first check the db file_name = my.get_from_db_naming(search_type) if file_name: file_type = my.get_file_type() if file_type in ['web', 'icon']: basename, ext = os.path.splitext(file_name) file_name = '%s_%s%s' % (basename, file_type, ext) return file_name func_name = search_type.replace("/", "_") try: file_name = eval("my.%s()" % func_name) except Exception, e: if e[0].find("object has no attribute '%s'" % func_name) != -1: file_name = my.get_default() else: raise file_name = Common.get_filesystem_name(file_name) return file_name
def process_file_path(file_path): '''makes a file path completely kosher with the file system. Only do it on basename or it would remove the : from C:/''' return Common.get_filesystem_name(file_path)
def get_display(my): menus = [] widget = DivWdg(id='ProjectSelectWdg', css='spt_panel') widget.set_attr('spt_class_name', 'tactic.ui.app.ProjectSelectWdg') if not WebContainer.get_web().is_IE(): widget.add_style("float: right") from tactic.ui.widget import SingleButtonWdg button = SingleButtonWdg(title='Open Project', icon=IconWdg.PROJECT, show_arrow=True) widget.add(button) #from tactic.ui.activator import ButtonForDropdownMenuWdg #menu_data = [] #menu_id = "project_select_menu" allowed = Project.get_user_projects() allowed_codes = [x.get_code() for x in allowed] search = Search("sthpw/project") search.add_filters("code", allowed_codes) # ignore some builtin projects search.add_where("\"code\" not in ('admin','sthpw','unittest')") search.add_op("begin") #search.add_filter("is_template", 'true', quoted=False, op='!=') search.add_filter("is_template", True, op='!=') search.add_filter("is_template", 'NULL', quoted=False, op='is') search.add_op("or") projects = search.get_sobjects() from tactic.ui.container import Menu, MenuItem menu = Menu(width=240) menus.append(menu) menu.set_allow_icons(False) security = Environment.get_security() if security.check_access("builtin", "view_site_admin", "allow", default="deny") or security.check_access("builtin", "create_projects", "allow", default="deny"): menu_item = MenuItem(type='title', label='Project Action') menu.add(menu_item) menu_item = MenuItem(type='action', label='Create New Project') menu.add(menu_item) menu_item.add_behavior( { 'cbjs_action': ''' //spt.popup.open('create_project_wizard'); //Effects.fade_in($('create_project_wizard'), 200); var env = spt.Environment.get(); var project = env.get_project(); if (project == 'admin') { spt.tab.set_main_body_top(); var class_name = 'tactic.ui.app.ProjectCreateWdg'; spt.tab.add_new("create_project", "Create Project", class_name); } else { document.location = "/tactic/admin/link/create_project"; } ''' } ) search = Search("config/url") search.add_filter("url", "/index") url = search.get_sobject() if url: menu_item = MenuItem(type='action', label='Open Index') menu.add(menu_item) menu_item.add_behavior( { 'cbjs_action': ''' var env = spt.Environment.get(); var project = env.get_project(); //document.location = "/tactic/" + project + "/"; window.open('/tactic/'+project+'/'); ''' } ) menu_item = MenuItem(type='title', label='Open Project') menu.add(menu_item) def add_project_menu(menu, project): project_code = project.get_code() menu_item = MenuItem(type='action', label=project.get_value("title")) web = WebContainer.get_web() browser = web.get_browser() if browser != 'Qt': menu_item.add_behavior( { 'type': 'click_up', 'project_code': project_code, 'cbjs_action': ''' window.open('/tactic/%s/'); ''' % project_code } ) else: menu_item.add_behavior( { 'project_code': project_code, 'cbjs_action': ''' spt.app_busy.show("Jumping to Project ["+bvr.project_code+"]", ""); document.location = '/tactic/%s/'; ''' % project_code } ) menu.add(menu_item) search = Search("sthpw/project") search.add_column("category", distinct=True) categories = [x.get_value("category") for x in search.get_sobjects() ] for category in categories: if category == '': continue # FIXME: a little inefficient, but should be ok for now category_projects = [] for project in projects: if project.get_value("category") != category: continue project_code = project.get_code() if not security.check_access("project", project_code, "view"): continue category_projects.append(project) if category_projects: suffix = Common.get_filesystem_name(category) label = "%s (%s)" % (category, len(category_projects)) menu_item = MenuItem(type='submenu', label=label) menu_item.set_submenu_tag_suffix(suffix) menu.add(menu_item) submenu = Menu(width=200, menu_tag_suffix=suffix) menus.append(submenu) for project in category_projects: add_project_menu(submenu, project) from pyasm.security import get_security_version security_version = get_security_version() for project in projects: if project.get_value("category") != "": continue project_code = project.get_code() if security_version >= 2: key = { "code": project_code } key2 = { "code": "*" } keys = [key, key2] default = "deny" if not security.check_access("project", keys, "allow", default=default): continue else: if not security.check_access("project", project_code, "view", default="allow"): continue add_project_menu(menu, project) if not projects: menu_item = MenuItem(type='action', label="- No Projects Created -") menu_item.add_behavior( { 'cbjs_action': ''' ''' } ) menu.add(menu_item) if security.check_access("builtin", "view_site_admin", "allow") or security.check_access("builtin", "view_template_projects", "allow"): search = Search("sthpw/project") #search.add_filter("is_template", 'true', quoted=False) search.add_filter("is_template", True) projects = search.get_sobjects() if projects: menu_item = MenuItem(type='title', label="Template Projects") menu.add(menu_item) for project in projects: project_code = project.get_code() if security_version >= 2: key = { "code": project_code } key2 = { "code": "*" } keys = [key, key2] default = "deny" if not security.check_access("project", keys, "allow", default=default): continue else: if not security.check_access("project", project_code, "view", default="allow"): continue menu_item = MenuItem(type='action', label=project.get_value("title")) menu_item.add_behavior( { 'project_code': project_code, 'cbjs_action': ''' spt.app_busy.show("Jumping to Project ["+bvr.project_code+"]", ""); document.location = '/projects/%s/' ''' % project_code } ) menu.add(menu_item) if security.check_access("builtin", "view_site_admin", "allow", default="deny") or security.check_access("builtin", "create_projects", "allow", default="deny"): menu_item = MenuItem(type='title', label="Admin") menu.add(menu_item) project = Project.get_by_code("admin") add_project_menu(menu, project) from tactic.ui.container import SmartMenu smenu_set = SmartMenu.add_smart_menu_set( button, { 'BUTTON_MENU': menus } ) SmartMenu.assign_as_local_activator( button, "BUTTON_MENU", True ) return widget
def naming_to_file(my, template, sobject, snapshot, file=None, ext=None, file_type=None): ''' # chr001_model_v004_00001.ext ''' version_padding = Config.get_value("checkin", "version_padding") if version_padding: version_padding = int(version_padding) else: version_padding = 3 version_expr = "%%0.%dd" % version_padding # the main sobject project = sobject.get_project() # parse the pattern string expression = re.compile(r'{([\w|\.|\#]+\[?\d?\]?)}') temp_list = expression.findall(template) # if nothing is found, then just return parse through an expression if not temp_list: #return template # put in the ability to add expressions from pyasm.biz import ExpressionParser xp = ExpressionParser() env_sobjects = { 'snapshot': snapshot, 'file': file } file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': base = file_name ext = None else: base, ext = os.path.splitext(file_name) if not ext: value = None else: # external ext starts with a . ext = ext.lstrip(".") value = ext vars = {'EXT': value, 'BASEFILE': base} result = xp.eval(template, sobject, mode='string', env_sobjects=env_sobjects, vars=vars) test = template test = test.replace("{", "") test = test.replace("}", "") # don't allow / in filename test = test.replace("/", "_") if test != result: return result result = template for part in temp_list: index = -1 if part.find(".") != -1: # explict declarations object, attr = part.split(".") if attr.endswith(']'): # ugly, but it works attr, index = attr.split("[") index = int(index.rstrip("]")) if object == "sobject": value = sobject.get_value(attr) elif object == "snapshot": if not snapshot: continue value = snapshot.get_value(attr) if attr in ['version', 'revision']: if value: value = version_expr % int(value) else: value = "0"*version_padding #value = snapshot.get_value(attr) elif object == "file": if attr == 'file_type': if file_type: value = file_type else: value = 'main' else: value = file.get_value(attr) elif object == "parent": parent = sobject.get_parent() if not parent: value = "NO_PARENT" else: value = parent.get_value(attr) elif object in ["login","user"]: login = Environment.get_login() value = login.get_value(attr) elif object == "project": project = Project.get() value = project.get_value(attr) else: raise NamingException("Can't parse part [%s] in template" % part) else: # use implicit declarations attr = part if attr.endswith(']'): # ugly, but it works attr, index = attr.split("[") index = int(index.rstrip("]")) if attr == "context": value = snapshot.get_value(attr) elif attr == "snapshot_type": value = snapshot.get_value(attr) elif attr == "version": value = snapshot.get_value(attr) if value: value = version_expr % int(value) else: value = "0"*version_padding elif attr == "revision": value = snapshot.get_value(attr) if value: value = version_expr % int(value) else: value = "0"*version_padding elif attr.startswith("#"): if not snapshot: continue value = snapshot.get_value("version") expr = "%%0.%sd" % len(attr) if value: value = expr % int(value) else: value = "0" * len(attr) elif attr == "basefile": file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': value = file_name else: base, ext = os.path.splitext(file_name) value = base elif attr == "ext": if not ext: file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': value = '' else: base, ext = os.path.splitext(file_name) value = ext.lstrip(".") else: # external ext starts with a . ext = ext.lstrip(".") value = ext elif attr in ["login","user"]: login = Environment.get_login() value = login.get_value("login") elif attr == "file_type": if file_type: value = file_type else: value = 'main' elif attr.startswith('date'): # {date,%Y-%m-%d_%H-%M-%S]} import time parts = attr.split(",", 1) if len(parts) == 2: format = parts[1] else: format = "%Y%m%d" value = time.strftime(format, time.localtime()) else: value = sobject.get_value(attr) # tbis applies to context for now if index != -1: value = re.split("[/]", value) if len(value) <= index: value = '!' else: value = value[index] #if not value: # raise NamingException("Value for part [%s] is empty" % part) if isinstance(value, int): value = str(value) result = result.replace("{%s}" % part, value) # don't allow / in filename, # FIXME: it's not put in get_filesystem_name since it # is used for directory name also, need to modify that result = result.replace("/", "_") # post process result so that it looks good result = Common.get_filesystem_name(result) return result
def naming_to_file(my, template, sobject, snapshot, file=None, ext=None, file_type=None): ''' # chr001_model_v004_00001.ext ''' version_padding = Config.get_value("checkin", "version_padding") if version_padding: version_padding = int(version_padding) else: version_padding = 3 version_expr = "%%0.%dd" % version_padding # the main sobject project = sobject.get_project() # parse the pattern string expression = re.compile(r'{(.*?)}') #expression = re.compile(r'{([\w|\.|\#]+\[?\d?\]?)}') temp_list = expression.findall(template) result = template from pyasm.biz import ExpressionParser xp = ExpressionParser() # if nothing is found, then just return parse through an expression ''' if not temp_list: #return template # put in the ability to add expressions env_sobjects = { 'snapshot': snapshot, 'file': file } file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': base = file_name ext = None else: base, ext = os.path.splitext(file_name) if not ext: value = None else: # external ext starts with a . ext = ext.lstrip(".") value = ext vars = {'EXT': value, 'BASEFILE': base} result = xp.eval(template, sobject, mode='string', env_sobjects=env_sobjects, vars=vars) test = template test = test.replace("{", "") test = test.replace("}", "") # don't allow / in filename test = test.replace("/", "_") if test != result: return result ''' base = None if file: file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': base = file_name ext = None else: base, file_ext = os.path.splitext(file_name) # passed in ext takes prescedence if not ext: ext = file_ext if not ext: value = None else: # external ext starts with a . ext = ext.lstrip(".") value = ext vars = {'EXT': value, 'BASEFILE': base} for part in temp_list: index = -1 if part.startswith(("@","$")): env_sobjects = { 'snapshot': snapshot, 'file': file } value = xp.eval("{%s}" % part, sobject, env_sobjects=env_sobjects, vars=vars, single=True) elif part.find(".") != -1: # explict declarations object, attr = part.split(".") if attr.endswith(']'): # ugly, but it works attr, index = attr.split("[") index = int(index.rstrip("]")) if object == "sobject": value = sobject.get_value(attr) elif object == "snapshot": if not snapshot: continue value = snapshot.get_value(attr) if attr in ['version', 'revision']: if value: value = version_expr % int(value) else: value = "0"*version_padding #value = snapshot.get_value(attr) elif object == "file": if attr == 'file_type': if file_type: value = file_type else: value = 'main' else: value = file.get_value(attr) elif object == "parent": parent = sobject.get_parent() if not parent: value = "NO_PARENT" else: value = parent.get_value(attr) elif object in ["login","user"]: login = Environment.get_login() value = login.get_value(attr) elif object == "project": project = Project.get() value = project.get_value(attr) else: raise NamingException("Can't parse part [%s] in template" % part) else: # use implicit declarations attr = part if attr.endswith(']'): # ugly, but it works attr, index = attr.split("[") index = int(index.rstrip("]")) if attr in ["context","process","snapshot_type"]: value = snapshot.get_value(attr) elif attr == "version": value = snapshot.get_value(attr) if value: value = version_expr % int(value) else: value = "0"*version_padding elif attr == "revision": value = snapshot.get_value(attr) if value: value = version_expr % int(value) else: value = "0"*version_padding elif attr.startswith("#"): if not snapshot: continue value = snapshot.get_value("version") expr = "%%0.%sd" % len(attr) if value: value = expr % int(value) else: value = "0" * len(attr) elif attr == "basefile": file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': value = file_name else: base, ext = os.path.splitext(file_name) value = base elif attr == "ext": if not ext: file_name = file.get_value("file_name") base_type = file.get_value("base_type") if base_type =='directory': value = '' else: base, ext = os.path.splitext(file_name) value = ext.lstrip(".") else: # external ext starts with a . ext = ext.lstrip(".") value = ext elif attr in ["login","user"]: login = Environment.get_login() value = login.get_value("login") elif attr == "file_type": if file_type: value = file_type else: value = 'main' elif attr.startswith('date'): # {date,%Y-%m-%d_%H-%M-%S]} import time parts = attr.split(",", 1) if len(parts) == 2: format = parts[1] else: format = "%Y%m%d" value = time.strftime(format, time.localtime()) else: value = sobject.get_value(attr) # tbis applies to context for now if index != -1: value = re.split("[/]", value) if len(value) <= index: value = '!' else: value = value[index] #if not value: # raise NamingException("Value for part [%s] is empty" % part) if isinstance(value, int): value = str(value) elif value is None: value = "" result = result.replace("{%s}" % part, value) # don't allow / in filename, # FIXME: it's not put in get_filesystem_name since it # is used for directory name also, need to modify that result = result.replace("/", "_") # remove trailing . if any if result and result[-1] == '.': result = result[:-1] # post process result so that it looks good result = Common.get_filesystem_name(result) return result