def command(self):
        super(MappingConvertCommand, self).command()
        self._check_args_length(1)

        from openspending.lib import json
        from openspending.etl.mappingimporter import MappingImporter

        mapping_url = self.args[0]
        importer = MappingImporter()
        mapping = importer.import_from_url(mapping_url)
        print json.dumps(mapping, indent=2)
Exemplo n.º 2
0
    def explorer(self, id=None):
        c.dataset = model.Dataset.by_id(id)
        c.keys_meta = dict([(k.key, {"label": k.label,
                "description": k.get("description", "")})
                for k in model.Dimension.find({"dataset": c.dataset.name})])
        if "breakdownKeys" in c.dataset:
            c.breakdown_keys = c.dataset["breakdownKeys"]
        else:
            c.breakdown_keys = c.keys_meta.keys()[:3]

        c.keys_meta_json = json.dumps(c.keys_meta)
        c.breakdown_keys_json = json.dumps(c.breakdown_keys)
        return render('dataset/explorer.html')
Exemplo n.º 3
0
    def _cmd_check(self):
        if len(self.args) != 2:
            raise self.BadCommand("Usage: paster ckan check <pkgname>")

        package_name = self.args[1]
        from openspending.lib import ckan, json

        p = ckan.Package(package_name)

        def _get_url_or_error(resource_name):
            try:
                r = p.openspending_resource(resource_name)
                if r:
                    return r["url"]
                else:
                    return r
            except ckan.ResourceError as e:
                return str(e)

        res = {
            "is_importable": p.is_importable(),
            "data": _get_url_or_error('data'),
            "model": _get_url_or_error('model'),
            "model:mapping": _get_url_or_error('model:mapping')
        }

        print(json.dumps(res, indent=2, sort_keys=True))
Exemplo n.º 4
0
    def bubbles(self, name, breakdown_field, drilldown_fields, format="html"):
        c.drilldown_fields = json.dumps(drilldown_fields.split(','))
        dataset = name
        c.dataset = model.dataset.find_one_by('name', name)
        c.dataset_name = name

        # TODO: make this a method
        c.template = 'dataset/view_bubbles.html'

        curs = model.entry.find({'dataset.name':name})# , {breakdown_field: True})
        breakdown_names = list(set([ i[breakdown_field]['name'] for i in curs ]))

        count = len(breakdown_names)

        styles = [ s for s in rgb_rainbow(count) ]
        breakdown_styles = dict([ (breakdown_names[n], styles[n]) for n in range(0, count) ])
        c.breakdown_styles = [ "'%s' : { color: '%s' }," % (k, v) for k, v in breakdown_styles.iteritems() ]
        c.breakdown_field = breakdown_field

        handle_request(request, c, c.dataset)
        if c.view is None:
            self._make_browser()

        if hasattr(c, 'time'):
            delattr(c, 'time') # disable treemap(!)

        return render(c.template)
Exemplo n.º 5
0
 def timeline(self, id):
     c.dataset = model.Dataset.by_id(id)
     view = View.by_name(c.dataset, "default")
     viewstate = ViewState(c.dataset, view, None)
     data = []
     meta = []
     for entry, year_data in viewstate.aggregates:
         meta.append({"label": entry.get("label"),
                      "description": entry.get("description", ""),
                      "name": entry.get("name"),
                      "index": len(meta),
                      "taxonomy": entry.get("taxonomy")})
         sorted_year_data = sorted(year_data.items(), key=lambda kv: kv[0])
         data.append([{"x": k, "y": v,
                       "meta": len(meta) - 1} for
                      k, v in sorted_year_data])
     c.data = json.dumps(data)
     c.meta = json.dumps(meta)
     return render('dataset/timeline.html')
 def filter(self, stream):
     if hasattr(c, "viewstate") and hasattr(c, "time"):
         if len(c.viewstate.aggregates) > 1:
             opts = {
                 "dataset": c.dataset.name,
                 "drilldown": c.view.drilldown,
                 "endpoint": "/api",
                 "cuts": json.dumps(["year:" + c.time] + ["%s:%s" % d for d in c.view.cuts]),
             }
             stream = stream | Transformer("html/body").append(HTML(JS_SNIPPET % opts))
             stream = stream | Transformer("html/head").append(HTML(CSS_SNIPPET))
             stream = stream | Transformer('//div[@id="vis"]').append(HTML(BODY_SNIPPET))
     return stream
Exemplo n.º 7
0
 def timeline(self, name):
     self._get_dataset(name)
     c.dataset = Dataset.by_name(name)
     view = View.by_name(c.dataset, "default")
     viewstate = ViewState(c.dataset, view, None)
     data = []
     meta = []
     for entry, year_data in viewstate.aggregates:
         meta.append({
             "label": entry.get("label"),
             "description": entry.get("description", ""),
             "name": entry.get("name"),
             "index": len(meta),
             "taxonomy": entry.get("taxonomy")
         })
         sorted_year_data = sorted(year_data.items(), key=lambda kv: kv[0])
         data.append([{
             "x": k,
             "y": v,
             "meta": len(meta) - 1
         } for k, v in sorted_year_data])
     c.data = json.dumps(data)
     c.meta = json.dumps(meta)
     return render('dataset/timeline.html')
Exemplo n.º 8
0
    def test_02(self):
        co = ChangeObject()
        objectid = (u'package', 1, 2, 3)
        co.object_id = objectid
        co.operation = ChangeObject.OperationType.CREATE
        co.data = json.dumps({
            'field1': 'aaaaaa',
            'field2': 'bbbbbb'
            }, sort_keys=True)
        cs = Changeset()
        cs.manifest.append(co)
        cs.author = 'xyz'
        cs.save()

        changeobjs = list(ChangeObject.find())
        assert len(changeobjs) == 1
        co = changeobjs[0]
        assert co.changeset.id == cs.id
        out = Changeset.by_id(cs.id)
        assert len(out.manifest) == 1
        assert out.manifest[0].object_id == co.object_id
Exemplo n.º 9
0
def check(package_name):
    p = ckan.Package(package_name)

    def _get_url_or_error(resource_name):
        try:
            r = p.openspending_resource(resource_name)
            if r:
                return r["url"]
            else:
                return r
        except ckan.ResourceError as e:
            return str(e)

    res = {
        "is_importable": p.is_importable(),
        "data": _get_url_or_error('data'),
        "model": _get_url_or_error('model'),
    }

    print(json.dumps(res, indent=2, sort_keys=True))
    return 0
Exemplo n.º 10
0
def to_json(data, indent=2):
    return json.dumps(data, default=default_json, indent=indent)
Exemplo n.º 11
0
 def serialize(self, node, appstruct):
     return json.dumps(appstruct)
Exemplo n.º 12
0
 def serialize(self, node, appstruct):
     return json.dumps(appstruct)
Exemplo n.º 13
0
def to_json(data, indent=2):
    return json.dumps(data, default=default_json, indent=indent)
Exemplo n.º 14
0
 def to_json(self):
     return json.dumps(self.data)