Esempio n. 1
0
def read_file(directory, name):
    full_path = os.path.join(data_dir(), directory, to_file_format(name))
    if not os.path.isfile(full_path):
        raise DocumentNotFound("File not found: {}".format(full_path))

    with open(full_path, 'r') as f:
        return json.loads(f.read())
Esempio n. 2
0
 def get(self, dashboard_id):
     try:
         source = read_file(self._base_dir, dashboard_id)
         return Dashboard(source, dashboard_id)
     except DocumentNotFound:
         raise DocumentNotFound(
             "There is no such dashboard: {}".format(dashboard_id))
Esempio n. 3
0
    def panel(self, name):
        id = get_id(name)
        panels = [p for p in self._panels if p.id == id]

        if not panels:
            raise DocumentNotFound("There is no panel with id {}".format(id))

        return panels[0]
Esempio n. 4
0
    def get(self, dashboard_id):
        try:
            source = self._call('GET', 'dashboards/db/{}'.format(dashboard_id))
        except requests.HTTPError as exc:
            if exc.response.status_code == 404:
                raise DocumentNotFound(
                    "There is no such dashboard: {}".format(dashboard_id))

            raise
        return Dashboard(source['dashboard'], dashboard_id)
Esempio n. 5
0
    def get(self, dashboard_id):
        hits = self._search(doc_type=DASHBOARD_TYPE,
                            _source=["dashboard"],
                            body={'query': {
                                'match': {
                                    '_id': dashboard_id
                                }
                            }})

        if not hits:
            raise DocumentNotFound(
                "There is no such dashboard: {}".format(dashboard_id))

        source = json.loads(hits[0]['_source']['dashboard'])

        return Dashboard(source, dashboard_id)
Esempio n. 6
0
    def get(self, dashboard_id):
        query = """SELECT data
                   FROM dashboard
                   WHERE slug = %(slug)s"""

        result = self._execute(query, slug=dashboard_id)
        if not result:
            raise DocumentNotFound(
                "There is no such dashboard: {}".format(dashboard_id))

        source = result[0][0]
        if isinstance(source, bytes):
            source = source.decode('utf-8')

        source = json.loads(source)

        return Dashboard(source, dashboard_id)
Esempio n. 7
0
    def list(self, dashboard_name=None, row_name=None, panel_name=None):
        if not dashboard_name:
            return self._storage.list()

        dashboard = self.get(dashboard_name)

        if not row_name:
            return [row.name for row in dashboard.rows]

        row = dashboard.row(row_name)
        panels = [panel.name for panel in row.panels]

        if panel_name:
            if panel_name in panels:
                raise InvalidPath("Panel contains no sub-nodes")
            else:
                raise DocumentNotFound("There is no such panel: {}".format(panel_name))
        else:
            return panels
Esempio n. 8
0
    def _get_row_id(self, name):
        id = get_id(name)
        if id <= 0 or len(self._rows) < id:
            raise DocumentNotFound("There is no row at index {}".format(id))

        return id
Esempio n. 9
0
def remove_file(directory, name):
    full_path = os.path.join(data_dir(), directory, to_file_format(name))
    if not os.path.isfile(full_path):
        raise DocumentNotFound("File not found: {}".format(full_path))

    os.unlink(full_path)
Esempio n. 10
0
def list_files(*paths):
    full_path = os.path.join(data_dir(), *paths)
    if not os.path.isdir(full_path):
        raise DocumentNotFound("No documents found")

    return [from_file_format(file) for file in os.listdir(full_path)]