diff --git a/UML.wsd b/UML.wsd new file mode 100644 index 0000000..0d00ee3 --- /dev/null +++ b/UML.wsd @@ -0,0 +1,8 @@ +@startuml + +class Device { + +id: string + +name: string + +model: string + +getInterfaces() +} \ No newline at end of file diff --git a/check_correct.py b/check_correct.py new file mode 100644 index 0000000..ba0359e --- /dev/null +++ b/check_correct.py @@ -0,0 +1,20 @@ +from parse import parse_csv_to_structure + + +def check_correct(data): + assert isinstance(data, dict) + + for key, value in data.items(): + assert isinstance(key, str) + assert isinstance(value, dict) + + # for subkey, subvalue in value.items(): + # assert isinstance(subkey, str) + # assert isinstance(subvalue, str) + + +if __name__ == "__main__": + data = parse_csv_to_structure("input.csv") + if check_correct(data): + print("yay!") + print(data) diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..333e23c --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1763421233, + "narHash": "sha256-Stk9ZYRkGrnnpyJ4eqt9eQtdFWRRIvMxpNRf4sIegnw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "89c2b2330e733d6cdb5eae7b899326930c2c0648", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..07fa9b8 --- /dev/null +++ b/flake.nix @@ -0,0 +1,21 @@ +{ + description = "Python development env with venv + Jupyter"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let pkgs = import nixpkgs { inherit system; }; + in { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + python312 + python312Packages.virtualenv + python312Packages.pip + ]; + }; + }); +} diff --git a/input.csv b/input.csv new file mode 100644 index 0000000..21a7b21 --- /dev/null +++ b/input.csv @@ -0,0 +1,3 @@ + , prop1 , prop2 , prop3 +ob1 , val1 , , val2 +obj2 , , val3 , val4 \ No newline at end of file diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..ed0a1ef --- /dev/null +++ b/parse.py @@ -0,0 +1,30 @@ +import csv + +def parse_csv_to_structure(path): + result = {} + + with open(path, newline='') as f: + reader = csv.reader(f) + rows = list(reader) + + headers = rows[0][1:] + + for row in rows[1:]: + obj = row[0].strip() + + if not obj: + continue + + result[obj] = {} + for header, value in zip(headers, row[1:]): + value = value.strip() + + if value: + result[obj][header] = value + + return result + + +if __name__ == "__main__": + data = parse_csv_to_structure("input.csv") + print(data) diff --git a/structures copy.py b/structures copy.py new file mode 100644 index 0000000..7b256fd --- /dev/null +++ b/structures copy.py @@ -0,0 +1,23 @@ +class Wire: + def __init__(self, from_device, to_device): + self.from_device = from_device + self.to_device = to_device + + +class Device: + def __init__(self, name, model, properties): + self.name = name + self.model = model + self.properties = properties + + +class Switch(Device): + pass + + +class Router(Device): + pass + + +class Host(Device): + pass diff --git a/structures.py b/structures.py new file mode 100644 index 0000000..48df349 --- /dev/null +++ b/structures.py @@ -0,0 +1,61 @@ +from dataclasses import dataclass, field +from typing import Dict, Any + + +@dataclass +class Device: + name: str + model: str + properties: Dict[str, Any] = field(default_factory=dict) + + def __post_init__(self): + if not self.name: + raise ValueError("Device name cannot be empty") + if not self.model: + raise ValueError("Device model cannot be empty") + if not isinstance(self.properties, dict): + raise TypeError("properties must be a dict") + + +@dataclass +class Switch(Device): + def __post_init__(self): + super().__post_init__() + + if "ports" not in self.properties: + raise ValueError(f"Switch '{self.name}' must define 'ports'") + if ( + not isinstance(self.properties["ports"], int) + or self.properties["ports"] <= 0 + ): + raise ValueError("'ports' must be a positive integer") + + +@dataclass +class Router(Device): + def __post_init__(self): + super().__post_init__() + + if "interfaces" not in self.properties: + raise ValueError(f"Router '{self.name}' must define 'interfaces'") + if ( + not isinstance(self.properties["interfaces"], int) + or self.properties["interfaces"] <= 0 + ): + raise ValueError("'interfaces' must be a positive integer") + + +@dataclass +class Host(Device): + def __post_init__(self): + super().__post_init__() + + ip = self.properties.get("ip") + if not ip or not isinstance(ip, str): + raise ValueError(f"Host '{self.name}' must contain a valid 'ip' string") + + +@dataclass +class Wire: + from_device: Device + to_device: Device