Пример #1
0
class UtilCommand(cmd.Cmd):

    def do_manifest_convert(self, args):
        from api_etl import Config
        self.config = Config()
        owner = self.config.database('owner')
        self.conn = psycopg2.connect("dbname=apiserver user={}".format(owner))

        self.lookup = {
            "character varying": "string",
            "text": "string"
        }

        root = self.config.config.get('manifest', 'location')
        for f in glob.glob(os.path.join(root, '*.yml')):
            m = Manifest(f)
            theme = m.data['title'].lower()

            for service in m.get_services():
                out = {
                    "theme": theme,
                    "id": service.name,
                    "title": service.description,
                    "tablename": service.name,
                }

                q = """
                  SELECT column_name, data_type
                  FROM information_schema.columns
                  WHERE table_schema = 'public'
                    AND table_name   = '{table}';
                """.format(table=service.name)

                cur = self.conn.cursor()
                cur.execute(q)
                result = cur.fetchall()
                cur.close()

                out['fields'] = []
                for (n,t) in result:
                    d = {"name": n, "type": self.lookup.get(t, t)}
                    out['fields'].append(d)

                if isinstance(service.table_settings, list):
                    settings = service.table_settings[0]
                else:
                    settings = service.table_settings
                out['choice_fields'] = settings.get('choice_fields', [])

                if hasattr(service, "searchables"):
                    out['queries'] = service.searchables

                outfile = os.path.join(root, "manifests", service.name + ".json")
                print outfile
                with open(outfile, "w") as fout:
                    json.dump(out, fout, indent=2)

        self.conn.close()
Пример #2
0
    def create_db(self, name):
        """ Create the database for the '''args''' theme """
        from api_etl import Config

        c = Config()
        owner = c.database('owner')
        role = c.database('reader_username')

        cmds = [
            'createdb {db} -O {owner} -E utf-8',
            'psql {db} -c "GRANT CONNECT ON DATABASE {db} TO {role}"',
            'psql {db} -c "GRANT USAGE ON SCHEMA public TO {role}"',
            'psql {db} -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO {role}"',
            'psql {db} -c "GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO {role}"',
            'psql {db} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO {role}"',
            'psql {db} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO {role}"',
            'psql {db} -c "REVOKE CREATE ON SCHEMA public FROM public"'
        ]
        print ';\n'.join(cmds).format(db=name, role=role, owner=owner) + ";"
Пример #3
0
    def create_db(self, name):
        """ Create the database for the '''args''' theme """
        from api_etl import Config

        c = Config()
        owner = c.database('owner')
        role = c.database('reader_username')

        cmds = [
            'createdb {db} -O {owner} -E utf-8',
            'psql {db} -c "GRANT CONNECT ON DATABASE {db} TO {role}"',
            'psql {db} -c "GRANT USAGE ON SCHEMA public TO {role}"',
            'psql {db} -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO {role}"',
            'psql {db} -c "GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO {role}"',
            'psql {db} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO {role}"',
            'psql {db} -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO {role}"',
            'psql {db} -c "REVOKE CREATE ON SCHEMA public FROM public"'
        ]
        print ';\n'.join(cmds).format(db=name, role=role, owner=owner) + ";"
Пример #4
0
class UtilCommand(cmd.Cmd):
    def do_manifest_convert(self, args):
        from api_etl import Config
        self.config = Config()
        owner = self.config.database('owner')
        self.conn = psycopg2.connect("dbname=apiserver user={}".format(owner))

        self.lookup = {"character varying": "string", "text": "string"}

        root = self.config.config.get('manifest', 'location')
        for f in glob.glob(os.path.join(root, '*.yml')):
            m = Manifest(f)
            theme = m.data['title'].lower()

            for service in m.get_services():
                out = {
                    "theme": theme,
                    "id": service.name,
                    "title": service.description,
                    "tablename": service.name,
                }

                q = """
                  SELECT column_name, data_type
                  FROM information_schema.columns
                  WHERE table_schema = 'public'
                    AND table_name   = '{table}';
                """.format(table=service.name)

                cur = self.conn.cursor()
                cur.execute(q)
                result = cur.fetchall()
                cur.close()

                out['fields'] = []
                for (n, t) in result:
                    d = {"name": n, "type": self.lookup.get(t, t)}
                    out['fields'].append(d)

                if isinstance(service.table_settings, list):
                    settings = service.table_settings[0]
                else:
                    settings = service.table_settings
                out['choice_fields'] = settings.get('choice_fields', [])

                if hasattr(service, "searchables"):
                    out['queries'] = service.searchables

                outfile = os.path.join(root, "manifests",
                                       service.name + ".json")
                print outfile
                with open(outfile, "w") as fout:
                    json.dump(out, fout, indent=2)

        self.conn.close()