def test_cloneDirectory(self): from plone.resource.directory import PersistentResourceDirectory from plone.resource.utils import cloneResourceDirectory root = BTreeFolder2('portal_resources') root._setOb('demo', BTreeFolder2('demo')) root['demo']._setOb('foo', BTreeFolder2('foo')) root['demo']._setOb('bar', BTreeFolder2('bar')) source = PersistentResourceDirectory(root['demo']['foo']) target = PersistentResourceDirectory(root['demo']['bar']) source.writeFile('file1.txt', 'file1') source.writeFile('subdir1/file2.txt', 'file2') source.makeDirectory('subdir2') cloneResourceDirectory(source, target) self.assertEqual(source.listDirectory(), target.listDirectory()) self.assertEqual(source['subdir1'].listDirectory(), target['subdir1'].listDirectory()) self.assertEqual(source['subdir2'].listDirectory(), target['subdir2'].listDirectory()) self.assertEqual(source.readFile('file1.txt'), target.readFile('file1.txt')) self.assertEqual(source.readFile('subdir1/file2.txt'), target.readFile('subdir1/file2.txt'))
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if isinstance(themeName, unicode): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() if not manifest.has_section('theme'): manifest.add_section('theme') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) if manifest.has_option('theme', 'prefix'): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set('theme', 'prefix', prefix) if manifest.has_option('theme', 'rules'): rule = manifest.get('theme', 'rules') rule_file_name = rule.split('/')[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set('theme', 'rules', rules) manifestContents = StringIO() manifest.write(manifestContents) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme %s not found" % baseOn) themeName = getUtility(IURLNormalizer).normalize(title) if isinstance(themeName, unicode): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() if themeName in resources: idx = 1 while "%s-%d" % ( themeName, idx, ) in resources: idx += 1 themeName = "%s-%d" % ( themeName, idx, ) resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() if not manifest.has_section('theme'): manifest.add_section('theme') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) manifestContents = StringIO() manifest.write(manifestContents) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme %s not found" % baseOn) themeName = getUtility(IURLNormalizer).normalize(title) if isinstance(themeName, unicode): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() if themeName in resources: idx = 1 while "%s-%d" % (themeName, idx,) in resources: idx += 1 themeName = "%s-%d" % (themeName, idx,) resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() if not manifest.has_section('theme'): manifest.add_section('theme') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) manifestContents = StringIO() manifest.write(manifestContents) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def cloneLocalRapidoApp(src_theme, dest_theme, app_id, make_link=False): src_theme_dir = get_theme_directory(src_theme) dest_theme_dir = get_theme_directory(dest_theme) if not src_theme_dir: raise ThemeNotFound("{} theme not found".format(src_theme)) if not dest_theme_dir: raise ThemeNotFound("{} theme not found".format(dest_theme)) if not dest_theme_dir.isDirectory("rapido"): dest_theme_dir.makeDirectory("rapido") if src_theme_dir.isDirectory("rapido"): if src_theme_dir["rapido"].isDirectory(app_id): if dest_theme_dir["rapido"].isDirectory(app_id): raise RapidoAppAlreadyExists( "A rapido app with {} id already exists in {}".format( app_id, dest_theme)) if not make_link: dest_theme_dir["rapido"].makeDirectory(app_id) cloneResourceDirectory(src_theme_dir["rapido"][app_id], dest_theme_dir["rapido"][app_id]) else: f = StringIO.StringIO() f.write(src_theme) try: dest_theme_dir["rapido"].writeFile(app_id + '.lnk', f) finally: f.close() elif src_theme_dir["rapido"].isFile(app_id + '.lnk'): f = src_theme_dir["rapido"].openFile(app_id + '.lnk') try: dest_theme_dir["rapido"].writeFile(app_id + '.lnk', f) finally: f.close() else: raise RapidoAppNotFound( "{} rapido app is not found in {} theme".format( app_id, src_theme)) else: raise RapidoAppNotFound( "{} rapido app is not found in {} theme".format(app_id, src_theme))
def cloneLocalRapidoApp(src_theme, dest_theme, app_id, make_link=False): src_theme_dir = get_theme_directory(src_theme) dest_theme_dir = get_theme_directory(dest_theme) if not src_theme_dir: raise ThemeNotFound("{} theme not found".format(src_theme)) if not dest_theme_dir: raise ThemeNotFound("{} theme not found".format(dest_theme)) if not dest_theme_dir.isDirectory("rapido"): dest_theme_dir.makeDirectory("rapido") if src_theme_dir.isDirectory("rapido"): if src_theme_dir["rapido"].isDirectory(app_id): if dest_theme_dir["rapido"].isDirectory(app_id): raise RapidoAppAlreadyExists("A rapido app with {} id already exists in {}".format(app_id, dest_theme)) if not make_link: dest_theme_dir["rapido"].makeDirectory(app_id) cloneResourceDirectory(src_theme_dir["rapido"][app_id], dest_theme_dir["rapido"][app_id]) else: f = StringIO.StringIO() f.write(src_theme) try: dest_theme_dir["rapido"].writeFile(app_id + '.lnk', f) finally: f.close() elif src_theme_dir["rapido"].isFile(app_id + '.lnk'): f = src_theme_dir["rapido"].openFile(app_id + '.lnk') try: dest_theme_dir["rapido"].writeFile(app_id + '.lnk', f) finally: f.close() else: raise RapidoAppNotFound("{} rapido app is not found in {} theme".format( app_id, src_theme )) else: raise RapidoAppNotFound("{} rapido app is not found in {} theme".format( app_id, src_theme ))
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if six.PY2 and isinstance(themeName, six.text_type): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: if six.PY2: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() else: # configparser can only read/write text # but in py3 plone.resource objects are BytesIO objects. fp = target.openFile(MANIFEST_FILENAME) try: data = fp.read() finally: fp.close() manifest.read_string(safe_unicode(data)) if not manifest.has_section('theme'): manifest.add_section('theme') if six.PY2 and isinstance(title, six.text_type): title = title.encode('utf-8') if six.PY2 and isinstance(description, six.text_type): description = description.encode('utf-8') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) if manifest.has_option('theme', 'prefix'): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set('theme', 'prefix', prefix) if manifest.has_option('theme', 'rules'): rule = manifest.get('theme', 'rules') rule_file_name = rule.split('/')[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set('theme', 'rules', rules) paths_to_fix = ['development-css', 'production-css', 'tinymce-content-css', 'development-js', 'production-js'] for var_path in paths_to_fix: if not manifest.has_option('theme', var_path): continue val = manifest.get('theme', var_path) if not val: continue template_prefix = '++%s++%s/' % (THEME_RESOURCE_NAME, baseOn) if template_prefix in val: # okay, fix val = val.replace(template_prefix, '++%s++%s/' % (THEME_RESOURCE_NAME, themeName)) manifest.set('theme', var_path, val) if six.PY2: manifestContents = six.StringIO() manifest.write(manifestContents) else: # in py3 plone.resource is BytesIO objects # but configparser can only deal with text (StringIO). # So we need to do this stupid dance to write manifest.cfg tempfile = six.StringIO() manifest.write(tempfile) tempfile.seek(0) data = tempfile.read() tempfile.close() manifestContents = six.BytesIO(data.encode('utf8')) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if isinstance(themeName, six.text_type): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() if not manifest.has_section('theme'): manifest.add_section('theme') if not isinstance(title, str): title = title.encode('utf-8') if not isinstance(description, str): description = description.encode('utf-8') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) if manifest.has_option('theme', 'prefix'): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set('theme', 'prefix', prefix) if manifest.has_option('theme', 'rules'): rule = manifest.get('theme', 'rules') rule_file_name = rule.split('/')[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set('theme', 'rules', rules) paths_to_fix = [ 'development-css', 'production-css', 'tinymce-content-css', 'development-js', 'production-js' ] for var_path in paths_to_fix: if not manifest.has_option('theme', var_path): continue val = manifest.get('theme', var_path) if not val: continue template_prefix = '++%s++%s/' % (THEME_RESOURCE_NAME, baseOn) if template_prefix in val: # okay, fix val = val.replace(template_prefix, '++%s++%s/' % (THEME_RESOURCE_NAME, themeName)) manifest.set('theme', var_path, val) manifestContents = StringIO() manifest.write(manifestContents) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def createThemeFromTemplate(title, description, baseOn="template"): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if isinstance(themeName, unicode): themeName = themeName.encode("utf-8") resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() if not manifest.has_section("theme"): manifest.add_section("theme") manifest.set("theme", "title", title) manifest.set("theme", "description", description) if manifest.has_option("theme", "prefix"): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set("theme", "prefix", prefix) if manifest.has_option("theme", "rules"): rule = manifest.get("theme", "rules") rule_file_name = rule.split("/")[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set("theme", "rules", rules) paths_to_fix = ["development-css", "production-css", "tinymce-content-css", "development-js", "production-js"] for var_path in paths_to_fix: if not manifest.has_option("theme", var_path): continue val = manifest.get("theme", var_path) if not val: continue template_prefix = "/++%s++%s/" % (THEME_RESOURCE_NAME, baseOn) if val.startswith(template_prefix): # okay, fix val = val.replace(template_prefix, "/++%s++%s/" % (THEME_RESOURCE_NAME, themeName)) manifest.set("theme", var_path, val) manifestContents = StringIO() manifest.write(manifestContents) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if six.PY2 and isinstance(themeName, six.text_type): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: if six.PY2: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() else: # configparser can only read/write text # but in py3 plone.resource objects are BytesIO objects. fp = target.openFile(MANIFEST_FILENAME) try: data = fp.read() finally: fp.close() manifest.read_string(safe_unicode(data)) if not manifest.has_section('theme'): manifest.add_section('theme') if six.PY2 and isinstance(title, six.text_type): title = title.encode('utf-8') if six.PY2 and isinstance(description, six.text_type): description = description.encode('utf-8') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) if manifest.has_option('theme', 'prefix'): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set('theme', 'prefix', prefix) if manifest.has_option('theme', 'rules'): rule = manifest.get('theme', 'rules') rule_file_name = rule.split('/')[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set('theme', 'rules', rules) paths_to_fix = [ 'development-css', 'production-css', 'tinymce-content-css', 'development-js', 'production-js' ] for var_path in paths_to_fix: if not manifest.has_option('theme', var_path): continue val = manifest.get('theme', var_path) if not val: continue template_prefix = '++%s++%s/' % (THEME_RESOURCE_NAME, baseOn) if template_prefix in val: # okay, fix val = val.replace(template_prefix, '++%s++%s/' % (THEME_RESOURCE_NAME, themeName)) manifest.set('theme', var_path, val) if six.PY2: manifestContents = six.StringIO() manifest.write(manifestContents) else: # in py3 plone.resource is BytesIO objects # but configparser can only deal with text (StringIO). # So we need to do this stupid dance to write manifest.cfg tempfile = six.StringIO() manifest.write(tempfile) tempfile.seek(0) data = tempfile.read() tempfile.close() manifestContents = six.BytesIO(data.encode('utf8')) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName