def update_links(self, source, target): base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) handler = self.handler get_value = handler.get_record_value for record in handler.get_records_in_order(): path = get_value(record, 'name') if not path: continue ref = get_reference(path) if ref.scheme: continue # Strip the view path = ref.path name = path.get_name() if name and name[0] == ';': view = '/' + name path = path[:-1] else: view = '' path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new reference with the right path new_ref = deepcopy(ref) new_ref.path = str(new_base.get_pathto(target)) + view handler.update_record(record.id, **{'name': str(new_ref)}) get_context().server.change_resource(self)
def update_links(self, source, target): WebPage.update_links(self, source, target) site_root = self.get_site_root() available_languages = site_root.get_property('website_languages') base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) for lang in available_languages: path = self.get_property('thumbnail', language=lang) if not path: continue ref = get_reference(path) if ref.scheme: continue path, view = get_path_and_view(ref.path) path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new reference with the right path new_ref = deepcopy(ref) new_ref.path = str(new_base.get_pathto(target)) + view self.set_property('thumbnail', str(new_ref), language=lang) get_context().database.change_resource(self)
def update_links(self, source, target): # FIXME BaseTheme does not take into account 'child' BaseTheme.update_links(self, source, target) base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) # banner_path site_root = self.get_site_root() available_languages = site_root.get_property("website_languages") for language in available_languages: value = self.get_property("banner_path", language=language) if not value: continue path = old_base.resolve2(value) if path == source: # Hit the old name # Build the new reference with the right path self.set_property("banner_path", new_base.get_pathto(target), language=language) get_context().database.change_resource(self)
def get_resource(self, path, soft=False): if type(path) is not Path: path = Path(path) # 1. Get the metadata if path.is_absolute(): abspath = path else: abspath = self.abspath.resolve2(path) return self.database.get_resource(abspath, soft=soft)
def update_links(self, source, target): base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) site_root = self.get_site_root() languages = site_root.get_property('website_languages') links = [] for key, datatype in self.get_metadata_schema().items(): multilingual = getattr(datatype, 'multilingual', False) langs = languages if multilingual is True else [None] if issubclass(datatype, XHTMLBody): for lang in langs: events = self.get_property(key, language=lang) if not events: continue events = _change_link(source, target, old_base, new_base, events) events = list(events) self.set_property(key, events, language=lang) elif issubclass(datatype, PathDataType): # Relative path for lang in langs: path = self.get_property(key, language=lang) if path is None: continue path = str(old_base.resolve2(path)) if path == source: # Hit the old name new_path = str(new_base.get_pathto(target)) self.set_property(key, new_path, language=lang) elif issubclass(datatype, AbsolutePathDataTypeEnumerate): # Absolute path for lang in langs: path = self.get_property(key, language=lang) if path is None: continue path = str(path) path = resources_new2old.get(path, path) if path == source: # Hit the old name self.set_property(key, str(target), language=lang) # Tagaware ? if isinstance(self, TagsAware): TagsAware.update_links(self, source, target) # Change resource get_context().database.change_resource(self)
class PathComparisonTestCase(TestCase): def setUp(self): self.path_wo_slash = Path('/a/b/c') self.path_w_slash = Path('/a/b/c/') self.wo_to_w = self.path_wo_slash.get_pathto(self.path_w_slash) ######################################################################### # Comparing Path objects def test_with_eq_without_trailing_slash(self): """A path is not the same with a trailing slash.""" self.assertNotEqual(self.path_wo_slash, self.path_w_slash) def test_wo_to_w_eq_path_dot(self): """The path to the same with a trailing slash returns Path('.').""" self.assertEqual(self.wo_to_w, Path('.')) ######################################################################### # Comparing with string conversions. def test_path_wo_slash_eq_string(self): """A path without trailing slash equals its string conversion.""" self.assertEqual(self.path_wo_slash, str(self.path_wo_slash)) def test_path_w_slash_eq_string(self): """A path with trailing slash equals its string conversion.""" self.assertEqual(self.path_w_slash, str(self.path_w_slash)) def test_path_to_similar_eq_string_dot(self): """The path to the same with a trailing slash equals '.'.""" self.assertEqual(self.wo_to_w, '.')
def update_links(self, source, target): source = Path(source) site_root = self.get_site_root() # Tags tags_base = site_root.get_abspath().resolve2("tags") if tags_base.get_prefix(source) == tags_base: tags = list(self.get_property("tags")) source_name = source.get_name() target_name = Path(target).get_name() for tag in tags: if tag == source_name: # Hit index = tags.index(source_name) tags[index] = target_name self.set_property("tags", tags) get_context().database.change_resource(self)
def get_available_languages(self): """Returns the language codes for the user interface. """ source = itools_source_language target = itools_target_languages # A package based on itools cls = self.__class__ if cls is not Root: exec('import %s as pkg' % cls.__module__.split('.', 1)[0]) config = Path(pkg.__path__[0]).resolve_name('setup.conf') config = ro_database.get_handler(str(config), ConfigFile) source = config.get_value('source_language', default=source) target = config.get_value('target_languages', default=target) target = target.split() if source in target: target.remove(source) target.insert(0, source) return target
def update_links(self, source, target): super(BoxFeed, self).update_links(source, target) container_path = self.get_property('container_path') if container_path: if container_path == '/': # Even if site_root is renammed, '/' is '/' pass else: resources_new2old = get_context().database.resources_new2old site_root_abspath = self.get_site_root().abspath base = str(site_root_abspath) old_base = resources_new2old.get(base, base) old_base = Path(old_base) # Path is relative to site_root path = old_base.resolve2(container_path) if path == source: # Hit the old name new_path = site_root_abspath.get_pathto(target) self.set_property('container_path', new_path)
def update_links(self, source, target): super(DiaporamaTable, self).update_links(source, target) # Caution multilingual property base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) site_root = self.get_site_root() available_languages = site_root.get_property('website_languages') handler = self.handler record_properties = handler.record_properties # TODO To improve get_value = handler.get_record_value for record in handler.get_records(): for lang in available_languages: for key in ('img_path', 'img_link'): path = get_value(record, key, lang) if not path: continue ref = get_reference(path) if ref.scheme: continue path, view = get_path_and_view(ref.path) path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new reference with the right path new_ref = deepcopy(ref) new_ref.path = str(new_base.get_pathto(target)) + view datatype = record_properties.get(key, String) new_path = Property(datatype.decode(str(new_ref)), language=lang) handler.update_record(record.id, **{key: new_path}) get_context().database.change_resource(self)
def get_item_value(self, resource, context, item, column): if column == 'checkbox': if self.is_folder(item): return None proxy = super(AddImage_BrowseContent, self) return proxy.get_item_value(resource, context, item, column) elif column == 'icon': if self.is_folder(item): # icon path_to_icon = item.get_resource_icon(48) if path_to_icon.startswith(';'): path_to_icon = Path('%s/' % item.name).resolve(path_to_icon) else: path = item.abspath path_to_icon = ";thumb?width=48&height=48" if path: path_to_resource = Path(str(path) + '/') path_to_icon = path_to_resource.resolve(path_to_icon) return path_to_icon else: proxy = super(AddImage_BrowseContent, self) return proxy.get_item_value(resource, context, item, column)
def update_links(self, source, target): WebPage.update_links(self, source, target) base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) path = self.get_property('title_link') if path: ref = get_reference(path) if not ref.scheme: path, view = get_path_and_view(ref.path) path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new reference with the right path new_ref = deepcopy(ref) new_ref.path = str(new_base.get_pathto(target)) + view self.set_property('title_link', str(new_ref)) get_context().database.change_resource(self)
def get_resource(self, path, soft=False): if type(path) is not Path: path = Path(path) if path.is_absolute(): here = self.get_root() else: here = self while path and path[0] == '..': here = here.parent path = path[1:] for name in path: resource = here._get_resource(name) if resource is None: if soft is True: return None raise LookupError, 'resource "%s" not found' % path resource.parent = here resource.name = name here = resource return here
def update_links(self, source, target): Folder.update_links(self, source, target) TagsAware.update_links(self, source, target) base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) # metadata schema = self.class_schema for key, datatype in schema.iteritems(): if issubclass(datatype, PathDataType) is False: continue value = self.get_property(key) if not value: continue ref = get_reference(value) if ref.scheme: continue path, view = get_path_and_view(ref.path) path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new path with the right path new_path = str(new_base.get_pathto(target)) + view self.set_property(key, Path(new_path)) # comments comments = self.metadata.get_property('comment') or [] for comment in comments: # XXX hardcoded, not typed for key in ('attachment',): value = comment.get_parameter(key) if not value: continue ref = get_reference(value) if ref.scheme: continue path, view = get_path_and_view(ref.path) path = str(old_base.resolve2(path)) if path == source: # Hit the old name # Build the new path with the right path new_path = str(new_base.get_pathto(target)) + view comment.set_parameter(key, new_path) self.set_property('comment', comments)
def update_links(self, source, target): source = Path(source) site_root = self.get_site_root() available_languages = site_root.get_property('website_languages') # Tags tags_base = site_root.get_abspath().resolve2('tags') if tags_base.get_prefix(source) == tags_base: tags = list(self.get_property('tags')) source_name = source.get_name() target_name = Path(target).get_name() for tag in tags: if tag == source_name: # Hit index = tags.index(source_name) tags[index] = target_name self.set_property('tags', tags) # Thumbnail base = self.get_canonical_path() resources_new2old = get_context().database.resources_new2old base = str(base) old_base = resources_new2old.get(base, base) old_base = Path(old_base) new_base = Path(base) for lang in available_languages: path = self.get_property('thumbnail', lang) if not path: continue ref = get_reference(path) if ref.scheme: continue path = old_base.resolve2(path) if path == source: # Hit self.set_property('thumbnail', new_base.get_pathto(target), lang) get_context().database.change_resource(self)
def setUp(self): self.path_wo_slash = Path('/a/b/c') self.path_w_slash = Path('/a/b/c/') self.wo_to_w = self.path_wo_slash.get_pathto(self.path_w_slash)
def get_namespace(self, resource, context): root = context.root here = context.resource site_root = here.get_site_root() site_root_abspath = site_root.get_abspath() shop = site_root.get_resource('shop') categories_abspath = str(site_root_abspath.resolve2('categories')) show_nb_products = resource.get_property('show_nb_products') show_first_category = resource.get_property('show_first_category') show_second_level = resource.get_property('show_second_level') here_abspath = here.get_abspath() here_real_abspath = str(here_abspath) here_parent_abspath = here_abspath.resolve2('..') current_level = here_real_abspath.count('/') if here.metadata.format == 'category': here_abspath = str(here.get_abspath()) # Max level deploy = count '/' + 1 # here_abspath at level 1 does not contain '/' here_abspath_level = here_abspath.count('/') max_level_deploy = here_abspath_level + 1 else: if here.metadata.format == 'product': # Special case for the product, here_* values are compute # with here = parent category parent_category = here.parent parent_category_abspath = parent_category.get_abspath() here_abspath = str(parent_category_abspath) here_abspath_level = here_abspath.count('/') max_level_deploy = here_abspath_level + 1 here_parent_abspath = parent_category_abspath.resolve2('..') else: # Tweak here_abspath here_abspath = categories_abspath here_abspath_level = here_abspath.count('/') max_level_deploy = categories_abspath.count('/') + 1 here_abspath_p = Path(here_abspath) # Get search with all publics products all_products = root.search( AndQuery(PhraseQuery('format', shop.product_class.class_id), PhraseQuery('workflow_state', 'public'))) # Get search with all categories all_categories = root.search( AndQuery(PhraseQuery('parent_paths', categories_abspath), PhraseQuery('format', 'category'))) # Build a dict with brains by level cat_per_level = {} for index, cat in enumerate( all_categories.get_documents(sort_by='abspath')): # Skip first category --> /categories if index == 0 and show_first_category is False: continue level = cat.abspath.count('/') # Skip second level (if we are not on level /categories/') if (show_second_level is False and current_level > 2 and level == 3 and not here_real_abspath == cat.abspath and not here_parent_abspath == cat.abspath): continue # Skip bad level if level > max_level_deploy: continue diff_level = here_abspath_level - level path_to_resolve = ['..' for x in range(diff_level)] path_to_resolve = '/'.join(path_to_resolve) # Path uses to compute the prefix with the current category here_prefix = here_abspath_p.resolve2(path_to_resolve) # Compute the prefix prefix = here_prefix.get_prefix(cat.abspath) if prefix == here_prefix: # special case when prefix equals here_prefix pass elif len(prefix) != level - 1: # bad, not in the same arborescence continue # Get the product number in the category sub_results = all_products.search( PhraseQuery('parent_paths', cat.abspath)) cats = cat_per_level.setdefault(level, []) cats.append({'doc': cat, 'nb_products': len(sub_results)}) # Build the tree starting with the higher level tree_template = resource.get_resource(self.tree_template) levels = sorted(cat_per_level.keys(), reverse=True) tree = None for level in levels: items = [] for data in cat_per_level[level]: doc = data['doc'] if here_abspath.startswith(doc.abspath): sub_tree = tree css = 'in-path ' else: sub_tree = None css = '' css = 'active ' if here_abspath == doc.abspath else css # Href (get_link emulation) href = str(site_root_abspath.get_pathto(doc.abspath)) css += checkid(href) css = css.replace('.', '-dot-') if resource.get_property('use_small_title'): title = doc.m_breadcrumb_title or doc.m_title or doc.name else: title = doc.m_title or doc.name d = { 'title': title, 'href': '/%s' % href, 'sub_tree': sub_tree, 'nb_products': data['nb_products'], 'css': css } items.append(d) tree = stl(tree_template, { 'items': items, 'show_nb_products': show_nb_products, 'css': None }) return {'title': resource.get_title(), 'tree': tree}
def update_locale(srx_handler, exclude_folders, no_wrap=False): # Read configuration for languages config = get_config() src_language = config.get_value('source_language', default='en') # Get local folder package_root = config.get_value('package_root') if lfs.exists(package_root): locale_folder_path = Path('{0}/locale'.format(package_root)) else: locale_folder_path = Path('locale/') locale_folder = lfs.open(locale_folder_path) # Initialize message catalog po = POFile() lines = [] for line in open('MANIFEST').readlines(): line = line.strip() exclude_folder = False for x in exclude_folders: if line.startswith(x): exclude_folder = True break if exclude_folder is False: lines.append(line) # Process Python and HTML files write('* Extract text strings') extensions = [ '.py', '.js', '.xhtml.%s' % src_language, '.xml.%s' % src_language, '.html.%s' % src_language] for path in lines: # Filter files for extension in extensions: if path.endswith(extension): break else: continue # Get the units write('.') try: handler = ro_database.get_handler(path) except Exception: print print '*' print '* Error:', path print '*' raise try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = locale_folder_path.get_pathto(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print write('* Update PO template ') data = po.to_str() # Write the po into the locale.pot try: locale_pot = locale_folder.open('locale.pot', WRITE) except IOError: # The locale.pot file does not exist create and open locale_pot = locale_folder.make_file('locale.pot') else: with locale_pot: locale_pot.write(data) # Update PO files filenames = set([ x for x in locale_folder.get_names() if x[-3:] == '.po' ]) filenames.add('%s.po' % src_language) for language in config.get_value('target_languages'): filenames.add('%s.po' % language) filenames = list(filenames) filenames.sort() print '* Update PO files:' locale_pot_path = locale_folder.get_absolute_path('locale.pot') for filename in filenames: if locale_folder.exists(filename): write(' %s ' % filename) file_path = locale_folder.get_absolute_path(filename) if no_wrap: call(['msgmerge', '--no-wrap', '-U', '-s', file_path, locale_pot_path]) else: call(['msgmerge', '-U', '-s', file_path, locale_pot_path]) else: print ' %s (new)' % filename file_path = locale_folder.get_absolute_path(filename) lfs.copy(locale_pot_path, file_path) print
def resolve2(base, path): if type(base) is not Path: base = Path(base) path = base.resolve2(path) return str(path)
def get_basename(path): if type(path) is not Path: path = Path(path) return path.get_name()
def normalize_key(self, path, __root=Path('/')): return self.backend.normalize_key(path, __root)
def test_pathto_w_slash(self): before = Path('/a/b/') after = Path('/a/b/c') self.assertEqual(before.get_pathto(after), 'c')
def get_canonical_path(self): if self.parent is None: return Path('/') parent_path = self.parent.get_canonical_path() return parent_path.resolve_name(self.name)
def test_relnorm(self): """ Check that '../' = '../' """ path = Path('../../a//.//b/c') self.assertEqual(str(path), '../../a/b/c')
def test_absnorm(self): """ Test the normalization '/..' = '/' """ path = Path('/../../a/b/c') self.assertEqual(str(path), '/a/b/c')
def test_backnorm(self): """ Test the normalization 'a/../b' = 'b' """ path = Path('a/b/c/../d') self.assertEqual(str(path), 'a/b/d')
def test7(self): a = Path('a/b/') self.assertEqual(a.get_pathtoroot(), '../')
def test5(self): a = Path('/a/very/long/path') self.assertEqual(a.get_pathtoroot(), '../../../')
def test_resolve2_w_slash(self): before = Path('/a/b/') after = Path('/a/b/c') self.assertEqual(before.resolve_name('c'), after)
def _make_resource(cls, folder, name, **kw): root = get_context().root # Compute resource path resource_path = Path(folder.key).resolve2(name) resource = root.get_resource(resource_path) resource.set_multilingual_properties(**kw)
def test3(self): a = Path('/a/b') self.assertEqual(a.get_pathtoroot(), '../')
else: continue # Get the units write('.') handler = ro_database.get_handler(path) try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = Path('..').resolve2(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print # Update locale.pot if not vfs.exists('locale/locale.pot'): vfs.make_file('locale/locale.pot') write('* Update PO template ') data = po.to_str() file = vfs.open('locale/locale.pot', WRITE) try: file.write(data) finally: file.close()
def get_resource_from_brain(self, brain): cls = self.get_cls(brain.format) return cls(abspath=Path(brain.abspath), database=self, brain=brain)
def get_menu_namespace_level(self, context, url, use_first_child=False): menu_abspath = self.abspath here = context.resource here_abspath = here.abspath here_view_name = url[-1] here_abspath_and_view = '%s/%s' % (here_abspath, here_view_name) items = [] for resource in self.get_resources_in_order(): uri = resource.get_value('path') if not self._is_allowed_to_access(context, uri): continue ref, path, view = split_reference(uri) title = resource.get_value('title') target = resource.get_value('target') # Case 1: External link if ref.scheme: items.append({ 'id': 'menu_%s' % resource.name, 'path': str(ref), 'real_path': None, 'title': title, 'description': None, 'in_path': False, 'active': False, 'class': None, 'target': target, 'items': [] }) continue # Case 2: Internal link # Sub level subtabs = resource.get_menu_namespace_level( context, url, use_first_child) resource = self.get_resource(path, soft=True) item_id = 'menu_%s' % resource.name # Use first child by default we use the resource itself resource_path = path # Keep the real path to avoid highlight problems resource_original_path = path # use first child if use_first_child and subtabs: sub_path = subtabs[0]['real_path'] # if the first child is an external link => skip it if sub_path is not None: resource_path = sub_path # Set active, in_path active = in_path = False # add default view if view: resource_method = view[2:] item_id += '_%s' % resource_method else: resource_method = resource.get_default_view_name() resource_abspath_and_view = '%s/;%s' % (resource.abspath, resource_method) if here_abspath_and_view == resource_abspath_and_view: active = True else: # Use the original path for the highlight res_abspath = menu_abspath.resolve2(resource_original_path) common_prefix = here_abspath.get_prefix(res_abspath) # Avoid to always set the root entree 'in_path' # If common prefix equals root abspath set in_path to False # otherwise compare common_prefix and res_abspath if common_prefix != Path('/'): in_path = (common_prefix == res_abspath) # Build the new reference with the right path ref2 = deepcopy(ref) resource = self.get_resource(resource_path) ref2.path = context.get_link(resource) if view: ref2.path += view items.append({ 'id': item_id, 'path': str(ref2), 'real_path': resource.abspath, 'title': title, 'description': None, # FIXME 'in_path': active or in_path, 'active': active, 'class': None, 'target': target, 'items': subtabs }) # Set class x = None for i, item in enumerate(items): if item['active']: x = i break if item['in_path'] and x is None: x = i break if x is not None: items[x]['class'] = 'in-path' if len(items) > 0: # Add class "first" to the first item css = items[0]['class'] or '' items[0]['class'] = css + ' first' # Add class "last" to the last item css = items[-1]['class'] or '' items[-1]['class'] = css + ' last' return items
def get_path(path): if type(path) is not Path: path = Path(path) return str(path)
def test1(self): a = Path('/a/b/c') b = Path('/a/b/d/e') self.assertEqual(a.get_prefix(b), '/a/b')
def init_from_environ(self, environ, user=None): from server import get_server # Set environ self.environ = environ path = environ.get('PATH_INFO') self.path = path self.header_response = [] self.content_type = None self.status = None # Get database server = get_server() self.server = server self.database = server.database # Root self.root = self.database.get_resource('/') # The request method self.method = environ.get('REQUEST_METHOD') # Get body self.body = self.get_body_from_environ() # The query query = environ.get('QUERY_STRING') self.query = decode_query(query) # Accept language accept_language = self.environ.get('HTTP_ACCEPT_LANGUAGE', '') if accept_language is None: accept_language = '' try: self.accept_language = AcceptLanguageType.decode(accept_language) except: # Cannot decode accept language pass # The URI as it was typed by the client xfp = environ.get('HTTP_X_FORWARDED_PROTO') src_scheme = xfp or 'http' xff = environ.get('HTTP_X-Forwarded-Host') if xff: xff = xff.split(',', 1)[0].strip() src_host = xff or environ.get('HTTP_HOST') if query: uri = '%s://%s%s?%s' % (src_scheme, src_host, path, query) else: uri = '%s://%s%s' % (src_scheme, src_host, path) self.uri = get_reference(uri) # Split the path into path and method ("a/b/c/;view") path = path if type(path) is Path else Path(path) name = path.get_name() if name and name[0] == ';': self.path = path[:-1] self.view_name = name[1:] else: self.path = path self.view_name = None # Cookies self.cookies = {} # Media files (CSS, javascript) # Set the list of needed resources. The method we are going to # call may need external resources to be rendered properly, for # example it could need an style sheet or a javascript file to # be included in the html head (which it can not control). This # attribute lets the interface to add those resources. self.styles = [] self.scripts = [] # Log user if user is given if user: self.login(user) # The authenticated user self.authenticate() # Search self._context_user_search = self._user_search(self.user) # The Site Root self.find_site_root() self.site_root.before_traverse(self) # Hook # Not a cron self.is_cron = False # Set header self.set_header('Server', 'ikaaro.web')
class LocalFolder(object): def __init__(self, path="."): if not exists(path): raise IOError, "No such directory: '%s'" % path if isfile(path): raise IOError, "Is a directory: '%s'" % path self.path = Path(abspath(path)) def _resolve_path(self, path): path = self.path.resolve2(path) return str(path) ####################################################################### # Public API ####################################################################### def exists(self, path): path = self._resolve_path(path) return exists(path) def is_file(self, path): path = self._resolve_path(path) return isfile(path) def is_folder(self, path): path = self._resolve_path(path) return isdir(path) def can_read(self, path): path = self._resolve_path(path) return access(path, R_OK) def can_write(self, path): path = self._resolve_path(path) return access(path, W_OK) def make_file(self, path): path = self._resolve_path(path) parent_path = dirname(path) if exists(parent_path): if exists(path): raise OSError, "File exists: '%s'" % path else: makedirs(parent_path) return file(path, "wb") def make_folder(self, path): path = self._resolve_path(path) return makedirs(path) def get_ctime(self, path): path = self._resolve_path(path) ctime = getctime(path) return datetime.fromtimestamp(ctime) def get_mtime(self, path): path = self._resolve_path(path) mtime = getmtime(path) return datetime.fromtimestamp(mtime) def get_atime(self, path): path = self._resolve_path(path) atime = getatime(path) return datetime.fromtimestamp(atime) def get_mimetype(self, path): path = self._resolve_path(path) # Not a file ? if not isfile(path): return "application/x-not-regular-file" name = basename(path) return get_mimetype(name) def get_size(self, path): path = self._resolve_path(path) return getsize(path) def open(self, path, mode=None): path = self._resolve_path(path) if isdir(path): return self.__class__(path) mode = MODES.get(mode, "rb") return file(path, mode) def remove(self, path): path = self._resolve_path(path) if isdir(path): # Remove folder contents rmtree(path) else: os_remove(path) def copy(self, source, target): source = self._resolve_path(source) target = self._resolve_path(target) if isdir(source): # Copy inside target if exists(target): target = join(target, basename(source)) copytree(source, target) else: # Will overwrite target file shutil_copy(source, target) def move(self, source, target): source = self._resolve_path(source) target = self._resolve_path(target) return renames(source, target) def get_names(self, path="."): path = self._resolve_path(path) try: return listdir(path) except OSError, e: # Path does not exist or is not a directory if e.errno == 2 or e.errno == 20: return [] raise
parser.add_option('-s', '--srx', help='Use an other SRX file than the default one.') options, args = parser.parse_args() if len(args) != 0: parser.error('incorrect number of arguments') # Read configuration for languages config = get_config() src_language = config.get_value('source_language', default='en') # Get local folder package_root = config.get_value('package_root') if lfs.exists(package_root): locale_folder_path = Path('{0}/locale'.format(package_root)) else: locale_folder_path = Path('locale/') locale_folder = lfs.open(locale_folder_path) # The SRX file if options.srx is not None: srx_handler = ro_database.get_handler(options.srx) else: srx_handler = None # Initialize message catalog po = POFile() lines = [] for line in open('MANIFEST').readlines(): line = line.strip()
def _before_commit(self): root = self.get_resource('/') context = get_context() if context.database != self: raise ValueError('The contextual database is not coherent') # Update resources for path in deepcopy(self.resources_new2old): resource = root.get_resource(path) resource.update_resource(context) # 1. Update links when resources moved # XXX With this code '_on_move_resource' is called for new resources, # should this be done? old2new = [ (s, t) for s, t in self.resources_old2new.items() if t and s != t ] old2new.sort(key=lambda x: x[1]) # Sort by target for source, target in old2new: target = Path(target) resource = root.get_resource(target) resource._on_move_resource(source) # 2. Find out resources to re-index because they depend on another # resource that changed to_reindex = set() aux = set() aux2 = set(self.resources_old2new.keys()) while len(aux) != len(aux2): aux = set(aux2) # XXX we regroup items by 200 because Xapian is slow # when there's too much items in OrQuery l_aux = list(aux) for sub_aux in [l_aux[n:n+200] for n in range(0, len(l_aux), 200)]: query = [ PhraseQuery('onchange_reindex', x) for x in sub_aux ] query = OrQuery(*query) search = self.search(query) for brain in search.get_documents(): path = brain.abspath aux2.add(path) to_reindex.add(path) # 3. Documents to unindex (the update_links methods calls # 'change_resource' which may modify the resources_old2new dictionary) docs_to_unindex = self.resources_old2new.keys() self.resources_old2new.clear() # 4. Update mtime/last_author user = context.user userid = user.name if user else None for path in self.resources_new2old: if context.set_mtime: resource = root.get_resource(path) resource.metadata.set_property('mtime', context.timestamp) resource.metadata.set_property('last_author', userid) # Remove from to_reindex if resource has been deleted to_reindex = to_reindex - set(docs_to_unindex) # 5. Index docs_to_index = self.resources_new2old.keys() docs_to_index = set(docs_to_index) | to_reindex docs_to_unindex = list(set(docs_to_unindex) - docs_to_index) docs_to_index = list(docs_to_index) aux = [] for path in docs_to_index: resource = root.get_resource(path, soft=True) if resource: values = resource.get_catalog_values() aux.append((resource, values)) docs_to_index = aux self.resources_new2old.clear() # 6. Find out commit author & message if user: user_email = user.get_value('email') git_author = (userid, user_email or 'nobody') else: git_author = ('nobody', 'nobody') git_msg = getattr(context, 'git_message', None) if not git_msg: if context.method and context.uri: git_msg = "%s %s" % (context.method, context.uri) action = getattr(context, 'form_action', None) if action: git_msg += " action: %s" % action else: git_msg = git_msg.encode('utf-8') # Ok git_date = context.fix_tzinfo(context.timestamp) return git_author, git_date, git_msg, docs_to_index, docs_to_unindex
def test_resolve_wo_slash(self): before = Path('/a/b') after = Path('/a/c') self.assertEqual(before.resolve('c'), after)
def decode(value): return Path(value)
def test_wo_to_w_eq_path_dot(self): """The path to the same with a trailing slash returns Path('.').""" self.assertEqual(self.wo_to_w, Path('.'))
def decode(value): if not value: return '' return Path(value)
def test2(self): a = Path('/a/') self.assertEqual(a.get_pathtoroot(), '')
def update_locale(srx_handler, exclude_folders, no_wrap=False): # Read configuration for languages config = get_config() src_language = config.get_value('source_language', default='en') # Get local folder package_root = config.get_value('package_root') if lfs.exists(package_root): locale_folder_path = Path('{0}/locale'.format(package_root)) else: locale_folder_path = Path('locale/') locale_folder = lfs.open(locale_folder_path) # Initialize message catalog po = POFile() lines = [] for line in open('MANIFEST').readlines(): line = line.strip() exclude_folder = False for x in exclude_folders: if line.startswith(x): exclude_folder = True break if exclude_folder is False: lines.append(line) # Process Python and HTML files write('* Extract text strings') extensions = [ '.py', '.js', '.xhtml.%s' % src_language, '.xml.%s' % src_language, '.html.%s' % src_language ] for path in lines: # Filter files for extension in extensions: if path.endswith(extension): break else: continue # Get the units write('.') try: handler = ro_database.get_handler(path) except Exception: print print '*' print '* Error:', path print '*' raise try: units = handler.get_units(srx_handler=srx_handler) units = list(units) except Exception: print print '*' print '* Error:', path print '*' raise relative_path = locale_folder_path.get_pathto(path) for source, context, line in units: po.add_unit(relative_path, source, context, line) print write('* Update PO template ') data = po.to_str() # Write the po into the locale.pot try: locale_pot = locale_folder.open('locale.pot', WRITE) except IOError: # The locale.pot file does not exist create and open locale_pot = locale_folder.make_file('locale.pot') else: with locale_pot: locale_pot.write(data) # Update PO files filenames = set([x for x in locale_folder.get_names() if x[-3:] == '.po']) filenames.add('%s.po' % src_language) for language in config.get_value('target_languages'): filenames.add('%s.po' % language) filenames = list(filenames) filenames.sort() print '* Update PO files:' locale_pot_path = locale_folder.get_absolute_path('locale.pot') for filename in filenames: if locale_folder.exists(filename): write(' %s ' % filename) file_path = locale_folder.get_absolute_path(filename) if no_wrap: call([ 'msgmerge', '--no-wrap', '-U', '-s', file_path, locale_pot_path ]) else: call(['msgmerge', '-U', '-s', file_path, locale_pot_path]) else: print ' %s (new)' % filename file_path = locale_folder.get_absolute_path(filename) lfs.copy(locale_pot_path, file_path) print
def __init__(self, path="."): if not exists(path): raise IOError, "No such directory: '%s'" % path if isfile(path): raise IOError, "Is a directory: '%s'" % path self.path = Path(abspath(path))
def get_namespace(self, resource, context): path = context.query['path'] if path is None: path = Path('.') if path.startswith_slash: path.startswith_slash = False # Namespace: the location base = '/%s/;browse_tests' % context.site_root.get_pathto(resource) link = base + '?path=%s' location = [{'name': MSG(u'Test Suite'), 'link': link % '.'}] for i, name in enumerate(path): p = path[:i+1] try: test_suite.get_handler(p) except LookupError: location.append({'name': name, 'link': None}) body = MSG(u'The "{path}" resource has not been found') body = body.gettext(path=p) return {'location': location, 'body': body} else: location.append({'name': name, 'link': link % p}) # Get the handler handler = test_suite.get_handler(path) # (1) View PO file root = context.root if isinstance(handler, POFile): template = root.get_resource('/ui/odf-i18n/view_po.xml') units = handler.get_units() msgs = [ {'id': x.source, 'str': x.target} for x in units ] namespace = {'messages': msgs} body = stl(template, namespace) return {'location': location, 'body': body} # Load setup file if handler.has_handler('setup.conf'): setup = handler.get_handler('setup.conf', cls=ConfigFile) else: setup = None # (2) Browse Folders children = handler.get_handler_names() children.sort() a_handler = handler.get_handler(children[0]) if isinstance(a_handler, Folder): files = [] for child in children: child_handler = handler.get_handler(child) number = 0 for x in test_suite.database.fs.traverse(child_handler.key): if x.endswith('.po'): number += 1 files.append({'child_name': child, 'to_child': link % ('%s/%s' % (path, child)), 'number': number}) namespace = {'content': files} template = root.get_resource('/ui/odf-i18n/browse_folder.xml') body = stl(template, namespace) return {'location': location, 'body': body} # (3) Test Folder if setup is None: title = description = reference = url_reference = None else: title = setup.get_value('title') description = setup.get_value('description') reference = setup.get_value('reference') url_reference = setup.get_value('url_reference') # Format the description (may contain XML) description = XMLParser(description) files = [] for child in children: if child != 'setup.conf': child_path = '%s/%s' % (path, child) view = (link % child_path) if child.endswith('.po') else None files.append({ 'child_name': child, 'view': view, 'to_child': ';download?path=%s' % child_path}) template = root.get_resource('/ui/odf-i18n/browse_test.xml') namespace = { 'title': title, 'description': description, 'reference': reference, 'url_reference': url_reference, 'content': files} body = stl(template, namespace) return {'location': location, 'body': body}
def get_namespace(self, resource, context): root = context.root here = context.resource site_root = here.get_site_root() site_root_abspath = site_root.get_abspath() shop = site_root.get_resource('shop') categories_abspath = str(site_root_abspath.resolve2('categories')) show_nb_products = resource.get_property('show_nb_products') show_first_category = resource.get_property('show_first_category') show_second_level = resource.get_property('show_second_level') here_abspath = here.get_abspath() here_real_abspath = str(here_abspath) here_parent_abspath = here_abspath.resolve2('..') current_level = here_real_abspath.count('/') if here.metadata.format == 'category': here_abspath = str(here.get_abspath()) # Max level deploy = count '/' + 1 # here_abspath at level 1 does not contain '/' here_abspath_level = here_abspath.count('/') max_level_deploy = here_abspath_level + 1 else: if here.metadata.format == 'product': # Special case for the product, here_* values are compute # with here = parent category parent_category = here.parent parent_category_abspath = parent_category.get_abspath() here_abspath = str(parent_category_abspath) here_abspath_level = here_abspath.count('/') max_level_deploy = here_abspath_level + 1 here_parent_abspath = parent_category_abspath.resolve2('..') else: # Tweak here_abspath here_abspath = categories_abspath here_abspath_level = here_abspath.count('/') max_level_deploy = categories_abspath.count('/') + 1 here_abspath_p = Path(here_abspath) # Get search with all publics products all_products = root.search(AndQuery( PhraseQuery('format', shop.product_class.class_id), PhraseQuery('workflow_state', 'public'))) # Get search with all categories all_categories = root.search(AndQuery( PhraseQuery('parent_paths', categories_abspath), PhraseQuery('format', 'category'))) # Build a dict with brains by level cat_per_level = {} for index, cat in enumerate(all_categories.get_documents( sort_by='abspath')): # Skip first category --> /categories if index == 0 and show_first_category is False: continue level = cat.abspath.count('/') # Skip second level (if we are not on level /categories/') if (show_second_level is False and current_level > 2 and level == 3 and not here_real_abspath == cat.abspath and not here_parent_abspath == cat.abspath): continue # Skip bad level if level > max_level_deploy: continue diff_level = here_abspath_level - level path_to_resolve = [ '..' for x in range(diff_level) ] path_to_resolve = '/'.join(path_to_resolve) # Path uses to compute the prefix with the current category here_prefix = here_abspath_p.resolve2(path_to_resolve) # Compute the prefix prefix = here_prefix.get_prefix(cat.abspath) if prefix == here_prefix: # special case when prefix equals here_prefix pass elif len(prefix) != level-1: # bad, not in the same arborescence continue # Get the product number in the category sub_results = all_products.search(PhraseQuery('parent_paths', cat.abspath)) cats = cat_per_level.setdefault(level, []) cats.append({'doc': cat, 'nb_products': len(sub_results)}) # Build the tree starting with the higher level tree_template = resource.get_resource(self.tree_template) levels = sorted(cat_per_level.keys(), reverse=True) tree = None for level in levels: items = [] for data in cat_per_level[level]: doc = data['doc'] if here_abspath.startswith(doc.abspath): sub_tree = tree css = 'in-path ' else: sub_tree = None css = '' css = 'active ' if here_abspath == doc.abspath else css # Href (get_link emulation) href = str(site_root_abspath.get_pathto(doc.abspath)) css += checkid(href) css = css.replace('.', '-dot-') if resource.get_property('use_small_title'): title = doc.m_breadcrumb_title or doc.m_title or doc.name else: title = doc.m_title or doc.name d = {'title': title, 'href': '/%s' % href, 'sub_tree': sub_tree, 'nb_products': data['nb_products'], 'css': css} items.append(d) tree = stl(tree_template, {'items': items, 'show_nb_products': show_nb_products, 'css': None}) return {'title': resource.get_title(), 'tree': tree}
def _before_commit(self): context = get_context() root = context.root # 1. Update links when resources moved # XXX With this code '_on_move_resource' is called for new resources, # should this be done? old2new = [(s, t) for s, t in self.resources_old2new.items() if t and s != t] old2new.sort(key=lambda x: x[1]) # Sort by target for source, target in old2new: target = Path(target) resource = root.get_resource(target) resource._on_move_resource(source) # 2. Find out resources to re-index because they depend on another # resource that changed to_reindex = set() aux = set() aux2 = set(self.resources_old2new.keys()) while len(aux) != len(aux2): aux = set(aux2) query = [PhraseQuery('onchange_reindex', x) for x in aux] query = OrQuery(*query) search = self.search(query) for brain in search.get_documents(): path = brain.abspath aux2.add(path) to_reindex.add(path) # 3. Documents to unindex (the update_links methods calls # 'change_resource' which may modify the resources_old2new dictionary) docs_to_unindex = self.resources_old2new.keys() docs_to_unindex = list(set(docs_to_unindex) | to_reindex) self.resources_old2new.clear() # 4. Update mtime/last_author user = context.user userid = user.name if user else None if context.set_mtime: for path in self.resources_new2old: resource = root.get_resource(path) resource.metadata.set_property('mtime', context.timestamp) resource.metadata.set_property('last_author', userid) # 5. Index docs_to_index = self.resources_new2old.keys() docs_to_index = list(set(docs_to_index) | to_reindex) aux = [] for path in docs_to_index: resource = root.get_resource(path, soft=True) if resource: values = resource.get_catalog_values() aux.append((resource, values)) docs_to_index = aux self.resources_new2old.clear() # 6. Find out commit author & message if user: git_author = (userid, user.get_value('email')) else: git_author = ('nobody', 'nobody') git_msg = getattr(context, 'git_message', None) if not git_msg: git_msg = "%s %s" % (context.method, context.uri) action = getattr(context, 'form_action', None) if action: git_msg += " action: %s" % action else: git_msg = git_msg.encode('utf-8') # Ok git_date = context.fix_tzinfo(context.timestamp) return git_author, git_date, git_msg, docs_to_index, docs_to_unindex