コード例 #1
0
    def test_open_firestore(self):
        from fs.opener import open_fs

        fire_fs = open_fs("firestore://")
        self.assertIsInstance(fire_fs, FirestoreFS)
コード例 #2
0
 def make_source_fs(self):
     return open_fs("mem://")
コード例 #3
0
 def test_opener(self):
     from fs.ftpfs import FTPFS
     ftp_fs = open_fs('ftp://*****:*****@ftp.example.org')
     self.assertIsInstance(ftp_fs, FTPFS)
     self.assertEqual(ftp_fs.host, 'ftp.example.org')
コード例 #4
0
 def test_opener(self):
     ftp_fs = open_fs("ftp://*****:*****@ftp.example.org")
     self.assertIsInstance(ftp_fs, FTPFS)
     self.assertEqual(ftp_fs.host, "ftp.example.org")
コード例 #5
0
ファイル: test_opener.py プロジェクト: songzcn/pyfilesystem2
 def test_open_fs(self):
     mem_fs = opener.open_fs("mem://")
     mem_fs_2 = opener.open_fs(mem_fs)
     self.assertEqual(mem_fs, mem_fs_2)
コード例 #6
0
 def get_fs(self, path):
     if path is None:
         path = join(get_moya_dir(), './documentation')
     fs = open_fs(path, create=True)
     return fs
コード例 #7
0
ファイル: test_zipfs.py プロジェクト: zopyx/pyfilesystem2
 def make_source_fs(self):
     return open_fs('mem://')
コード例 #8
0
    def load(self, fs, settings_path=None):
        self.loaded = True
        self.load_fs = fs
        self.loaded_ini = fs.desc("lib.ini")
        try:
            self._cfg = cfg = SettingsContainer.read(fs, "lib.ini")
        except FSError as e:
            raise errors.LibraryLoadError(
                'failed to load lib.ini from "{path}" ({exc})',
                path=fs.desc("lib.ini"),
                exc=e,
                lib=self,
            )

        def cfgget(section, key, bool=False, default=Ellipsis):
            try:
                if bool:
                    value = cfg[section][key].strip().lower() in ("yes",
                                                                  "true")
                else:
                    value = cfg[section][key]
            except KeyError:
                if default is Ellipsis:
                    raise errors.LibraryLoadError(
                        "required key [{}]/{} not found in lib.ini".format(
                            section, key),
                        lib=self,
                    )
                return default
            else:
                return value

        self.long_name = cfgget("lib", "name")

        if self.long_name in self.archive.libs:
            raise errors.LibraryLoadError(
                "already loaded this library from '{}'".format(
                    self.loaded_ini),
                lib=self,
                diagnosis=
                "Check for a previous <import> that loads this library",
            )

        py_requires = cfgget("lib", "pyrequires", default=None)
        if py_requires:
            try:
                version_ok = pyversion.check(py_requires)
            except ValueError as e:
                raise errors.LibraryLoadError(
                    "bad Py version specification in [lib]/pyrequires ({})".
                    format(text_type(e)),
                    lib=self,
                )
            if not version_ok:
                versions = ", ".join(
                    "Python {}.{}".format(*v)
                    for v in pyversion.list_compatible(py_requires))
                raise errors.LibraryLoadError(
                    "one of the following Python versions required: {versions}",
                    lib=self.long_name,
                    versions=versions,
                )

        self.title = cfgget("lib", "title", default=None)
        self.url = cfgget("lib", "url", default=None)
        try:
            self.version = Version(cfgget("lib", "version"))
        except ValueError as e:
            raise errors.LibraryLoadError(text_type(e), lib=self.long_name)

        self.namespace = cfgget("lib", "namespace")
        self.docs_location = cfgget("lib", "location", default=None)
        self.tests_location = cfgget("tests", "location", default=None)

        self.system_settings = {
            "templates_directory": self.long_name or "",
            "data_directory": self.long_name or "",
        }

        project_cfg = self.archive.cfg

        settings = SettingsSectionContainer()

        def update_settings(section):
            settings.update(
                (k, SettingContainer(v)) for k, v in iteritems(cfg[section]))

        if "author" in cfg:
            self.author.update(cfg["author"])

        if "lib" in cfg:
            self.libinfo.update(cfg["lib"])

        if "settings" in cfg:
            update_settings("settings")

        if "templates" in cfg:
            self.templates_info = cfg["templates"]

        if "data" in cfg:
            self.data_info = cfg["data"]
            location = cfgget("data", "location")
            try:
                self.data_fs = fs.opendir(location)
            except FSError as e:
                raise errors.LibraryLoadError(
                    "Unable to read data from {path} ({exc})",
                    path=location,
                    exc=e,
                    lib=self,
                )

        if "documentation" in cfg:
            self.documentation_location = cfg["documentation"].get(
                "location", "./docs")

        if "translations" in cfg:
            i18n = cfg["translations"]
            self.translations_location = i18n.get("location", "./translations")
            self.default_language = i18n.get("default_language", "en")
            self.languages = split_commas(i18n.get("languages", "en"))
            self._localedir = self.load_fs.getsyspath(
                self.translations_location)
            if self.languages:
                startup_log.debug(
                    "%s reading translations %s",
                    self,
                    textual_list(self.languages, "and"),
                )
                self.translations.read("messages", self._localedir,
                                       self.languages)

        if project_cfg and ("lib:" + self.long_name) in project_cfg:
            update_settings("lib:" + self.long_name)

        self.settings = settings

        for section_name, section in iteritems(cfg):
            if ":" in section_name:
                what, name = section_name.split(":", 1)
            else:
                continue

            if what.startswith("py"):
                if self.no_py:
                    continue
                try:
                    version_ok = pyversion.check(what)
                except ValueError as e:
                    raise errors.LibraryLoadError(
                        "Bad Py version specification ({})".format(
                            text_type(e)),
                        lib=self,
                    )
                if version_ok:
                    location = cfgget(section_name, "location")
                    py_fs = fs.opendir(location)
                    try:
                        fs_import(self, py_fs, name or self.long_name)
                    except errors.StartupFailedError as e:
                        raise errors.LibraryLoadError(text_type(e),
                                                      exc=e,
                                                      lib=self)
                    except Exception as e:
                        raise
                        # console.exception(e, tb=True).div()
                        raise errors.LibraryLoadError(
                            "Error in Python extension",
                            py_exception=e,
                            lib=self)

            elif what == "media":
                location = cfgget(section_name, "location")
                try:
                    media_fs = fs.opendir(location)
                except FSError as e:
                    raise errors.LibraryLoadError(
                        "Unable to read media from {path} ({exc})",
                        path=location,
                        exc=e,
                        lib=self,
                    )
                if media_fs.hassyspath("/"):
                    self.media[name] = open_fs(media_fs.getsyspath("/"))
                else:
                    self.media[name] = media_fs

        if self.docs_location:
            with self.load_fs.opendir(self.docs_location) as docs_fs:
                self.import_documents(docs_fs, recurse=True)
コード例 #9
0
ファイル: environment.py プロジェクト: moyaproject/moya
 def make_default(self):
     return Environment(open_fs("mem://"))
コード例 #10
0
 def location_fs(self):
     if self._location_fs is None:
         self._location_fs = open_fs(self.location)
     return self._location_fs
コード例 #11
0
    def import_documents(self,
                         fs,
                         wildcard="*.xml",
                         recurse=False,
                         files=None):
        """Imports a number of documents in to the library"""

        if isinstance(fs, string_types):
            fs = open_fs(fs)
        # fs = wrap.DirCache(fs)
        if files is None:
            if recurse:
                files = sorted(fs.walk.files(filter=[wildcard]))
            else:
                files = sorted(info.name
                               for info in fs.filterdir(files=[wildcard],
                                                        exclude_dirs=["*"]))
        else:
            files = sorted(files)

        import_count = 0
        for filepath in files:
            parser = Parser(self.archive, fs, filepath, library=self)
            try:
                document = parser.parse()
                if document is not None:
                    self.documents.append(document)
                    import_count += 1

            except errors.ParseError as parse_error:
                line, col = parse_error.position

                if fs.hassyspath(filepath):
                    path = fs.getsyspath(filepath)
                else:
                    path = fs.desc(filepath)
                failed_doc = FailedDocument(
                    path=path,
                    code=parser.xml,
                    line=line,
                    col=col,
                    msg=text_type(parse_error),
                )

                self.failed_documents.append(failed_doc)

            except (errors.ElementError,
                    errors.UnknownElementError) as element_error:
                line = element_error.source_line
                col = 0
                if fs.hassyspath(filepath):
                    path = fs.getsyspath(filepath)
                else:
                    path = fs.desc(filepath)
                failed_doc = FailedDocument(
                    path=path,
                    code=parser.xml,
                    line=line,
                    col=col,
                    msg=text_type(element_error),
                )

                self.failed_documents.append(failed_doc)

        return import_count
コード例 #12
0
 def __init__(self, root, uid, gid):
     self.fs = open_fs(root)
     self.uid = uid
     self.gid = gid
コード例 #13
0
def install(
    project_path,
    server_xml_location,
    server_xml,
    server_name,
    lib_path,
    lib_name,
    app_name,
    mount=None,
):
    from lxml.etree import fromstring, ElementTree, parse
    from lxml.etree import XML, Comment

    changes = 0
    with open_fs(project_path) as project_fs:
        with project_fs.opendir(server_xml_location) as server_fs:

            with server_fs.open(server_xml, "rb") as server_xml_file:
                root = parse(server_xml_file)

            import_tag = XML('<import lib="{lib_name}"/>'.format(lib_name=lib_name))
            import_tag.tail = "\n"

            if app_name is None:
                install_tag = XML(
                    '<install lib="{lib_name}" />'.format(lib_name=lib_name)
                )
            else:
                install_tag = XML(
                    '<install lib="{lib_name}" name="{app_name}"/>'.format(
                        app_name=app_name, lib_name=lib_name
                    )
                )

            install_tag.tail = "\n"

            def has_child(node, tag, **attribs):
                for el in node.findall(tag):
                    if all(el.get(k, None) == v for k, v in attribs.items()):
                        return True
                return False

            server_el = "{{http://moyaproject.com}}server[@docname='{}']".format(
                server_name
            )
            for server in root.findall(server_el):

                def get_comment():
                    comment = Comment("added by moya-pm")
                    return comment

                if not has_child(
                    server, "{http://moyaproject.com}import", lib=lib_name
                ):
                    server.insert(0, import_tag)
                    server.insert(0, get_comment())
                    changes += 1
                if not has_child(
                    server, "{http://moyaproject.com}install", lib=lib_name
                ):
                    server.append(Comment("added by moya-pm"))
                    server.append(install_tag)
                    changes += 1
                    if mount is not None and app_name is not None:
                        if not has_child(
                            server, "{http://moyaproject.com}mount", app_name=app_name
                        ):
                            mount_tag = XML(
                                '<mount app="{app_name}" url="{mount}" />'.format(
                                    app_name=app_name, mount=mount
                                )
                            )
                            mount_tag.tail = "\n"
                            server.append(get_comment())
                            server.append(mount_tag)
                            changes += 1

            with server_fs.open(server_xml, "wb") as server_xml_file:
                root.write(server_xml_file)
    return bool(changes)
コード例 #14
0
ファイル: config.py プロジェクト: moyaproject/moya
    def logic(self, context):
        start = time()
        (
            name,
            _location,
            lib,
            py,
            priority,
            template_priority,
            data_priority,
        ) = self.get_parameters(
            context,
            "name",
            "location",
            "lib",
            "py",
            "priority",
            "templatepriority",
            "datapriority",
        )
        if template_priority is None:
            template_priority = priority
        archive = self.document.archive
        absolute = False
        import_fs = None
        if lib is not None:
            import_fs = archive.find_lib(lib)
            if import_fs is None:
                self.throw(
                    "import.fail",
                    "lib '{}' not found; searched {}".format(
                        lib, tools.textual_list(archive.lib_paths, join_word="and")
                    ),
                )
        elif _location is not None:
            location = _location
        else:
            if py in sys.modules:
                reload(sys.modules[py])
            try:
                __import__(py)
            except ImportError as e:
                raise errors.ElementError(
                    "unable to import Python module '{}'".format(py),
                    element=self,
                    diagnosis=text_type(e),
                )
            module = sys.modules[py]
            location = dirname(abspath(module.__file__))
            absolute = True

        try:
            if import_fs is None:
                if "::/" in location:
                    import_fs = open_fs(location)
                else:
                    if absolute:
                        import_fs = open_fs(location)
                    else:
                        project_fs = context["fs"]
                        try:
                            if project_fs.hassyspath("/"):
                                project_path = project_fs.getsyspath("/")
                                import_path = join(project_path, location)
                                try:
                                    import_fs = open_fs(import_path)
                                except ResourceNotFound:
                                    self.throw(
                                        "import.fail",
                                        "location '{}' was not found".format(
                                            import_path
                                        ),
                                        diagnosis="Check the location is exists and is a directory.",
                                    )
                            else:
                                import_fs = context["fs"].opendir(location)
                        except (IllegalBackReference, FSError) as e:
                            self.throw(
                                "import.fail",
                                "unable to import location '{}' from {}".format(
                                    location, project_fs
                                ),
                                diagnosis=text_type(e),
                            )
            lib = archive.load_library(
                import_fs,
                priority=priority,
                template_priority=template_priority,
                data_priority=data_priority,
                long_name=name,
                rebuild=context.root.get("_rebuild", False),
            )
            if lib.failed_documents:
                if _location is not None:
                    msg = "Failed to load library '{}' from location '{}'"
                    raise errors.StartupFailedError(
                        msg.format(name or lib.long_name, _location)
                    )
                elif py:
                    msg = "Failed to load library '{}' from Python module '{}'"
                    raise errors.StartupFailedError(
                        msg.format(name or lib.long_name, py)
                    )
                else:
                    raise errors.StartupFailedError(
                        "Failed to load library '{}'".format(name or lib.long_name)
                    )
            startup_log.debug("%s imported %.1fms", lib, (time() - start) * 1000.0)
            if lib.priority:
                startup_log.debug("%s priority is %s", lib, lib.priority)
            if lib.template_priority:
                startup_log.debug(
                    "%s template priority is %s", lib, lib.template_priority
                )
        except Exception as e:
            if not self.archive.test_build:
                raise
コード例 #15
0
 def setUpClass(cls):
     cls.tmpfs = open_fs("temp://tarfstest")
コード例 #16
0
            lib.documents.append(document)
            lib.register_element(element)
            lib.register_named_element(element_name, element)

        lib.filters.update(expose.exposed_filters)

        expose.exposed_elements.clear()
        expose.exposed_filters.clear()

    finally:
        ElementRegistry.pop_registry()
        hook.uninstall()


if __name__ == "__main__":
    from fs.opener import open_fs
    m = open_fs("mem://")
    m.createfile('__init__.py')
    m.makedir("test")
    m.setbytes(b'test/__init__.py', 'print "Imported!"\ndef run():print "It Works!"')
    m.tree()

    hook = LibraryImportHook(m)
    hook.install()

    module = __import__("__moyapy__.test")
    #print module.test.run
    module.test.run()

    #print imp.find_module("moyapy.test")
コード例 #17
0
 def setUpClass(cls):
     cls.tmpfs = open_fs("temp://")
コード例 #18
0
    def run(self):
        parser = self.get_argparse()
        args = parser.parse_args(sys.argv[1:])

        if args.version is None:
            major, minor = __version__.split('.')[:2]
            version = "{}.{}".format(major, minor)
        else:
            version = args.version

        try:
            with open(expanduser(args.settings), 'rt') as f_ini:
                cfg = SettingsContainer.read_from_file(f_ini)
                print("Read settings from {}".format(args.settings))
        except IOError:
            cfg = SettingsContainer()

        from ..docgen.extracter import Extracter
        from ..docgen.builder import Builder
        from ..command import doc_project
        location = dirname(doc_project.__file__)

        try:
            base_docs_fs = OSFS('text')
        except FSError:
            sys.stderr.write('run me from moya/docs directory\n')
            return -1
        extract_fs = OSFS(join('doccode', version), create=True)
        languages = [
            d for d in base_docs_fs.listdir(dirs_only=True) if len(d) == 2
        ]

        def do_extract():
            print("Extracting docs v{}".format(version))
            utils.remove_all(extract_fs, '/')
            try:
                archive, context, doc = moya_build.build_server(
                    location, 'settings.ini')
            except Exception:
                raise
                return -1

            extract_fs.makedir("site/docs", recursive=True)
            extract_fs.makedir("site/tags", recursive=True)
            #extract_fs.makedir("libs")

            with extract_fs.opendir('site/tags') as tags_fs:
                extracter = Extracter(archive, tags_fs)
                const_data = {}
                builtin_tags = []
                for namespace in self.builtin_namespaces:
                    xmlns = getattr(namespaces, namespace, None)
                    if xmlns is None:
                        raise ValueError(
                            "XML namespace '{}' is not in namespaces.py".
                            format(namespace))
                    namespace_tags = archive.registry.get_elements_in_xmlns(
                        xmlns).values()
                    builtin_tags.extend(namespace_tags)

                extracter.extract_tags(builtin_tags, const_data=const_data)

            for language in languages:
                with extract_fs.makedirs("site/docs",
                                         recreate=True) as language_fs:
                    doc_extracter = Extracter(None, language_fs)
                    docs_fs = base_docs_fs.opendir(language)
                    doc_extracter.extract_site_docs(docs_fs, dirname=language)

        if args.extract:
            do_extract()

        if args.build:
            theme_path = cfg.get('paths', 'theme', None)
            dst_path = join('html', version)
            if theme_path is None:
                theme_fs = OSFS('theme')
            else:
                theme_fs = open_fs(theme_path)

            output_path = cfg.get('paths', 'output', None)

            if output_path is None:
                output_base_fs = OSFS(dst_path, create=True)
            else:
                output_root_base_fs = open_fs(output_path)
                output_base_fs = output_root_base_fs.makedirs(dst_path,
                                                              recreate=True)

            #output_base_fs = OSFS(join('html', version), create=True)
            utils.remove_all(output_base_fs, '/')

            def do_build():
                print("Building docs v{}".format(version))
                lib_info = {}
                lib_paths = {}
                for long_name, lib in self.document_libs:
                    lib_info[long_name] = moya_build.get_lib_info(lib)
                    lib_paths[long_name] = output_base_fs.getsyspath(
                        join('libs', long_name, 'index.html'))
                for language in languages:
                    docs_fs = base_docs_fs.makedirs(language)
                    output_fs = output_base_fs.makedirs(language)
                    utils.remove_all(output_fs, '/')

                    with extract_fs.opendir("site") as extract_site_fs:
                        builder = Builder(extract_site_fs, output_fs, theme_fs)
                        from ..tools import timer
                        with timer('render time'):
                            builder.build({
                                "libs": lib_info,
                                "lib_paths": lib_paths
                            })

                    # output_base_fs.makedir("libs", allow_recreate=True)
                    # for long_name, lib in self.document_libs:
                    #     source_path = extract_fs.getsyspath(join("libs", long_name))
                    #     output_path = output_base_fs.getsyspath('libs')
                    #     cmd_template = 'moya --debug doc build {} --theme libtheme --source "{}" --output "{}"'
                    #     cmd = cmd_template.format(lib, source_path, output_path)
                    #     os.system(cmd)

            def extract_build():
                do_extract()
                do_build()

            do_build()

            if not args.nobrowser:
                import webbrowser
                index_url = "file://" + output_base_fs.getsyspath(
                    'en/index.html')
                print(index_url)
                webbrowser.open(index_url)

            if args.watch:
                print("Watching for changes...")
                observer = Observer()
                path = base_docs_fs.getsyspath('/')

                reload_watcher = ReloadChangeWatcher(base_docs_fs,
                                                     extract_build)
                observer.schedule(reload_watcher, path, recursive=True)
                observer.start()

                while 1:
                    try:
                        time.sleep(0.1)
                    except:
                        break

        return 0
コード例 #19
0
 def test_open_userdata_no_version(self):
     app_fs = opener.open_fs("userdata://fstest:willmcgugan", create=True)
     self.assertEqual(app_fs.app_dirs.appname, "fstest")
     self.assertEqual(app_fs.app_dirs.appauthor, "willmcgugan")
     self.assertEqual(app_fs.app_dirs.version, None)
コード例 #20
0
    def __init__(self,
                 filesystem_url,
                 settings_path,
                 server="main",
                 logging=None,
                 disable_autoreload=False,
                 breakpoint=False,
                 breakpoint_startup=False,
                 validate_db=False,
                 simulate_slow_network=False,
                 debug_memory=False,
                 strict=False,
                 master_settings=None,
                 test_build=False,
                 develop=False,
                 load_expression_cache=True,
                 post_build_hook=None):
        self.filesystem_url = filesystem_url
        self.settings_path = settings_path
        self.server_ref = server
        self.logging = logging
        self.breakpoint = breakpoint
        self.validate_db = validate_db
        self.watching_fs = None
        self.rebuild_required = False
        self._new_build_lock = RLock()
        self.archive = None
        self._self = weakref.ref(self, self.on_close)
        self.simulate_slow_network = simulate_slow_network
        self.debug_memory = debug_memory
        self.master_settings = master_settings
        self.test_build = test_build
        self.develop = develop
        self.load_expression_cache = load_expression_cache
        self.post_build_hook = post_build_hook

        if logging is not None:
            with open_fs(self.filesystem_url) as logging_fs:
                init_logging_fs(logging_fs, logging)
        try:
            self.build(breakpoint=breakpoint_startup, strict=strict)
        except Exception as e:
            startup_log.critical(text_type(e))
            raise

        if self.archive.debug_memory:
            self.debug_memory = True

        if self.debug_memory and objgraph is None:
            self.debug_memory = False
            runtime_log.error(
                'memory debugging requires objgraph (https://pypi.python.org/pypi/objgraph)'
            )

        if self.debug_memory:
            runtime_log.warning(
                'memory debugging is on, this will effect performance')

        self.watcher = None
        if self.archive.auto_reload and not disable_autoreload:
            try:
                location = self.archive.project_fs.getsyspath('/')
            except FSError:
                log.warning(
                    'project filesystem has no syspath, disabling autoreload')
            else:
                watch_location = os.path.join(
                    location, self.archive.cfg.get('autoreload', 'location',
                                                   ''))
                self.watcher = ReloadChangeWatcher(open_fs(watch_location),
                                                   self)
コード例 #21
0
 def make_fs(self):
     return open_fs(
         "ftp://{}:{}@{}:{}".format(
             self.user, self.pasw, self.server.host, self.server.port
         )
     )
コード例 #22
0
 def test_open_userdata_no_version(self):
     app_fs = opener.open_fs('userdata://fstest:willmcgugan', create=True)
     self.assertEqual(app_fs.app_dirs.appname, 'fstest')
     self.assertEqual(app_fs.app_dirs.appauthor, 'willmcgugan')
     self.assertEqual(app_fs.app_dirs.version, None)
コード例 #23
0
ファイル: test_opener.py プロジェクト: songzcn/pyfilesystem2
 def test_unknown_protocol(self):
     with self.assertRaises(errors.UnsupportedProtocol):
         opener.open_fs('unknown://')
コード例 #24
0
 def make_source_fs(self):
     return open_fs("temp://")
コード例 #25
0
 def make_source_fs(self):
     return open_fs('temp://')
コード例 #26
0
ファイル: start.py プロジェクト: moyaproject/moya
    def start_project(self):
        console = self.console

        if not self.args.acceptdefaults:
            console.table([
                [
                    Cell("Moya Project Wizard",
                         bold=True,
                         fg="green",
                         center=True)
                ],
                [
                    """This will ask you a few questions, then create a new Moya project based on your answers.

Default values are shown in blue (hit return to accept defaults). Some defaults may be taken from your ".moyarc" file, if it exists."""
                ],
            ])

        author = self.get_author_details()
        project = {}
        project["title"] = ProjectTitle.ask(console, default=self.args.title)
        longname = make_name(author["organization"], project["title"])
        project["database"] = Database.ask(console, default="y")
        if project["database"]:
            project["auth"] = Auth.ask(console, default="y")
            project["signup"] = Signup.ask(console, default="y")
            project["pages"] = Pages.ask(console, default="y")
            project["blog"] = Blog.ask(console, default="y")
        project["feedback"] = Feedback.ask(console, default="y")
        project["comments"] = project.get("blog", False) or project.get(
            "pages", False)
        project["wysihtml5"] = project.get("blog", False) or project.get(
            "pages", False)
        project["jsonrpc"] = JSONRPC.ask(console, default="y")

        dirname = longname.split(".", 1)[-1].replace(".", "_")
        dirname = ProjectDirName.ask(console, default="./" + dirname)

        data = {
            "author": author,
            "project": project,
            "timezone": self.get_timezone(),
            "secret": make_secret(),
        }

        from ...command.sub import project_template

        memfs = open_fs("mem://")
        templatebuilder.compile_fs_template(memfs,
                                            project_template.template,
                                            data=data)

        dest_fs = open_fs(self.args.location or dirname,
                          create=True,
                          writeable=True)
        continue_overwrite = "overwrite"
        if not dest_fs.isempty("."):
            if self.args.force:
                continue_overwrite = "overwrite"
            elif self.args.new:
                continue_overwrite = "new"
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite == "overwrite":
            fs.copy.copy_dir(memfs, "/", dest_fs, "/")
            console.table([
                [
                    Cell(
                        "Project files written successfully!",
                        fg="green",
                        bold=True,
                        center=True,
                    )
                ],
                [
                    """See readme.txt in the project directory for the next steps.\n\nBrowse to http://moyaproject.com/gettingstarted/ if you need further help."""
                ],
            ])
            return 0
        elif continue_overwrite == "new":
            files_copied = copy_new(memfs, dest_fs)
            table = [[
                Cell(
                    "{} new file(s) written".format(len(files_copied)),
                    fg="green",
                    bold=True,
                    center=True,
                )
            ]]
            for path in files_copied:
                table.append([Cell(dest_fs.desc(path), bold=True, fg="black")])
            console.table(table)
            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1
コード例 #27
0
 def test_not_writeable(self):
     with self.assertRaises(NotWriteable):
         open_fs("zip://foo.zip", writeable=True)
コード例 #28
0
ファイル: start.py プロジェクト: moyaproject/moya
    def start_library(self):
        console = self.console

        from ...tools import get_moya_dir
        from os.path import join, abspath

        project_path = None
        if self.args.location is not None:
            library_path = self.args.location
        else:
            try:
                project_path = get_moya_dir(self.args.project_location)
            except:
                console.error(
                    "Please run 'moya start library' inside your project directory, or specifiy the -o switch"
                )
                return False
            library_path = abspath(join(project_path, "./local/"))

        cfg = None
        if not self.args.location and project_path:
            from ... import build

            cfg = build.read_config(project_path, self.get_settings())

        if not self.args.acceptdefaults:
            console.table([
                [
                    Cell("Moya Library Wizard",
                         bold=True,
                         fg="green",
                         center=True)
                ],
                [
                    """This will ask you a few questions, then create a new library in your Moya project based on your answers.

Default values are shown in grey (simply hit return to accept defaults). Some defaults may be taken from your ".moyarc" file, if it exists.
"""
                ],
            ])
        author = self.get_author_details()
        library = {}
        library["title"] = LibraryTitle.ask(console, default=self.args.title)
        longname = self.args.longname or make_name(author["organization"],
                                                   library["title"])
        longname = library["longname"] = LibraryLongName.ask(console,
                                                             default=longname)
        library["url"] = LibraryURL.ask(console, default="")
        library["namespace"] = LibraryNamespace.ask(console, default="")
        mount = None
        appname = None

        do_mount = DoMount.ask(console, default="yes")
        if do_mount:
            mount = Mount.ask(
                console,
                default=self.args.mount
                or "/{}/".format(make_name(library["title"])),
            )
            appname = AppName.ask(console,
                                  default=self.args.name
                                  or make_name(library["title"]))

        data = dict(author=author,
                    library=library,
                    timezone=self.get_timezone())

        actions = []

        from ...command.sub import library_template

        memfs = open_fs("mem://")
        templatebuilder.compile_fs_template(memfs,
                                            library_template.template,
                                            data=data)
        dest_fs = open_fs(join(library_path, library["longname"]),
                          create=True,
                          writeable=True)

        continue_overwrite = "overwrite"
        if not dest_fs.isempty("/"):
            if self.args.force:
                continue_overwrite = "overwrite"
            elif self.args.new:
                continue_overwrite = "new"
            else:
                continue_overwrite = DirNotEmpty.ask(console, default="cancel")

        if continue_overwrite != "cancel":
            if continue_overwrite == "overwrite":
                fs.copy.copy_dir(memfs, "/", dest_fs, "/")
                actions.append("Written library files to {}".format(
                    dest_fs.getsyspath(".")))
            elif continue_overwrite == "new":
                files_copied = copy_new(memfs, dest_fs)
                table = [[
                    Cell(
                        "{} new file(s) written".format(len(files_copied)),
                        fg="green",
                        bold=True,
                        center=True,
                    )
                ]]
                for path in files_copied:
                    table.append(
                        [Cell(dest_fs.desc(path), bold=True, fg="black")])
                console.table(table)
                return 0

            if cfg:
                project_cfg = cfg["project"]
                location = project_cfg["location"]
                server_name = "main"

                if location:
                    with open_fs(project_path) as project_fs:
                        with project_fs.opendir(location) as server_fs:
                            from lxml.etree import fromstring, ElementTree, parse
                            from lxml.etree import XML, Comment

                            server_xml_path = server_fs.getsyspath(
                                project_cfg["startup"])
                            root = parse(server_xml_path)
                            import_tag = XML(
                                '<import location="./local/{longname}" />\n\n'.
                                format(**library))
                            import_tag.tail = "\n"
                            install_tag = None

                            if mount:
                                tag = '<install name="{appname}" lib="{longname}" mount="{mount}" />'
                            else:
                                tag = '<install name="{appname}" lib="{longname}" />'
                            install_tag = XML(
                                tag.format(appname=appname,
                                           longname=longname,
                                           mount=mount))
                            install_tag.tail = "\n\n"

                            def has_child(node, tag, **attribs):
                                for el in node.findall(tag):
                                    if all(
                                            el.get(k, None) == v
                                            for k, v in attribs.items()):
                                        return True
                                return False

                            for server in root.findall(
                                    "{{http://moyaproject.com}}server[@docname='{}']"
                                    .format(server_name)):
                                add_import_tag = not has_child(
                                    server,
                                    "{http://moyaproject.com}import",
                                    location="./local/{}".format(longname),
                                )
                                add_install_tag = (not has_child(
                                    server,
                                    "{http://moyaproject.com}install",
                                    lib=longname,
                                ) and install_tag is not None)

                                if add_import_tag or add_install_tag:
                                    comment = Comment(
                                        "Added by 'moya start library'")
                                    comment.tail = "\n"
                                    server.append(comment)
                                if add_import_tag:
                                    server.append(import_tag)
                                    actions.append("Added <import> tag")
                                if add_install_tag:
                                    server.append(install_tag)
                                    actions.append("Added <install> tag")
                                    if mount:
                                        actions.append(
                                            "Mounted application on {}".format(
                                                mount))

                            root.write(server_xml_path)

            table = [[
                Cell(
                    "Library files written successfully!",
                    fg="green",
                    bold=True,
                    center=True,
                )
            ]]

            actions_text = "\n".join(" * " + action for action in actions)
            table.append([Cell(actions_text, fg="blue", bold=True)])
            table.append([
                """A new library has been added to the project, containing some simple example functionality.\nSee http://moyaproject.com/docs/creatinglibraries/ for more information."""
            ])
            console.table(table)

            return 0

        console.text("No project files written.", fg="red", bold=True).nl()
        return -1
コード例 #29
0
ファイル: test_opener.py プロジェクト: zopyx/pyfilesystem2
 def test_open_memfs(self):
     fs = opener.open_fs("mem://")
     self.assertIsInstance(fs, MemoryFS)
コード例 #30
0
def test_opener_webdav_443():
    result = open_fs('webdav://foo.bar:443/webdav')
    assert result.url.startswith('https://foo.bar')