fetchphones.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import os
  2. import yaml
  3. from django.conf import settings
  4. from django.core.management.base import BaseCommand, CommandError
  5. from suitablephones.models import Bluetooth, Camera, Device
  6. import logging
  7. def set_camera(args):
  8. cam = Camera.objects.filter(**args)
  9. if not cam.exists():
  10. cam = Camera()
  11. for subkey, subvalue in args.items():
  12. if subkey == 'info':
  13. mp = subvalue.split(' ')[0]
  14. setattr(cam, 'megapixel', mp)
  15. info = ' '.join(map(str, subvalue.split(' ')[2:]))
  16. setattr(cam, subkey, info)
  17. setattr(cam, subkey, subvalue)
  18. cam.save()
  19. else:
  20. cam = cam.get()
  21. return cam
  22. class Command(BaseCommand):
  23. help = 'Aiuto'
  24. #def add_arguments(self, parser):
  25. # parser.add_argument('poll_ids', nargs='+', type=int
  26. def handle(self, *args, **options):
  27. Device.objects.all().delete()
  28. Bluetooth.objects.all().delete()
  29. Camera.objects.all().delete()
  30. devicesdir = settings.LINEAGEWIKI + "/_data/devices/"
  31. # import pdb; pdb.set_trace()
  32. for filename in os.listdir(devicesdir):
  33. dev = Device()
  34. cameras = []
  35. bluetooth = {}
  36. peripherals = []
  37. with open(os.path.join(devicesdir, filename), "r") as stream:
  38. try:
  39. data = yaml.safe_load(stream)
  40. print(data['codename'])
  41. for key, value in data.items():
  42. # if key == "architecture":
  43. # if type(value) == dict():
  44. # setattr(dev, key, value)
  45. # else: #type(value) == str():
  46. # setattr(dev, key, dict({ 'cpu' : value }))
  47. #match caso:
  48. # case dict():
  49. # print('dict')
  50. if key == "bluetooth":
  51. if not 'profiles' in value.keys():
  52. value['profiles'] = None
  53. # get() returned more than one Bluetooth -- it returned 2!
  54. bt = Bluetooth.objects.filter(**value)
  55. # __exact? https://docs.djangoproject.com/en/4.1/ref/models/querysets/#exact
  56. if not bt.exists():
  57. bt = Bluetooth()
  58. for subkey, subvalue in value.items():
  59. if not hasattr(value, 'profiles'):
  60. setattr(bt, 'profiles', None)
  61. setattr(bt, subkey, subvalue)
  62. bt.save()
  63. else:
  64. bt = bt.get(spec=value['spec'], profiles=value['profiles'])
  65. bluetooth = bt
  66. elif key == "cameras":
  67. for c in value:
  68. cam = set_camera(c)
  69. cameras.append(cam)
  70. elif key == "peripherals":
  71. if str(value) == "None":
  72. value = None
  73. elif key == "storage":
  74. if str(value) == "None":
  75. value = None
  76. else:
  77. setattr(dev, key, value)
  78. except yaml.YAMLError as exc:
  79. print(exc)
  80. stream.close()
  81. dev.save()
  82. dev.bluetooth = bluetooth
  83. for cam in cameras:
  84. dev.cameras.add(cam)
  85. dev.save()