Python yaml.CSafeLoader() Examples

The following are 30 code examples of yaml.CSafeLoader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module yaml , or try the search function .
Example #1
Source File: config.py    From pyvac with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def configure(filename='conf/pyvac.yaml', init_celery=True, default_app=None):
    with open(filename) as fdesc:
        conf = yaml.load(fdesc, YAMLLoader)
    if conf.get('logging'):
        dictConfig(conf.get('logging'))

    if init_celery:
        if not default_app:
            try:
                from celery import current_app as default_app
            except ImportError:  # pragma: no cover
                from celery.app import default_app

        default_app.config_from_object(conf.get('celeryconfig'))
        # XXX: must call loader for celery to register all the tasks
        default_app.loader.import_default_modules()

    return conf 
Example #2
Source File: file_utils.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def read_yaml(root, file_name):
    """
    Read data from yaml file and return as dictionary

    :param root: Directory name
    :param file_name: File name. Expects to have '.yaml' extension

    :return: Data in yaml file as dictionary
    """
    if not exists(root):
        raise MissingConfigException(
            "Cannot read '%s'. Parent dir '%s' does not exist." % (file_name, root))

    file_path = os.path.join(root, file_name)
    if not exists(file_path):
        raise MissingConfigException("Yaml file '%s' does not exist." % file_path)
    try:
        with codecs.open(file_path, mode='r', encoding=ENCODING) as yaml_file:
            return yaml.load(yaml_file, Loader=YamlSafeLoader)
    except Exception as e:
        raise e 
Example #3
Source File: pyyaml.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def Deserializer(stream_or_string, **options):
    """
    Deserialize a stream or string of YAML data.
    """
    if isinstance(stream_or_string, bytes):
        stream_or_string = stream_or_string.decode('utf-8')
    if isinstance(stream_or_string, six.string_types):
        stream = StringIO(stream_or_string)
    else:
        stream = stream_or_string
    try:
        for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options):
            yield obj
    except GeneratorExit:
        raise
    except Exception as e:
        # Map to deserializer error
        six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2]) 
Example #4
Source File: __init__.py    From insights-core with Apache License 2.0 6 votes vote down vote up
def parse_content(self, content):
        try:
            if type(content) is list:
                self.data = yaml.load('\n'.join(content), Loader=SafeLoader)
            else:
                self.data = yaml.load(content, Loader=SafeLoader)
            if self.data is None:
                raise SkipException("There is no data")
            if not isinstance(self.data, (dict, list)):
                raise ParseException("YAML didn't produce a dictionary or list.")
        except SkipException as se:
            tb = sys.exc_info()[2]
            six.reraise(SkipException, SkipException(str(se)), tb)
        except:
            tb = sys.exc_info()[2]
            cls = self.__class__
            name = ".".join([cls.__module__, cls.__name__])
            msg = "%s couldn't parse yaml." % name
            six.reraise(ParseException, ParseException(msg), tb) 
Example #5
Source File: ca_util.py    From keylime with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def read_private(warn=False):
    global global_password
    if global_password is None:
        setpassword(getpass.getpass("Please enter the password to decrypt your keystore: "))

    if os.path.exists('private.yml'):
        with open('private.yml','r') as f:
            toread = yaml.load(f, Loader=SafeLoader)
        key = crypto.kdf(global_password,toread['salt'])
        try:
            plain = crypto.decrypt(toread['priv'],key)
        except ValueError:
            raise Exception("Invalid password for keystore")

        return yaml.load(plain, Loader=SafeLoader),toread['salt']
    else:
        if warn:
            #file doesn't exist, just invent a salt
            logger.warning("Private certificate data %s does not exist yet."%os.path.abspath("private.yml"))
            logger.warning("Keylime will attempt to load private certificate data again when it is needed.")
        return {'revoked_keys':[]},base64.b64encode(crypto.generate_random_key()).decode() 
Example #6
Source File: utils.py    From schemathesis with MIT License 6 votes vote down vote up
def make_loader(*tags_to_remove: str) -> Type[yaml.SafeLoader]:
    """Create a YAML loader, that doesn't parse specific tokens into Python objects."""
    cls: Type[yaml.SafeLoader] = type("YAMLLoader", (SafeLoader,), {})
    cls.yaml_implicit_resolvers = {
        key: [(tag, regexp) for tag, regexp in mapping if tag not in tags_to_remove]
        for key, mapping in cls.yaml_implicit_resolvers.copy().items()
    }

    # Fix pyyaml scientific notation parse bug
    # See PR: https://github.com/yaml/pyyaml/pull/174 for upstream fix
    cls.add_implicit_resolver(  # type: ignore
        "tag:yaml.org,2002:float",
        re.compile(
            r"""^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+]?[0-9]+)?
                       |[-+]?(?:[0-9][0-9_]*)(?:[eE][-+]?[0-9]+)
                       |\.[0-9_]+(?:[eE][-+]?[0-9]+)?
                       |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
                       |[-+]?\.(?:inf|Inf|INF)
                       |\.(?:nan|NaN|NAN))$""",
            re.X,
        ),
        list("-+0123456789."),
    )

    return cls 
Example #7
Source File: graphics.py    From yamlloader with MIT License 6 votes vote down vote up
def importyaml(connection,metadata,sourcePath):
    eveGraphics = Table('eveGraphics',metadata)
    print "Importing Graphics"
    print "opening Yaml"
    with open(os.path.join(sourcePath,'fsd','graphicIDs.yaml'),'r') as yamlstream:
        print "importing"
        trans = connection.begin()
        graphics=load(yamlstream,Loader=SafeLoader)
        print "Yaml Processed into memory"
        for graphic in graphics:
            connection.execute(eveGraphics.insert(),
                            graphicID=graphic,
                            sofFactionName=graphics[graphic].get('sofFactionName',''),
                            graphicFile=graphics[graphic].get('graphicFile',''),
                            sofHullName=graphics[graphic].get('sofHullName',''),
                            sofRaceName=graphics[graphic].get('sofRaceName',''),
                            description=graphics[graphic].get('description',''))
    trans.commit() 
Example #8
Source File: groups.py    From yamlloader with MIT License 6 votes vote down vote up
def importyaml(connection,metadata,sourcePath,language='en'):
    invGroups = Table('invGroups',metadata)
    trnTranslations = Table('trnTranslations',metadata)
    print "Importing Groups"
    print "opening Yaml"
    with open(os.path.join(sourcePath,'fsd','groupIDs.yaml'),'r') as yamlstream:
        trans = connection.begin()
        groupids=load(yamlstream,Loader=SafeLoader)
        print "Yaml Processed into memory"
        for groupid in groupids:
            connection.execute(invGroups.insert(),
                            groupID=groupid,
                            categoryID=groupids[groupid].get('categoryID',0),
                            groupName=groupids[groupid].get('name',{}).get(language,'').decode('utf-8'),
                            iconID=groupids[groupid].get('iconID'),
                            useBasePrice=groupids[groupid].get('useBasePrice'),
                            anchored=groupids[groupid].get('anchored',0),
                            anchorable=groupids[groupid].get('anchorable',0),
                            fittableNonSingleton=groupids[groupid].get('fittableNonSingleton',0),
                            published=groupids[groupid].get('published',0))
            if (groupids[groupid].has_key('name')):
                for lang in groupids[groupid]['name']:
                    connection.execute(trnTranslations.insert(),tcID=7,keyID=groupid,languageID=lang,text=groupids[groupid]['name'][lang].decode('utf-8'));
    trans.commit() 
Example #9
Source File: custom_yaml.py    From INGInious with GNU Affero General Public License v3.0 6 votes vote down vote up
def load(stream):
    """
        Parse the first YAML document in a stream
        and produce the corresponding Python
        object. Use OrderedDicts to produce dicts.

        Safe version.
    """

    class OrderedLoader(SafeLoader):
        pass

    def construct_mapping(loader, node):
        loader.flatten_mapping(node)
        return OrderedDict(loader.construct_pairs(node))

    OrderedLoader.add_constructor(
        original_yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
        construct_mapping)

    return original_yaml.load(stream, OrderedLoader) 
Example #10
Source File: dogmaAttributeCategories.py    From yamlloader with MIT License 6 votes vote down vote up
def importyaml(connection,metadata,sourcePath,language='en'):
    print "Importing dogma attribute categories"
    dgmAttributeCategories = Table('dgmAttributeCategories',metadata)
    
    print "opening Yaml"
        
    trans = connection.begin()
    with open(os.path.join(sourcePath,'fsd','dogmaAttributeCategories.yaml'),'r') as yamlstream:
        print "importing"
        dogmaAttributeCategories=load(yamlstream,Loader=SafeLoader)
        print "Yaml Processed into memory"
        for dogmaAttributeCategoryID in dogmaAttributeCategories:
          attribute = dogmaAttributeCategories[dogmaAttributeCategoryID]
          connection.execute(dgmAttributeCategories.insert(),
                             categoryID=dogmaAttributeCategoryID,
                             categoryName=attribute['name'],
                             categoryDescription=attribute['description']
                )
    trans.commit() 
Example #11
Source File: bsdTables.py    From yamlloader with MIT License 6 votes vote down vote up
def importyaml(connection,metadata,sourcePath):

    print "Importing BSD Tables"

    files=glob.glob(os.path.join(sourcePath,'bsd','*.yaml'))
    for file in files:
        head, tail = os.path.split(file)
        tablename=tail.split('.')[0]
        print tablename
        tablevar = Table(tablename,metadata)
        print "Importing {}".format(file)
        print "Opening Yaml"
        trans = connection.begin()
        with open(file,'r') as yamlstream:
            rows=load(yamlstream,Loader=SafeLoader)
            print "Yaml Processed into memory"
            for row in rows:
                connection.execute(tablevar.insert().values(row))
        trans.commit() 
Example #12
Source File: provider_vtpm_add.py    From keylime with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def add_vtpm(inputfile):
    # read in the file
    with open(inputfile,'r') as f:
        group = yaml.load(f, Loader=SafeLoader)

    # fetch configuration parameters
    provider_reg_port = config.get('registrar', 'provider_registrar_port')
    provider_reg_ip = config.get('registrar', 'provider_registrar_ip')

    # request a vtpm uuid from the manager
    vtpm_uuid = vtpm_manager.add_vtpm_to_group(group['uuid'])

    # registrar it and get back a blob
    keyblob = registrar_client.doRegisterAgent(provider_reg_ip,provider_reg_port,vtpm_uuid,group['pubekpem'],group['ekcert'],group['aikpem'])

    # get the ephemeral registrar key by activating in the hardware tpm
    key = base64.b64encode(vtpm_manager.activate_group(group['uuid'], keyblob))

    # tell the registrar server we know the key
    registrar_client.doActivateAgent(provider_reg_ip,provider_reg_port,vtpm_uuid,key)

    logger.info("Registered new vTPM with UUID: %s"%(vtpm_uuid))

    return vtpm_uuid 
Example #13
Source File: models.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def initialize(cls, filename):
        try:
            with open(filename) as fdesc:
                conf = yaml.load(fdesc, YAMLLoader)
            cls.users_base = conf.get('users_base')
            base_date = conf.get('date')
            base_date = datetime.strptime(base_date, '%d/%m/%Y')
            cls.epoch = base_date
            log.info('Loaded user base file %s for CP vacation' % filename)
        except IOError:
            log.warn('Cannot load user base file %s for CP vacation' %
                     filename) 
Example #14
Source File: __init__.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def configure_workers(sender=None, conf=None, **kwargs):
    # The Worker (child process of the celeryd) must have
    # it's own SQL Connection (A unix forking operation preserve fd)
    with open(sys.argv[1]) as fdesc:
        conf = yaml.load(fdesc, YAMLLoader)
    # XXX Register the database
    create_engine('pyvac', conf.get('databases').get('pyvac'),
                  scoped=True)
    if conf.get('ldap'):
        LdapCache.configure(conf.get('ldap').get('conf'))
    SmtpCache.configure(conf.get('smtp'))

    # initialize configuration singleton
    ConfCache.configure(conf) 
Example #15
Source File: yaml_reader.py    From Yamale with MIT License 5 votes vote down vote up
def _pyyaml(f):
    import yaml
    try:
        Loader = yaml.CSafeLoader
    except AttributeError:  # System does not have libyaml
        Loader = yaml.SafeLoader
    return list(yaml.load_all(f, Loader=Loader)) 
Example #16
Source File: models.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def initialize(cls, filename):
        try:
            with open(filename) as fdesc:
                conf = yaml.load(fdesc, YAMLLoader)
            cls.users_base = conf.get('users_base')
            base_date = conf.get('date')
            base_date = datetime.strptime(base_date, '%d/%m/%Y')
            cls.epoch = base_date
            log.info('Loaded user base file %s for CP vacation' % filename)
        except IOError:
            log.warn('Cannot load user base file %s for CP vacation' %
                     filename) 
Example #17
Source File: request.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def includeme(config):
    """
    Pyramid includeme file for the :class:`pyramid.config.Configurator`
    """
    settings = config.registry.settings

    if 'pyvac.features.squad_overview' in settings:
        filename = settings['pyvac.features.squad_overview']
        try:
            with open(filename) as fdesc:
                conf = yaml.load(fdesc, YAMLLoader)
            SquadOverview.squad_leaders = conf.get('squad_leaders', {})
            log.info('Loaded squad_leaders file %s: %s' %
                     (filename, SquadOverview.squad_leaders))
        except IOError:
            log.warn('Cannot load squad_leaders file %s' % filename)

    if 'pyvac.features.chapter_overview' in settings:
        filename = settings['pyvac.features.chapter_overview']
        try:
            with open(filename) as fdesc:
                conf = yaml.load(fdesc, YAMLLoader)
            ChapterOverview.chapter_leaders = conf.get('chapter_leaders', {})
            log.info('Loaded chapter_leaders file %s: %s' %
                     (filename, ChapterOverview.chapter_leaders))
        except IOError:
            log.warn('Cannot load chapter_leaders file %s' % filename) 
Example #18
Source File: request.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def render(self):

        req_id = self.request.params.get('request_id')
        req = Request.by_id(self.session, req_id)
        if not req:
            return ''

        # check if request have already been consumed
        if not self.user.is_admin:
            today = datetime.now()
            if req.date_from <= today:
                log.error('User %s tried to CANCEL consumed request %d.' %
                          (self.user.login, req.id))
                return req.status

        # delete from calendar
        if req.status == 'APPROVED_ADMIN' and req.ics_url:
            settings = self.request.registry.settings
            with open(settings['pyvac.celery.yaml']) as fdesc:
                Conf = yaml.load(fdesc, YAMLLoader)
            caldav_url = Conf.get('caldav').get('url')
            delFromCal(caldav_url, req.ics_url)

        RequestHistory.new(self.session, req,
                           req.status, 'CANCELED',
                           self.user)
        req.update_status('CANCELED')
        # save who performed this action
        req.last_action_user_id = self.user.id
        # refund userpool
        req.refund_userpool(self.session)

        self.session.flush()
        return req.status 
Example #19
Source File: ldap.py    From pyvac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, filename):
        with open(filename) as fdesc:
            conf = yaml.load(fdesc, YAMLLoader)

        self._url = conf['ldap_url']
        self._base = conf['basedn']
        self._filter = conf['search_filter']

        self.mail_attr = conf['mail_attr']
        self.firstname_attr = conf['firstname_attr']
        self.lastname_attr = conf['lastname_attr']
        self.login_attr = conf['login_attr']
        self.manager_attr = conf['manager_attr']
        self.country_attr = conf['country_attr']

        self.admin_dn = conf['admin_dn']

        self.system_DN = conf['system_dn']
        self.system_password = conf['system_pass']

        self.team_dn = conf['team_dn']
        self.chapter_dn = conf.get('chapter_dn')
        self.default_user = conf.get('default_user', self.system_DN)

        self._conn = ldap.initialize(self._url)
        self._bind(self.system_DN, self.system_password)

        log.info('Ldap wrapper initialized') 
Example #20
Source File: default_handlers.py    From python-frontmatter with MIT License 5 votes vote down vote up
def load(self, fm, **kwargs):
        """
        Parse YAML front matter. This uses yaml.SafeLoader by default. 
        """
        kwargs.setdefault("Loader", SafeLoader)
        return yaml.load(fm, **kwargs) 
Example #21
Source File: s3_archiver.py    From manheim-c7n-tools with Apache License 2.0 5 votes vote down vote up
def _get_policy_names(self):
        """
        Read the custodian config file; return a list of policy names.

        :return: list of policy names
        :rtype: list
        """
        with open(self._conf_file, 'r') as fh:
            contents = fh.read()
        data = yaml.load(contents, Loader=SafeLoader)
        return [p['name'] for p in data['policies']] 
Example #22
Source File: policygen.py    From manheim-c7n-tools with Apache License 2.0 5 votes vote down vote up
def _read_file_yaml(self, path):
        """unit test helper - return YAML from file contents"""
        with open(path, 'r') as fh:
            contents = fh.read()
        try:
            return yaml.load(contents, Loader=SafeLoader)
        except Exception:
            sys.stderr.write("Exception loading YAML: %s\n" % path)
            raise 
Example #23
Source File: yamlwrapper.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def load(*args, **kwargs):
    """Delegate to yaml load.
    """
    if kwargs is None:
        kwargs = {}
    kwargs['Loader'] = Loader
    return yaml.load(*args, **kwargs) 
Example #24
Source File: yamlwrapper.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def load_all(*args, **kwargs):
    """Delegate to yaml loadall.
    """
    if kwargs is None:
        kwargs = {}
    kwargs['Loader'] = Loader
    return yaml.load_all(*args, **kwargs) 
Example #25
Source File: conf.py    From gandi.cli with GNU General Public License v3.0 5 votes vote down vote up
def load(cls, filename, name=None):
        """ Load yaml configuration from filename. """
        if not os.path.exists(filename):
            return {}
        name = name or filename

        if name not in cls._conffiles:
            with open(filename) as fdesc:
                content = yaml.load(fdesc, YAMLLoader)
                # in case the file is empty
                if content is None:
                    content = {}
                cls._conffiles[name] = content

        return cls._conffiles[name] 
Example #26
Source File: config.py    From ceph-lcm with Apache License 2.0 5 votes vote down vote up
def yaml_load(filename):
    with filename.open("rt") as yamlfp:
        return yaml.load(yamlfp, Loader=YAMLLoader) 
Example #27
Source File: ddyaml.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def safe_yaml_load(stream, Loader=yLoader):
    if Loader != yLoader:
        stream_name = get_stream_name(stream)
        log.debug("Unsafe loading of YAML has been disabled - using safe loader instead in %s", stream_name)

    if pyyaml_load:
        return pyyaml_load(stream, Loader=yLoader)

    return yaml.load(stream, Loader=yLoader) 
Example #28
Source File: ddyaml.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def safe_yaml_load_all(stream, Loader=yLoader):
    if Loader != yLoader:
        stream_name = get_stream_name(stream)
        log.debug("Unsafe loading of YAML has been disabled - using safe loader instead in %s", stream_name)

    if pyyaml_load_all:
        return pyyaml_load_all(stream, Loader=yLoader)

    return yaml.load_all(stream, Loader=yLoader) 
Example #29
Source File: charm.py    From operator with Apache License 2.0 5 votes vote down vote up
def _loadYaml(source):
    if yaml.__with_libyaml__:
        return yaml.load(source, Loader=yaml.CSafeLoader)
    return yaml.load(source, Loader=yaml.SafeLoader) 
Example #30
Source File: test_storage.py    From operator with Apache License 2.0 5 votes vote down vote up
def test_is_c_loader(self):
        loader = storage._SimpleLoader(io.StringIO(''))
        if getattr(yaml, 'CSafeLoader', None) is not None:
            self.assertIsInstance(loader, yaml.CSafeLoader)
        else:
            self.assertIsInstance(loader, yaml.SafeLoader)