/usr/share/pyshared/wader/common/provider.py is in python-wader 0.5.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 | # -*- coding: utf-8 -*-
# Copyright (C) 2008-2009 Warp Networks, S.L.
# Author: Pablo MartÃ
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Data providers"""
from __future__ import with_statement
import datetime
import sqlite3
import os
import sys
from time import mktime
from calendar import timegm
from pytz import timezone, country_names
from xml.dom.minidom import parse
from wader.common.consts import EXTRA_DIR, MBPI, NETWORKS_DB
from wader.common.sms import Message as _Message
from wader.common.utils import (get_value_and_pop, get_tz_aware_now,
get_tz_aware_mtime)
if sqlite3.version_info >= (2, 4, 1):
# Starting in 2.4.1, the str type is not accepted anymore, therefore,
# we convert all str objects to Unicode
sqlite3.register_adapter(str, lambda s: s.decode('utf-8'))
SMS_SCHEMA = """
create table message (
id integer primary key autoincrement,
date integer not null,
number text not null,
text text,
flags integer,
thread_id integer not null constraint fk_thread_id references thread(id) on delete cascade);
create table thread (
id integer primary key autoincrement,
date integer default 0, -- updated by trigger
number text not null, -- specified in creation
message_count integer default 0, -- updated by trigger
snippet text, -- updated by trigger
read integer default 0, -- updated by trigger
folder_id integer not null constraint fk_folder_id references folder(id) on delete cascade);
create table folder (
id integer primary key autoincrement,
name text not null);
create table version (
version integer default 1);
create index message_flags_index on message(flags);
create index message_thread_index on message(thread_id, id);
create index thread_folder_index on thread(folder_id, id);
-- delete on cascade thread -> message
create trigger fkd_thread_message before delete on "thread"
when exists (select 1 from "message" where old."id" == "thread_id")
begin
delete from "message" where "thread_id" = old."id";
end;
-- delete on cascade folder -> thread
create trigger fkd_folder_thread before delete on "folder"
when exists (select 1 from "thread" where old."id" == "folder_id")
begin
delete from "thread" where "folder_id" = old."id";
end;
-- prevent insertion of messages with an invalid thread_id
create trigger fki_message_thread before insert on "message"
when new."thread_id" is not null and not exists (select 1 from "thread" where new."thread_id" == "id")
begin
select raise(abort, 'constraint fki_message_thread failed: thread_id does not exist in thread table');
end;
-- update snippet, message_count and date of thread upon message insertion
create trigger fki_update_thread_values after insert on "message"
when new."thread_id" is not null and exists (select 1 from "thread" where new."thread_id" == "id")
begin
update "thread"
set
snippet = substr(new."text", 0, 100),
message_count = (select message_count from thread where id = new."thread_id") + 1,
date = strftime('%s', 'now')
where id = new."thread_id";
update "thread"
set
read = (select read from thread where id = new."thread_id") + 1
where id = new."thread_id" and msg_is_read(new."flags");
end;
create trigger fku_mark_message_read after update on "message"
when not msg_is_read(old."flags") and msg_is_read(new."flags")
begin
update "thread"
set
read = (select read from thread where id = new."thread_id") + 1
where id = new."thread_id";
end;
create trigger fku_mark_message_unread after update on "message"
when msg_is_read(old."flags") and not msg_is_read(new."flags")
begin
update "thread"
set
read = (select read from thread where id = new."thread_id") - 1
where id = new."thread_id";
end;
-- prevent thread.id updates when there are messages associated to it
create trigger fku_thread_message after update of id on "thread"
when exists (select 1 from "message" where old."id" == "thread_id")
begin
select raise(abort, 'constraint fku_thread_message failed: can not update thread.id as there are messages associated to it');
end;
-- prevent message.thread_id updates with non existing thread.id
create trigger fku_message_thread before update of thread_id on "message"
when new."thread_id" is not null and not exists (select 1 from "thread" where new."thread_id" == "id")
begin
select raise(abort, 'constraint fku_message_thread failed: can not update message.thread_id with non existing thread_id');
end;
create trigger fki_thread_folder before insert on "thread"
when new."folder_id" is not null and not exists (select 1 from "folder" where new."folder_id" == "id")
begin
select raise(abort, 'constraint fki_thread_folder failed: folder_id does not exist in folder table');
end;
create trigger fku_folder_thread after update of id on "folder"
when exists (select 1 from "thread" where old."id" == "folder_id")
begin
select raise(abort, 'constraint fku_folder_thread failed: can not update folder.id as there are threads associated to it');
end;
create trigger fku_thread_folder before update of folder_id on "thread"
when new."folder_id" is not null and not exists (select 1 from "folder" where new."folder_id" == "id")
begin
select raise(abort, 'constraint fku_thread_folder failed: can not update thread.folder_id as it does not exist');
end;
-- decrease thread.message_count after deleting a message
create trigger fkd_update_message_count after delete on "message"
when old."thread_id" is not null
begin
update "thread"
set message_count = (select message_count from thread where id = old.thread_id) - 1
where id = old."thread_id";
end;
"""
NETWORKS_SCHEMA = """
create table network_info(
id text primary key,
name text,
country text);
create table apn(
id integer primary key autoincrement,
apn text not null,
username text,
password text,
dns1 text,
dns2 text,
type text,
auth text,
smsc text,
mmsc text,
wap1 text,
wap2 text,
wap_apn text,
wap_username text,
wap_password text,
wap_auth text,
network_id integer not null
constraint fk_mi_network_id references network_info(id) on delete cascade);
create table sources_info (
objs integer default 0,
mbpi integer default 0);
create table version (
version integer not null);
-- delete on cascade network_info -> apn
create trigger fkd_network_info_apn before delete on "network_info"
when exists (select 1 from "apn" where old."id" == "network_id")
begin
delete from "apn" where "network_id" = old."id";
end;
-- prevent insert on apn with unknown network_id
create trigger fki_apns_with_unknown_network_id before insert on "apn"
when new."network_id" is not null and not exists (select 1 from "network_info" where new."network_id" == "id")
begin
select raise(abort, 'constraint failed');
end;
-- prevent update of network_info_id if there are APNs associated to old id
create trigger fku_prevent_apn_network_info_network_id_bad_update after update of id on "network_info"
when exists (select 1 from "apn" where old."id" == "network_id")
begin
select raise(abort, 'constraint failed');
end;
-- prevent update of apn.network_id if it does not exists
create trigger fku_prevent_apn_network_id_bad_update before update of network_id on "apn"
when new."network_id" is not null and not exists (select 1 from "network_info" where new."network_id" == "id")
begin
select raise(abort, 'constraint failed');
end;
-- populate apn version
insert into version values (%(version)d);
"""
USAGE_SCHEMA = """
create table usage(
id integer primary key autoincrement,
start_time datetime not null,
end_time datetime not null,
bytes_recv integer not null,
bytes_sent integer not null,
umts boolean);
create table version (
version integer);
-- populate usage version
insert into version values (%(version)d);
"""
# constants
INBOX, OUTBOX, DRAFTS = 1, 2, 3
UNREAD, READ = 0x01, 0x02
# GSM spec
# 0 - Unread message that has been received
# 1 - Read message that has been received
# 2 - Unsent message that has been stored
# 3 - Sent message that has been stored
# 4 - Any message
TYPE_CONTRACT, TYPE_PREPAID = 'Contract', 'Prepaid'
def message_read(flags):
"""
Returns a bool indicating whether the message had been read or not
"""
# second bit is the "was read" flag
return (int(flags) & READ) >> 1
def adapt_datetime(_datetime):
if _datetime.tzinfo is None:
# Naive object - Force to UTC, previous behaviour was to use mktime to
# localize. We should raise an exception, but the problem
# is that if done in the adapter, the error seen is very
# obscure and I expect difficult to relate to the cause.
return timegm(_datetime.timetuple())
return timegm(_datetime.astimezone(timezone('UTC')).timetuple())
def convert_datetime(ts):
return datetime.datetime.fromtimestamp(float(ts), timezone('UTC'))
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
sqlite3.register_converter("datetime", convert_datetime)
def date_to_datetime(date):
# takes a date which is in local time and returns a timezone aware datetime
timestamp = mktime(date.timetuple())
return datetime.datetime.fromtimestamp(timestamp, timezone('UTC'))
# common classes
class DBError(Exception):
"""Base class for DB related errors"""
class DBProvider(object):
"""Base class for the DB providers"""
def __init__(self, path, schema, **kw):
self.conn = sqlite3.connect(path, isolation_level=None, **kw)
c = self.conn.cursor()
try:
c.executescript(schema)
except sqlite3.OperationalError:
# ignore error, the database already exists
pass
def close(self):
"""Closes the provider and frees resources"""
try:
self.conn.cursor().execute("vacuum")
except sqlite3.OperationalError:
pass
self.conn.close()
# data usage
class UsageItem(object):
"""I represent data usage in the DB"""
def __init__(self, index=None, bytes_recv=None, bytes_sent=None,
end_time=None, start_time=None, umts=None):
self.index = index
self.bytes_recv = bytes_recv
self.bytes_sent = bytes_sent
self.end_time = end_time
self.start_time = start_time
self.umts = umts
def __repr__(self):
args = (str(self.end_time - self.start_time), self.bytes_recv,
self.bytes_sent)
return "<UsageItem time: %s bytes_recv: %d bytes_sent: %d>" % args
def __eq__(self, other):
if other.index is not None and self.index is not None:
return self.index == other.index
raise ValueError("Cannot compare myself with %s" % other)
def __ne__(self, other):
return not self.__eq__(other)
def total(self):
return self.bytes_recv + self.bytes_sent
def is_3g(self):
return self.umts
def is_gprs(self):
return not self.umts
@classmethod
def from_row(cls, row):
return cls(index=row[0], start_time=row[1], end_time=row[2],
bytes_recv=int(row[3]), bytes_sent=int(row[4]),
umts=bool(row[5]))
class UsageProvider(DBProvider):
"""DB usage provider"""
version = 1
def __init__(self, path):
args = dict(version=self.version)
super(UsageProvider, self).__init__(path, USAGE_SCHEMA % args,
detect_types=sqlite3.PARSE_DECLTYPES)
def add_usage_item(self, start, end, bytes_recv, bytes_sent, umts):
c = self.conn.cursor()
args = (None, start, end, bytes_recv, bytes_sent, umts)
c.execute("insert into usage(id, start_time, end_time, bytes_recv,"
"bytes_sent, umts) values (?,?,?,?,?,?)", args)
return UsageItem(umts=umts, start_time=start, end_time=end,
bytes_recv=bytes_recv, bytes_sent=bytes_sent,
index=c.lastrowid)
def delete_usage_item(self, item):
c = self.conn.cursor()
c.execute("delete from usage where id=?", (item.index,))
def get_usage_for_day(self, day):
"""
Returns all `UsageItem` for ``day``
:type day: ``datetime.date``
"""
c = self.conn.cursor()
if not isinstance(day, datetime.date):
raise ValueError("Don't know what to do with %s" % day)
tomorrow = day + datetime.timedelta(days=1)
args = (date_to_datetime(day), date_to_datetime(tomorrow))
c.execute("select * from usage where start_time >= ? and end_time < ?",
args)
return [UsageItem.from_row(row) for row in c.fetchall()]
def get_usage_for_month(self, month):
"""
Returns all ``UsageItem`` for ``month``
:type month: ``datetime.date``
"""
c = self.conn.cursor()
if not isinstance(month, datetime.date):
raise ValueError("Don't know what to do with %s" % month)
first_current_month_day = month.replace(day=1)
if month.month < 12:
_month = month.month + 1
_year = month.year
else:
_month = 1
_year = month.year + 1
first_next_month_day = month.replace(day=1, month=_month, year=_year)
args = (date_to_datetime(first_current_month_day),
date_to_datetime(first_next_month_day))
c.execute("select * from usage where start_time >= ? and start_time < ?",
args)
return [UsageItem.from_row(row) for row in c.fetchall()]
def get_total_usage(self, month=None):
c = self.conn.cursor()
if month is None:
sql = "select * from usage"
c.execute(sql)
else:
if not isinstance(month, datetime.date):
raise ValueError("Don't know what to do with %s" % month)
sql = "select * from usage where start_time >= ?"
c.execute(sql, (date_to_datetime(month),))
return [UsageItem.from_row(row) for row in c.fetchall()]
# networks
class NetworkOperator(object):
"""I represent a network operator in the DB"""
def __init__(self, netid=None, apn=None, username=None, password=None,
dns1=None, dns2=None, type=None, auth=None, smsc=None,
mmsc=None, country=None, name=None, wap_apn=None,
wap_username=None, wap_password=None, wap_auth=None,
wap1=None, wap2=None,):
self.netid = netid
self.apn = apn
self.username = username
self.password = password
self.dns1 = dns1
self.dns2 = dns2
self.type = type
self.auth = auth
self.wap_apn = wap_apn
self.wap_username = wap_username
self.wap_password = wap_password
self.wap_auth = wap_auth
self.wap1 = wap1
self.wap2 = wap2
self.smsc = smsc
self.mmsc = mmsc
self.country = country
self.name = name
def __repr__(self):
args = (self.name.capitalize(), self.country.capitalize(),
self.netid, str(self.type))
if self.wap2:
args += (self.wap2,)
return "<NetworkOperator %s>" % repr(args)
@classmethod
def from_row(cls, row, netid):
return cls(netid=[netid], name=row[0], country=row[1], apn=row[2],
username=row[3], password=row[4], dns1=row[5],
dns2=row[6], type=row[7], smsc=row[8], mmsc=row[9],
wap1=row[10], wap2=row[11], wap_apn=row[12],
wap_username=row[13], wap_password=row[14], auth=row[15],
wap_auth=row[16])
class NetworkProvider(DBProvider):
"""DB network provider"""
version = 2
def __init__(self, path=NETWORKS_DB):
args = dict(version=self.version)
super(NetworkProvider, self).__init__(path, NETWORKS_SCHEMA % args)
def is_current(self):
c = self.conn.cursor()
try:
c.execute("select version from version")
version = c.fetchone()[0]
if version < self.version:
return False
except (TypeError, sqlite3.OperationalError):
# version table wasn't populated in old DB
return False
try:
c.execute("select * from sources_info")
row = c.fetchone()
objs_mtime = convert_datetime(row[0])
mbpi_mtime = convert_datetime(row[1])
objs = os.path.join(EXTRA_DIR, 'networks.py')
if ((objs_mtime != get_tz_aware_mtime(objs)) or
(mbpi_mtime != get_tz_aware_mtime(MBPI))):
return False
except (TypeError, sqlite3.OperationalError):
# it does not exist
return False
return True
def get_network_by_id(self, imsi):
"""
Returns all the :class:`NetworkOperator` registered for ``imsi``
:rtype: list
"""
if not isinstance(imsi, basestring):
raise TypeError("argument must be a string subclass")
if len(imsi) < 14:
raise ValueError("Pass the whole imsi")
for n in [7, 6, 5]:
result = self._get_network_by_id(imsi[:n])
if result:
return result
return []
def _get_network_by_id(self, netid):
c = self.conn.cursor()
c.execute("select n.name, n.country, a.apn, a.username,"
"a.password, a.dns1, a.dns2, a.type, a.smsc, "
"a.mmsc, a.wap1, a.wap2, a.wap_apn, "
"a.wap_username, a.wap_password, a.auth, a.wap_auth "
"from network_info n inner join apn a on "
"n.id = a.network_id where n.id=?", (netid,))
ret = [NetworkOperator.from_row(row, netid) for row in c.fetchall()]
return ret
def populate_networks(self):
"""
Populates the networks database using default methods (currently
a list of network objects, and from the mobile-broadband-provider-info).
It turns off autocommit during import for speed
"""
try:
# only will succeed on development
networks = __import__('resources/extra/networks')
except ImportError:
try:
# this fails on feisty but not on gutsy
networks = __import__(os.path.join(EXTRA_DIR, 'networks'))
except ImportError:
sys.path.insert(0, EXTRA_DIR)
import networks
def is_valid(item):
return not item.startswith(("__", "Base", "NetworkOperator"))
# turn off autocommit
self.conn.isolation_level = 'DEFERRED'
# Note: cascades over apn table too
self.conn.cursor().execute("delete from network_info")
self.populate_networks_from_objs([getattr(networks, item)()
for item in dir(networks) if is_valid(item)])
try:
self.populate_networks_from_mbpi()
except TypeError:
pass # MBPI v1.0 format
# update timestamps
objs = os.path.join(EXTRA_DIR, 'networks.py')
args = (get_tz_aware_mtime(objs), get_tz_aware_mtime(MBPI))
self.conn.cursor().execute("delete from sources_info")
self.conn.cursor().execute(
"insert into sources_info values (?,?)", args)
self.conn.commit()
self.conn.isolation_level = None
def populate_networks_from_objs(self, networks):
"""
Populate the network database with ``networks``
:param networks: NetworkOperator instances
:type networks: iter
"""
c = self.conn.cursor()
for network in networks:
for netid in network.netid:
# check if this network object exists in the database
c.execute("select 1 from network_info where id=?", (netid,))
try:
c.fetchone()[0]
except TypeError:
# it does not exist
args = (netid, network.name, network.country)
c.execute("insert into network_info values (?,?,?)", args)
# check if the APN info exists in the database
args = (network.apn, network.username, network.password,
network.dns1, network.dns2, network.type,
network.smsc, network.mmsc, network.wap1, network.wap2,
network.wap_apn)
c.execute("select id from apn where apn=? and username=? "
"and password=? and dns1=? and dns2=? "
"and type=? and smsc=? and mmsc=? "
"and wap1=? and wap2=? and wap_apn=?", args)
try:
c.fetchone()[0]
except TypeError:
# it does not exist
args = (None, network.apn, network.username,
network.password, network.dns1, network.dns2,
network.type, network.auth, network.smsc,
network.mmsc, network.wap1, network.wap2,
network.wap_apn, network.wap_username,
network.wap_password, network.wap_auth, netid)
c.execute("insert into apn values (?,?,?,?,?,?,?,?,?,"
"?,?,?,?,?,?,?,?)", args)
def populate_networks_from_mbpi(self, xmlfile=MBPI):
"""
Populate the network database with info from``xmlfile``
:param xmlfile: the path to the mobile-broadband-provider-info xml file
"""
def getAttributeValue(element, name):
if element is None or not hasattr(element, 'attributes'):
return ''
attr = element.attributes[name]
if not attr:
return ''
return attr.value.strip()
def getElementsByTagNameNoDescend(parent, name):
ret = []
for node in parent.childNodes:
if node.nodeType == node.ELEMENT_NODE and \
(name == "*" or node.tagName == name):
ret.append(node)
return ret
def getValue(elementlist, index=0):
if elementlist is None or index >= len(elementlist):
return ''
if not elementlist[index].firstChild:
return ''
return elementlist[index].firstChild.data.strip()
root = parse(xmlfile)
svcs = getElementsByTagNameNoDescend(root, "serviceproviders")[0]
version = getAttributeValue(svcs, "format")
if version != '2.0':
raise(TypeError)
c = self.conn.cursor()
for gsm in svcs.getElementsByTagName("gsm"):
provider = gsm.parentNode
country = provider.parentNode
countrycode = getAttributeValue(country, "code")
if countrycode == '':
continue
elif countrycode == 'gb': # TZ DB is just plain wrong
countryname = 'United Kingdom'
else:
countryname = country_names[countrycode]
# we need to be more specific about path to 'provider/name'
# as apn also has a subtag called 'name'
tag = getElementsByTagNameNoDescend(provider, "name")
provname = getValue(tag)
for networkid in getElementsByTagNameNoDescend(gsm, "network-id"):
mcc = getAttributeValue(networkid, "mcc")
mnc = getAttributeValue(networkid, "mnc")
netid = mcc + mnc
# check if this network object exists in the database
c.execute("select 1 from network_info where id=?", (netid,))
try:
c.fetchone()[0]
except TypeError:
# it does not exist
args = (netid, provname, countryname)
c.execute("insert into network_info values (?,?,?)", args)
for apn in getElementsByTagNameNoDescend(gsm, "apn"):
apnname = getAttributeValue(apn, "value")
if apnname == '':
continue
tag = getElementsByTagNameNoDescend(apn, "name")
_type = getValue(tag)
if len(_type):
typename = u'MBPI - ' + _type
else:
typename = u'MBPI - ' + provname
tag = getElementsByTagNameNoDescend(apn, "username")
username = getValue(tag)
tag = getElementsByTagNameNoDescend(apn, "password")
password = getValue(tag)
tag = getElementsByTagNameNoDescend(apn, "dns")
dns1 = getValue(tag, 0)
dns2 = getValue(tag, 1)
# check if info exists in the database for this network,
# we are authorititive for VF Networks, and we don't want
# duplicates of the others either
args = (netid, apnname, username, password, dns1, dns2, typename)
c.execute("select id from apn where network_id=? and "
"("
"(not type like 'MBPI%')"
" or "
"(apn=? and username=? and password=? and"
" dns1=? and dns2=? and type=?)"
")", args)
try:
c.fetchone()[0]
except TypeError:
# it does not exist
args = (None, apnname, username, password, dns1, dns2,
typename, None, None, None, None, None, None,
None, None, None, netid)
c.execute("insert into apn values (?,?,?,?,?,?,?,?,"
"?,?,?,?,?,?,?,?,?)", args)
# SMS
class Folder(object):
"""I am a container for threads and messages"""
def __init__(self, name, index=None):
self.name = name
self.index = index
def __repr__(self):
return "<Folder index: %d name: %s>" % (self.index, self.name)
def __eq__(self, other):
return self.index == other.index
def __ne__(self, other):
return not self.__eq__(other)
@classmethod
def from_row(cls, row):
"""Returns a :class:`Folder` out of ``row``"""
return cls(row[1], row[0])
def to_row(self):
"""Returns a tuple object ready to be inserted in the DB"""
return (self.index, self.name)
inbox_folder = Folder("Inbox", 1)
outbox_folder = Folder("Outbox", 2)
drafts_folder = Folder("Drafts", 3)
class Message(_Message):
"""I am a :class:`_Message` that belongs to a thread and has flags"""
def __init__(self, *args, **kw):
self.flags = get_value_and_pop(kw, 'flags', READ)
self.thread = get_value_and_pop(kw, 'thread')
super(Message, self).__init__(*args, **kw)
def __repr__(self):
args = (self.index, self.thread.index)
return "<Message index %d thread_id %d>" % args
def to_row(self):
"""Returns a tuple ready to be added to the provider DB"""
return (self.index, self.datetime, self.number,
self.text, self.flags, self.thread.index)
@classmethod
def from_row(cls, row, thread=None):
# XXX: calling converter manually as DB 'date' type is integer
return cls(row[2], row[3], index=row[0], flags=row[4],
_datetime=convert_datetime(row[1]),
thread=thread)
class Thread(object):
"""I represent an SMS thread in the DB"""
def __init__(self, _datetime, number, index=None, message_count=1,
snippet='', read=1, folder=None):
super(Thread, self).__init__()
self.datetime = _datetime
self.number = number
self.index = index
self.message_count = message_count
self.snippet = snippet
self.read = read
self.folder = folder
def __repr__(self):
return "<Thread %d>" % self.index
def __eq__(self, other):
return self.index == other.index
def __ne__(self, other):
return not self.__eq__(other)
@classmethod
def from_row(cls, row, folder=None):
"""Returns a :class:`Thread` out of ``row``"""
# XXX: calling converter manually as DB 'date' type is integer
return cls(convert_datetime(row[1]),
row[2], index=row[0],
message_count=row[3], snippet=row[4], read=row[5],
folder=folder)
def to_row(self):
"""Returns a tuple ready to be inserted in the DB"""
return (self.index, self.datetime, self.number,
self.message_count, self.snippet, self.read, self.folder.index)
class SmsProvider(DBProvider):
"""DB Sms provider"""
def __init__(self, path):
super(SmsProvider, self).__init__(path, SMS_SCHEMA)
self.conn.create_function('msg_is_read', 1, message_read)
def add_folder(self, folder):
"""
Adds ``folder`` to the DB and returns the object updated with index
"""
c = self.conn.cursor()
c.execute("insert into folder values (?, ?)", folder.to_row())
folder.index = c.lastrowid
return folder
def add_thread(self, thread):
"""
Adds ``thread`` to the DB and returns the object updated with index
"""
c = self.conn.cursor()
c.execute("insert into thread values (?, ?, ?, ?, ?, ?, ?)",
thread.to_row())
thread.index = c.lastrowid
return thread
def add_sms(self, sms, folder=inbox_folder):
"""
Adds ``sms`` to the DB and returns the object updated with index
:param folder: Folder where this SMS will be added
"""
c = self.conn.cursor()
if sms.thread is None:
# create a new thread for this sms
thread = self.get_thread_by_number(sms.number, folder=folder)
sms.thread = thread
c.execute("insert into message values (?, ?, ?, ?, ?, ?)",
sms.to_row())
sms.index = c.lastrowid
return sms
def delete_folder(self, folder):
"""Deletes ``folder`` from the DB"""
# Folders 1, 2, 3 can not be deleted (Inbox, Outbox, Drafts)
if folder.index <= 3:
raise DBError("Folder %d can not be deleted" % folder.index)
c = self.conn.cursor()
c.execute("delete from folder where id=?", (folder.index,))
def delete_sms(self, sms):
"""Deletes ``sms`` from the DB"""
c = self.conn.cursor()
c.execute("delete from message where id=?", (sms.index,))
def delete_thread(self, thread):
"""Deletes ``thread`` from the DB"""
c = self.conn.cursor()
c.execute("delete from thread where id=?", (thread.index,))
def _get_folder_by_id(self, index):
"""Returns the :class:`Folder` identified by ``index``"""
c = self.conn.cursor()
c.execute("select * from folder where id=?", (index,))
try:
return Folder.from_row(c.fetchone())
except TypeError:
raise DBError("Folder %d does not exist" % index)
def _get_thread_by_id(self, index):
"""Returns the :class:`Thread` identified by ``index``"""
c = self.conn.cursor()
c.execute("select * from thread where id=?", (index,))
try:
row = c.fetchone()
folder = self._get_folder_by_id(row[6])
return Thread.from_row(row, folder=folder)
except TypeError:
raise DBError("Thread %d does not exist" % index)
def get_thread_by_number(self, number, folder=inbox_folder):
"""
Returns the :class:`Thread` that belongs to ``number`` under ``folder``
:rtype: :class:`Thread`
"""
c = self.conn.cursor()
sql = "select * from thread where number like ? and folder_id=?"
c.execute(sql, ("%%%s%%" % number, folder.index))
if c.rowcount == 1:
# there already exists a thread for this number under folder
return Thread.from_row(c.fetchone())
elif c.rowcount < 1:
# create thread for this number
thread = Thread(get_tz_aware_now(), number, folder=folder)
return self.add_thread(thread)
elif c.rowcount > 1:
raise DBError("Too many threads associated to number %s" % number)
raise DBError("No thread found for number %s" % number)
def list_folders(self):
"""
List all the :class:`Folder` objects in the DB
:rtype: iter
"""
c = self.conn.cursor()
c.execute("select * from folder")
return (Folder.from_row(row) for row in c.fetchall())
def list_from_folder(self, folder):
"""
List all the :class:`Thread` objects that belong to ``folder``
:rtype: iter
"""
c = self.conn.cursor()
sql = "select * from thread where folder_id=? order by date desc"
c.execute(sql, (folder.index,))
return (Thread.from_row(row, folder=folder) for row in c.fetchall())
def list_from_thread(self, thread):
"""
List all the :class:`Message` objects that belong to ``thread``
:rtype: iter
"""
c = self.conn.cursor()
sql = "select * from message where thread_id=? order by date desc"
c.execute(sql, (thread.index,))
return (Message.from_row(row, thread=thread) for row in c.fetchall())
def list_threads(self):
"""
List all the :class:`Thread` objects in the DB
:rtype: iter
"""
c = self.conn.cursor()
c.execute("select * from thread order by date desc")
for row in c:
folder = self._get_folder_by_id(row[6])
yield Thread.from_row(row, folder=folder)
def list_sms(self):
"""
List all the :class:`Message` objects in the DB
:rtype: iter
"""
c = self.conn.cursor()
c.execute("select * from message order by date desc")
for row in c:
thread = self._get_thread_by_id(row[5])
yield Message.from_row(row, thread=thread)
def move_to_folder(self, src, dst):
"""
Moves ``src`` to ``dst``
:return: The updated ``src`` object
"""
if not isinstance(dst, Folder):
raise TypeError("dst must be a Folder instance")
if isinstance(src, Thread):
return self._move_thread_to_folder(src, dst)
elif isinstance(src, Message):
return self._move_sms_to_folder(src, dst)
else:
raise TypeError("src must be a Thread or a Folder instance")
def update_sms_flags(self, sms, flags):
"""Updates ``sms`` with ``flags``"""
c = self.conn.cursor()
c.execute("update message set flags=? where id=?", (flags, sms.index))
sms.flags = flags
return sms
def _move_sms_to_folder(self, sms, folder):
c = self.conn.cursor()
sql = "select id from thread where number like ? and folder_id=?"
c.execute(sql, ("%%%s%%" % sms.number, folder.index))
if c.rowcount == 1:
# there already exists a thread for that number in folder
thread = self._get_thread_by_id(c.fetchone()[0])
c.execute("update message set thread_id=? where id=?",
(thread.index, sms.index))
sms.thread = thread
return sms
elif c.rowcount < 1:
# create thread for this number
thread = self.add_thread(
Thread(sms.datetime, sms.number, folder=folder))
c.execute("update message set thread_id=? where id=?",
(thread.index, sms.index))
sms.thread = thread
return sms
else:
msg = "Too many threads associated to number %s"
raise DBError(msg % sms.number)
def _move_thread_to_folder(self, thread, folder):
c = self.conn.cursor()
c.execute("update thread set folder_id=? where id=?",
(folder.index, thread.index))
thread.folder = folder
return thread
|