def read_project_config(self, command_options):
        project_config = {}
        config = ZanataConfig()
        # Read the project configuration file using --project-config option
        config_file = [os.path.join(os.getcwd(), filename) for filename in ["zanata.xml", "flies.xml"]]

        if command_options.has_key("project_config"):
            config_file.append(command_options["project_config"][0]["value"])

        for path in config_file:
            if os.path.exists(path):
                log.info("Loading zanata project config from: %s" % path)
                project_config = config.read_project_config(path)
                break
        return project_config
示例#2
0
    def read_project_config(self, command_options):
        project_config = {}
        config = ZanataConfig()
        #Read the project configuration file using --project-config option
        config_file = [os.path.join(os.getcwd(), filename) for filename\
                        in ['zanata.xml', 'flies.xml']]

        if command_options.has_key('project_config'):
            config_file.append(command_options['project_config'][0]['value'])

        for path in config_file:
            if os.path.exists(path):
                log.info("Loading zanata project config from: %s" % path)
                project_config = config.read_project_config(path)
                break
        return project_config
示例#3
0
def read_project_config(command_options):
    project_config = {}
    config = ZanataConfig()
    #Read the project configuration file using --project-config option
    config_file = [os.path.join(os.getcwd(), filename) for filename\
                    in ['zanata.xml', 'flies.xml']]

    if command_options.has_key('project_config'):
        config_file.append(command_options['project_config'][0]['value'])

    for path in config_file:
        if os.path.exists(path):
            log.info("Loading zanata project config from: %s" % path)
            project_config = config.read_project_config(path)
            break

    return project_config
示例#4
0
def read_user_config(url, command_options):
    user_name = ""
    apikey = ""
    config = ZanataConfig()
    #Try to read user-config file
    user_config = [
        os.path.join(os.path.expanduser("~") + '/.config', filename)
        for filename in ['zanata.ini', 'flies.ini']
    ]
    if command_options.has_key('user_config'):
        user_config.append(command_options['user_config'][0]['value'])

    for path in user_config:
        if os.path.exists(path):
            log.info("Loading zanata user config from: %s" % path)
            #Read the user-config file
            config.set_userconfig(path)
            try:
                server = config.get_server(url)
                if server:
                    user_name = config.get_config_value(
                        "username", "servers", server)
                    apikey = config.get_config_value("key", "servers", server)
            except Exception, e:
                log.info("Processing user-config file:%s" % str(e))
                break

            break
示例#5
0
    def __init__(self, command_options, *args, **kwargs):
        self.command_options = command_options
        self.log = Logger()
        self.config = ZanataConfig()

        self.remote_config, self.local_config,\
            self.command_dict, self.context_data = [{} for i in range(4)]

        for option_set in self.command_options:
            self.command_dict.update(
                {option_set: self.command_options[option_set][0]['value']}
            )
示例#6
0
def read_user_config(url, command_options):
    user_name = ""
    apikey = ""
    config = ZanataConfig()
    #Try to read user-config file
    user_config = [os.path.join(os.path.expanduser("~") + '/.config', filename) for filename in ['zanata.ini', 'flies.ini']]

    if command_options.has_key('user_config'):
        user_config.append(command_options['user_config'][0]['value'])

    for path in user_config:
        if os.path.exists(path):
            log.info("Loading zanata user config from: %s" % path)

            #Read the user-config file
            config.set_userconfig(path)

            try:
                server = config.get_server(url)
                if server:
                    user_name = config.get_config_value("username", "servers", server)
                    apikey = config.get_config_value("key", "servers", server)
            except Exception, e:
                log.info("Processing user-config file:%s" % str(e))
                break
            
            break
 def __init__(self, options):
     self.command_options = options
     self.project_config = {}
     self.log = Logger()
     self.config = ZanataConfig()
class OptionsUtil:
    def __init__(self, options):
        self.command_options = options
        self.project_config = {}
        self.log = Logger()
        self.config = ZanataConfig()

    def apply_configfiles(self):
        url = self.apply_project_config()
        username, apikey = self.apply_user_config(url)

        # The value in commandline options will overwrite the value in user-config file
        if self.command_options.has_key("user_name"):
            username = self.command_options["user_name"][0]["value"]

        if self.command_options.has_key("key"):
            apikey = self.command_options["key"][0]["value"]

        self.log.info("zanata server: %s" % url)

        return url, username, apikey

    def apply_project_config(self):
        # Read the project configuration file using --project-config option
        config_file = [os.path.join(os.getcwd(), filename) for filename in ["zanata.xml", "flies.xml"]]

        if self.command_options.has_key("project_config"):
            config_file.append(self.command_options["project_config"][0]["value"])

        for path in config_file:
            if os.path.exists(path):
                self.log.info("Loading zanata project config from: %s" % path)
            self.project_config = self.config.read_project_config(path)
            break

        if not self.project_config:
            self.log.info("Can not find zanata.xml, please specify the path of zanata.xml")

        # process the url of server
        if self.project_config.has_key("project_url"):
            url = self.project_config["project_url"]

        # The value in options will override the value in project-config file
        if self.command_options.has_key("url"):
            self.log.info("Overriding url of server with command line option")
            url = self.command_options["url"][0]["value"]

        if not url or url.isspace():
            self.log.error("Please specify valid server url in zanata.xml or with '--url' option")
            sys.exit(1)

        url = self.trim_url(url)

        return url

    def trim_url(self, url):
        if " " in url or "\n" in url:
            self.log.info("Warning, the url which contains '\\n' or whitespace is not valid, please check zanata.xml")
        url = url.strip()

        if url[-1] == "/":
            url = url[:-1]

        return url

    def get_localemap(self):
        if self.project_config and self.project_config.has_key("locale_map"):
            locale_map = self.project_config["locale_map"]

        return locale_map

    def apply_user_config(self, url):
        user_name = ""
        apikey = ""
        # Try to read user-config file
        user_config = [
            os.path.join(os.path.expanduser("~") + "/.config", filename) for filename in ["zanata.ini", "flies.ini"]
        ]

        if self.command_options.has_key("user_config"):
            user_config.append(self.command_options["user_config"][0]["value"])

        for path in user_config:
            if os.path.exists(path):
                self.log.info("Loading zanata user config from: %s" % path)

            # Read the user-config file
            self.config.set_userconfig(path)

            try:
                server = self.config.get_server(url)
                if server:
                    user_name = self.config.get_config_value("username", "servers", server)
                    apikey = self.config.get_config_value("key", "servers", server)
            except Exception, e:
                self.log.info("Processing user-config file:%s" % str(e))
                break

            break

        if not (user_name, apikey):
            self.log.info("Can not find user-config file in home folder, current path or path in 'user-config' option")

        return (user_name, apikey)
 def __init__(self, options):
     self.command_options = options
     self.project_config = {}
     self.log = Logger()
     self.config = ZanataConfig()
class OptionsUtil:
    def __init__(self, options):
        self.command_options = options
        self.project_config = {}
        self.log = Logger()
        self.config = ZanataConfig()

    def apply_configfiles(self):
        url = self.apply_project_config()
        username, apikey = self.apply_user_config(url)

        # The value in commandline options will overwrite the value in user-config file
        if self.command_options.has_key('user_name'):
            username = self.command_options['user_name'][0]['value']

        if self.command_options.has_key('key'):
            apikey = self.command_options['key'][0]['value']

        self.log.info("zanata server: %s" % url)

        return url, username, apikey

    def apply_project_config(self):
        url = ""
        # Read the project configuration file using --project-config option
        config_file = [os.path.join(os.getcwd(), filename) for filename
                       in ['zanata.xml', 'flies.xml']]

        if self.command_options.has_key('project_config'):
            config_file.append(self.command_options['project_config'][0]['value'])

        for path in config_file:

            if os.path.exists(path):
                self.log.info("Loading zanata project config from: %s" % path)
                self.project_config = self.config.read_project_config(path)
                break

        if not self.project_config:
            self.log.info("Can not find zanata.xml, please specify the path of zanata.xml")

        # process the url of server
        if self.project_config.has_key('project_url'):
            url = self.project_config['project_url']

        # The value in options will override the value in project-config file
        if self.command_options.has_key('url'):
            self.log.info("Overriding url of server with command line option")
            url = self.command_options['url'][0]['value']

        if not url or url.isspace():
            self.log.error("Please specify valid server url in zanata.xml or with '--url' option")
            sys.exit(1)

        url = self.trim_url(url)

        return url

    def trim_url(self, url):
        if ' ' in url or '\n' in url:
            self.log.info("Warning, the url which contains '\\n' or whitespace is not valid, please check zanata.xml")
        url = url.strip()

        if url[-1] == "/":
            url = url[:-1]

        return url

    def get_localemap(self):
        if self.project_config and self.project_config.has_key('locale_map'):
            locale_map = self.project_config['locale_map']

        return locale_map

    def apply_user_config(self, url):
        user_name = ""
        apikey = ""
        # Try to read user-config file
        user_config = [os.path.join(os.path.expanduser("~") + '/.config', filename) for filename in ['zanata.ini', 'flies.ini']]

        if self.command_options.has_key('user_config'):
            user_config.append(self.command_options['user_config'][0]['value'])

        for path in user_config:
            if os.path.exists(path):
                self.log.info("Loading zanata user config from: %s" % path)

            # Read the user-config file
            self.config.set_userconfig(path)

            try:
                server = self.config.get_server(url)
                if server:
                    user_name = self.config.get_config_value("username", "servers", server)
                    apikey = self.config.get_config_value("key", "servers", server)
            except Exception, e:
                self.log.info("Processing user-config file:%s" % str(e))
                break

            break

        if not (user_name, apikey):
            self.log.info("Can not find user-config file in home folder, current path or path in 'user-config' option")

        return (user_name, apikey)
示例#11
0
class ProjectContext(object):
    """
    Base class to build context_data dict for the project.
    Order of precedence would be (1) command options (2) local config (3) remote config
    """
    def __init__(self, command_options, *args, **kwargs):
        self.command_options = command_options
        self.log = Logger()
        self.config = ZanataConfig()

        self.remote_config, self.local_config,\
            self.command_dict, self.context_data = [{} for i in range(4)]

        for option_set in self.command_options:
            self.command_dict.update(
                {option_set: self.command_options[option_set][0]['value']}
            )

    def get_context_data(self):
        """
        updates context_data with remote_config, local_config and command_dict
        """
        build_configs = [self.build_local_config,
                         self.build_remote_config]
        [method() for method in build_configs]
        # lowest to higest
        precedence = [self.remote_config, self.local_config, self.command_dict]
        context_data = reduce(
            lambda option, value: dict(option.items() + value.items()), precedence
        )
        return self.filter_context_data(context_data)

    def filter_context_data(self, data):
        """
        filters context data
        """
        # key was to fill http_header['token']
        remove_items = ['key']
        for item in remove_items:
            data.pop(item, None)
        return data

    def build_local_config(self):
        """
        This builds local configuration dict.
        """
        local_config_methods = [self._update_project_config,
                                self._update_url,
                                self._update_user_config,
                                self._update_http_headers,
                                self._update_client_version]
        [method() for method in local_config_methods]

    def _update_project_config(self):
        """
        Reads project configuration file, and updates local config.
        (1) --project-config option (2) zanata.xml in cwd
        """
        config_files = [os.path.join(os.getcwd(), filename) for filename
                        in project_config_file_tuple]

        if 'project_config' in self.command_dict:
            config_files.insert(0, self.command_dict['project_config'])

        for path in config_files:
            if os.path.exists(path):
                log.info("Loading zanata project config from: %s" % path)
                self.local_config.update(self.config.read_project_config(path))
                break

    def _update_url(self):
        """
        Process Server URL and update dicts
        """
        url_sources = (self.command_dict, self.local_config)
        url = url_sources[0].get('url') or url_sources[1].get('url')
        # validate url
        url_regex = re.compile(
            r'^(?:http)s?://'
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|'
            r'localhost|'
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
            r'(?::\d+)?'
            r'(?:/?|[/?]\S+)$', re.IGNORECASE)
        if not url or url.isspace() or not url_regex.match(url):
            log.error("Please specify valid server url in zanata.xml or with '--url' option")
            sys.exit(1)
        # format url
        if ' ' in url or '\n' in url:
            log.info("Warning, the url which contains '\\n' or whitespace is not valid, please check zanata.xml")
            url = url.strip()
        if url[-1] == "/":
            url = url[:-1]
        # update url source dict
        for source in url_sources:
            if 'url' in source:
                source['url'] = url

    def _update_user_config(self):
        """
        Reads user configuration file and updates local config.
        (1) --user-config option (2) zanata.ini in $HOME/.config
        """
        user_config = [os.path.join(os.path.expanduser("~") + '/.config', filename)
                       for filename in user_config_file_tuple]
        if 'user_config' in self.command_dict and self.command_dict.get('user_config'):
            user_config.append(self.command_dict['user_config'])

        for path in user_config:
            if os.path.exists(path):
                log.info("Loading zanata user config from: %s" % path)
                # Read the user-config file
                self.config.set_userconfig(path)
                try:
                    server = self.config.get_server(self.get_url())
                    if server:
                        user_name = self.config.get_config_value("username", "servers", server)
                        apikey = self.config.get_config_value("key", "servers", server)
                        if not (user_name, apikey):
                            log.info("Can not find user-config file in home folder,"
                                     "current path or path in '--user-config' option")
                        else:
                            self.local_config.update({'user_name': user_name, 'key': apikey})
                            log.info("zanata server: %s" % self.get_url())
                            return True
                except Exception, e:
                    log.info("Processing user-config file: %s" % str(e))
                    break
                break