diff options
author | Hochikong <michellehzg@gmail.com> | 2016-05-22 19:14:39 +0800 |
---|---|---|
committer | Hochikong <michellehzg@gmail.com> | 2016-05-22 19:14:39 +0800 |
commit | 4d1282a78cfcea783b13994a5e354133328936e9 (patch) | |
tree | 2c34432af7f8dc78e280c1051d5ebd0c1186760a | |
parent | 3515194ab93359ff7bb8ccc295c840bf335ff185 (diff) | |
download | python-vyos-mgmt-4d1282a78cfcea783b13994a5e354133328936e9.tar.gz python-vyos-mgmt-4d1282a78cfcea783b13994a5e354133328936e9.zip |
add the class DataIO
-rw-r--r-- | vyroute/DataIO.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/vyroute/DataIO.py b/vyroute/DataIO.py new file mode 100644 index 0000000..979820e --- /dev/null +++ b/vyroute/DataIO.py @@ -0,0 +1,81 @@ +# author=hochikong +import shelve + + +class IO(object): + + def post(self): + pass + + def put(self): + pass + + +class RawIO(IO): + # The router node record is in a python shelve object (.dat file). + def __init__(self, datafile): + """Initial member self.file. + + :param datafile: a string + """ + self.file = datafile + + def post(self, key, data): + """Create a new record in the shelve object. + + :param key: a string + :param data: a python dictionary + :return : a python dictionary + """ + temp = shelve.open(self.file) + temp[key] = data + temp.close() + return {"Result": "Create successfully."} + + def put(self, key, data): + """Update a record or delete a existing record. + + :param key: a string + :param data: a python dictionary + :return: different python dictionaries + """ + if data == {"content": "delete"}: + temp = shelve.open(self.file) + try: + del temp[key] + return {"Result": "Delete successfully."} + except KeyError: + return {"Result": "There is no key '%s'." % key} + finally: + temp.close() + else: + self.post(key, data) + return {"Result": "Update successfully."} + + def get(self, key="all"): + """Query a specific record or all records. + + :param key: 'none' or a vaild key + :param args: 'all' or 'none' + :return: different python dictionaries + """ + temp = shelve.open(self.file) + if key == "all": + print temp + temp.close() + else: + try: + print temp[key] + except KeyError: + return {"Result": "'There is no key '%s'." % key} + finally: + temp.close() + + + + + + + + + |