Coverage for netbox/data_backends.py: 70%

20 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from contextlib import contextmanager 

2from urllib.parse import urlparse 

3 

4__all__ = ( 

5 'DataBackend', 

6) 

7 

8 

9class DataBackend: 

10 """ 

11 A data backend represents a specific system of record for data, such as a git repository or Amazon S3 bucket. 

12 

13 Attributes: 

14 name: The identifier under which this backend will be registered in NetBox 

15 label: The human-friendly name for this backend 

16 is_local: A boolean indicating whether this backend accesses local data 

17 parameters: A dictionary mapping configuration form field names to their classes 

18 sensitive_parameters: An iterable of field names for which the values should not be displayed to the user 

19 """ 

20 is_local = False 

21 parameters = {} 

22 sensitive_parameters = [] 

23 

24 # Prevent Django's template engine from calling the backend 

25 # class when referenced via DataSource.backend_class 

26 do_not_call_in_templates = True 

27 

28 def __init__(self, url, **kwargs): 

29 self.url = url 

30 self.params = kwargs 

31 self.config = self.init_config() 

32 

33 def init_config(self): 

34 """ 

35 A hook to initialize the instance's configuration. The data returned by this method is assigned to the 

36 instance's `config` attribute upon initialization, which can be referenced by the `fetch()` method. 

37 """ 

38 return 

39 

40 @property 

41 def url_scheme(self): 

42 return urlparse(self.url).scheme.lower() 

43 

44 @contextmanager 

45 def fetch(self): 

46 """ 

47 A context manager which performs the following: 

48 

49 1. Handles all setup and synchronization 

50 2. Yields the local path at which data has been replicated 

51 3. Performs any necessary cleanup 

52 """ 

53 raise NotImplementedError()