예제 #1
0
def get_handlers():
    handlers = {}
    for handler_path in glob(path.abspath(path.join(path.dirname(path.realpath(__file__)), '../', 'handlers', '*.yml'))):
        loader_path = re.sub(r'[^\/]+$', '', handler_path)
        matches = re.findall(r'[^\/]+$', handler_path)
        if len(matches) <= 0:
            exit('Houston, we have a problem')
        filename = matches[0]
        variables = {}
        dump = 'volback-unique-' + encode_service.str_encode(str(time.time()))
        with open(handler_path, 'r') as f:
            data = yaml.load(f)
            for key in _.keys(data):
                variables[key] = data[key]['variables'] if 'variables' in data[key] else {}
                variables[key]['dump_path'] = path.join(data[key]['data'], dump)
        variables['env'] = os.environ
        env = Environment(loader=FileSystemLoader(loader_path), trim_blocks=True)
        template = env.get_template(filename)
        data = yaml.load(template.render(**variables))
        for key in _.keys(data):
            handlers[key] = {
                'data': data[key]['data'],
                'backup': data[key]['backup'],
                'restore': data[key]['restore'],
                'dump': dump
            }
    return handlers
예제 #2
0
def register_or_get_nailpack(app, api, nailpack_name):
    if api:
        if hasattr(api, '_registered_nailpacks'):
            if _.includes(_.keys(api._registered_nailpacks), nailpack_name):
                return api._registered_nailpacks[nailpack_name]
            nailpack = importlib.import_module(nailpack_name).Nailpack()
            api._registered_nailpacks[nailpack_name] = nailpack
        else:
            nailpack = importlib.import_module(nailpack_name).Nailpack()
            registered_nailpacks = {}
            registered_nailpacks[nailpack_name] = nailpack
            setattr(api, '_registered_nailpacks', registered_nailpacks)
        setattr(nailpack, 'api', api)
    else:
        nailpack = importlib.import_module(nailpack_name).Nailpack()
        if hasattr(app, '_registered_nailpacks'):
            if _.includes(_.keys(app._registered_nailpacks), nailpack_name):
                return app._registered_nailpacks[nailpack_name]
            app._registered_nailpacks.append(nailpack.name)
        else:
            registered_nailpacks = {}
            registered_nailpacks[nailpack_name] = nailpack
            setattr(app, '_registered_nailpacks', registered_nailpacks)
    for key in helpers.pubkeys(nailpack):
        if app:
            regsiter_event(app, key, getattr(nailpack, key))
        if api:
            regsiter_event(api, key, getattr(nailpack, key))
    if app:
        setattr(nailpack, 'app', app)
    setattr(nailpack, 'name', nailpack_name)
    setattr(nailpack, 'level', get_level(nailpack))
    return nailpack
예제 #3
0
 def populate(self, **kwargs):
     """Extended ndb.Model populate method, so it can ignore properties, which are not
     defined in model class without throwing error
     """
     kwargs = _.omit(kwargs, Base.PUBLIC_PROPERTIES + ['key', 'id'])  # We don't want to populate those properties
     kwargs = _.pick(kwargs, _.keys(self._properties))  # We want to populate only real model properties
     super(Base, self).populate(**kwargs)
예제 #4
0
def main():

    #WTForms validators
    def set_checker(form, field):
        if len(request.values) > 2:
            return True
        raise ValidationError('Please select at least one set.')

    def blacklist_checker(form, field):
        if re.match(".*[,;]$", request.values['blacklist']):
            raise ValidationError(
                'Please don\'t have trailing punctuation in the blacklist box.'
            )

    class RandomizerForm(Form):
        base = BooleanField('Base')
        intrigue = BooleanField('Intrigue')
        seaside = BooleanField('Seaside')
        alchemy = BooleanField('Alchemy')
        prosperity = BooleanField('Prosperity')
        cornucopia = BooleanField('Cornucopia')
        hinterlands = BooleanField('Hinterlands')
        darkages = BooleanField('Dark Ages')
        guilds = BooleanField('Guilds')
        adventures = BooleanField('Adventures')
        blacklist = TextAreaField('Blacklisted cards', [blacklist_checker])

        randomize_button = SubmitField('Get cards!', [set_checker])

    form = RandomizerForm(request.values)

    if ('randomize_button' in request.values) and form.validate():

        conn = db.DB()

        sets = []
        for set in _.keys(request.values):
            if set == 'darkages':
                sets.append("\'Dark Ages\'")
            elif set != 'randomize_button' and set != 'blacklist':
                sets.append("\'" + set + "\'")

        where_string = ', '.join(sets)

        query = """CREATE OR REPLACE VIEW picked_cards as
                SELECT * FROM cards
                WHERE CardSet IN ({}) AND ({}) ORDER BY RAND() LIMIT 10""".format(
            where_string, not_statement.blacklist(request.values['blacklist']))

        conn.execute(query)

        query = "SELECT * FROM picked_cards ORDER BY CardSet, Cost"

        content = conn.execute(query).fetchall()

        img_links = utils.cardImgLinker(content)

        return render_template('main.html', form=form, links=img_links)

    return render_template('main.html', form=form)
예제 #5
0
파일: base.py 프로젝트: derasd/woTravel
    def create_or_update(cls,key=None, urlsafe=False, parent=None, **kwargs):
        """ Updates an entity or creates a new one.
        If key is None it creates a new entity, if key is set it gets it.

        Returns
            key
        """
        def omit(data, omit_list):
            values = {}
            for k, v in data.iteritems():
                if isinstance(v, dict):    # iterable, not string
                    values[k] = omit(v,omit_list)
                elif k not in omit_list:
                    values[k] = v
            return  values
        print "CREATE OR UPDATE"
        print cls

        if key and (key != "new" or key !="add"):
            print "GOT key"
            print key
            if urlsafe:
                print "User urlsave"
                key = ndb.Key(urlsafe=key)
                print key
            db = key.get()
            print kwargs
            kwargs = omit(kwargs, Base.PUBLIC_PROPERTIES + ['key', 'id'])  # We don't want to populate those properties
            print "kwargs after omiting"
            print "============================="
            print kwargs
            db.populate(**kwargs)
        else:
            print "Save new one"
            print "properties are:"
            print _.keys(cls._properties)
            # We don't want to populate those properties
            kwargs = omit(kwargs, Base.PUBLIC_PROPERTIES + ['key', 'id'])
            # We want to populate only real model properties
            kwargs = _.pick(kwargs, _.keys(cls._properties))
            # TODO should be deep!
            print "____________________________________"
            print "kwargs"
            print kwargs
            db = cls(parent=parent,**kwargs)
        key = db.put()
        return key
예제 #6
0
파일: app.py 프로젝트: JarettRoman/dominion
def main():

    #WTForms validators
    def set_checker(form, field):
        if len(request.values) > 2:
            return True
        raise ValidationError('Please select at least one set.')

    def blacklist_checker(form, field):
        if re.match(".*[,;]$", request.values['blacklist']):
            raise ValidationError('Please don\'t have trailing punctuation in the blacklist box.')

    class RandomizerForm(Form):
        base = BooleanField('Base')
        intrigue = BooleanField('Intrigue')
        seaside = BooleanField('Seaside')
        alchemy = BooleanField('Alchemy')
        prosperity = BooleanField('Prosperity')
        cornucopia = BooleanField('Cornucopia')
        hinterlands = BooleanField('Hinterlands')
        darkages = BooleanField('Dark Ages')
        guilds = BooleanField('Guilds')
        adventures = BooleanField('Adventures')
        blacklist = TextAreaField('Blacklisted cards', [blacklist_checker])

        randomize_button = SubmitField('Get cards!', [set_checker])

    form = RandomizerForm(request.values)

    if ('randomize_button' in request.values) and form.validate():

        conn = db.DB()

        sets = []
        for set in _.keys(request.values):
            if set == 'darkages':
                sets.append("\'Dark Ages\'")
            elif set != 'randomize_button' and set != 'blacklist':
                sets.append("\'" + set + "\'")

        where_string = ', '.join(sets)

        query = """CREATE OR REPLACE VIEW picked_cards as
                SELECT * FROM cards
                WHERE CardSet IN ({}) AND ({}) ORDER BY RAND() LIMIT 10""".format(where_string,
                                                                                  not_statement.blacklist(request.values['blacklist']))

        conn.execute(query)

        query = "SELECT * FROM picked_cards ORDER BY CardSet, Cost"

        content = conn.execute(query).fetchall()

        img_links = utils.cardImgLinker(content)

        return render_template('main.html', form=form, links=img_links)

    return render_template('main.html', form=form)
예제 #7
0
파일: util.py 프로젝트: outpark/webapp-one
def dict_to_list(input_dict):
    """Creates list from dictionary with true booloean values
    This function is primarily useful for converting passed data from Angular checkboxes,
     since angular ng-model can't return list of checked group of checkboxes, instead
     it returns something like {'a': True, 'b': True} for each checkbox

    Example:
        >>> dict_to_list({'a': True, 'b': True, 'c': False})
        ['a', 'b']

    Args:
        input_dict (dict): Dict with boolean values

    Returns:
        list: list of truthful values
    """
    return _.keys(_.pick(input_dict, _.identity))
예제 #8
0
 def run_required(self):
     global steps_ran
     steps = self.app.steps
     cached = True
     if not hasattr(self, 'requires'):
         return False
     if len(self.requires) <= 0:
         cached = False
     for required in self.requires:
         maybe_cached = True
         if _.includes(_.keys(steps_ran), required):
             maybe_cached = steps_ran[required]
         else:
             maybe_cached = getattr(steps, required).start()
         if not maybe_cached:
             cached = maybe_cached
     return cached
예제 #9
0
파일: util.py 프로젝트: Huijari/gae_test
def dict_to_list(input_dict):
    """Creates list from dictionary with true booloean values
    This function is primarily useful for converting passed data from Angular checkboxes,
     since angular ng-model can't return list of checked group of checkboxes, instead
     it returns something like {'a': True, 'b': True} for each checkbox

    Example:
        >>> dict_to_list({'a': True, 'b': True, 'c': False})
        ['a', 'b']

    Args:
        input_dict (dict): Dict with boolean values

    Returns:
        list: list of truthful values
    """
    return _.keys(_.pick(input_dict, _.identity))
예제 #10
0
파일: base.py 프로젝트: derasd/woTravel
    def to_dict(self, include=None):
        """Return a dict containing the entity's property values,
        so it can be passed to client

        Args:
            include (list, optional): Set of property names to include,
                default all properties.
                For nested elements the list can use a dict:
                    eg.
                        ['name','description,{'tags':['name','count']}]
        """
        repr_dict = {}
        if include is None:
            include = _.keys(self._properties)
            #return super(Base, self).to_dict(include=include)

        def to_value(attr,name_str, name=None):
            if isinstance(attr, datetime.date):
                return attr.isoformat()
            elif isinstance(attr, ndb.Key):
                if name_str == 'key':
                    return self.key.urlsafe()
                else:
                    return attr.urlsafe()
            elif isinstance(attr, ndb.GeoPt):
                return {'lat':attr.lat,
                        'lng': attr.lon,
                        'lon': attr.lon}
            elif hasattr(attr, '__iter__'):    # iterable, not string
                return [to_value(a,name_str,name) for a in attr]
            elif hasattr(attr, 'to_dict'):    # hooray for duck typing!
                incl = None if not isinstance(name,dict) else name[name_str]
                return attr.to_dict(include=incl)
            else:
                return attr

        for name in include:
            name_str = name if not isinstance(name,dict) else name.keys()[0]
            attr = getattr(self, name_str)
            repr_dict[name_str] = to_value(attr,name_str,name)
            if name_str == 'key':
                repr_dict['id'] = self.key.id()
        return repr_dict
예제 #11
0
def load_conf(conf):
    if _.includes(sys.argv, '-h') or _.includes(sys.argv, '--help'):
        return conf
    cwd_path = os.getcwd()
    flag = None
    if _.includes(sys.argv, '--source'):
        flag = '--source'
    elif _.includes(sys.argv, '--src'):
        flag = '--src'
    elif _.includes(sys.argv, '-s'):
        flag = '-s'
    if flag:
        flag_index = _.index_of(sys.argv, flag)
        if len(sys.argv) > flag_index + 1:
            cwd_path = path.abspath(sys.argv[flag_index + 1])
    config_path = path.join(cwd_path, 'config.yml')
    if not path.exists(config_path):
        Halo(text='config not found: ' + config_path).fail()
        exit(1)
    with open(config_path, 'r') as f:
        try:
            conf = munchify(_.merge({}, conf, yaml.load(f)))
        except yaml.YAMLError as err:
            print(err)
            exit(1)
    conf.paths.cwd = cwd_path
    if 'version' in conf:
        conf.version = str(conf.version)
    conf.paths.install = path.join(conf.paths.mount, 'casper')
    if not path.exists(path.join(conf.paths.install, 'filesystem.squashfs')):
        conf.paths.install = path.join(conf.paths.mount, 'install')
    output_path = conf.paths.output
    conf = munchify(
        _.merge({}, conf, {
            'paths':
            _.zip_object(
                _.keys(conf.paths),
                _.map(conf.paths,
                      lambda x: path.abspath(path.join(conf.paths.cwd, x))))
        }))
    conf.paths.output = path.abspath(output_path)
    return conf
예제 #12
0
    def serialize_nested(self, dest, current, attribute, opts):

        embeds = []
        attributes = []

        if opts and opts.get('attributes'):
            embeds = filter(lambda x: opts.get(x), opts['attributes'])
            attributes = filter(lambda x: opts.get(x) is None,
                                opts['attributes'])
        else:
            attributes = _.keys(dest)

        ret = {}
        if attributes:
            ret['attributes'] = self.pick(dest, attributes)

        for embed in embeds:
            if self._is_complex_type(dest[embed]):
                self.serialize(ret, dest, embed, opts[embed])

        return ret['attributes']
예제 #13
0
        print("")
    for cat in results:
        print("{}) {}".format(index, json.dumps(cat)))
        index += 1
        if index > 20:
            return


# load up the taxonomy
categories = []
with open("taxonomy.csv", "r") as csvfile:
    cats = csv.DictReader(csvfile)
    for cat in cats:
        categories.append(cat)

    directories = _.keys(categories[0])
    print("Supported directories: {}".format(json.dumps(directories)))

    find = _.find(categories, {'gcid': "gcid:{}".format(args.gcid)})
    if find is None:
        find = {'gcid': 'gcid:{}'.format(args.gcid)}

    print("")
    print("Current config: {}".format(json.dumps(find)))

    for directory in directories:
        # handle gcid condition
        if directory == 'google':
            find['google'] = args.name
            continue
예제 #14
0
 def get_all_properties(cls):
     """Gets all model's ndb properties"""
     return ['key', 'id'] + _.keys(cls._properties)
예제 #15
0
 def get_data_type(self, container):
     data_type = container['Config']['Image'][:container['Config']['Image'].
                                              index(':')]
     if not _.includes(_.keys(self.handlers), data_type):
         data_type = 'raw'
     return data_type